cmw_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package cmw
  2. import (
  3. "testing"
  4. "github.com/fxamacker/cbor/v2"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func mustCBOR(t *testing.T, v any) []byte {
  9. t.Helper()
  10. b, err := cbor.Marshal(v)
  11. require.NoError(t, err)
  12. return b
  13. }
  14. func TestParse(t *testing.T) {
  15. const mediaType = `application/vnd.veraison.tsm-report+json; provider="tdx_guest"`
  16. value := []byte(`{"provider":"tdx_guest"}`)
  17. tests := []struct {
  18. name string
  19. data []byte
  20. want *Record
  21. wantErr string
  22. }{
  23. {"ok json", []byte(`["application/eat+jwt", "ZXlKaGJHY2lPaUpTVXpJMU5pSjku", 8]`),
  24. &Record{MediaType: "application/eat+jwt", Value: []byte("eyJhbGciOiJSUzI1NiJ9."), Indicator: AttestationResults}, ""},
  25. {"ok json padded", []byte(`["text/plain", "Zm9vYg=="]`),
  26. &Record{MediaType: "text/plain", Value: []byte("foob")}, ""},
  27. {"ok json whitespace", []byte("\n [\"text/plain\", \"Zm9v\", 4] \n"),
  28. &Record{MediaType: "text/plain", Value: []byte("foo"), Indicator: Evidence}, ""},
  29. {"ok cbor", mustCBOR(t, []any{mediaType, value, uint(4)}),
  30. &Record{MediaType: mediaType, Value: value, Indicator: Evidence}, ""},
  31. {"ok cbor no indicator", mustCBOR(t, []any{mediaType, value}),
  32. &Record{MediaType: mediaType, Value: value}, ""},
  33. {"fail empty", []byte(" "), nil, "cmw is empty"},
  34. {"fail json collection", []byte(`{"a": ["text/plain", "Zm9v"]}`), nil, "collections are not supported"},
  35. {"fail json syntax", []byte(`["text/plain", "Zm9v"`), nil, "error parsing cmw json record"},
  36. {"fail json one element", []byte(`["text/plain"]`), nil, "must have 2 or 3 elements, it has 1"},
  37. {"fail json four elements", []byte(`["text/plain", "Zm9v", 4, 1]`), nil, "must have 2 or 3 elements, it has 4"},
  38. {"fail json type", []byte(`[1, "Zm9v"]`), nil, "type must be a media type string"},
  39. {"fail json value type", []byte(`["text/plain", 1]`), nil, "value must be a base64url string"},
  40. {"fail json value encoding", []byte(`["text/plain", "Zm9v+/"]`), nil, "value must be a base64url string"},
  41. {"fail json indicator", []byte(`["text/plain", "Zm9v", -1]`), nil, "indicator must be an unsigned integer"},
  42. {"fail json empty type", []byte(`["", "Zm9v"]`), nil, "type is empty"},
  43. {"fail json empty value", []byte(`["text/plain", ""]`), nil, "value is empty"},
  44. {"fail cbor collection", mustCBOR(t, map[string]any{"a": []any{mediaType, value}}), nil, "collections are not supported"},
  45. {"fail cbor tag", mustCBOR(t, cbor.Tag{Number: 1668546817 + 10000, Content: value}), nil, "tag form is not supported"},
  46. {"fail cbor content-format", mustCBOR(t, []any{uint(10000), value}), nil, "typed by a coap content-format are not supported"},
  47. {"fail cbor type", mustCBOR(t, []any{[]byte("x"), value}), nil, "type must be a media type string"},
  48. {"fail cbor value", mustCBOR(t, []any{mediaType, "text"}), nil, "value must be a byte string"},
  49. {"fail cbor indicator", mustCBOR(t, []any{mediaType, value, "x"}), nil, "indicator must be an unsigned integer"},
  50. {"fail cbor elements", mustCBOR(t, []any{mediaType}), nil, "must have 2 or 3 elements, it has 1"},
  51. {"fail cbor trailing data", append(mustCBOR(t, []any{mediaType, value}), 0x00), nil, "error parsing cmw cbor record"},
  52. {"fail neither", []byte{0x01}, nil, "neither a json nor a cbor record"},
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. got, err := Parse(tt.data)
  57. if tt.wantErr != "" {
  58. assert.ErrorContains(t, err, tt.wantErr)
  59. assert.Nil(t, got)
  60. return
  61. }
  62. require.NoError(t, err)
  63. assert.Equal(t, tt.want, got)
  64. })
  65. }
  66. }
  67. func TestIndicator_Has(t *testing.T) {
  68. assert.True(t, Evidence.Has(Evidence))
  69. assert.True(t, (Evidence | Endorsements).Has(Evidence))
  70. assert.False(t, Evidence.Has(AttestationResults))
  71. assert.False(t, Indicator(0).Has(Evidence))
  72. }