@@ -978,3 +978,143 @@ func TestSign1Message_toBeSigned(t *testing.T) {
978978 })
979979 }
980980}
981+
982+ func TestUntaggedSign1Message_MarshalCBOR (t * testing.T ) {
983+ tests := []struct {
984+ name string
985+ m * UntaggedSign1Message
986+ want []byte
987+ wantErr string
988+ }{
989+ {
990+ name : "valid message" ,
991+ m : & UntaggedSign1Message {
992+ Headers : Headers {
993+ Protected : ProtectedHeader {
994+ HeaderLabelAlgorithm : AlgorithmES256 ,
995+ },
996+ Unprotected : UnprotectedHeader {
997+ HeaderLabelContentType : 42 ,
998+ },
999+ },
1000+ Payload : []byte ("foo" ),
1001+ Signature : []byte ("bar" ),
1002+ },
1003+ want : []byte {
1004+ 0x84 ,
1005+ 0x43 , 0xa1 , 0x01 , 0x26 , // protected
1006+ 0xa1 , 0x03 , 0x18 , 0x2a , // unprotected
1007+ 0x43 , 0x66 , 0x6f , 0x6f , // payload
1008+ 0x43 , 0x62 , 0x61 , 0x72 , // signature
1009+ },
1010+ },
1011+ }
1012+ for _ , tt := range tests {
1013+ t .Run (tt .name , func (t * testing.T ) {
1014+ got , err := tt .m .MarshalCBOR ()
1015+
1016+ if err != nil && (err .Error () != tt .wantErr ) {
1017+ t .Errorf ("UntaggedSign1Message.MarshalCBOR() error = %v, wantErr %v" , err , tt .wantErr )
1018+ return
1019+ } else if err == nil && (tt .wantErr != "" ) {
1020+ t .Errorf ("UntaggedSign1Message.MarshalCBOR() error = %v, wantErr %v" , err , tt .wantErr )
1021+ return
1022+ }
1023+ if ! reflect .DeepEqual (got , tt .want ) {
1024+ t .Errorf ("UntaggedSign1Message.MarshalCBOR() = %v, want %v" , got , tt .want )
1025+ }
1026+ })
1027+ }
1028+ }
1029+
1030+ func TestUntaggedSign1Message_UnmarshalCBOR (t * testing.T ) {
1031+ // test others
1032+ tests := []struct {
1033+ name string
1034+ data []byte
1035+ want UntaggedSign1Message
1036+ wantErr string
1037+ }{
1038+ {
1039+ name : "valid message" ,
1040+ data : []byte {
1041+ 0x84 ,
1042+ 0x43 , 0xa1 , 0x01 , 0x26 , // protected
1043+ 0xa1 , 0x03 , 0x18 , 0x2a , // unprotected
1044+ 0x43 , 0x66 , 0x6f , 0x6f , // payload
1045+ 0x43 , 0x62 , 0x61 , 0x72 , // signature
1046+ },
1047+ want : UntaggedSign1Message {
1048+ Headers : Headers {
1049+ RawProtected : []byte {0x43 , 0xa1 , 0x01 , 0x26 },
1050+ Protected : ProtectedHeader {
1051+ HeaderLabelAlgorithm : AlgorithmES256 ,
1052+ },
1053+ RawUnprotected : []byte {0xa1 , 0x03 , 0x18 , 0x2a },
1054+ Unprotected : UnprotectedHeader {
1055+ HeaderLabelContentType : int64 (42 ),
1056+ },
1057+ },
1058+ Payload : []byte ("foo" ),
1059+ Signature : []byte ("bar" ),
1060+ },
1061+ },
1062+ {
1063+ name : "tagged message" ,
1064+ data : []byte {
1065+ 0xd2 , // tag
1066+ 0x84 ,
1067+ 0x43 , 0xa1 , 0x01 , 0x26 , // protected
1068+ 0xa1 , 0x03 , 0x18 , 0x2a , // unprotected
1069+ 0x43 , 0x66 , 0x6f , 0x6f , // payload
1070+ 0x43 , 0x62 , 0x61 , 0x72 , // signature
1071+ },
1072+ wantErr : "cbor: invalid COSE_Sign1 object" ,
1073+ },
1074+ {
1075+ name : "empty data" ,
1076+ data : []byte {},
1077+ wantErr : "cbor: zero length data" ,
1078+ },
1079+ }
1080+ for _ , tt := range tests {
1081+ t .Run (tt .name , func (t * testing.T ) {
1082+ var got UntaggedSign1Message
1083+ err := got .UnmarshalCBOR (tt .data )
1084+ if (err != nil ) && (err .Error () != tt .wantErr ) {
1085+ t .Errorf ("Sign1Message.UnmarshalCBOR() error = %v, wantErr %v" , err , tt .wantErr )
1086+ return
1087+ } else if err == nil && (tt .wantErr != "" ) {
1088+ t .Errorf ("Sign1Message.UnmarshalCBOR() error = %v, wantErr %v" , err , tt .wantErr )
1089+ return
1090+ }
1091+ if ! reflect .DeepEqual (got , tt .want ) {
1092+ t .Errorf ("Sign1Message.UnmarshalCBOR() = %v, want %v" , got , tt .want )
1093+ }
1094+ })
1095+ }
1096+ }
1097+
1098+ func TestUntaggedSign1Message_nil (t * testing.T ) {
1099+ var m * UntaggedSign1Message
1100+
1101+ _ , err := m .MarshalCBOR ()
1102+ if err .Error () != "cbor: MarshalCBOR on nil Sign1Message pointer" {
1103+ t .Errorf ("UntaggedSign1Message.MarshalCBOR unexpected err: %v" , err )
1104+ }
1105+
1106+ err = m .UnmarshalCBOR ([]byte {})
1107+ if err .Error () != "cbor: UnmarshalCBOR on nil UntaggedSign1Message pointer" {
1108+ t .Errorf ("UntaggedSign1Message.UnmarshalCBOR unexpected err: %v" , err )
1109+ }
1110+
1111+ err = m .Sign (nil , []byte {}, nil )
1112+ if err .Error () != "signing nil Sign1Message" {
1113+ t .Errorf ("UntaggedSign1Message.Sign unexpected err: %v" , err )
1114+ }
1115+
1116+ err = m .Verify ([]byte {}, nil )
1117+ if err .Error () != "verifying nil Sign1Message" {
1118+ t .Errorf ("UntaggedSign1Message.Sign unexpected err: %v" , err )
1119+ }
1120+ }
0 commit comments