Skip to content

Commit

Permalink
Implement CoSWID command and add comprehensive tests for creation, di…
Browse files Browse the repository at this point in the history
…splay, and validation functionalities

Signed-off-by: Priyanshu Thapliyal <[email protected]>
  • Loading branch information
Priyanshuthapliyal2005 committed Jan 12, 2025
1 parent 42726c2 commit 1a8cf0b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cmd/coswid.go
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)
}
43 changes: 43 additions & 0 deletions cmd/coswidCreate_test.go
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)

Check failure on line 25 in cmd/coswidCreate_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `afero.WriteFile` is not checked (errcheck)

// 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)

Check failure on line 37 in cmd/coswidCreate_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `afero.WriteFile` is not checked (errcheck)
afero.WriteFile(afero.NewOsFs(), "schema.json", []byte(schema), 0644)

Check failure on line 38 in cmd/coswidCreate_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `afero.WriteFile` is not checked (errcheck)

// Test case
err := validateJSON("template.json", "schema.json")
assert.NoError(t, err)
}
42 changes: 42 additions & 0 deletions cmd/coswidDisplay_test.go
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)
}
46 changes: 46 additions & 0 deletions cmd/coswidValidate_test.go
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{})

Check failure on line 44 in cmd/coswidValidate_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)
assert.NoError(t, cmd.Execute())
}
17 changes: 17 additions & 0 deletions cmd/coswid_test.go
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())
}
Binary file modified cocli.exe
Binary file not shown.

0 comments on commit 1a8cf0b

Please sign in to comment.