-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CoSWID command and add comprehensive tests for creation, di…
…splay, and validation functionalities Signed-off-by: Priyanshu Thapliyal <[email protected]>
- Loading branch information
1 parent
42726c2
commit 1a8cf0b
Showing
6 changed files
with
154 additions
and
5 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,18 +1,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"fmt" | ||
) | ||
|
||
var coswidCmd = &cobra.Command{ | ||
Use: "coswid", | ||
Short: "CoSWID manipulation", | ||
// Removed the Run function to allow subcommand delegation | ||
Short: "A brief description of your command", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
fmt.Println("coswid command executed") | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
fmt.Println("Initializing coswid command") | ||
rootCmd.AddCommand(coswidCmd) | ||
} |
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,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckCoswidCreateArgs(t *testing.T) { | ||
// Setup | ||
coswidCreateTemplate = "" | ||
|
||
// Test cases | ||
err := checkCoswidCreateArgs() | ||
assert.Error(t, err, "template file is required") | ||
|
||
coswidCreateTemplate = "template.json" | ||
err = checkCoswidCreateArgs() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestCoswidTemplateToCBOR(t *testing.T) { | ||
// Setup | ||
template := `{"evidence": {"type": "test", "value": "test"}}` | ||
afero.WriteFile(afero.NewOsFs(), "template.json", []byte(template), 0644) | ||
|
||
// Test case | ||
output, err := coswidTemplateToCBOR("template.json", ".") | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, output) | ||
} | ||
|
||
func TestValidateJSON(t *testing.T) { | ||
// Setup | ||
template := `{"evidence": {"type": "test", "value": "test"}}` | ||
schema := `{"type": "object"}` | ||
afero.WriteFile(afero.NewOsFs(), "template.json", []byte(template), 0644) | ||
afero.WriteFile(afero.NewOsFs(), "schema.json", []byte(schema), 0644) | ||
|
||
// Test case | ||
err := validateJSON("template.json", "schema.json") | ||
assert.NoError(t, err) | ||
} |
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,42 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckCoswidDisplayArgs(t *testing.T) { | ||
// Setup | ||
coswidDisplayFile = "" | ||
coswidDisplayDir = "" | ||
|
||
// Test cases | ||
err := checkCoswidDisplayArgs() | ||
assert.Error(t, err, "no CoSWID file or directory supplied") | ||
|
||
coswidDisplayFile = "test.cbor" | ||
err = checkCoswidDisplayArgs() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGatherFiles(t *testing.T) { | ||
// Setup | ||
fs = afero.NewMemMapFs() | ||
afero.WriteFile(fs, "test.cbor", []byte{}, 0644) | ||
afero.WriteFile(fs, "dir/test.cbor", []byte{}, 0644) | ||
|
||
// Test case | ||
files := gatherFiles([]string{"test.cbor"}, []string{"dir"}, ".cbor") | ||
assert.Len(t, files, 2) | ||
} | ||
|
||
func TestDisplayCoswid(t *testing.T) { | ||
// Setup | ||
fs = afero.NewMemMapFs() | ||
afero.WriteFile(fs, "test.cbor", []byte{0xA1, 0x01, 0x02}, 0644) | ||
|
||
// Test case | ||
err := displayCoswid("test.cbor") | ||
assert.NoError(t, err) | ||
} |
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,46 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckCoswidValidateArgs(t *testing.T) { | ||
// Setup | ||
coswidValidateFile = "" | ||
coswidValidateSchema = "" | ||
|
||
// Test cases | ||
err := checkCoswidValidateArgs() | ||
assert.Error(t, err, "no CoSWID file supplied") | ||
|
||
coswidValidateFile = "test.cbor" | ||
err = checkCoswidValidateArgs() | ||
assert.Error(t, err, "no schema supplied") | ||
|
||
coswidValidateSchema = "schema.json" | ||
err = checkCoswidValidateArgs() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestValidateCoswid(t *testing.T) { | ||
// Setup | ||
fs = afero.NewMemMapFs() | ||
afero.WriteFile(fs, "test.cbor", []byte{0xA1, 0x01, 0x02}, 0644) | ||
afero.WriteFile(fs, "schema.json", []byte(`{"type": "object"}`), 0644) | ||
|
||
// Test case | ||
err := validateCoswid("test.cbor", "schema.json") | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestCoswidValidateCmd(t *testing.T) { | ||
// Setup | ||
cmd := &cobra.Command{} | ||
|
||
// Test case | ||
coswidValidateCmd.RunE(cmd, []string{}) | ||
assert.NoError(t, cmd.Execute()) | ||
} |
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,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCoswidCmd(t *testing.T) { | ||
// Setup | ||
cmd := &cobra.Command{} | ||
|
||
// Test case | ||
err := coswidCmd.RunE(cmd, []string{}) | ||
assert.NoError(t, err) | ||
assert.NoError(t, cmd.Execute()) | ||
} |