nonce_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package acme
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/assert"
  7. "github.com/smallstep/certificates/db"
  8. "github.com/smallstep/nosql"
  9. "github.com/smallstep/nosql/database"
  10. )
  11. func TestNewNonce(t *testing.T) {
  12. type test struct {
  13. db nosql.DB
  14. err *Error
  15. id *string
  16. }
  17. tests := map[string]func(t *testing.T) test{
  18. "fail/cmpAndSwap-error": func(t *testing.T) test {
  19. return test{
  20. db: &db.MockNoSQLDB{
  21. MCmpAndSwap: func(bucket, key, old, newval []byte) ([]byte, bool, error) {
  22. assert.Equals(t, bucket, nonceTable)
  23. assert.Equals(t, old, nil)
  24. return nil, false, errors.New("force")
  25. },
  26. },
  27. err: ServerInternalErr(errors.Errorf("error storing nonce: force")),
  28. }
  29. },
  30. "fail/cmpAndSwap-false": func(t *testing.T) test {
  31. return test{
  32. db: &db.MockNoSQLDB{
  33. MCmpAndSwap: func(bucket, key, old, newval []byte) ([]byte, bool, error) {
  34. assert.Equals(t, bucket, nonceTable)
  35. assert.Equals(t, old, nil)
  36. return nil, false, nil
  37. },
  38. },
  39. err: ServerInternalErr(errors.Errorf("error storing nonce; value has changed since last read")),
  40. }
  41. },
  42. "ok": func(t *testing.T) test {
  43. var _id string
  44. id := &_id
  45. return test{
  46. db: &db.MockNoSQLDB{
  47. MCmpAndSwap: func(bucket, key, old, newval []byte) ([]byte, bool, error) {
  48. assert.Equals(t, bucket, nonceTable)
  49. assert.Equals(t, old, nil)
  50. *id = string(key)
  51. return nil, true, nil
  52. },
  53. },
  54. id: id,
  55. }
  56. },
  57. }
  58. for name, run := range tests {
  59. t.Run(name, func(t *testing.T) {
  60. tc := run(t)
  61. if n, err := newNonce(tc.db); err != nil {
  62. if assert.NotNil(t, tc.err) {
  63. ae, ok := err.(*Error)
  64. assert.True(t, ok)
  65. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  66. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  67. assert.Equals(t, ae.Type, tc.err.Type)
  68. }
  69. } else {
  70. if assert.Nil(t, tc.err) {
  71. assert.Equals(t, n.ID, *tc.id)
  72. assert.True(t, n.Created.Before(time.Now().Add(time.Minute)))
  73. assert.True(t, n.Created.After(time.Now().Add(-time.Minute)))
  74. }
  75. }
  76. })
  77. }
  78. }
  79. func TestUseNonce(t *testing.T) {
  80. type test struct {
  81. id string
  82. db nosql.DB
  83. err *Error
  84. }
  85. tests := map[string]func(t *testing.T) test{
  86. "fail/update-not-found": func(t *testing.T) test {
  87. id := "foo"
  88. return test{
  89. db: &db.MockNoSQLDB{
  90. MUpdate: func(tx *database.Tx) error {
  91. assert.Equals(t, tx.Operations[0].Bucket, nonceTable)
  92. assert.Equals(t, tx.Operations[0].Key, []byte(id))
  93. assert.Equals(t, tx.Operations[0].Cmd, database.Get)
  94. assert.Equals(t, tx.Operations[1].Bucket, nonceTable)
  95. assert.Equals(t, tx.Operations[1].Key, []byte(id))
  96. assert.Equals(t, tx.Operations[1].Cmd, database.Delete)
  97. return database.ErrNotFound
  98. },
  99. },
  100. id: id,
  101. err: BadNonceErr(nil),
  102. }
  103. },
  104. "fail/update-error": func(t *testing.T) test {
  105. id := "foo"
  106. return test{
  107. db: &db.MockNoSQLDB{
  108. MUpdate: func(tx *database.Tx) error {
  109. assert.Equals(t, tx.Operations[0].Bucket, nonceTable)
  110. assert.Equals(t, tx.Operations[0].Key, []byte(id))
  111. assert.Equals(t, tx.Operations[0].Cmd, database.Get)
  112. assert.Equals(t, tx.Operations[1].Bucket, nonceTable)
  113. assert.Equals(t, tx.Operations[1].Key, []byte(id))
  114. assert.Equals(t, tx.Operations[1].Cmd, database.Delete)
  115. return errors.New("force")
  116. },
  117. },
  118. id: id,
  119. err: ServerInternalErr(errors.Errorf("error deleting nonce %s: force", id)),
  120. }
  121. },
  122. "ok": func(t *testing.T) test {
  123. id := "foo"
  124. return test{
  125. db: &db.MockNoSQLDB{
  126. MUpdate: func(tx *database.Tx) error {
  127. assert.Equals(t, tx.Operations[0].Bucket, nonceTable)
  128. assert.Equals(t, tx.Operations[0].Key, []byte(id))
  129. assert.Equals(t, tx.Operations[0].Cmd, database.Get)
  130. assert.Equals(t, tx.Operations[1].Bucket, nonceTable)
  131. assert.Equals(t, tx.Operations[1].Key, []byte(id))
  132. assert.Equals(t, tx.Operations[1].Cmd, database.Delete)
  133. return nil
  134. },
  135. },
  136. id: id,
  137. }
  138. },
  139. }
  140. for name, run := range tests {
  141. t.Run(name, func(t *testing.T) {
  142. tc := run(t)
  143. if err := useNonce(tc.db, tc.id); err != nil {
  144. if assert.NotNil(t, tc.err) {
  145. ae, ok := err.(*Error)
  146. assert.True(t, ok)
  147. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  148. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  149. assert.Equals(t, ae.Type, tc.err.Type)
  150. }
  151. }
  152. })
  153. }
  154. }