| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package pkixscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/ |
| 6 | |
| 7 | import ( |
| 8 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/" |
| 9 | "testingscip-go gomod github.com/golang/go/src go1.16 testing/" |
| 10 | |
| 11 | "github.com/zmap/zcrypto/encoding/asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/" |
| 12 | ) |
| 13 | |
| 14 | func TestAttributeTypeValueJSONscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/TestAttributeTypeValueJSON().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 15 | tests := []struct { |
| 16 | atv AttributeTypeAndValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue# |
| 17 | expected string |
| 18 | }{ |
| 19 | { |
| 20 | atv: AttributeTypeAndValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#{Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type.: oidCommonNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/oidCommonName., Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value.: "some.common.name"}, |
| 21 | expected: `{"type":"2.5.4.3","value":"some.common.name"}`, |
| 22 | }, |
| 23 | { |
| 24 | atv: AttributeTypeAndValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#{}, |
| 25 | expected: `{}`, |
| 26 | }, |
| 27 | { |
| 28 | atv: AttributeTypeAndValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#{Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type.: []int{}, Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value.: "some value"}, |
| 29 | expected: `{"value":"some value"}`, |
| 30 | }, |
| 31 | } |
| 32 | for i, test := range tests { |
| 33 | b, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(&test.atv) |
| 34 | if len(test.expected) > 0 && err != nil { |
| 35 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: failed marshal: %s", i, err) |
| 36 | continue |
| 37 | } |
| 38 | if s := string(b); s != test.expected { |
| 39 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: expected %s, got %s", i, test.expected, s) |
| 40 | } |
| 41 | parsed := AttributeTypeAndValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#{} |
| 42 | if err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Unmarshal().(b, &parsed); err != nil { |
| 43 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: could not unmarshal: %s", i, err) |
| 44 | } |
| 45 | if !test.atv.Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type..Equalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#Equal().(parsed.Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type.) { |
| 46 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: expected %v, got %v", i, test.atv.Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type., parsed.Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type.) |
| 47 | } |
| 48 | if test.atv.Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value. != nil { |
| 49 | parsedValue, _ := parsed.Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value..(string) |
| 50 | if test.atv.Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value..(string) != parsedValue { |
| 51 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: expected %v, got %v", i, test.atv.Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value., parsed.Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value.) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestExtensionJSONscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/TestExtensionJSON().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 58 | tests := []struct { |
| 59 | ext Extensionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension# |
| 60 | expected string |
| 61 | }{ |
| 62 | { |
| 63 | ext: Extensionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#{ |
| 64 | Idscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Id.: asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#{1, 2, 3, 4, 5}, |
| 65 | Criticalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Critical.: false, |
| 66 | Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Value.: []byte{6, 7, 8, 9, 0}, |
| 67 | }, |
| 68 | expected: `{"id":"1.2.3.4.5","critical":false,"value":"BgcICQA="}`, |
| 69 | }, |
| 70 | } |
| 71 | for i, test := range tests { |
| 72 | b, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(&test.ext) |
| 73 | if err != nil { |
| 74 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: failed to marshal: %s", i, err) |
| 75 | continue |
| 76 | } |
| 77 | if s := string(b); s != test.expected { |
| 78 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: expected %s, got %s", i, test.expected, s) |
| 79 | } |
| 80 | parsed := Extensionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#{} |
| 81 | if err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Unmarshal().(b, &parsed); err != nil { |
| 82 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: could not unmarshal: %s", i, err) |
| 83 | } |
| 84 | if !test.ext.Idscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Id..Equalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#Equal().(parsed.Idscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Id.) { |
| 85 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: unmarshaled Id mismatch, expected %v, got %v", i, test.ext.Idscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Id., parsed.Idscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Extension#Id.) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestNameJSONscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/TestNameJSON().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 91 | tests := []struct { |
| 92 | name Namescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name# |
| 93 | expected string |
| 94 | }{ |
| 95 | { |
| 96 | name: Namescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#{}, |
| 97 | expected: `{}`, |
| 98 | }, |
| 99 | { |
| 100 | name: Namescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#{ |
| 101 | SerialNumberscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#SerialNumber.: "12345", |
| 102 | CommonNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#CommonName.: "common", |
| 103 | Countryscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#Country.: []string{"US", "RU"}, |
| 104 | Organizationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#Organization.: []string{"University of Michigan"}, |
| 105 | OrganizationalUnitscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#OrganizationalUnit.: []string{"0x21"}, |
| 106 | Localityscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#Locality.: []string{"Ann Arbor"}, |
| 107 | Provincescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#Province.: []string{"Michigan"}, |
| 108 | StreetAddressscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#StreetAddress.: []string{"2260 Hayward St"}, |
| 109 | PostalCodescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#PostalCode.: []string{"48109"}, |
| 110 | DomainComponentscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#DomainComponent.: nil, |
| 111 | ExtraNamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#ExtraNames.: []AttributeTypeAndValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#{{Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type.: oidCommonNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/oidCommonName., Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value.: "name"}, {Typescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Type.: oidSerialNumberscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/oidSerialNumber., Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AttributeTypeAndValue#Value.: "67890"}}, |
| 112 | }, |
| 113 | expected: `{"common_name":["common","name"],"serial_number":["12345","67890"],"country":["US","RU"],"locality":["Ann Arbor"],"province":["Michigan"],"street_address":["2260 Hayward St"],"organization":["University of Michigan"],"organizational_unit":["0x21"],"postal_code":["48109"]}`, |
| 114 | }, |
| 115 | } |
| 116 | for i, test := range tests { |
| 117 | b, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(&test.name) |
| 118 | if len(test.expected) > 0 && err != nil { |
| 119 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: failed marshal: %s", i, err) |
| 120 | continue |
| 121 | } |
| 122 | if s := string(b); s != test.expected { |
| 123 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: expected %s, got %s", i, test.expected, s) |
| 124 | } |
| 125 | parsed := Namescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/Name#{} |
| 126 | if err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Unmarshal().(b, &parsed); err != nil { |
| 127 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: could not unmarshal: %s", i, err) |
| 128 | } |
| 129 | // TODO: Check equality of parsed and original. |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestOtherNameJSONscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/TestOtherNameJSON().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 134 | tests := []struct { |
| 135 | otherName OtherNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/OtherName# |
| 136 | expected string |
| 137 | unmarshal bool |
| 138 | }{ |
| 139 | { |
| 140 | otherName: OtherNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/OtherName#{}, |
| 141 | expected: `{}`, |
| 142 | unmarshal: false, |
| 143 | }, |
| 144 | { |
| 145 | otherName: OtherNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/OtherName#{ |
| 146 | TypeIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/OtherName#TypeID.: asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#{2, 16, 840, 1, 113730, 2, 2}, // image/JPEG 2.16.840.1.113730.2.2 |
| 147 | Valuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/OtherName#Value.: asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.RawValuescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/RawValue#{ |
| 148 | Tagscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/RawValue#Tag.: 0, |
| 149 | Classscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/RawValue#Class.: asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.ClassContextSpecificscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ClassContextSpecific., |
| 150 | IsCompoundscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/RawValue#IsCompound.: true, |
| 151 | Bytesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/RawValue#Bytes.: []byte{0, 1, 2, 3, 4, 5}, |
| 152 | }, |
| 153 | }, |
| 154 | expected: `{"id":"2.16.840.1.113730.2.2","value":"AAECAwQF"}`, |
| 155 | unmarshal: true, |
| 156 | }, |
| 157 | } |
| 158 | for i, test := range tests { |
| 159 | b, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(&test.otherName) |
| 160 | if len(test.expected) > 0 && err != nil { |
| 161 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: unabled to marshal: %s", i, err) |
| 162 | continue |
| 163 | } |
| 164 | if s := string(b); s != test.expected { |
| 165 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: expected %s, got %s", i, test.expected, s) |
| 166 | } |
| 167 | parsed := OtherNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/OtherName#{} |
| 168 | if test.unmarshal { |
| 169 | if err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Unmarshal().(b, &parsed); err != nil { |
| 170 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%d: could not unmarshal: %s", i, err) |
| 171 | } |
| 172 | // TODO: Check equality of parsed and original |
| 173 | } |
| 174 | } |
| 175 | } |