-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from lud0v1c/main
Cryptography module, integrity checking commands and small fixes
- Loading branch information
Showing
12 changed files
with
362 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
# App related | ||
build/ | ||
testing.log | ||
*.out | ||
*.exe | ||
.cpackget* | ||
tmp/ | ||
.vagrant/ | ||
# IDEs | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 */ | ||
/* Copyright Contributors to the cpackget project. */ | ||
|
||
package commands | ||
|
||
import ( | ||
"github.com/open-cmsis-pack/cpackget/cmd/cryptography" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var checksumCreateCmdFlags struct { | ||
// hashAlgorithm is the cryptographic hash function to be used | ||
hashAlgorithm string | ||
|
||
// outputDir is the target directory where the checksum file is written to | ||
outputDir string | ||
} | ||
|
||
func init() { | ||
ChecksumCreateCmd.Flags().StringVarP(&checksumCreateCmdFlags.hashAlgorithm, "hash-function", "a", cryptography.Hashes[0], "specifies the hash function to be used") | ||
ChecksumCreateCmd.Flags().StringVarP(&checksumCreateCmdFlags.outputDir, "output-dir", "o", "", "specifies output directory for the checksum file") | ||
} | ||
|
||
var ChecksumCreateCmd = &cobra.Command{ | ||
Use: "checksum-create [<local .path pack>]", | ||
Short: "Generates a .checksum file containing the digests of a pack", | ||
Long: ` | ||
Creates a .checksum file of a local pack. This is file contains the digests | ||
of the contents of the pack. Example <Vendor.Pack.1.2.3.sha256.checksum> file: | ||
"6f95628e4e0824b0ff4a9f49dad1c3eb073b27c2dd84de3b985f0ef3405ca9ca Vendor.Pack.1.2.3.pdsc | ||
435fsdf..." | ||
The referenced pack must be in its original/compressed form (.pack), and be present locally: | ||
$ cpackget checksum-create Vendor.Pack.1.2.3.pack | ||
The default Cryptographic Hash Function used is "` + cryptography.Hashes[0] + `". In the future other hash functions | ||
might be supported. The used function will be prefixed to the ".checksum" extension. | ||
By default the checksum file will be created in the same directory as the provided pack.`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cryptography.GenerateChecksum(args[0], checksumCreateCmdFlags.outputDir, checksumCreateCmdFlags.hashAlgorithm) | ||
}, | ||
} | ||
|
||
var ChecksumVerifyCmd = &cobra.Command{ | ||
Use: "checksum-verify [<local .path pack>] [<local .checksum path>]", | ||
Short: "Verifies the integrity of a pack using its .checksum file", | ||
Long: ` | ||
Verifies the contents of a pack, checking its integrity against its .checksum file (created | ||
with "checksum-create"): | ||
$ cpackget checksum-verify Vendor.Pack.1.2.3.pack Vendor.Pack.1.2.3.sha256.checksum | ||
The used hash function is inferred from the checksum filename, and if any of the digests | ||
computed doesn't match the one provided in the checksum file an error will be thrown.`, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cryptography.VerifyChecksum(args[0], args[1]) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 */ | ||
/* Copyright Contributors to the cpackget project. */ | ||
|
||
package commands_test | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
errs "github.com/open-cmsis-pack/cpackget/cmd/errors" | ||
) | ||
|
||
var checksumCreateCmdTests = []TestCase{ | ||
{ | ||
name: "test different number of parameters", | ||
args: []string{"checksum-create"}, | ||
expectedErr: errors.New("accepts 1 arg(s), received 0"), | ||
}, | ||
{ | ||
name: "test creating checksum of unexisting pack", | ||
args: []string{"checksum-create", "DoesNotExist.Pack.1.2.3.pack"}, | ||
expectedErr: errs.ErrFileNotFound, | ||
}, | ||
{ | ||
name: "test using unexisting hash function", | ||
args: []string{"checksum-create", "Pack.1.2.3.pack", "-a", "sha1"}, | ||
expectedErr: errs.ErrInvalidHashFunction, | ||
setUpFunc: func(t *TestCase) { | ||
f, _ := os.Create("Pack.1.2.3.pack.sha256.checksum") | ||
f.Close() | ||
}, | ||
tearDownFunc: func() { | ||
os.Remove("Pack.1.2.3.pack.sha256.checksum") | ||
}, | ||
}, | ||
} | ||
|
||
var checksumVerifyCmdTests = []TestCase{ | ||
{ | ||
name: "test different number of parameters", | ||
args: []string{"checksum-verify"}, | ||
expectedErr: errors.New("accepts 2 arg(s), received 0"), | ||
}, | ||
{ | ||
name: "test verifying checksum of unexisting pack", | ||
args: []string{"checksum-verify", "DoesNotExist.Pack.1.2.3.pack", "DoesNotExist.Pack.1.2.3.pack.sha256.checksum"}, | ||
expectedErr: errs.ErrFileNotFound, | ||
setUpFunc: func(t *TestCase) { | ||
f, _ := os.Create("DoesNotExist.Pack.1.2.3.pack.sha256.checksum") | ||
f.Close() | ||
}, | ||
tearDownFunc: func() { | ||
os.Remove("DoesNotExist.Pack.1.2.3.pack.sha256.checksum") | ||
}, | ||
}, | ||
{ | ||
name: "test verifying checksum of unexisting checksum file", | ||
args: []string{"checksum-verify", "Pack.1.2.3.pack", "DoesNotExist.Pack.1.2.3.pack.sha256.checksum"}, | ||
expectedErr: errs.ErrFileNotFound, | ||
setUpFunc: func(t *TestCase) { | ||
f, _ := os.Create("Pack.1.2.3.pack.sha256.checksum") | ||
f.Close() | ||
}, | ||
tearDownFunc: func() { | ||
os.Remove("Pack.1.2.3.pack.sha256.checksum") | ||
}, | ||
}, | ||
} | ||
|
||
func TestChecksumCreateCmd(t *testing.T) { | ||
runTests(t, checksumCreateCmdTests) | ||
} | ||
|
||
func TestChecksumVerifyCmd(t *testing.T) { | ||
runTests(t, checksumVerifyCmdTests) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.