sql_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. package sql
  2. import (
  3. "math"
  4. "reflect"
  5. "testing"
  6. "time"
  7. "github.com/cloudflare/cfssl/certdb"
  8. "github.com/cloudflare/cfssl/certdb/testdb"
  9. "github.com/jmoiron/sqlx"
  10. )
  11. const (
  12. sqliteDBFile = "../testdb/certstore_development.db"
  13. fakeAKI = "fake_aki"
  14. )
  15. func TestNoDB(t *testing.T) {
  16. dba := &Accessor{}
  17. _, err := dba.GetCertificate("foobar serial", "random aki")
  18. if err == nil {
  19. t.Fatal("should return error")
  20. }
  21. }
  22. type TestAccessor struct {
  23. Accessor certdb.Accessor
  24. DB *sqlx.DB
  25. }
  26. func (ta *TestAccessor) Truncate() {
  27. testdb.Truncate(ta.DB)
  28. }
  29. func TestSQLite(t *testing.T) {
  30. db := testdb.SQLiteDB(sqliteDBFile)
  31. ta := TestAccessor{
  32. Accessor: NewAccessor(db),
  33. DB: db,
  34. }
  35. testEverything(ta, t)
  36. }
  37. // roughlySameTime decides if t1 and t2 are close enough.
  38. func roughlySameTime(t1, t2 time.Time) bool {
  39. // return true if the difference is smaller than 1 sec.
  40. return math.Abs(float64(t1.Sub(t2))) < float64(time.Second)
  41. }
  42. func testEverything(ta TestAccessor, t *testing.T) {
  43. testInsertCertificateAndGetCertificate(ta, t)
  44. testInsertCertificateAndGetUnexpiredCertificate(ta, t)
  45. testInsertCertificateAndGetUnexpiredCertificateNullCommonName(ta, t)
  46. testUpdateCertificateAndGetCertificate(ta, t)
  47. testInsertOCSPAndGetOCSP(ta, t)
  48. testInsertOCSPAndGetUnexpiredOCSP(ta, t)
  49. testUpdateOCSPAndGetOCSP(ta, t)
  50. testUpsertOCSPAndGetOCSP(ta, t)
  51. }
  52. func testInsertCertificateAndGetCertificate(ta TestAccessor, t *testing.T) {
  53. ta.Truncate()
  54. expiry := time.Date(2010, time.December, 25, 23, 0, 0, 0, time.UTC)
  55. want := certdb.CertificateRecord{
  56. PEM: "fake cert data",
  57. Serial: "fake serial",
  58. AKI: fakeAKI,
  59. Status: "good",
  60. Reason: 0,
  61. Expiry: expiry,
  62. }
  63. want.SetMetadata(map[string]interface{}{"k": "v"})
  64. if err := ta.Accessor.InsertCertificate(want); err != nil {
  65. t.Fatal(err)
  66. }
  67. rets, err := ta.Accessor.GetCertificate(want.Serial, want.AKI)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if len(rets) != 1 {
  72. t.Fatal("should only return one record.")
  73. }
  74. got := rets[0]
  75. // reflection comparison with zero time objects are not stable as it seems
  76. if want.Serial != got.Serial || want.Status != got.Status ||
  77. want.AKI != got.AKI || !got.RevokedAt.IsZero() ||
  78. want.PEM != got.PEM || !roughlySameTime(got.Expiry, expiry) {
  79. t.Errorf("want Certificate %+v, got %+v", want, got)
  80. }
  81. gotMeta, err := got.GetMetadata()
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. expected := map[string]interface{}{"k": "v"}
  86. if !reflect.DeepEqual(gotMeta, expected) {
  87. t.Fatalf("expected: %+v, got: %+v", expected, gotMeta)
  88. }
  89. unexpired, err := ta.Accessor.GetUnexpiredCertificates()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if len(unexpired) != 0 {
  94. t.Error("should not have unexpired certificate record")
  95. }
  96. }
  97. func testInsertCertificateAndGetUnexpiredCertificate(ta TestAccessor, t *testing.T) {
  98. ta.Truncate()
  99. expiry := time.Now().Add(time.Minute)
  100. want := certdb.CertificateRecord{
  101. PEM: "fake cert data",
  102. Serial: "fake serial 2",
  103. AKI: fakeAKI,
  104. Status: "good",
  105. Reason: 0,
  106. Expiry: expiry,
  107. CALabel: "foo",
  108. }
  109. if err := ta.Accessor.InsertCertificate(want); err != nil {
  110. t.Fatal(err)
  111. }
  112. rets, err := ta.Accessor.GetCertificate(want.Serial, want.AKI)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if len(rets) != 1 {
  117. t.Fatal("should return exactly one record")
  118. }
  119. got := rets[0]
  120. // reflection comparison with zero time objects are not stable as it seems
  121. if want.Serial != got.Serial || want.Status != got.Status ||
  122. want.AKI != got.AKI || !got.RevokedAt.IsZero() ||
  123. want.PEM != got.PEM || !roughlySameTime(got.Expiry, expiry) {
  124. t.Errorf("want Certificate %+v, got %+v", want, got)
  125. }
  126. unexpired, err := ta.Accessor.GetUnexpiredCertificates()
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if len(unexpired) != 1 {
  131. t.Error("Should have 1 unexpired certificate record:", len(unexpired))
  132. }
  133. unexpiredFiltered, err := ta.Accessor.GetUnexpiredCertificatesByLabel([]string{"foo"})
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if l := len(unexpiredFiltered); l != 1 {
  138. t.Error("Should have 1 unexpiredFiltered certificate record:", l)
  139. }
  140. unexpiredFiltered, err = ta.Accessor.GetUnexpiredCertificatesByLabel([]string{"bar"})
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if l := len(unexpiredFiltered); l != 0 {
  145. t.Error("Should have 0 unexpiredFiltered certificate record:", l)
  146. }
  147. }
  148. func testInsertCertificateAndGetUnexpiredCertificateNullCommonName(ta TestAccessor, t *testing.T) {
  149. ta.Truncate()
  150. expiry := time.Now().Add(time.Minute)
  151. want := certdb.CertificateRecord{
  152. PEM: "fake cert data",
  153. Serial: "fake serial 2",
  154. AKI: fakeAKI,
  155. Status: "good",
  156. Reason: 0,
  157. Expiry: expiry,
  158. }
  159. if err := ta.Accessor.InsertCertificate(want); err != nil {
  160. t.Fatal(err)
  161. }
  162. // simulate situation where there are rows before migrate 002 has been run
  163. ta.DB.MustExec(`update certificates
  164. set issued_at = NULL,
  165. not_before = NULL,
  166. metadata = NULL,
  167. sans = NULL,
  168. common_name = NULL;`)
  169. rets, err := ta.Accessor.GetCertificate(want.Serial, want.AKI)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. if len(rets) != 1 {
  174. t.Fatal("should return exactly one record")
  175. }
  176. got := rets[0]
  177. // reflection comparison with zero time objects are not stable as it seems
  178. if want.Serial != got.Serial || want.Status != got.Status ||
  179. want.AKI != got.AKI || !got.RevokedAt.IsZero() ||
  180. want.PEM != got.PEM || !roughlySameTime(got.Expiry, expiry) {
  181. t.Errorf("want Certificate %+v, got %+v", want, got)
  182. }
  183. unexpired, err := ta.Accessor.GetUnexpiredCertificates()
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if len(unexpired) != 1 {
  188. t.Error("Should have 1 unexpired certificate record:", len(unexpired))
  189. }
  190. }
  191. func testUpdateCertificateAndGetCertificate(ta TestAccessor, t *testing.T) {
  192. ta.Truncate()
  193. expiry := time.Now().Add(time.Hour)
  194. want := certdb.CertificateRecord{
  195. PEM: "fake cert data",
  196. Serial: "fake serial 3",
  197. AKI: fakeAKI,
  198. Status: "good",
  199. Reason: 0,
  200. Expiry: expiry,
  201. }
  202. // Make sure the revoke on a non-existent cert fails
  203. if err := ta.Accessor.RevokeCertificate(want.Serial, want.AKI, 2); err == nil {
  204. t.Fatal("Expected error")
  205. }
  206. if err := ta.Accessor.InsertCertificate(want); err != nil {
  207. t.Fatal(err)
  208. }
  209. // reason 2 is CACompromise
  210. if err := ta.Accessor.RevokeCertificate(want.Serial, want.AKI, 2); err != nil {
  211. t.Fatal(err)
  212. }
  213. rets, err := ta.Accessor.GetCertificate(want.Serial, want.AKI)
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. if len(rets) != 1 {
  218. t.Fatal("should return exactly one record")
  219. }
  220. got := rets[0]
  221. // reflection comparison with zero time objects are not stable as it seems
  222. if want.Serial != got.Serial || got.Status != "revoked" ||
  223. want.AKI != got.AKI || got.RevokedAt.IsZero() ||
  224. want.PEM != got.PEM {
  225. t.Errorf("want Certificate %+v, got %+v", want, got)
  226. }
  227. rets, err = ta.Accessor.GetRevokedAndUnexpiredCertificates()
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. got = rets[0]
  232. // reflection comparison with zero time objects are not stable as it seems
  233. if want.Serial != got.Serial || got.Status != "revoked" ||
  234. want.AKI != got.AKI || got.RevokedAt.IsZero() ||
  235. want.PEM != got.PEM {
  236. t.Errorf("want Certificate %+v, got %+v", want, got)
  237. }
  238. rets, err = ta.Accessor.GetRevokedAndUnexpiredCertificatesByLabel("")
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. got = rets[0]
  243. // reflection comparison with zero time objects are not stable as it seems
  244. if want.Serial != got.Serial || got.Status != "revoked" ||
  245. want.AKI != got.AKI || got.RevokedAt.IsZero() ||
  246. want.PEM != got.PEM {
  247. t.Errorf("want Certificate %+v, got %+v", want, got)
  248. }
  249. rets, err = ta.Accessor.GetRevokedAndUnexpiredCertificatesByLabelSelectColumns("")
  250. if err != nil {
  251. t.Fatal(err)
  252. }
  253. got = rets[0]
  254. // reflection comparison with zero time objects are not stable as it seems
  255. if want.Serial != got.Serial || got.RevokedAt.IsZero() {
  256. t.Errorf("want Certificate %+v, got %+v", want, got)
  257. }
  258. }
  259. func testInsertOCSPAndGetOCSP(ta TestAccessor, t *testing.T) {
  260. ta.Truncate()
  261. expiry := time.Date(2010, time.December, 25, 23, 0, 0, 0, time.UTC)
  262. want := certdb.OCSPRecord{
  263. Serial: "fake serial",
  264. AKI: fakeAKI,
  265. Body: "fake body",
  266. Expiry: expiry,
  267. }
  268. setupGoodCert(ta, t, want)
  269. if err := ta.Accessor.InsertOCSP(want); err != nil {
  270. t.Fatal(err)
  271. }
  272. rets, err := ta.Accessor.GetOCSP(want.Serial, want.AKI)
  273. if err != nil {
  274. t.Fatal(err)
  275. }
  276. if len(rets) != 1 {
  277. t.Fatal("should return exactly one record")
  278. }
  279. got := rets[0]
  280. if want.Serial != got.Serial || want.Body != got.Body ||
  281. !roughlySameTime(want.Expiry, got.Expiry) {
  282. t.Errorf("want OCSP %+v, got %+v", want, got)
  283. }
  284. unexpired, err := ta.Accessor.GetUnexpiredOCSPs()
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. if len(unexpired) != 0 {
  289. t.Error("should not have unexpired certificate record")
  290. }
  291. }
  292. func testInsertOCSPAndGetUnexpiredOCSP(ta TestAccessor, t *testing.T) {
  293. ta.Truncate()
  294. want := certdb.OCSPRecord{
  295. Serial: "fake serial 2",
  296. AKI: fakeAKI,
  297. Body: "fake body",
  298. Expiry: time.Now().Add(time.Minute),
  299. }
  300. setupGoodCert(ta, t, want)
  301. if err := ta.Accessor.InsertOCSP(want); err != nil {
  302. t.Fatal(err)
  303. }
  304. rets, err := ta.Accessor.GetOCSP(want.Serial, want.AKI)
  305. if err != nil {
  306. t.Fatal(err)
  307. }
  308. if len(rets) != 1 {
  309. t.Fatal("should return exactly one record")
  310. }
  311. got := rets[0]
  312. if want.Serial != got.Serial || want.Body != got.Body ||
  313. !roughlySameTime(want.Expiry, got.Expiry) {
  314. t.Errorf("want OCSP %+v, got %+v", want, got)
  315. }
  316. unexpired, err := ta.Accessor.GetUnexpiredOCSPs()
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. if len(unexpired) != 1 {
  321. t.Error("should not have other than 1 unexpired certificate record:", len(unexpired))
  322. }
  323. }
  324. func testUpdateOCSPAndGetOCSP(ta TestAccessor, t *testing.T) {
  325. ta.Truncate()
  326. want := certdb.OCSPRecord{
  327. Serial: "fake serial 3",
  328. AKI: fakeAKI,
  329. Body: "fake body",
  330. Expiry: time.Date(2010, time.December, 25, 23, 0, 0, 0, time.UTC),
  331. }
  332. setupGoodCert(ta, t, want)
  333. // Make sure the update fails
  334. if err := ta.Accessor.UpdateOCSP(want.Serial, want.AKI, want.Body, want.Expiry); err == nil {
  335. t.Fatal("Expected error")
  336. }
  337. if err := ta.Accessor.InsertOCSP(want); err != nil {
  338. t.Fatal(err)
  339. }
  340. want.Body = "fake body revoked"
  341. newExpiry := time.Now().Add(time.Hour)
  342. if err := ta.Accessor.UpdateOCSP(want.Serial, want.AKI, want.Body, newExpiry); err != nil {
  343. t.Fatal(err)
  344. }
  345. rets, err := ta.Accessor.GetOCSP(want.Serial, want.AKI)
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. if len(rets) != 1 {
  350. t.Fatal("should return exactly one record")
  351. }
  352. got := rets[0]
  353. want.Expiry = newExpiry
  354. if want.Serial != got.Serial || got.Body != "fake body revoked" ||
  355. !roughlySameTime(newExpiry, got.Expiry) {
  356. t.Errorf("want OCSP %+v, got %+v", want, got)
  357. }
  358. }
  359. func testUpsertOCSPAndGetOCSP(ta TestAccessor, t *testing.T) {
  360. ta.Truncate()
  361. want := certdb.OCSPRecord{
  362. Serial: "fake serial 3",
  363. AKI: fakeAKI,
  364. Body: "fake body",
  365. Expiry: time.Date(2010, time.December, 25, 23, 0, 0, 0, time.UTC),
  366. }
  367. setupGoodCert(ta, t, want)
  368. if err := ta.Accessor.UpsertOCSP(want.Serial, want.AKI, want.Body, want.Expiry); err != nil {
  369. t.Fatal(err)
  370. }
  371. rets, err := ta.Accessor.GetOCSP(want.Serial, want.AKI)
  372. if err != nil {
  373. t.Fatal(err)
  374. }
  375. if len(rets) != 1 {
  376. t.Fatal("should return exactly one record")
  377. }
  378. got := rets[0]
  379. if want.Serial != got.Serial || want.Body != got.Body ||
  380. !roughlySameTime(want.Expiry, got.Expiry) {
  381. t.Errorf("want OCSP %+v, got %+v", want, got)
  382. }
  383. newExpiry := time.Now().Add(time.Hour)
  384. if err := ta.Accessor.UpsertOCSP(want.Serial, want.AKI, "fake body revoked", newExpiry); err != nil {
  385. t.Fatal(err)
  386. }
  387. rets, err = ta.Accessor.GetOCSP(want.Serial, want.AKI)
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. if len(rets) != 1 {
  392. t.Fatal("should return exactly one record")
  393. }
  394. got = rets[0]
  395. want.Expiry = newExpiry
  396. if want.Serial != got.Serial || got.Body != "fake body revoked" ||
  397. !roughlySameTime(newExpiry, got.Expiry) {
  398. t.Errorf("want OCSP %+v, got %+v", want, got)
  399. }
  400. }
  401. func setupGoodCert(ta TestAccessor, t *testing.T, r certdb.OCSPRecord) {
  402. certWant := certdb.CertificateRecord{
  403. AKI: r.AKI,
  404. CALabel: "default",
  405. Expiry: time.Now().Add(time.Minute),
  406. PEM: "fake cert data",
  407. Serial: r.Serial,
  408. Status: "good",
  409. Reason: 0,
  410. }
  411. if err := ta.Accessor.InsertCertificate(certWant); err != nil {
  412. t.Fatal(err)
  413. }
  414. }