github.com/emmansun/gmsm@v0.29.1/smx509/parser_test.go (about) 1 package smx509 2 3 import ( 4 "encoding/asn1" 5 "encoding/hex" 6 "testing" 7 8 cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1" 9 ) 10 11 func TestParseASN1String(t *testing.T) { 12 tests := []struct { 13 name string 14 tag cryptobyte_asn1.Tag 15 value []byte 16 expected string 17 expectedErr string 18 }{ 19 { 20 name: "T61String", 21 tag: cryptobyte_asn1.T61String, 22 value: []byte{80, 81, 82}, 23 expected: string("PQR"), 24 }, 25 { 26 name: "PrintableString", 27 tag: cryptobyte_asn1.PrintableString, 28 value: []byte{80, 81, 82}, 29 expected: string("PQR"), 30 }, 31 { 32 name: "PrintableString (invalid)", 33 tag: cryptobyte_asn1.PrintableString, 34 value: []byte{1, 2, 3}, 35 expectedErr: "invalid PrintableString", 36 }, 37 { 38 name: "UTF8String", 39 tag: cryptobyte_asn1.UTF8String, 40 value: []byte{80, 81, 82}, 41 expected: string("PQR"), 42 }, 43 { 44 name: "UTF8String (invalid)", 45 tag: cryptobyte_asn1.UTF8String, 46 value: []byte{255}, 47 expectedErr: "invalid UTF-8 string", 48 }, 49 { 50 name: "BMPString", 51 tag: cryptobyte_asn1.Tag(asn1.TagBMPString), 52 value: []byte{80, 81}, 53 expected: string("偑"), 54 }, 55 { 56 name: "BMPString (invalid length)", 57 tag: cryptobyte_asn1.Tag(asn1.TagBMPString), 58 value: []byte{255}, 59 expectedErr: "invalid BMPString", 60 }, 61 { 62 name: "IA5String", 63 tag: cryptobyte_asn1.IA5String, 64 value: []byte{80, 81}, 65 expected: string("PQ"), 66 }, 67 { 68 name: "IA5String (invalid)", 69 tag: cryptobyte_asn1.IA5String, 70 value: []byte{255}, 71 expectedErr: "invalid IA5String", 72 }, 73 { 74 name: "NumericString", 75 tag: cryptobyte_asn1.Tag(asn1.TagNumericString), 76 value: []byte{49, 50}, 77 expected: string("12"), 78 }, 79 { 80 name: "NumericString (invalid)", 81 tag: cryptobyte_asn1.Tag(asn1.TagNumericString), 82 value: []byte{80}, 83 expectedErr: "invalid NumericString", 84 }, 85 } 86 87 for _, tc := range tests { 88 t.Run(tc.name, func(t *testing.T) { 89 out, err := parseASN1String(tc.tag, tc.value) 90 if err != nil && err.Error() != tc.expectedErr { 91 t.Fatalf("parseASN1String returned unexpected error: got %q, want %q", err, tc.expectedErr) 92 } else if err == nil && tc.expectedErr != "" { 93 t.Fatalf("parseASN1String didn't fail, expected: %s", tc.expectedErr) 94 } 95 if out != tc.expected { 96 t.Fatalf("parseASN1String returned unexpected value: got %q, want %q", out, tc.expected) 97 } 98 }) 99 } 100 } 101 102 // The SM2 public key with alg = oidPublicKeySM2 and SM2 curve 103 var sm2PublicKeyHex = "305a301406082a811ccf5501822d06082a811ccf5501822d0342000409586fff35c1f805b5c74f7281c3ade8fe211ffa70bf0ddd1c7268f62ae664331410e3039eeb03209afdc7fa834235c7b3ef528d32bf8b401eb98d32f498b4b7" 104 105 // The SM2 public key with alg = oidPublicKeySM2 and NIST P256 curve 106 var sm2NistP256PubulicKeyHex = "305a301406082a811ccf5501822d06082a8648ce3d0301070342000476110a45e7e86c1e96ba3c3300da61049a529c20a7ea7f026e50a2dbed60558087346bcb04cb0f0f8dcab8cca9967b8c7cc5aa0c874f024b73208b28f408bfca" 107 108 func TestParseSM2PublicKey(t *testing.T) { 109 der, err := hex.DecodeString(sm2PublicKeyHex) 110 if err != nil { 111 t.Fatal(err) 112 } 113 _, err = ParsePKIXPublicKey(der) 114 if err != nil { 115 t.Fatal(err) 116 } 117 } 118 119 func TestParseSM2PublicKeyWithNistP256(t *testing.T) { 120 der, err := hex.DecodeString(sm2NistP256PubulicKeyHex) 121 if err != nil { 122 t.Fatal(err) 123 } 124 _, err = ParsePKIXPublicKey(der) 125 if err == nil || err.Error() != "x509: unsupported SM2 curve" { 126 t.Fatal("should throw x509: unsupported SM2 curve") 127 } 128 }