sign_options_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/pkg/errors"
  12. "github.com/smallstep/assert"
  13. "github.com/smallstep/cli/crypto/pemutil"
  14. "github.com/smallstep/cli/crypto/x509util"
  15. )
  16. func Test_emailOnlyIdentity_Valid(t *testing.T) {
  17. uri, err := url.Parse("https://example.com/1.0/getUser")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. type args struct {
  22. req *x509.CertificateRequest
  23. }
  24. tests := []struct {
  25. name string
  26. e emailOnlyIdentity
  27. args args
  28. wantErr bool
  29. }{
  30. {"ok", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{"name@smallstep.com"}}}, false},
  31. {"DNSNames", "name@smallstep.com", args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, true},
  32. {"IPAddresses", "name@smallstep.com", args{&x509.CertificateRequest{IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)}}}, true},
  33. {"URIs", "name@smallstep.com", args{&x509.CertificateRequest{URIs: []*url.URL{uri}}}, true},
  34. {"no-emails", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{}}}, true},
  35. {"empty-email", "", args{&x509.CertificateRequest{EmailAddresses: []string{""}}}, true},
  36. {"multiple-emails", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{"name@smallstep.com", "foo@smallstep.com"}}}, true},
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. if err := tt.e.Valid(tt.args.req); (err != nil) != tt.wantErr {
  41. t.Errorf("emailOnlyIdentity.Valid() error = %v, wantErr %v", err, tt.wantErr)
  42. }
  43. })
  44. }
  45. }
  46. func Test_defaultPublicKeyValidator_Valid(t *testing.T) {
  47. _shortRSA, err := pemutil.Read("./testdata/certs/short-rsa.csr")
  48. assert.FatalError(t, err)
  49. shortRSA, ok := _shortRSA.(*x509.CertificateRequest)
  50. assert.Fatal(t, ok)
  51. _rsa, err := pemutil.Read("./testdata/certs/rsa.csr")
  52. assert.FatalError(t, err)
  53. rsaCSR, ok := _rsa.(*x509.CertificateRequest)
  54. assert.Fatal(t, ok)
  55. _ecdsa, err := pemutil.Read("./testdata/certs/ecdsa.csr")
  56. assert.FatalError(t, err)
  57. ecdsaCSR, ok := _ecdsa.(*x509.CertificateRequest)
  58. assert.Fatal(t, ok)
  59. _ed25519, err := pemutil.Read("./testdata/certs/ed25519.csr")
  60. assert.FatalError(t, err)
  61. ed25519CSR, ok := _ed25519.(*x509.CertificateRequest)
  62. assert.Fatal(t, ok)
  63. v := defaultPublicKeyValidator{}
  64. tests := []struct {
  65. name string
  66. csr *x509.CertificateRequest
  67. err error
  68. }{
  69. {
  70. "fail/unrecognized-key-type",
  71. &x509.CertificateRequest{PublicKey: "foo"},
  72. errors.New("unrecognized public key of type 'string' in CSR"),
  73. },
  74. {
  75. "fail/rsa/too-short",
  76. shortRSA,
  77. errors.New("rsa key in CSR must be at least 2048 bits (256 bytes)"),
  78. },
  79. {
  80. "ok/rsa",
  81. rsaCSR,
  82. nil,
  83. },
  84. {
  85. "ok/ecdsa",
  86. ecdsaCSR,
  87. nil,
  88. },
  89. {
  90. "ok/ed25519",
  91. ed25519CSR,
  92. nil,
  93. },
  94. }
  95. for _, tt := range tests {
  96. t.Run(tt.name, func(t *testing.T) {
  97. if err := v.Valid(tt.csr); err != nil {
  98. if assert.NotNil(t, tt.err) {
  99. assert.HasPrefix(t, err.Error(), tt.err.Error())
  100. }
  101. } else {
  102. assert.Nil(t, tt.err)
  103. }
  104. })
  105. }
  106. }
  107. func Test_commonNameValidator_Valid(t *testing.T) {
  108. type args struct {
  109. req *x509.CertificateRequest
  110. }
  111. tests := []struct {
  112. name string
  113. v commonNameValidator
  114. args args
  115. wantErr bool
  116. }{
  117. {"ok", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  118. {"empty", "", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, true},
  119. {"wrong", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  120. }
  121. for _, tt := range tests {
  122. t.Run(tt.name, func(t *testing.T) {
  123. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  124. t.Errorf("commonNameValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  125. }
  126. })
  127. }
  128. }
  129. func Test_commonNameSliceValidator_Valid(t *testing.T) {
  130. type args struct {
  131. req *x509.CertificateRequest
  132. }
  133. tests := []struct {
  134. name string
  135. v commonNameSliceValidator
  136. args args
  137. wantErr bool
  138. }{
  139. {"ok", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  140. {"ok", []string{"example.com", "foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  141. {"empty", []string{""}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, true},
  142. {"wrong", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  143. }
  144. for _, tt := range tests {
  145. t.Run(tt.name, func(t *testing.T) {
  146. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  147. t.Errorf("commonNameSliceValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  148. }
  149. })
  150. }
  151. }
  152. func Test_emailAddressesValidator_Valid(t *testing.T) {
  153. type args struct {
  154. req *x509.CertificateRequest
  155. }
  156. tests := []struct {
  157. name string
  158. v emailAddressesValidator
  159. args args
  160. wantErr bool
  161. }{
  162. {"ok0", []string{}, args{&x509.CertificateRequest{EmailAddresses: []string{}}}, false},
  163. {"ok1", []string{"max@smallstep.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@smallstep.com"}}}, false},
  164. {"ok2", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, false},
  165. {"ok3", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com", "max@step.com"}}}, false},
  166. {"fail1", []string{"max@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com"}}}, true},
  167. {"fail2", []string{"mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, true},
  168. {"fail3", []string{"mike@step.com", "max@step.com"}, args{&x509.CertificateRequest{DNSNames: []string{"mike@step.com", "mex@step.com"}}}, true},
  169. }
  170. for _, tt := range tests {
  171. t.Run(tt.name, func(t *testing.T) {
  172. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  173. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  174. }
  175. })
  176. }
  177. }
  178. func Test_dnsNamesValidator_Valid(t *testing.T) {
  179. type args struct {
  180. req *x509.CertificateRequest
  181. }
  182. tests := []struct {
  183. name string
  184. v dnsNamesValidator
  185. args args
  186. wantErr bool
  187. }{
  188. {"ok0", []string{}, args{&x509.CertificateRequest{DNSNames: []string{}}}, false},
  189. {"ok1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, false},
  190. {"ok2", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "bar.zar"}}}, false},
  191. {"ok3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, false},
  192. {"fail1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, true},
  193. {"fail2", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, true},
  194. {"fail3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "zar.bar"}}}, true},
  195. }
  196. for _, tt := range tests {
  197. t.Run(tt.name, func(t *testing.T) {
  198. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  199. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  200. }
  201. })
  202. }
  203. }
  204. func Test_ipAddressesValidator_Valid(t *testing.T) {
  205. ip1 := net.IPv4(10, 3, 2, 1)
  206. ip2 := net.IPv4(10, 3, 2, 2)
  207. ip3 := net.IPv4(10, 3, 2, 3)
  208. type args struct {
  209. req *x509.CertificateRequest
  210. }
  211. tests := []struct {
  212. name string
  213. v ipAddressesValidator
  214. args args
  215. wantErr bool
  216. }{
  217. {"ok0", []net.IP{}, args{&x509.CertificateRequest{IPAddresses: []net.IP{}}}, false},
  218. {"ok1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1}}}, false},
  219. {"ok2", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip2}}}, false},
  220. {"ok3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, false},
  221. {"fail1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2}}}, true},
  222. {"fail2", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, true},
  223. {"fail3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip3}}}, true},
  224. }
  225. for _, tt := range tests {
  226. t.Run(tt.name, func(t *testing.T) {
  227. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  228. t.Errorf("ipAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  229. }
  230. })
  231. }
  232. }
  233. func Test_validityValidator_Valid(t *testing.T) {
  234. type test struct {
  235. cert *x509.Certificate
  236. opts Options
  237. vv *validityValidator
  238. err error
  239. }
  240. tests := map[string]func() test{
  241. "fail/notAfter-past": func() test {
  242. return test{
  243. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  244. cert: &x509.Certificate{NotAfter: time.Now().Add(-5 * time.Minute)},
  245. opts: Options{},
  246. err: errors.New("notAfter cannot be in the past"),
  247. }
  248. },
  249. "fail/notBefore-after-notAfter": func() test {
  250. return test{
  251. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  252. cert: &x509.Certificate{NotBefore: time.Now().Add(10 * time.Minute),
  253. NotAfter: time.Now().Add(5 * time.Minute)},
  254. opts: Options{},
  255. err: errors.New("notAfter cannot be before notBefore"),
  256. }
  257. },
  258. "fail/duration-too-short": func() test {
  259. n := now()
  260. return test{
  261. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  262. cert: &x509.Certificate{NotBefore: n,
  263. NotAfter: n.Add(3 * time.Minute)},
  264. opts: Options{},
  265. err: errors.New("is less than the authorized minimum certificate duration of "),
  266. }
  267. },
  268. "ok/duration-exactly-min": func() test {
  269. n := now()
  270. return test{
  271. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  272. cert: &x509.Certificate{NotBefore: n,
  273. NotAfter: n.Add(5 * time.Minute)},
  274. opts: Options{},
  275. }
  276. },
  277. "fail/duration-too-great": func() test {
  278. n := now()
  279. return test{
  280. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  281. cert: &x509.Certificate{NotBefore: n,
  282. NotAfter: n.Add(24*time.Hour + time.Second)},
  283. err: errors.New("is more than the authorized maximum certificate duration of "),
  284. }
  285. },
  286. "ok/duration-exactly-max": func() test {
  287. n := time.Now()
  288. return test{
  289. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  290. cert: &x509.Certificate{NotBefore: n,
  291. NotAfter: n.Add(24 * time.Hour)},
  292. }
  293. },
  294. "ok/duration-exact-min-with-backdate": func() test {
  295. now := time.Now()
  296. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(5 * time.Minute)}
  297. time.Sleep(time.Second)
  298. return test{
  299. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  300. cert: cert,
  301. opts: Options{Backdate: time.Second},
  302. }
  303. },
  304. "ok/duration-exact-max-with-backdate": func() test {
  305. backdate := time.Second
  306. now := time.Now()
  307. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(24*time.Hour + backdate)}
  308. time.Sleep(backdate)
  309. return test{
  310. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  311. cert: cert,
  312. opts: Options{Backdate: backdate},
  313. }
  314. },
  315. }
  316. for name, run := range tests {
  317. t.Run(name, func(t *testing.T) {
  318. tt := run()
  319. if err := tt.vv.Valid(tt.cert, tt.opts); err != nil {
  320. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  321. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  322. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  323. }
  324. } else {
  325. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  326. }
  327. })
  328. }
  329. }
  330. func Test_profileDefaultDuration_Option(t *testing.T) {
  331. type test struct {
  332. so Options
  333. pdd profileDefaultDuration
  334. cert *x509.Certificate
  335. valid func(*x509.Certificate)
  336. }
  337. tests := map[string]func() test{
  338. "ok/notBefore-notAfter-duration-empty": func() test {
  339. return test{
  340. pdd: profileDefaultDuration(0),
  341. so: Options{},
  342. cert: new(x509.Certificate),
  343. valid: func(cert *x509.Certificate) {
  344. n := now()
  345. assert.True(t, n.After(cert.NotBefore))
  346. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  347. assert.True(t, n.Add(24*time.Hour).After(cert.NotAfter))
  348. assert.True(t, n.Add(24*time.Hour).Add(-1*time.Minute).Before(cert.NotAfter))
  349. },
  350. }
  351. },
  352. "ok/notBefore-set": func() test {
  353. nb := time.Now().Add(5 * time.Minute).UTC()
  354. return test{
  355. pdd: profileDefaultDuration(0),
  356. so: Options{NotBefore: NewTimeDuration(nb)},
  357. cert: new(x509.Certificate),
  358. valid: func(cert *x509.Certificate) {
  359. assert.Equals(t, cert.NotBefore, nb)
  360. assert.Equals(t, cert.NotAfter, nb.Add(24*time.Hour))
  361. },
  362. }
  363. },
  364. "ok/duration-set": func() test {
  365. d := 4 * time.Hour
  366. return test{
  367. pdd: profileDefaultDuration(d),
  368. so: Options{Backdate: time.Second},
  369. cert: new(x509.Certificate),
  370. valid: func(cert *x509.Certificate) {
  371. n := now()
  372. assert.True(t, n.After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n, cert.NotBefore))
  373. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  374. assert.True(t, n.Add(d).After(cert.NotAfter))
  375. assert.True(t, n.Add(d).Add(-1*time.Minute).Before(cert.NotAfter))
  376. },
  377. }
  378. },
  379. "ok/notAfter-set": func() test {
  380. na := now().Add(10 * time.Minute).UTC()
  381. return test{
  382. pdd: profileDefaultDuration(0),
  383. so: Options{NotAfter: NewTimeDuration(na)},
  384. cert: new(x509.Certificate),
  385. valid: func(cert *x509.Certificate) {
  386. n := now()
  387. assert.True(t, n.After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n, cert.NotBefore))
  388. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  389. assert.Equals(t, cert.NotAfter, na)
  390. },
  391. }
  392. },
  393. "ok/notBefore-and-notAfter-set": func() test {
  394. nb := time.Now().Add(5 * time.Minute).UTC()
  395. na := time.Now().Add(10 * time.Minute).UTC()
  396. d := 4 * time.Hour
  397. return test{
  398. pdd: profileDefaultDuration(d),
  399. so: Options{NotBefore: NewTimeDuration(nb), NotAfter: NewTimeDuration(na)},
  400. cert: new(x509.Certificate),
  401. valid: func(cert *x509.Certificate) {
  402. assert.Equals(t, cert.NotBefore, nb)
  403. assert.Equals(t, cert.NotAfter, na)
  404. },
  405. }
  406. },
  407. }
  408. for name, run := range tests {
  409. t.Run(name, func(t *testing.T) {
  410. tt := run()
  411. prof := &x509util.Leaf{}
  412. prof.SetSubject(tt.cert)
  413. assert.FatalError(t, tt.pdd.Option(tt.so)(prof), "unexpected error")
  414. tt.valid(prof.Subject())
  415. })
  416. }
  417. }
  418. func Test_profileLimitDuration_Option(t *testing.T) {
  419. n, fn := mockNow()
  420. defer fn()
  421. type test struct {
  422. pld profileLimitDuration
  423. so Options
  424. cert *x509.Certificate
  425. valid func(*x509.Certificate)
  426. err error
  427. }
  428. tests := map[string]func() test{
  429. "fail/notBefore-after-limit": func() test {
  430. d, err := ParseTimeDuration("8h")
  431. assert.FatalError(t, err)
  432. return test{
  433. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  434. so: Options{NotBefore: d},
  435. cert: new(x509.Certificate),
  436. err: errors.New("provisioning credential expiration ("),
  437. }
  438. },
  439. "fail/requested-notAfter-after-limit": func() test {
  440. d, err := ParseTimeDuration("4h")
  441. assert.FatalError(t, err)
  442. return test{
  443. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  444. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d},
  445. cert: new(x509.Certificate),
  446. err: errors.New("provisioning credential expiration ("),
  447. }
  448. },
  449. "ok/valid-notAfter-requested": func() test {
  450. d, err := ParseTimeDuration("2h")
  451. assert.FatalError(t, err)
  452. return test{
  453. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  454. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d, Backdate: 1 * time.Minute},
  455. cert: new(x509.Certificate),
  456. valid: func(cert *x509.Certificate) {
  457. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  458. assert.Equals(t, cert.NotAfter, n.Add(5*time.Hour))
  459. },
  460. }
  461. },
  462. "ok/valid-notAfter-nil-limit-over-default": func() test {
  463. return test{
  464. pld: profileLimitDuration{def: 1 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  465. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  466. cert: new(x509.Certificate),
  467. valid: func(cert *x509.Certificate) {
  468. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  469. assert.Equals(t, cert.NotAfter, n.Add(4*time.Hour))
  470. },
  471. }
  472. },
  473. "ok/valid-notAfter-nil-limit-under-default": func() test {
  474. return test{
  475. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  476. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  477. cert: new(x509.Certificate),
  478. valid: func(cert *x509.Certificate) {
  479. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  480. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  481. },
  482. }
  483. },
  484. "ok/over-limit-with-backdate": func() test {
  485. return test{
  486. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  487. so: Options{Backdate: 1 * time.Minute},
  488. cert: new(x509.Certificate),
  489. valid: func(cert *x509.Certificate) {
  490. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  491. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  492. },
  493. }
  494. },
  495. "ok/under-limit-with-backdate": func() test {
  496. return test{
  497. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(30 * time.Hour)},
  498. so: Options{Backdate: 1 * time.Minute},
  499. cert: new(x509.Certificate),
  500. valid: func(cert *x509.Certificate) {
  501. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  502. assert.Equals(t, cert.NotAfter, n.Add(24*time.Hour))
  503. },
  504. }
  505. },
  506. }
  507. for name, run := range tests {
  508. t.Run(name, func(t *testing.T) {
  509. tt := run()
  510. prof := &x509util.Leaf{}
  511. prof.SetSubject(tt.cert)
  512. if err := tt.pld.Option(tt.so)(prof); err != nil {
  513. if assert.NotNil(t, tt.err) {
  514. assert.HasPrefix(t, err.Error(), tt.err.Error())
  515. }
  516. } else {
  517. if assert.Nil(t, tt.err) {
  518. tt.valid(prof.Subject())
  519. }
  520. }
  521. })
  522. }
  523. }