sign_options_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "net"
  6. "net/url"
  7. "testing"
  8. "time"
  9. )
  10. func Test_emailOnlyIdentity_Valid(t *testing.T) {
  11. uri, err := url.Parse("https://example.com/1.0/getUser")
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. type args struct {
  16. req *x509.CertificateRequest
  17. }
  18. tests := []struct {
  19. name string
  20. e emailOnlyIdentity
  21. args args
  22. wantErr bool
  23. }{
  24. {"ok", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{"name@smallstep.com"}}}, false},
  25. {"DNSNames", "name@smallstep.com", args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, true},
  26. {"IPAddresses", "name@smallstep.com", args{&x509.CertificateRequest{IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)}}}, true},
  27. {"URIs", "name@smallstep.com", args{&x509.CertificateRequest{URIs: []*url.URL{uri}}}, true},
  28. {"no-emails", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{}}}, true},
  29. {"empty-email", "", args{&x509.CertificateRequest{EmailAddresses: []string{""}}}, true},
  30. {"multiple-emails", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{"name@smallstep.com", "foo@smallstep.com"}}}, true},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. if err := tt.e.Valid(tt.args.req); (err != nil) != tt.wantErr {
  35. t.Errorf("emailOnlyIdentity.Valid() error = %v, wantErr %v", err, tt.wantErr)
  36. }
  37. })
  38. }
  39. }
  40. func Test_commonNameValidator_Valid(t *testing.T) {
  41. type args struct {
  42. req *x509.CertificateRequest
  43. }
  44. tests := []struct {
  45. name string
  46. v commonNameValidator
  47. args args
  48. wantErr bool
  49. }{
  50. {"ok", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  51. {"empty", "", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, true},
  52. {"wrong", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  57. t.Errorf("commonNameValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  58. }
  59. })
  60. }
  61. }
  62. func Test_dnsNamesValidator_Valid(t *testing.T) {
  63. type args struct {
  64. req *x509.CertificateRequest
  65. }
  66. tests := []struct {
  67. name string
  68. v dnsNamesValidator
  69. args args
  70. wantErr bool
  71. }{
  72. {"ok0", []string{}, args{&x509.CertificateRequest{DNSNames: []string{}}}, false},
  73. {"ok1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, false},
  74. {"ok2", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "bar.zar"}}}, false},
  75. {"ok3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, false},
  76. {"fail1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, true},
  77. {"fail2", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, true},
  78. {"fail3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "zar.bar"}}}, true},
  79. }
  80. for _, tt := range tests {
  81. t.Run(tt.name, func(t *testing.T) {
  82. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  83. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  84. }
  85. })
  86. }
  87. }
  88. func Test_ipAddressesValidator_Valid(t *testing.T) {
  89. ip1 := net.IPv4(10, 3, 2, 1)
  90. ip2 := net.IPv4(10, 3, 2, 2)
  91. ip3 := net.IPv4(10, 3, 2, 3)
  92. type args struct {
  93. req *x509.CertificateRequest
  94. }
  95. tests := []struct {
  96. name string
  97. v ipAddressesValidator
  98. args args
  99. wantErr bool
  100. }{
  101. {"ok0", []net.IP{}, args{&x509.CertificateRequest{IPAddresses: []net.IP{}}}, false},
  102. {"ok1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1}}}, false},
  103. {"ok2", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip2}}}, false},
  104. {"ok3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, false},
  105. {"fail1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2}}}, true},
  106. {"fail2", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, true},
  107. {"fail3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip3}}}, true},
  108. }
  109. for _, tt := range tests {
  110. t.Run(tt.name, func(t *testing.T) {
  111. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  112. t.Errorf("ipAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  113. }
  114. })
  115. }
  116. }
  117. func Test_validityValidator_Valid(t *testing.T) {
  118. type fields struct {
  119. min time.Duration
  120. max time.Duration
  121. }
  122. type args struct {
  123. crt *x509.Certificate
  124. }
  125. tests := []struct {
  126. name string
  127. fields fields
  128. args args
  129. wantErr bool
  130. }{
  131. // TODO: Add test cases.
  132. }
  133. for _, tt := range tests {
  134. t.Run(tt.name, func(t *testing.T) {
  135. v := &validityValidator{
  136. min: tt.fields.min,
  137. max: tt.fields.max,
  138. }
  139. if err := v.Valid(tt.args.crt); (err != nil) != tt.wantErr {
  140. t.Errorf("validityValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  141. }
  142. })
  143. }
  144. }