Skip to content

Commit

Permalink
manifest: add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Jan 4, 2024
1 parent 8c29355 commit 6cbd7e0
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package manifest

import (
"encoding/base64"
"encoding/json"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -50,3 +52,93 @@ func TestSVN(t *testing.T) {
}
})
}

func TestHexString(t *testing.T) {
testCases := []struct {
b []byte
s string
}{
{b: []byte{0x00}, s: "00"},
{b: []byte{0x01}, s: "01"},
{b: []byte{0x0f}, s: "0f"},
{b: []byte{0x10}, s: "10"},
{b: []byte{0x11}, s: "11"},
{b: []byte{0xff}, s: "ff"},
{b: []byte{0x00, 0x01}, s: "0001"},
}

for _, tc := range testCases {
t.Run(tc.s, func(t *testing.T) {
assert := assert.New(t)
hexString := NewHexString(tc.b)
assert.Equal(tc.s, hexString.String())
b, err := hexString.Bytes()
assert.NoError(err)
assert.Equal(tc.b, b)
})
}

t.Run("invalid hexstring", func(t *testing.T) {
assert := assert.New(t)
hexString := HexString("invalid")
_, err := hexString.Bytes()
assert.Error(err)
})
}

func TestHexStrings(t *testing.T) {
testCases := []struct {
hs HexStrings
bs [][]byte
wantErr bool
}{
{
hs: HexStrings{"00", "01"},
bs: [][]byte{{0x00}, {0x01}},
},
{
hs: HexStrings{"00", "01", "0f", "10", "11", "ff"},
bs: [][]byte{{0x00}, {0x01}, {0x0f}, {0x10}, {0x11}, {0xff}},
},
{
hs: HexStrings{"00", "01", "0f", "10", "11", "ff", "invalid"},
wantErr: true,
},
}

for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
assert := assert.New(t)
bs, err := tc.hs.ByteSlices()
if tc.wantErr {
assert.Error(err)
return
}
assert.Equal(tc.bs, bs)
})
}
}

func TestPolicy(t *testing.T) {
t.Run("valid", func(t *testing.T) {
assert := assert.New(t)

policy := []byte("test-policy")
expectedHash := HexString("48a7cea3db9b9bf087e58bdff6e7a4260a0227b90ba0fceb97060a3c76e004e1")

annotation := base64.StdEncoding.EncodeToString(policy)
p, err := NewPolicyFromAnnotation([]byte(annotation))

assert.NoError(err)
assert.Equal(policy, p.Bytes())
assert.Equal(expectedHash, p.Hash())
})
t.Run("invalid", func(t *testing.T) {
assert := assert.New(t)

annotation := "invalid"
_, err := NewPolicyFromAnnotation([]byte(annotation))

assert.Error(err)
})
}

0 comments on commit 6cbd7e0

Please sign in to comment.