storage_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package hpkp
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "time"
  7. )
  8. var createdAt = time.Now().Unix()
  9. func TestMemStorage_Lookup(t *testing.T) {
  10. m := NewMemStorage()
  11. m.Add("example.org", &Header{
  12. IncludeSubDomains: false,
  13. Permanent: false,
  14. Created: createdAt,
  15. MaxAge: 100,
  16. })
  17. m.Add("a.example.org", &Header{
  18. IncludeSubDomains: true,
  19. Permanent: false,
  20. Created: createdAt,
  21. MaxAge: 100,
  22. })
  23. m.Add("a.example.com", &Header{
  24. IncludeSubDomains: true,
  25. Permanent: false,
  26. Created: createdAt,
  27. MaxAge: 100,
  28. })
  29. m.Add("b.example.com", &Header{
  30. IncludeSubDomains: false,
  31. Permanent: false,
  32. Created: createdAt,
  33. MaxAge: 100,
  34. })
  35. done := make(chan bool)
  36. var orgErr error
  37. go func() {
  38. orgErr = orgTest(m, t)
  39. // try to make a data race
  40. m.Add("example.org", &Header{
  41. IncludeSubDomains: false,
  42. Permanent: false,
  43. Created: createdAt,
  44. MaxAge: 100,
  45. })
  46. done <- true
  47. }()
  48. var comErr error
  49. go func() {
  50. comErr = comTest(m, t)
  51. // try to make a data race
  52. m.Add("a.example.com", &Header{
  53. IncludeSubDomains: true,
  54. Permanent: false,
  55. Created: createdAt,
  56. MaxAge: 100,
  57. })
  58. done <- true
  59. }()
  60. // wait for tests to finish
  61. <-done
  62. <-done
  63. if orgErr != nil {
  64. t.Fatal(orgErr)
  65. }
  66. if comErr != nil {
  67. t.Fatal(comErr)
  68. }
  69. }
  70. func orgTest(m Storage, t *testing.T) error {
  71. tests := []struct {
  72. name string
  73. host string
  74. expected *Header
  75. }{
  76. {
  77. name: "root match org",
  78. host: "example.org",
  79. expected: &Header{
  80. IncludeSubDomains: false,
  81. Permanent: false,
  82. Created: createdAt,
  83. MaxAge: 100,
  84. },
  85. },
  86. {
  87. name: "subdomain match org",
  88. host: "a.example.org",
  89. expected: &Header{
  90. IncludeSubDomains: true,
  91. Permanent: false,
  92. Created: createdAt,
  93. MaxAge: 100,
  94. },
  95. },
  96. {
  97. name: "subdomain miss-match org",
  98. host: "b.example.org",
  99. expected: nil,
  100. },
  101. }
  102. for _, test := range tests {
  103. out := m.Lookup(test.host)
  104. if !reflect.DeepEqual(out, test.expected) {
  105. t.Logf("host: %s", test.host)
  106. t.Logf("want:%v", test.expected)
  107. t.Logf("got:%v", out)
  108. return fmt.Errorf("test case failed: %s", test.name)
  109. }
  110. }
  111. return nil
  112. }
  113. func comTest(m Storage, t *testing.T) error {
  114. tests := []struct {
  115. name string
  116. host string
  117. expected *Header
  118. }{
  119. {
  120. name: "subdomain enabled",
  121. host: "z.a.example.com",
  122. expected: &Header{
  123. IncludeSubDomains: true,
  124. Permanent: false,
  125. Created: createdAt,
  126. MaxAge: 100,
  127. },
  128. },
  129. {
  130. name: "sub-subdomain",
  131. host: "z.y.a.example.com",
  132. expected: &Header{
  133. IncludeSubDomains: true,
  134. Permanent: false,
  135. Created: createdAt,
  136. MaxAge: 100,
  137. },
  138. },
  139. {
  140. name: "subdomain disabled",
  141. host: "z.b.example.com",
  142. expected: nil,
  143. },
  144. {
  145. name: "exact match",
  146. host: "b.example.com",
  147. expected: &Header{
  148. IncludeSubDomains: false,
  149. Permanent: false,
  150. Created: createdAt,
  151. MaxAge: 100,
  152. },
  153. },
  154. {
  155. name: "complete missmatch",
  156. host: "z.example.com",
  157. expected: nil,
  158. },
  159. }
  160. for _, test := range tests {
  161. out := m.Lookup(test.host)
  162. if !reflect.DeepEqual(out, test.expected) {
  163. t.Logf("host: %s", test.host)
  164. t.Logf("want:%v", test.expected)
  165. t.Logf("got:%v", out)
  166. return fmt.Errorf("test case failed: %s", test.name)
  167. }
  168. }
  169. return nil
  170. }
  171. func TestMemStorage_Add(t *testing.T) {
  172. m := &MemStorage{}
  173. // permanent
  174. permanentDomain := Header{
  175. IncludeSubDomains: false,
  176. Permanent: true,
  177. Created: time.Now().Unix(),
  178. MaxAge: 0,
  179. }
  180. m.Add("example.org", &permanentDomain)
  181. expected := map[string]Header{
  182. "example.org": permanentDomain,
  183. }
  184. if !reflect.DeepEqual(m.domains, expected) {
  185. t.Logf("want:%v", expected)
  186. t.Logf("got:%v", m.domains)
  187. t.Fatal("Add failed after permanent")
  188. }
  189. // normal
  190. normalDomain := Header{
  191. IncludeSubDomains: false,
  192. Permanent: false,
  193. Created: time.Now().Unix(),
  194. MaxAge: 100,
  195. }
  196. m.Add("a.example.org", &normalDomain)
  197. expected = map[string]Header{
  198. "example.org": permanentDomain,
  199. "a.example.org": normalDomain,
  200. }
  201. if !reflect.DeepEqual(m.domains, expected) {
  202. t.Logf("want:%v", expected)
  203. t.Logf("got:%v", m.domains)
  204. t.Fatal("Add failed after adding normal")
  205. }
  206. // remove normal
  207. removeNormalDomain := Header{
  208. IncludeSubDomains: false,
  209. Permanent: false,
  210. Created: time.Now().Unix(),
  211. MaxAge: 0,
  212. }
  213. m.Add("a.example.org", &removeNormalDomain)
  214. expected = map[string]Header{
  215. "example.org": permanentDomain,
  216. }
  217. if !reflect.DeepEqual(m.domains, expected) {
  218. t.Logf("want:%v", expected)
  219. t.Logf("got:%v", m.domains)
  220. t.Fatal("Add failed after removing normal")
  221. }
  222. // attempt to remove the permanent
  223. removePermanetDomain := Header{
  224. IncludeSubDomains: false,
  225. Permanent: false,
  226. Created: time.Now().Unix(),
  227. MaxAge: 0,
  228. }
  229. m.Add("example.org", &removePermanetDomain)
  230. expected = map[string]Header{
  231. "example.org": permanentDomain,
  232. }
  233. if !reflect.DeepEqual(m.domains, expected) {
  234. t.Logf("want:%v", expected)
  235. t.Logf("got:%v", m.domains)
  236. t.Fatal("Add failed after attempting to remove the permanent domain")
  237. }
  238. }