extensions_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. package x509util
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "encoding/asn1"
  6. "encoding/json"
  7. "math/big"
  8. "net"
  9. "net/url"
  10. "reflect"
  11. "testing"
  12. )
  13. func Test_convertName(t *testing.T) {
  14. type args struct {
  15. s string
  16. }
  17. tests := []struct {
  18. name string
  19. args args
  20. want string
  21. }{
  22. {"lowerCase", args{"FooBAR"}, "foobar"},
  23. {"underscore", args{"foo_bar"}, "foobar"},
  24. {"mixed", args{"FOO_Bar"}, "foobar"},
  25. }
  26. for _, tt := range tests {
  27. t.Run(tt.name, func(t *testing.T) {
  28. if got := convertName(tt.args.s); got != tt.want {
  29. t.Errorf("convertName() = %v, want %v", got, tt.want)
  30. }
  31. })
  32. }
  33. }
  34. func Test_newExtension(t *testing.T) {
  35. type args struct {
  36. e pkix.Extension
  37. }
  38. tests := []struct {
  39. name string
  40. args args
  41. want Extension
  42. }{
  43. {"ok", args{pkix.Extension{Id: []int{1, 2, 3, 4}, Value: []byte("foo")}}, Extension{ID: []int{1, 2, 3, 4}, Critical: false, Value: []byte("foo")}},
  44. {"critical", args{pkix.Extension{Id: []int{1, 2, 3, 4}, Critical: true, Value: []byte("foo")}}, Extension{ID: []int{1, 2, 3, 4}, Critical: true, Value: []byte("foo")}},
  45. }
  46. for _, tt := range tests {
  47. t.Run(tt.name, func(t *testing.T) {
  48. if got := newExtension(tt.args.e); !reflect.DeepEqual(got, tt.want) {
  49. t.Errorf("newExtension() = %v, want %v", got, tt.want)
  50. }
  51. })
  52. }
  53. }
  54. func Test_newExtensions(t *testing.T) {
  55. ext1 := pkix.Extension{Id: []int{1, 2, 3, 4}, Value: []byte("foo")}
  56. ext2 := pkix.Extension{Id: []int{4, 3, 2, 1}, Critical: true, Value: []byte("bar")}
  57. type args struct {
  58. extensions []pkix.Extension
  59. }
  60. tests := []struct {
  61. name string
  62. args args
  63. want []Extension
  64. }{
  65. {"ok", args{[]pkix.Extension{ext1, ext2}}, []Extension{
  66. {ID: []int{1, 2, 3, 4}, Critical: false, Value: []byte("foo")},
  67. {ID: []int{4, 3, 2, 1}, Critical: true, Value: []byte("bar")},
  68. }},
  69. {"nil", args{}, nil},
  70. }
  71. for _, tt := range tests {
  72. t.Run(tt.name, func(t *testing.T) {
  73. if got := newExtensions(tt.args.extensions); !reflect.DeepEqual(got, tt.want) {
  74. t.Errorf("newExtensions() = %v, want %v", got, tt.want)
  75. }
  76. })
  77. }
  78. }
  79. func TestExtension_Set(t *testing.T) {
  80. type fields struct {
  81. ID ObjectIdentifier
  82. Critical bool
  83. Value []byte
  84. }
  85. type args struct {
  86. c *x509.Certificate
  87. }
  88. tests := []struct {
  89. name string
  90. fields fields
  91. args args
  92. want *x509.Certificate
  93. }{
  94. {"ok", fields{[]int{1, 2, 3, 4}, true, []byte("foo")}, args{&x509.Certificate{}}, &x509.Certificate{
  95. ExtraExtensions: []pkix.Extension{{Id: []int{1, 2, 3, 4}, Critical: true, Value: []byte("foo")}},
  96. }},
  97. {"existing", fields{[]int{1, 2, 3, 4}, true, []byte("foo")}, args{&x509.Certificate{
  98. ExtraExtensions: []pkix.Extension{
  99. {Id: []int{1, 1, 1, 1}, Critical: false, Value: []byte("foo")},
  100. },
  101. }}, &x509.Certificate{
  102. ExtraExtensions: []pkix.Extension{
  103. {Id: []int{1, 1, 1, 1}, Critical: false, Value: []byte("foo")},
  104. {Id: []int{1, 2, 3, 4}, Critical: true, Value: []byte("foo")},
  105. },
  106. }},
  107. }
  108. for _, tt := range tests {
  109. t.Run(tt.name, func(t *testing.T) {
  110. e := Extension{
  111. ID: tt.fields.ID,
  112. Critical: tt.fields.Critical,
  113. Value: tt.fields.Value,
  114. }
  115. e.Set(tt.args.c)
  116. if !reflect.DeepEqual(tt.args.c, tt.want) {
  117. t.Errorf("Extension.Set() = %v, want %v", tt.args.c, tt.want)
  118. }
  119. })
  120. }
  121. }
  122. func TestObjectIdentifier_MarshalJSON(t *testing.T) {
  123. tests := []struct {
  124. name string
  125. o ObjectIdentifier
  126. want []byte
  127. wantErr bool
  128. }{
  129. {"ok", []int{1, 2, 3, 4}, []byte(`"1.2.3.4"`), false},
  130. {"empty", []int{}, []byte(`""`), false},
  131. {"nil", nil, []byte(`""`), false},
  132. }
  133. for _, tt := range tests {
  134. t.Run(tt.name, func(t *testing.T) {
  135. got, err := tt.o.MarshalJSON()
  136. if (err != nil) != tt.wantErr {
  137. t.Errorf("ObjectIdentifier.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  138. return
  139. }
  140. if !reflect.DeepEqual(got, tt.want) {
  141. t.Errorf("ObjectIdentifier.MarshalJSON() = %v, want %v", got, tt.want)
  142. }
  143. })
  144. }
  145. }
  146. func TestObjectIdentifier_UnmarshalJSON(t *testing.T) {
  147. type args struct {
  148. data []byte
  149. }
  150. tests := []struct {
  151. name string
  152. args args
  153. want ObjectIdentifier
  154. wantErr bool
  155. }{
  156. {"ok", args{[]byte(`"1.2.3.4"`)}, []int{1, 2, 3, 4}, false},
  157. {"empty", args{[]byte(`""`)}, []int{}, false},
  158. {"null", args{[]byte(`null`)}, []int{}, false},
  159. {"number", args{[]byte(`123`)}, nil, true},
  160. {"badFormat", args{[]byte(`"1.2.foo.4"`)}, nil, true},
  161. }
  162. for _, tt := range tests {
  163. t.Run(tt.name, func(t *testing.T) {
  164. var got ObjectIdentifier
  165. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  166. t.Errorf("ObjectIdentifier.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  167. }
  168. if !reflect.DeepEqual(got, tt.want) {
  169. t.Errorf("ObjectIdentifier.UnmarshalJSON() = %v, want %v", got, tt.want)
  170. }
  171. })
  172. }
  173. }
  174. func TestSubjectAlternativeName_Set(t *testing.T) {
  175. panicCount := 0
  176. type fields struct {
  177. Type string
  178. Value string
  179. }
  180. type args struct {
  181. c *x509.Certificate
  182. }
  183. tests := []struct {
  184. name string
  185. fields fields
  186. args args
  187. want *x509.Certificate
  188. }{
  189. {"dns", fields{"dns", "foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{DNSNames: []string{"foo.com"}}},
  190. {"dnsAdd", fields{"DNS", "bar.com"}, args{&x509.Certificate{DNSNames: []string{"foo.com"}}}, &x509.Certificate{DNSNames: []string{"foo.com", "bar.com"}}},
  191. {"email", fields{"email", "john@doe.com"}, args{&x509.Certificate{}}, &x509.Certificate{EmailAddresses: []string{"john@doe.com"}}},
  192. {"emailAdd", fields{"EMAIL", "jane@doe.com"}, args{&x509.Certificate{EmailAddresses: []string{"john@doe.com"}}}, &x509.Certificate{EmailAddresses: []string{"john@doe.com", "jane@doe.com"}}},
  193. {"ip", fields{"ip", "127.0.0.1"}, args{&x509.Certificate{}}, &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}},
  194. {"ipAdd", fields{"IP", "::1"}, args{&x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}}, &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}}},
  195. {"ipBad", fields{"IP", "fooo"}, args{&x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}}, &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}},
  196. {"uri", fields{"uri", "https://foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}},
  197. {"uriAdd", fields{"URI", "uri:foo:bar"}, args{&x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}, {Scheme: "uri", Opaque: "foo:bar"}}}},
  198. {"uriBad", fields{"URI", "::1"}, args{&x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}},
  199. {"AutoDNS", fields{"", "foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{DNSNames: []string{"foo.com"}}},
  200. {"AutoDNSAdd", fields{"auto", "bar.com"}, args{&x509.Certificate{DNSNames: []string{"foo.com"}}}, &x509.Certificate{DNSNames: []string{"foo.com", "bar.com"}}},
  201. {"AutoEmail", fields{"AUTO", "john@doe.com"}, args{&x509.Certificate{}}, &x509.Certificate{EmailAddresses: []string{"john@doe.com"}}},
  202. {"AutoEmailAdd", fields{"", "jane@doe.com"}, args{&x509.Certificate{EmailAddresses: []string{"john@doe.com"}}}, &x509.Certificate{EmailAddresses: []string{"john@doe.com", "jane@doe.com"}}},
  203. {"IPAutoIP", fields{"AutO", "127.0.0.1"}, args{&x509.Certificate{}}, &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}},
  204. {"AutoIPAdd", fields{"", "::1"}, args{&x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}}, &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}}},
  205. {"AutoURI", fields{"Auto", "https://foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}},
  206. {"AutoURIAdd", fields{"", "uri:foo:bar"}, args{&x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}, {Scheme: "uri", Opaque: "foo:bar"}}}},
  207. {"panic", fields{"panic", "foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{DNSNames: []string{"foo.com"}}},
  208. {"panicAdd", fields{"panic", "bar.com"}, args{&x509.Certificate{DNSNames: []string{"foo.com"}}}, &x509.Certificate{DNSNames: []string{"foo.com"}}},
  209. }
  210. for _, tt := range tests {
  211. t.Run(tt.name, func(t *testing.T) {
  212. defer func() {
  213. if r := recover(); r != nil {
  214. panicCount++
  215. }
  216. }()
  217. s := SubjectAlternativeName{
  218. Type: tt.fields.Type,
  219. Value: tt.fields.Value,
  220. }
  221. s.Set(tt.args.c)
  222. if !reflect.DeepEqual(tt.args.c, tt.want) {
  223. t.Errorf("SubjectAlternativeName.Set() = %v, want %v", tt.args.c, tt.want)
  224. }
  225. })
  226. }
  227. if panicCount != 2 {
  228. t.Errorf("SubjectAlternativeName.Set() number of panics = %d, want 2", panicCount)
  229. }
  230. }
  231. func TestKeyUsage_Set(t *testing.T) {
  232. type args struct {
  233. c *x509.Certificate
  234. }
  235. tests := []struct {
  236. name string
  237. k KeyUsage
  238. args args
  239. want *x509.Certificate
  240. }{
  241. {"ok", KeyUsage(x509.KeyUsageDigitalSignature), args{&x509.Certificate{}}, &x509.Certificate{KeyUsage: x509.KeyUsageDigitalSignature}},
  242. {"overwrite", KeyUsage(x509.KeyUsageCRLSign | x509.KeyUsageCertSign), args{&x509.Certificate{KeyUsage: x509.KeyUsageDigitalSignature}}, &x509.Certificate{KeyUsage: x509.KeyUsageCRLSign | x509.KeyUsageCertSign}},
  243. }
  244. for _, tt := range tests {
  245. t.Run(tt.name, func(t *testing.T) {
  246. tt.k.Set(tt.args.c)
  247. if !reflect.DeepEqual(tt.args.c, tt.want) {
  248. t.Errorf("KeyUsage.Set() = %v, want %v", tt.args.c, tt.want)
  249. }
  250. })
  251. }
  252. }
  253. func TestKeyUsage_UnmarshalJSON(t *testing.T) {
  254. type args struct {
  255. data []byte
  256. }
  257. tests := []struct {
  258. name string
  259. args args
  260. want KeyUsage
  261. wantErr bool
  262. }{
  263. // Normalized
  264. {"DigitalSignature", args{[]byte(`"DigitalSignature"`)}, KeyUsage(x509.KeyUsageDigitalSignature), false},
  265. {"ContentCommitment", args{[]byte(`"ContentCommitment"`)}, KeyUsage(x509.KeyUsageContentCommitment), false},
  266. {"KeyEncipherment", args{[]byte(`"KeyEncipherment"`)}, KeyUsage(x509.KeyUsageKeyEncipherment), false},
  267. {"DataEncipherment", args{[]byte(`"DataEncipherment"`)}, KeyUsage(x509.KeyUsageDataEncipherment), false},
  268. {"KeyAgreement", args{[]byte(`"KeyAgreement"`)}, KeyUsage(x509.KeyUsageKeyAgreement), false},
  269. {"CertSign", args{[]byte(`"CertSign"`)}, KeyUsage(x509.KeyUsageCertSign), false},
  270. {"CRLSign", args{[]byte(`"CRLSign"`)}, KeyUsage(x509.KeyUsageCRLSign), false},
  271. {"EncipherOnly", args{[]byte(`"EncipherOnly"`)}, KeyUsage(x509.KeyUsageEncipherOnly), false},
  272. {"DecipherOnly", args{[]byte(`"DecipherOnly"`)}, KeyUsage(x509.KeyUsageDecipherOnly), false},
  273. // Snake case
  274. {"digital_signature", args{[]byte(`"digital_signature"`)}, KeyUsage(x509.KeyUsageDigitalSignature), false},
  275. {"content_commitment", args{[]byte(`"content_commitment"`)}, KeyUsage(x509.KeyUsageContentCommitment), false},
  276. {"key_encipherment", args{[]byte(`"key_encipherment"`)}, KeyUsage(x509.KeyUsageKeyEncipherment), false},
  277. {"data_encipherment", args{[]byte(`"data_encipherment"`)}, KeyUsage(x509.KeyUsageDataEncipherment), false},
  278. {"key_agreement", args{[]byte(`"key_agreement"`)}, KeyUsage(x509.KeyUsageKeyAgreement), false},
  279. {"cert_sign", args{[]byte(`"cert_sign"`)}, KeyUsage(x509.KeyUsageCertSign), false},
  280. {"crl_sign", args{[]byte(`"crl_sign"`)}, KeyUsage(x509.KeyUsageCRLSign), false},
  281. {"encipher_only", args{[]byte(`"encipher_only"`)}, KeyUsage(x509.KeyUsageEncipherOnly), false},
  282. {"decipher_only", args{[]byte(`"decipher_only"`)}, KeyUsage(x509.KeyUsageDecipherOnly), false},
  283. // MultiString
  284. {"DigitalSignatureAsArray", args{[]byte(`["digital_signature"]`)}, KeyUsage(x509.KeyUsageDigitalSignature), false},
  285. {"DigitalSignature|KeyEncipherment", args{[]byte(`["DigitalSignature", "key_encipherment"]`)}, KeyUsage(x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment), false},
  286. // Errors
  287. {"invalid", args{[]byte(`"invalid"`)}, KeyUsage(0), true},
  288. {"number", args{[]byte(`123`)}, KeyUsage(0), true},
  289. {"object", args{[]byte(`{}`)}, KeyUsage(0), true},
  290. {"badJSON", args{[]byte(`{`)}, KeyUsage(0), true},
  291. }
  292. for _, tt := range tests {
  293. t.Run(tt.name, func(t *testing.T) {
  294. var got KeyUsage
  295. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  296. t.Errorf("KeyUsage.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  297. }
  298. if !reflect.DeepEqual(got, tt.want) {
  299. t.Errorf("KeyUsage.UnmarshalJSON() = %v, want %v", got, tt.want)
  300. }
  301. })
  302. }
  303. }
  304. func TestExtKeyUsage_Set(t *testing.T) {
  305. eku1 := []x509.ExtKeyUsage{
  306. x509.ExtKeyUsageClientAuth,
  307. x509.ExtKeyUsageServerAuth,
  308. }
  309. eku2 := []x509.ExtKeyUsage{
  310. x509.ExtKeyUsageCodeSigning,
  311. }
  312. type args struct {
  313. c *x509.Certificate
  314. }
  315. tests := []struct {
  316. name string
  317. k ExtKeyUsage
  318. args args
  319. want *x509.Certificate
  320. }{
  321. {"ok", ExtKeyUsage(eku1), args{&x509.Certificate{}}, &x509.Certificate{ExtKeyUsage: eku1}},
  322. {"overwrite", ExtKeyUsage(eku2), args{&x509.Certificate{ExtKeyUsage: eku1}}, &x509.Certificate{ExtKeyUsage: eku2}},
  323. }
  324. for _, tt := range tests {
  325. t.Run(tt.name, func(t *testing.T) {
  326. tt.k.Set(tt.args.c)
  327. if !reflect.DeepEqual(tt.args.c, tt.want) {
  328. t.Errorf("ExtKeyUsage.Set() = %v, want %v", tt.args.c, tt.want)
  329. }
  330. })
  331. }
  332. }
  333. func TestExtKeyUsage_UnmarshalJSON(t *testing.T) {
  334. type args struct {
  335. data []byte
  336. }
  337. tests := []struct {
  338. name string
  339. args args
  340. want ExtKeyUsage
  341. wantErr bool
  342. }{
  343. // Normalized
  344. {"Any", args{[]byte(`"Any"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageAny}), false},
  345. {"ServerAuth", args{[]byte(`"ServerAuth"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}), false},
  346. {"ClientAuth", args{[]byte(`"ClientAuth"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}), false},
  347. {"CodeSigning", args{[]byte(`"CodeSigning"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}), false},
  348. {"EmailProtection", args{[]byte(`"EmailProtection"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageEmailProtection}), false},
  349. {"IPSECEndSystem", args{[]byte(`"IPSECEndSystem"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageIPSECEndSystem}), false},
  350. {"IPSECTunnel", args{[]byte(`"IPSECTunnel"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageIPSECTunnel}), false},
  351. {"IPSECUser", args{[]byte(`"IPSECUser"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageIPSECUser}), false},
  352. {"TimeStamping", args{[]byte(`"TimeStamping"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping}), false},
  353. {"OCSPSigning", args{[]byte(`"OCSPSigning"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageOCSPSigning}), false},
  354. {"MicrosoftServerGatedCrypto", args{[]byte(`"MicrosoftServerGatedCrypto"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageMicrosoftServerGatedCrypto}), false},
  355. {"NetscapeServerGatedCrypto", args{[]byte(`"NetscapeServerGatedCrypto"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageNetscapeServerGatedCrypto}), false},
  356. {"MicrosoftCommercialCodeSigning", args{[]byte(`"MicrosoftCommercialCodeSigning"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageMicrosoftCommercialCodeSigning}), false},
  357. {"MicrosoftKernelCodeSigning", args{[]byte(`"MicrosoftKernelCodeSigning"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageMicrosoftKernelCodeSigning}), false},
  358. // Snake case
  359. {"any", args{[]byte(`"any"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageAny}), false},
  360. {"server_auth", args{[]byte(`"server_auth"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}), false},
  361. {"client_auth", args{[]byte(`"client_auth"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}), false},
  362. {"code_signing", args{[]byte(`"code_signing"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}), false},
  363. {"email_protection", args{[]byte(`"email_protection"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageEmailProtection}), false},
  364. {"ipsec_end_system", args{[]byte(`"ipsec_end_system"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageIPSECEndSystem}), false},
  365. {"ipsec_tunnel", args{[]byte(`"ipsec_tunnel"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageIPSECTunnel}), false},
  366. {"ipsec_user", args{[]byte(`"ipsec_user"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageIPSECUser}), false},
  367. {"time_stamping", args{[]byte(`"time_stamping"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping}), false},
  368. {"ocsp_signing", args{[]byte(`"ocsp_signing"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageOCSPSigning}), false},
  369. {"microsoft_server_gated_crypto", args{[]byte(`"microsoft_server_gated_crypto"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageMicrosoftServerGatedCrypto}), false},
  370. {"netscape_server_gated_crypto", args{[]byte(`"netscape_server_gated_crypto"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageNetscapeServerGatedCrypto}), false},
  371. {"microsoft_commercial_code_signing", args{[]byte(`"microsoft_commercial_code_signing"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageMicrosoftCommercialCodeSigning}), false},
  372. {"microsoft_kernel_code_signing", args{[]byte(`"microsoft_kernel_code_signing"`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageMicrosoftKernelCodeSigning}), false},
  373. // Multistring
  374. {"CodeSigningAsArray", args{[]byte(`["code_signing"]`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}), false},
  375. {"ServerAuth+ClientAuth", args{[]byte(`["ServerAuth","client_auth"]`)}, ExtKeyUsage([]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}), false},
  376. // Errors
  377. {"invalid", args{[]byte(`"invalid"`)}, nil, true},
  378. {"number", args{[]byte(`123`)}, nil, true},
  379. {"object", args{[]byte(`{}`)}, nil, true},
  380. {"badJSON", args{[]byte(`{`)}, nil, true},
  381. }
  382. for _, tt := range tests {
  383. t.Run(tt.name, func(t *testing.T) {
  384. var got ExtKeyUsage
  385. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  386. t.Errorf("ExtKeyUsage.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  387. }
  388. if !reflect.DeepEqual(got, tt.want) {
  389. t.Errorf("ExtKeyUsage.UnmarshalJSON() = %v, want %v", got, tt.want)
  390. }
  391. })
  392. }
  393. }
  394. func TestSubjectKeyID_Set(t *testing.T) {
  395. type args struct {
  396. c *x509.Certificate
  397. }
  398. tests := []struct {
  399. name string
  400. id SubjectKeyID
  401. args args
  402. want *x509.Certificate
  403. }{
  404. {"ok", []byte("subjectKeyID"), args{&x509.Certificate{}}, &x509.Certificate{SubjectKeyId: []byte("subjectKeyID")}},
  405. {"overwrite", []byte("newSubjectKeyID"), args{&x509.Certificate{SubjectKeyId: []byte("subjectKeyID")}}, &x509.Certificate{SubjectKeyId: []byte("newSubjectKeyID")}},
  406. }
  407. for _, tt := range tests {
  408. t.Run(tt.name, func(t *testing.T) {
  409. tt.id.Set(tt.args.c)
  410. if !reflect.DeepEqual(tt.args.c, tt.want) {
  411. t.Errorf("SubjectKeyID.Set() = %v, want %v", tt.args.c, tt.want)
  412. }
  413. })
  414. }
  415. }
  416. func TestAuthorityKeyID_Set(t *testing.T) {
  417. type args struct {
  418. c *x509.Certificate
  419. }
  420. tests := []struct {
  421. name string
  422. id AuthorityKeyID
  423. args args
  424. want *x509.Certificate
  425. }{
  426. {"ok", []byte("authorityKeyID"), args{&x509.Certificate{}}, &x509.Certificate{AuthorityKeyId: []byte("authorityKeyID")}},
  427. {"overwrite", []byte("newAuthorityKeyID"), args{&x509.Certificate{AuthorityKeyId: []byte("authorityKeyID")}}, &x509.Certificate{AuthorityKeyId: []byte("newAuthorityKeyID")}},
  428. }
  429. for _, tt := range tests {
  430. t.Run(tt.name, func(t *testing.T) {
  431. tt.id.Set(tt.args.c)
  432. if !reflect.DeepEqual(tt.args.c, tt.want) {
  433. t.Errorf("AuthorityKeyID.Set() = %v, want %v", tt.args.c, tt.want)
  434. }
  435. })
  436. }
  437. }
  438. func TestOCSPServer_UnmarshalJSON(t *testing.T) {
  439. type args struct {
  440. data []byte
  441. }
  442. tests := []struct {
  443. name string
  444. args args
  445. want OCSPServer
  446. wantErr bool
  447. }{
  448. {"string", args{[]byte(`"foo"`)}, []string{"foo"}, false},
  449. {"array", args{[]byte(`["foo", "bar", "zar"]`)}, []string{"foo", "bar", "zar"}, false},
  450. {"empty", args{[]byte(`[]`)}, []string{}, false},
  451. {"null", args{[]byte(`null`)}, nil, false},
  452. {"fail", args{[]byte(`["foo"`)}, nil, true},
  453. }
  454. for _, tt := range tests {
  455. t.Run(tt.name, func(t *testing.T) {
  456. var got OCSPServer
  457. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  458. t.Errorf("OCSPServer.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  459. }
  460. if !reflect.DeepEqual(got, tt.want) {
  461. t.Errorf("OCSPServer.UnmarshalJSON() = %v, want %v", got, tt.want)
  462. }
  463. })
  464. }
  465. }
  466. func TestOCSPServer_Set(t *testing.T) {
  467. type args struct {
  468. c *x509.Certificate
  469. }
  470. tests := []struct {
  471. name string
  472. o OCSPServer
  473. args args
  474. want *x509.Certificate
  475. }{
  476. {"ok", []string{"oscp.server"}, args{&x509.Certificate{}}, &x509.Certificate{OCSPServer: []string{"oscp.server"}}},
  477. {"overwrite", []string{"oscp.server", "oscp.com"}, args{&x509.Certificate{OCSPServer: []string{"oscp.server"}}}, &x509.Certificate{OCSPServer: []string{"oscp.server", "oscp.com"}}},
  478. }
  479. for _, tt := range tests {
  480. t.Run(tt.name, func(t *testing.T) {
  481. tt.o.Set(tt.args.c)
  482. if !reflect.DeepEqual(tt.args.c, tt.want) {
  483. t.Errorf("OCSPServer.Set() = %v, want %v", tt.args.c, tt.want)
  484. }
  485. })
  486. }
  487. }
  488. func TestIssuingCertificateURL_UnmarshalJSON(t *testing.T) {
  489. type args struct {
  490. data []byte
  491. }
  492. tests := []struct {
  493. name string
  494. args args
  495. want IssuingCertificateURL
  496. wantErr bool
  497. }{
  498. {"string", args{[]byte(`"foo"`)}, []string{"foo"}, false},
  499. {"array", args{[]byte(`["foo", "bar", "zar"]`)}, []string{"foo", "bar", "zar"}, false},
  500. {"empty", args{[]byte(`[]`)}, []string{}, false},
  501. {"null", args{[]byte(`null`)}, nil, false},
  502. {"fail", args{[]byte(`["foo"`)}, nil, true},
  503. }
  504. for _, tt := range tests {
  505. t.Run(tt.name, func(t *testing.T) {
  506. var got IssuingCertificateURL
  507. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  508. t.Errorf("IssuingCertificateURL.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  509. }
  510. if !reflect.DeepEqual(got, tt.want) {
  511. t.Errorf("IssuingCertificateURL.UnmarshalJSON() = %v, want %v", got, tt.want)
  512. }
  513. })
  514. }
  515. }
  516. func TestIssuingCertificateURL_Set(t *testing.T) {
  517. type args struct {
  518. c *x509.Certificate
  519. }
  520. tests := []struct {
  521. name string
  522. o IssuingCertificateURL
  523. args args
  524. want *x509.Certificate
  525. }{
  526. {"ok", []string{"issuing.server"}, args{&x509.Certificate{}}, &x509.Certificate{IssuingCertificateURL: []string{"issuing.server"}}},
  527. {"overwrite", []string{"issuing.server", "issuing.com"}, args{&x509.Certificate{IssuingCertificateURL: []string{"issuing.server"}}}, &x509.Certificate{IssuingCertificateURL: []string{"issuing.server", "issuing.com"}}},
  528. }
  529. for _, tt := range tests {
  530. t.Run(tt.name, func(t *testing.T) {
  531. tt.o.Set(tt.args.c)
  532. if !reflect.DeepEqual(tt.args.c, tt.want) {
  533. t.Errorf("IssuingCertificateURL.Set() = %v, want %v", tt.args.c, tt.want)
  534. }
  535. })
  536. }
  537. }
  538. func TestCRLDistributionPoints_UnmarshalJSON(t *testing.T) {
  539. type args struct {
  540. data []byte
  541. }
  542. tests := []struct {
  543. name string
  544. args args
  545. want CRLDistributionPoints
  546. wantErr bool
  547. }{
  548. {"string", args{[]byte(`"foo"`)}, []string{"foo"}, false},
  549. {"array", args{[]byte(`["foo", "bar", "zar"]`)}, []string{"foo", "bar", "zar"}, false},
  550. {"empty", args{[]byte(`[]`)}, []string{}, false},
  551. {"null", args{[]byte(`null`)}, nil, false},
  552. {"fail", args{[]byte(`["foo"`)}, nil, true},
  553. }
  554. for _, tt := range tests {
  555. t.Run(tt.name, func(t *testing.T) {
  556. var got CRLDistributionPoints
  557. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  558. t.Errorf("CRLDistributionPoints.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  559. }
  560. if !reflect.DeepEqual(got, tt.want) {
  561. t.Errorf("CRLDistributionPoints.UnmarshalJSON() = %v, want %v", got, tt.want)
  562. }
  563. })
  564. }
  565. }
  566. func TestCRLDistributionPoints_Set(t *testing.T) {
  567. type args struct {
  568. c *x509.Certificate
  569. }
  570. tests := []struct {
  571. name string
  572. o CRLDistributionPoints
  573. args args
  574. want *x509.Certificate
  575. }{
  576. {"ok", []string{"crl.server"}, args{&x509.Certificate{}}, &x509.Certificate{CRLDistributionPoints: []string{"crl.server"}}},
  577. {"overwrite", []string{"crl.server", "crl.com"}, args{&x509.Certificate{CRLDistributionPoints: []string{"crl.server"}}}, &x509.Certificate{CRLDistributionPoints: []string{"crl.server", "crl.com"}}},
  578. }
  579. for _, tt := range tests {
  580. t.Run(tt.name, func(t *testing.T) {
  581. tt.o.Set(tt.args.c)
  582. if !reflect.DeepEqual(tt.args.c, tt.want) {
  583. t.Errorf("CRLDistributionPoints.Set() = %v, want %v", tt.args.c, tt.want)
  584. }
  585. })
  586. }
  587. }
  588. func TestPolicyIdentifiers_MarshalJSON(t *testing.T) {
  589. tests := []struct {
  590. name string
  591. m PolicyIdentifiers
  592. want []byte
  593. wantErr bool
  594. }{
  595. {"ok", []asn1.ObjectIdentifier{[]int{1, 2, 3, 4}, []int{5, 6, 7, 8, 9, 0}}, []byte(`["1.2.3.4","5.6.7.8.9.0"]`), false},
  596. {"empty", []asn1.ObjectIdentifier{}, []byte(`[]`), false},
  597. {"nil", nil, []byte(`null`), false},
  598. }
  599. for _, tt := range tests {
  600. t.Run(tt.name, func(t *testing.T) {
  601. got, err := json.Marshal(tt.m)
  602. if (err != nil) != tt.wantErr {
  603. t.Errorf("PolicyIdentifiers.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  604. return
  605. }
  606. if !reflect.DeepEqual(got, tt.want) {
  607. t.Errorf("PolicyIdentifiers.MarshalJSON() = %v, want %v", got, tt.want)
  608. }
  609. })
  610. }
  611. }
  612. func TestPolicyIdentifiers_UnmarshalJSON(t *testing.T) {
  613. type args struct {
  614. data []byte
  615. }
  616. tests := []struct {
  617. name string
  618. args args
  619. want PolicyIdentifiers
  620. wantErr bool
  621. }{
  622. {"string", args{[]byte(`"1.2.3.4"`)}, []asn1.ObjectIdentifier{[]int{1, 2, 3, 4}}, false},
  623. {"array", args{[]byte(`["1.2.3.4", "5.6.7.8.9.0"]`)}, []asn1.ObjectIdentifier{[]int{1, 2, 3, 4}, []int{5, 6, 7, 8, 9, 0}}, false},
  624. {"empty", args{[]byte(`[]`)}, []asn1.ObjectIdentifier{}, false},
  625. {"null", args{[]byte(`null`)}, nil, false},
  626. {"fail", args{[]byte(`":foo:bar"`)}, nil, true},
  627. {"failJSON", args{[]byte(`["https://iss#sub"`)}, nil, true},
  628. }
  629. for _, tt := range tests {
  630. t.Run(tt.name, func(t *testing.T) {
  631. var got PolicyIdentifiers
  632. if err := got.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  633. t.Errorf("PolicyIdentifiers.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  634. }
  635. if !reflect.DeepEqual(got, tt.want) {
  636. t.Errorf("PolicyIdentifiers.UnmarshalJSON() = %v, want %v", got, tt.want)
  637. }
  638. })
  639. }
  640. }
  641. func TestPolicyIdentifiers_Set(t *testing.T) {
  642. type args struct {
  643. c *x509.Certificate
  644. }
  645. tests := []struct {
  646. name string
  647. o PolicyIdentifiers
  648. args args
  649. want *x509.Certificate
  650. }{
  651. {"ok", []asn1.ObjectIdentifier{{1, 2, 3, 4}}, args{&x509.Certificate{}}, &x509.Certificate{PolicyIdentifiers: []asn1.ObjectIdentifier{{1, 2, 3, 4}}}},
  652. {"overwrite", []asn1.ObjectIdentifier{{1, 2, 3, 4}, {4, 3, 2, 1}}, args{&x509.Certificate{PolicyIdentifiers: []asn1.ObjectIdentifier{{1, 2, 3, 4}}}}, &x509.Certificate{PolicyIdentifiers: []asn1.ObjectIdentifier{{1, 2, 3, 4}, {4, 3, 2, 1}}}},
  653. }
  654. for _, tt := range tests {
  655. t.Run(tt.name, func(t *testing.T) {
  656. tt.o.Set(tt.args.c)
  657. if !reflect.DeepEqual(tt.args.c, tt.want) {
  658. t.Errorf("PolicyIdentifiers.Set() = %v, want %v", tt.args.c, tt.want)
  659. }
  660. })
  661. }
  662. }
  663. func TestBasicConstraints_Set(t *testing.T) {
  664. type fields struct {
  665. IsCA bool
  666. MaxPathLen int
  667. }
  668. type args struct {
  669. c *x509.Certificate
  670. }
  671. tests := []struct {
  672. name string
  673. fields fields
  674. args args
  675. want *x509.Certificate
  676. }{
  677. {"IsCAFalse", fields{false, 0}, args{&x509.Certificate{}}, &x509.Certificate{}},
  678. {"IsCAFalseWithPathLen", fields{false, 1}, args{&x509.Certificate{}}, &x509.Certificate{}},
  679. {"IsCAFalseWithAnyPathLen", fields{false, -1}, args{&x509.Certificate{}}, &x509.Certificate{}},
  680. {"IsCATrue", fields{true, 0}, args{&x509.Certificate{}}, &x509.Certificate{IsCA: true, MaxPathLen: 0, MaxPathLenZero: true, BasicConstraintsValid: true}},
  681. {"IsCATrueWithPathLen", fields{true, 1}, args{&x509.Certificate{}}, &x509.Certificate{IsCA: true, MaxPathLen: 1, MaxPathLenZero: false, BasicConstraintsValid: true}},
  682. {"IsCATrueWithAnyPathLen", fields{true, -1}, args{&x509.Certificate{}}, &x509.Certificate{IsCA: true, MaxPathLen: -1, MaxPathLenZero: false, BasicConstraintsValid: true}},
  683. {"overwriteToFalse", fields{false, 0}, args{&x509.Certificate{IsCA: true, MaxPathLen: 0, MaxPathLenZero: true, BasicConstraintsValid: true}}, &x509.Certificate{}},
  684. {"overwriteToTrue", fields{true, -100}, args{&x509.Certificate{IsCA: true, MaxPathLen: 0, MaxPathLenZero: true, BasicConstraintsValid: true}}, &x509.Certificate{IsCA: true, MaxPathLen: -1, MaxPathLenZero: false, BasicConstraintsValid: true}},
  685. }
  686. for _, tt := range tests {
  687. t.Run(tt.name, func(t *testing.T) {
  688. b := BasicConstraints{
  689. IsCA: tt.fields.IsCA,
  690. MaxPathLen: tt.fields.MaxPathLen,
  691. }
  692. b.Set(tt.args.c)
  693. if !reflect.DeepEqual(tt.args.c, tt.want) {
  694. t.Errorf("BasicConstraints.Set() = %v, want %v", tt.args.c, tt.want)
  695. }
  696. })
  697. }
  698. }
  699. func TestNameConstraints_Set(t *testing.T) {
  700. ipNet := func(s string) *net.IPNet {
  701. _, ipNet, err := net.ParseCIDR(s)
  702. if err != nil {
  703. t.Fatal(err)
  704. }
  705. return ipNet
  706. }
  707. type fields struct {
  708. Critical bool
  709. PermittedDNSDomains MultiString
  710. ExcludedDNSDomains MultiString
  711. PermittedIPRanges MultiIPNet
  712. ExcludedIPRanges MultiIPNet
  713. PermittedEmailAddresses MultiString
  714. ExcludedEmailAddresses MultiString
  715. PermittedURIDomains MultiString
  716. ExcludedURIDomains MultiString
  717. }
  718. type args struct {
  719. c *x509.Certificate
  720. }
  721. tests := []struct {
  722. name string
  723. fields fields
  724. args args
  725. want *x509.Certificate
  726. }{
  727. {"ok", fields{
  728. Critical: true,
  729. PermittedDNSDomains: []string{"foo.com", "bar.com"},
  730. ExcludedDNSDomains: []string{"zar.com"},
  731. PermittedIPRanges: []*net.IPNet{ipNet("1.2.0.0/16"), ipNet("2.3.4.0/8")},
  732. ExcludedIPRanges: []*net.IPNet{ipNet("3.0.0.0/24")},
  733. PermittedEmailAddresses: []string{"root@foo.com"},
  734. ExcludedEmailAddresses: []string{"admin@foo.com", "root@bar.com", "admin@bar.com"},
  735. PermittedURIDomains: []string{".foo.com", ".bar.com"},
  736. ExcludedURIDomains: []string{".zar.com"},
  737. }, args{&x509.Certificate{}}, &x509.Certificate{
  738. PermittedDNSDomainsCritical: true,
  739. PermittedDNSDomains: []string{"foo.com", "bar.com"},
  740. ExcludedDNSDomains: []string{"zar.com"},
  741. PermittedIPRanges: []*net.IPNet{ipNet("1.2.0.0/16"), ipNet("2.3.4.0/8")},
  742. ExcludedIPRanges: []*net.IPNet{ipNet("3.0.0.0/24")},
  743. PermittedEmailAddresses: []string{"root@foo.com"},
  744. ExcludedEmailAddresses: []string{"admin@foo.com", "root@bar.com", "admin@bar.com"},
  745. PermittedURIDomains: []string{".foo.com", ".bar.com"},
  746. ExcludedURIDomains: []string{".zar.com"},
  747. }},
  748. {"overwrite", fields{}, args{&x509.Certificate{
  749. PermittedDNSDomainsCritical: true,
  750. PermittedDNSDomains: []string{"foo.com", "bar.com"},
  751. ExcludedDNSDomains: []string{"zar.com"},
  752. PermittedIPRanges: []*net.IPNet{ipNet("1.2.0.0/16"), ipNet("2.3.4.0/8")},
  753. ExcludedIPRanges: []*net.IPNet{ipNet("3.0.0.0/24")},
  754. PermittedEmailAddresses: []string{"root@foo.com"},
  755. ExcludedEmailAddresses: []string{"admin@foo.com", "root@bar.com", "admin@bar.com"},
  756. PermittedURIDomains: []string{".foo.com", ".bar.com"},
  757. ExcludedURIDomains: []string{".zar.com"},
  758. }}, &x509.Certificate{}},
  759. }
  760. for _, tt := range tests {
  761. t.Run(tt.name, func(t *testing.T) {
  762. n := NameConstraints{
  763. Critical: tt.fields.Critical,
  764. PermittedDNSDomains: tt.fields.PermittedDNSDomains,
  765. ExcludedDNSDomains: tt.fields.ExcludedDNSDomains,
  766. PermittedIPRanges: tt.fields.PermittedIPRanges,
  767. ExcludedIPRanges: tt.fields.ExcludedIPRanges,
  768. PermittedEmailAddresses: tt.fields.PermittedEmailAddresses,
  769. ExcludedEmailAddresses: tt.fields.ExcludedEmailAddresses,
  770. PermittedURIDomains: tt.fields.PermittedURIDomains,
  771. ExcludedURIDomains: tt.fields.ExcludedURIDomains,
  772. }
  773. n.Set(tt.args.c)
  774. if !reflect.DeepEqual(tt.args.c, tt.want) {
  775. t.Errorf("NameConstraints.Set() = %v, want %v", tt.args.c, tt.want)
  776. }
  777. })
  778. }
  779. }
  780. func TestSerialNumber_Set(t *testing.T) {
  781. type fields struct {
  782. Int *big.Int
  783. }
  784. type args struct {
  785. c *x509.Certificate
  786. }
  787. tests := []struct {
  788. name string
  789. fields fields
  790. args args
  791. want *x509.Certificate
  792. }{
  793. {"ok", fields{big.NewInt(1234)}, args{&x509.Certificate{}}, &x509.Certificate{SerialNumber: big.NewInt(1234)}},
  794. {"overwrite", fields{big.NewInt(4321)}, args{&x509.Certificate{SerialNumber: big.NewInt(1234)}}, &x509.Certificate{SerialNumber: big.NewInt(4321)}},
  795. }
  796. for _, tt := range tests {
  797. t.Run(tt.name, func(t *testing.T) {
  798. s := SerialNumber{
  799. Int: tt.fields.Int,
  800. }
  801. s.Set(tt.args.c)
  802. if !reflect.DeepEqual(tt.args.c, tt.want) {
  803. t.Errorf("SerialNumber.Set() = %v, want %v", tt.args.c, tt.want)
  804. }
  805. })
  806. }
  807. }
  808. func TestSerialNumber_MarshalJSON(t *testing.T) {
  809. tests := []struct {
  810. name string
  811. sn *SerialNumber
  812. want []byte
  813. wantErr bool
  814. }{
  815. {"ok", &SerialNumber{big.NewInt(1234)}, []byte("1234"), false},
  816. {"nilStruct", nil, []byte("null"), false},
  817. {"nilBigInt", &SerialNumber{}, []byte("null"), false},
  818. }
  819. for _, tt := range tests {
  820. t.Run(tt.name, func(t *testing.T) {
  821. got, err := tt.sn.MarshalJSON()
  822. if (err != nil) != tt.wantErr {
  823. t.Errorf("SerialNumber.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  824. return
  825. }
  826. if !reflect.DeepEqual(got, tt.want) {
  827. t.Errorf("SerialNumber.MarshalJSON() = %v, want %v", got, tt.want)
  828. }
  829. })
  830. }
  831. }
  832. func TestSerialNumber_UnmarshalJSON(t *testing.T) {
  833. expected := SerialNumber{big.NewInt(12345)}
  834. type args struct {
  835. data []byte
  836. }
  837. tests := []struct {
  838. name string
  839. args args
  840. want SerialNumber
  841. wantErr bool
  842. }{
  843. {"string", args{[]byte(`"12345"`)}, expected, false},
  844. {"stringHex", args{[]byte(`"0x3039"`)}, expected, false},
  845. {"number", args{[]byte(`12345`)}, expected, false},
  846. {"badString", args{[]byte(`"123s"`)}, SerialNumber{}, true},
  847. {"object", args{[]byte(`{}`)}, SerialNumber{}, true},
  848. {"badJSON", args{[]byte(`{`)}, SerialNumber{}, true},
  849. }
  850. for _, tt := range tests {
  851. t.Run(tt.name, func(t *testing.T) {
  852. var s SerialNumber
  853. if err := s.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
  854. t.Errorf("SerialNumber.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  855. }
  856. if !reflect.DeepEqual(s, tt.want) {
  857. t.Errorf("SerialNumber.UnmarshalJSON() = %v, want %v", s, tt.want)
  858. }
  859. })
  860. }
  861. }