@@ -18,55 +18,53 @@ func Test_Deserialize_monad_ok(t *testing.T) {
1818 tv []byte
1919 exp monad
2020 }{
21- /*
22- {
23- "JSON array with media type string",
24- []byte(`["application/vnd.intel.sgx", "3q2-7w"]`),
25- monad{
26- typ: Type{"application/vnd.intel.sgx"},
27- val: []byte{0xde, 0xad, 0xbe, 0xef},
28- ind: IndicatorNone,
29- format: FormatJSONRecord,
30- },
21+ {
22+ "JSON array with media type string" ,
23+ []byte (`["application/vnd.intel.sgx", "3q2-7w"]` ),
24+ monad {
25+ typ : Type {"application/vnd.intel.sgx" },
26+ val : []byte {0xde , 0xad , 0xbe , 0xef },
27+ ind : IndicatorNone ,
28+ format : FormatJSONRecord ,
3129 },
32- {
33- "JSON array with media type string and indicator",
34- []byte(`["application/vnd.intel.sgx", "3q2-7w", 31]`),
35- monad{
36- Type{"application/vnd.intel.sgx"},
37- []byte{0xde, 0xad, 0xbe, 0xef},
38- testIndicator,
39- FormatJSONRecord,
40- },
30+ },
31+ {
32+ "JSON array with media type string and indicator" ,
33+ []byte (`["application/vnd.intel.sgx", "3q2-7w", 31]` ),
34+ monad {
35+ Type {"application/vnd.intel.sgx" },
36+ []byte {0xde , 0xad , 0xbe , 0xef },
37+ testIndicator ,
38+ FormatJSONRecord ,
39+ },
40+ },
41+ {
42+ "CBOR array with CoAP C-F" ,
43+ // echo "[30001, h'deadbeef']" | diag2cbor.rb | xxd -p -i
44+ []byte {0x82 , 0x19 , 0x75 , 0x31 , 0x44 , 0xde , 0xad , 0xbe , 0xef },
45+ monad {
46+ Type {uint16 (30001 )},
47+ []byte {0xde , 0xad , 0xbe , 0xef },
48+ IndicatorNone ,
49+ FormatCBORRecord ,
4150 },
42- {
43- "CBOR array with CoAP C-F",
44- // echo "[30001, h'deadbeef']" | diag2cbor.rb | xxd -p -i
45- []byte{0x82, 0x19, 0x75, 0x31, 0x44, 0xde, 0xad, 0xbe, 0xef},
46- monad{
47- Type{uint16(30001)},
48- []byte{0xde, 0xad, 0xbe, 0xef},
49- IndicatorNone,
50- FormatCBORRecord,
51- },
51+ },
52+ {
53+ "CBOR array with media type string" ,
54+ // echo "[\"application/vnd.intel.sgx\", h'deadbeef']" | diag2cbor.rb | xxd -p -i
55+ []byte {
56+ 0x82 , 0x78 , 0x19 , 0x61 , 0x70 , 0x70 , 0x6c , 0x69 , 0x63 , 0x61 ,
57+ 0x74 , 0x69 , 0x6f , 0x6e , 0x2f , 0x76 , 0x6e , 0x64 , 0x2e , 0x69 ,
58+ 0x6e , 0x74 , 0x65 , 0x6c , 0x2e , 0x73 , 0x67 , 0x78 , 0x44 , 0xde ,
59+ 0xad , 0xbe , 0xef ,
5260 },
53- {
54- "CBOR array with media type string",
55- // echo "[\"application/vnd.intel.sgx\", h'deadbeef']" | diag2cbor.rb | xxd -p -i
56- []byte{
57- 0x82, 0x78, 0x19, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61,
58- 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x69,
59- 0x6e, 0x74, 0x65, 0x6c, 0x2e, 0x73, 0x67, 0x78, 0x44, 0xde,
60- 0xad, 0xbe, 0xef,
61- },
62- monad{
63- Type{string("application/vnd.intel.sgx")},
64- []byte{0xde, 0xad, 0xbe, 0xef},
65- IndicatorNone,
66- FormatCBORRecord,
67- },
61+ monad {
62+ Type {string ("application/vnd.intel.sgx" )},
63+ []byte {0xde , 0xad , 0xbe , 0xef },
64+ IndicatorNone ,
65+ FormatCBORRecord ,
6866 },
69- */
67+ },
7068 {
7169 "CBOR tag" ,
7270 // echo "1668576818(h'deadbeef')" | diag2cbor.rb | xxd -p -i
@@ -88,7 +86,8 @@ func Test_Deserialize_monad_ok(t *testing.T) {
8886
8987 err := actual .Deserialize (tt .tv )
9088 assert .NoError (t , err )
91-
89+ err = actual .monad .validate ()
90+ assert .NoError (t , err )
9291 assert .Equal (t , KindMonad , actual .GetKind ())
9392 assert .Equal (t , tt .exp .format , actual .GetFormat ())
9493 assert .Equal (t , tt .exp , actual .monad )
@@ -464,3 +463,102 @@ func Test_NewMonad_fail_bad_type(t *testing.T) {
464463 _ , err := NewMonad (0xffffffff , []byte {0x00 })
465464 assert .EqualError (t , err , `unsupported type int for CMW type` )
466465}
466+ func Test_Validate_monad_ok (t * testing.T ) {
467+ tests := []struct {
468+ name string
469+ typ any
470+ val []byte
471+ ind []Indicator
472+ }{
473+ {
474+ "minimal with string type" ,
475+ "application/vnd.intel.sgx" ,
476+ []byte {0xde , 0xad , 0xbe , 0xef },
477+ []Indicator {},
478+ },
479+ {
480+ "minimal with CoAP Content-Format" ,
481+ uint16 (30001 ),
482+ []byte {0xde , 0xad , 0xbe , 0xef },
483+ []Indicator {},
484+ },
485+ {
486+ "with single indicator" ,
487+ "application/eat+cwt" ,
488+ []byte {0xde , 0xad , 0xbe , 0xef },
489+ []Indicator {AttestationResults },
490+ },
491+ {
492+ "with multiple indicators" ,
493+ "application/corim+signed" ,
494+ []byte {0xde , 0xad , 0xbe , 0xef },
495+ []Indicator {ReferenceValues , Endorsements , TrustAnchors },
496+ },
497+ {
498+ "with maximum valid indicator value (31)" ,
499+ "application/evidence" ,
500+ []byte {0xff },
501+ []Indicator {Indicator (31 )},
502+ },
503+ }
504+
505+ for _ , tt := range tests {
506+ t .Run (tt .name , func (t * testing.T ) {
507+ cmw , err := NewMonad (tt .typ , tt .val , tt .ind ... )
508+ require .NoError (t , err )
509+
510+ err = cmw .monad .validate ()
511+ assert .NoError (t , err )
512+ })
513+ }
514+ }
515+
516+ func Test_Validate_monad_fails (t * testing.T ) {
517+ tests := []struct {
518+ name string
519+ tv * monad
520+ expectedErr string
521+ }{
522+ {
523+ "type not set" ,
524+ & monad {
525+ val : []byte {0xde , 0xad , 0xbe , 0xef },
526+ ind : IndicatorNone ,
527+ },
528+ "type not set" ,
529+ },
530+ {
531+ "value not set" ,
532+ & monad {
533+ typ : Type {"application/evidence" },
534+ ind : IndicatorNone ,
535+ },
536+ "value not set" ,
537+ },
538+ {
539+ "indicator exceeds maximum (32)" ,
540+ & monad {
541+ typ : Type {"application/evidence" },
542+ val : []byte {0xde , 0xad , 0xbe , 0xef },
543+ ind : Indicator (32 ),
544+ },
545+ "indicator value 32 exceeds maximum 31" ,
546+ },
547+ {
548+ "indicator exceeds maximum (255)" ,
549+ & monad {
550+ typ : Type {"application/evidence" },
551+ val : []byte {0xde , 0xad , 0xbe , 0xef },
552+ ind : Indicator (255 ),
553+ },
554+ "indicator value 255 exceeds maximum 31" ,
555+ },
556+ }
557+
558+ for _ , tt := range tests {
559+ t .Run (tt .name , func (t * testing.T ) {
560+ err := tt .tv .validate ()
561+ assert .EqualError (t , err , tt .expectedErr )
562+ })
563+ }
564+ }
0 commit comments