message_cache_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. package server
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "net/netip"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. var (
  13. exampleIP1234 = netip.MustParseAddr("1.2.3.4")
  14. )
  15. func TestSqliteCache_Messages(t *testing.T) {
  16. testCacheMessages(t, newSqliteTestCache(t))
  17. }
  18. func TestMemCache_Messages(t *testing.T) {
  19. testCacheMessages(t, newMemTestCache(t))
  20. }
  21. func testCacheMessages(t *testing.T, c *messageCache) {
  22. m1 := newDefaultMessage("mytopic", "my message")
  23. m1.Time = 1
  24. m2 := newDefaultMessage("mytopic", "my other message")
  25. m2.Time = 2
  26. require.Nil(t, c.AddMessage(m1))
  27. require.Nil(t, c.AddMessage(newDefaultMessage("example", "my example message")))
  28. require.Nil(t, c.AddMessage(m2))
  29. // Adding invalid
  30. require.Equal(t, errUnexpectedMessageType, c.AddMessage(newKeepaliveMessage("mytopic"))) // These should not be added!
  31. require.Equal(t, errUnexpectedMessageType, c.AddMessage(newOpenMessage("example"))) // These should not be added!
  32. // mytopic: count
  33. counts, err := c.MessageCounts()
  34. require.Nil(t, err)
  35. require.Equal(t, 2, counts["mytopic"])
  36. // mytopic: since all
  37. messages, _ := c.Messages("mytopic", sinceAllMessages, false)
  38. require.Equal(t, 2, len(messages))
  39. require.Equal(t, "my message", messages[0].Message)
  40. require.Equal(t, "mytopic", messages[0].Topic)
  41. require.Equal(t, messageEvent, messages[0].Event)
  42. require.Equal(t, "", messages[0].Title)
  43. require.Equal(t, 0, messages[0].Priority)
  44. require.Nil(t, messages[0].Tags)
  45. require.Equal(t, "my other message", messages[1].Message)
  46. // mytopic: since none
  47. messages, _ = c.Messages("mytopic", sinceNoMessages, false)
  48. require.Empty(t, messages)
  49. // mytopic: since m1 (by ID)
  50. messages, _ = c.Messages("mytopic", newSinceID(m1.ID), false)
  51. require.Equal(t, 1, len(messages))
  52. require.Equal(t, m2.ID, messages[0].ID)
  53. require.Equal(t, "my other message", messages[0].Message)
  54. require.Equal(t, "mytopic", messages[0].Topic)
  55. // mytopic: since 2
  56. messages, _ = c.Messages("mytopic", newSinceTime(2), false)
  57. require.Equal(t, 1, len(messages))
  58. require.Equal(t, "my other message", messages[0].Message)
  59. // example: count
  60. counts, err = c.MessageCounts()
  61. require.Nil(t, err)
  62. require.Equal(t, 1, counts["example"])
  63. // example: since all
  64. messages, _ = c.Messages("example", sinceAllMessages, false)
  65. require.Equal(t, "my example message", messages[0].Message)
  66. // non-existing: count
  67. counts, err = c.MessageCounts()
  68. require.Nil(t, err)
  69. require.Equal(t, 0, counts["doesnotexist"])
  70. // non-existing: since all
  71. messages, _ = c.Messages("doesnotexist", sinceAllMessages, false)
  72. require.Empty(t, messages)
  73. }
  74. func TestSqliteCache_MessagesScheduled(t *testing.T) {
  75. testCacheMessagesScheduled(t, newSqliteTestCache(t))
  76. }
  77. func TestMemCache_MessagesScheduled(t *testing.T) {
  78. testCacheMessagesScheduled(t, newMemTestCache(t))
  79. }
  80. func testCacheMessagesScheduled(t *testing.T, c *messageCache) {
  81. m1 := newDefaultMessage("mytopic", "message 1")
  82. m2 := newDefaultMessage("mytopic", "message 2")
  83. m2.Time = time.Now().Add(time.Hour).Unix()
  84. m3 := newDefaultMessage("mytopic", "message 3")
  85. m3.Time = time.Now().Add(time.Minute).Unix() // earlier than m2!
  86. m4 := newDefaultMessage("mytopic2", "message 4")
  87. m4.Time = time.Now().Add(time.Minute).Unix()
  88. require.Nil(t, c.AddMessage(m1))
  89. require.Nil(t, c.AddMessage(m2))
  90. require.Nil(t, c.AddMessage(m3))
  91. messages, _ := c.Messages("mytopic", sinceAllMessages, false) // exclude scheduled
  92. require.Equal(t, 1, len(messages))
  93. require.Equal(t, "message 1", messages[0].Message)
  94. messages, _ = c.Messages("mytopic", sinceAllMessages, true) // include scheduled
  95. require.Equal(t, 3, len(messages))
  96. require.Equal(t, "message 1", messages[0].Message)
  97. require.Equal(t, "message 3", messages[1].Message) // Order!
  98. require.Equal(t, "message 2", messages[2].Message)
  99. messages, _ = c.MessagesDue()
  100. require.Empty(t, messages)
  101. }
  102. func TestSqliteCache_Topics(t *testing.T) {
  103. testCacheTopics(t, newSqliteTestCache(t))
  104. }
  105. func TestMemCache_Topics(t *testing.T) {
  106. testCacheTopics(t, newMemTestCache(t))
  107. }
  108. func testCacheTopics(t *testing.T, c *messageCache) {
  109. require.Nil(t, c.AddMessage(newDefaultMessage("topic1", "my example message")))
  110. require.Nil(t, c.AddMessage(newDefaultMessage("topic2", "message 1")))
  111. require.Nil(t, c.AddMessage(newDefaultMessage("topic2", "message 2")))
  112. require.Nil(t, c.AddMessage(newDefaultMessage("topic2", "message 3")))
  113. topics, err := c.Topics()
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. require.Equal(t, 2, len(topics))
  118. require.Equal(t, "topic1", topics["topic1"].ID)
  119. require.Equal(t, "topic2", topics["topic2"].ID)
  120. }
  121. func TestSqliteCache_MessagesTagsPrioAndTitle(t *testing.T) {
  122. testCacheMessagesTagsPrioAndTitle(t, newSqliteTestCache(t))
  123. }
  124. func TestMemCache_MessagesTagsPrioAndTitle(t *testing.T) {
  125. testCacheMessagesTagsPrioAndTitle(t, newMemTestCache(t))
  126. }
  127. func testCacheMessagesTagsPrioAndTitle(t *testing.T, c *messageCache) {
  128. m := newDefaultMessage("mytopic", "some message")
  129. m.Tags = []string{"tag1", "tag2"}
  130. m.Priority = 5
  131. m.Title = "some title"
  132. require.Nil(t, c.AddMessage(m))
  133. messages, _ := c.Messages("mytopic", sinceAllMessages, false)
  134. require.Equal(t, []string{"tag1", "tag2"}, messages[0].Tags)
  135. require.Equal(t, 5, messages[0].Priority)
  136. require.Equal(t, "some title", messages[0].Title)
  137. }
  138. func TestSqliteCache_MessagesSinceID(t *testing.T) {
  139. testCacheMessagesSinceID(t, newSqliteTestCache(t))
  140. }
  141. func TestMemCache_MessagesSinceID(t *testing.T) {
  142. testCacheMessagesSinceID(t, newMemTestCache(t))
  143. }
  144. func testCacheMessagesSinceID(t *testing.T, c *messageCache) {
  145. m1 := newDefaultMessage("mytopic", "message 1")
  146. m1.Time = 100
  147. m2 := newDefaultMessage("mytopic", "message 2")
  148. m2.Time = 200
  149. m3 := newDefaultMessage("mytopic", "message 3")
  150. m3.Time = time.Now().Add(time.Hour).Unix() // Scheduled, in the future, later than m7 and m5
  151. m4 := newDefaultMessage("mytopic", "message 4")
  152. m4.Time = 400
  153. m5 := newDefaultMessage("mytopic", "message 5")
  154. m5.Time = time.Now().Add(time.Minute).Unix() // Scheduled, in the future, later than m7
  155. m6 := newDefaultMessage("mytopic", "message 6")
  156. m6.Time = 600
  157. m7 := newDefaultMessage("mytopic", "message 7")
  158. m7.Time = 700
  159. require.Nil(t, c.AddMessage(m1))
  160. require.Nil(t, c.AddMessage(m2))
  161. require.Nil(t, c.AddMessage(m3))
  162. require.Nil(t, c.AddMessage(m4))
  163. require.Nil(t, c.AddMessage(m5))
  164. require.Nil(t, c.AddMessage(m6))
  165. require.Nil(t, c.AddMessage(m7))
  166. // Case 1: Since ID exists, exclude scheduled
  167. messages, _ := c.Messages("mytopic", newSinceID(m2.ID), false)
  168. require.Equal(t, 3, len(messages))
  169. require.Equal(t, "message 4", messages[0].Message)
  170. require.Equal(t, "message 6", messages[1].Message) // Not scheduled m3/m5!
  171. require.Equal(t, "message 7", messages[2].Message)
  172. // Case 2: Since ID exists, include scheduled
  173. messages, _ = c.Messages("mytopic", newSinceID(m2.ID), true)
  174. require.Equal(t, 5, len(messages))
  175. require.Equal(t, "message 4", messages[0].Message)
  176. require.Equal(t, "message 6", messages[1].Message)
  177. require.Equal(t, "message 7", messages[2].Message)
  178. require.Equal(t, "message 5", messages[3].Message) // Order!
  179. require.Equal(t, "message 3", messages[4].Message) // Order!
  180. // Case 3: Since ID does not exist (-> Return all messages), include scheduled
  181. messages, _ = c.Messages("mytopic", newSinceID("doesntexist"), true)
  182. require.Equal(t, 7, len(messages))
  183. require.Equal(t, "message 1", messages[0].Message)
  184. require.Equal(t, "message 2", messages[1].Message)
  185. require.Equal(t, "message 4", messages[2].Message)
  186. require.Equal(t, "message 6", messages[3].Message)
  187. require.Equal(t, "message 7", messages[4].Message)
  188. require.Equal(t, "message 5", messages[5].Message) // Order!
  189. require.Equal(t, "message 3", messages[6].Message) // Order!
  190. // Case 4: Since ID exists and is last message (-> Return no messages), exclude scheduled
  191. messages, _ = c.Messages("mytopic", newSinceID(m7.ID), false)
  192. require.Equal(t, 0, len(messages))
  193. // Case 5: Since ID exists and is last message (-> Return no messages), include scheduled
  194. messages, _ = c.Messages("mytopic", newSinceID(m7.ID), true)
  195. require.Equal(t, 2, len(messages))
  196. require.Equal(t, "message 5", messages[0].Message)
  197. require.Equal(t, "message 3", messages[1].Message)
  198. }
  199. func TestSqliteCache_Prune(t *testing.T) {
  200. testCachePrune(t, newSqliteTestCache(t))
  201. }
  202. func TestMemCache_Prune(t *testing.T) {
  203. testCachePrune(t, newMemTestCache(t))
  204. }
  205. func testCachePrune(t *testing.T, c *messageCache) {
  206. now := time.Now().Unix()
  207. m1 := newDefaultMessage("mytopic", "my message")
  208. m1.Time = now - 10
  209. m1.Expires = now - 5
  210. m2 := newDefaultMessage("mytopic", "my other message")
  211. m2.Time = now - 5
  212. m2.Expires = now + 5 // In the future
  213. m3 := newDefaultMessage("another_topic", "and another one")
  214. m3.Time = now - 12
  215. m3.Expires = now - 2
  216. require.Nil(t, c.AddMessage(m1))
  217. require.Nil(t, c.AddMessage(m2))
  218. require.Nil(t, c.AddMessage(m3))
  219. counts, err := c.MessageCounts()
  220. require.Nil(t, err)
  221. require.Equal(t, 2, counts["mytopic"])
  222. require.Equal(t, 1, counts["another_topic"])
  223. expiredMessageIDs, err := c.MessagesExpired()
  224. require.Nil(t, err)
  225. require.Nil(t, c.DeleteMessages(expiredMessageIDs...))
  226. counts, err = c.MessageCounts()
  227. require.Nil(t, err)
  228. require.Equal(t, 1, counts["mytopic"])
  229. require.Equal(t, 0, counts["another_topic"])
  230. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  231. require.Nil(t, err)
  232. require.Equal(t, 1, len(messages))
  233. require.Equal(t, "my other message", messages[0].Message)
  234. }
  235. func TestSqliteCache_Attachments(t *testing.T) {
  236. testCacheAttachments(t, newSqliteTestCache(t))
  237. }
  238. func TestMemCache_Attachments(t *testing.T) {
  239. testCacheAttachments(t, newMemTestCache(t))
  240. }
  241. func testCacheAttachments(t *testing.T, c *messageCache) {
  242. expires1 := time.Now().Add(-4 * time.Hour).Unix()
  243. m := newDefaultMessage("mytopic", "flower for you")
  244. m.ID = "m1"
  245. m.Sender = exampleIP1234
  246. m.Attachment = &attachment{
  247. Name: "flower.jpg",
  248. Type: "image/jpeg",
  249. Size: 5000,
  250. Expires: expires1,
  251. URL: "https://ntfy.sh/file/AbDeFgJhal.jpg",
  252. }
  253. require.Nil(t, c.AddMessage(m))
  254. expires2 := time.Now().Add(2 * time.Hour).Unix() // Future
  255. m = newDefaultMessage("mytopic", "sending you a car")
  256. m.ID = "m2"
  257. m.Sender = exampleIP1234
  258. m.Attachment = &attachment{
  259. Name: "car.jpg",
  260. Type: "image/jpeg",
  261. Size: 10000,
  262. Expires: expires2,
  263. URL: "https://ntfy.sh/file/aCaRURL.jpg",
  264. }
  265. require.Nil(t, c.AddMessage(m))
  266. expires3 := time.Now().Add(1 * time.Hour).Unix() // Future
  267. m = newDefaultMessage("another-topic", "sending you another car")
  268. m.ID = "m3"
  269. m.Sender = exampleIP1234
  270. m.Attachment = &attachment{
  271. Name: "another-car.jpg",
  272. Type: "image/jpeg",
  273. Size: 20000,
  274. Expires: expires3,
  275. URL: "https://ntfy.sh/file/zakaDHFW.jpg",
  276. }
  277. require.Nil(t, c.AddMessage(m))
  278. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  279. require.Nil(t, err)
  280. require.Equal(t, 2, len(messages))
  281. require.Equal(t, "flower for you", messages[0].Message)
  282. require.Equal(t, "flower.jpg", messages[0].Attachment.Name)
  283. require.Equal(t, "image/jpeg", messages[0].Attachment.Type)
  284. require.Equal(t, int64(5000), messages[0].Attachment.Size)
  285. require.Equal(t, expires1, messages[0].Attachment.Expires)
  286. require.Equal(t, "https://ntfy.sh/file/AbDeFgJhal.jpg", messages[0].Attachment.URL)
  287. require.Equal(t, "1.2.3.4", messages[0].Sender.String())
  288. require.Equal(t, "sending you a car", messages[1].Message)
  289. require.Equal(t, "car.jpg", messages[1].Attachment.Name)
  290. require.Equal(t, "image/jpeg", messages[1].Attachment.Type)
  291. require.Equal(t, int64(10000), messages[1].Attachment.Size)
  292. require.Equal(t, expires2, messages[1].Attachment.Expires)
  293. require.Equal(t, "https://ntfy.sh/file/aCaRURL.jpg", messages[1].Attachment.URL)
  294. require.Equal(t, "1.2.3.4", messages[1].Sender.String())
  295. size, err := c.AttachmentBytesUsedBySender("1.2.3.4")
  296. require.Nil(t, err)
  297. require.Equal(t, int64(30000), size)
  298. size, err = c.AttachmentBytesUsedBySender("5.6.7.8")
  299. require.Nil(t, err)
  300. require.Equal(t, int64(0), size)
  301. }
  302. func TestSqliteCache_Attachments_Expired(t *testing.T) {
  303. testCacheAttachmentsExpired(t, newSqliteTestCache(t))
  304. }
  305. func TestMemCache_Attachments_Expired(t *testing.T) {
  306. testCacheAttachmentsExpired(t, newMemTestCache(t))
  307. }
  308. func testCacheAttachmentsExpired(t *testing.T, c *messageCache) {
  309. m := newDefaultMessage("mytopic", "flower for you")
  310. m.ID = "m1"
  311. m.Expires = time.Now().Add(time.Hour).Unix()
  312. require.Nil(t, c.AddMessage(m))
  313. m = newDefaultMessage("mytopic", "message with attachment")
  314. m.ID = "m2"
  315. m.Expires = time.Now().Add(2 * time.Hour).Unix()
  316. m.Attachment = &attachment{
  317. Name: "car.jpg",
  318. Type: "image/jpeg",
  319. Size: 10000,
  320. Expires: time.Now().Add(2 * time.Hour).Unix(),
  321. URL: "https://ntfy.sh/file/aCaRURL.jpg",
  322. }
  323. require.Nil(t, c.AddMessage(m))
  324. m = newDefaultMessage("mytopic", "message with external attachment")
  325. m.ID = "m3"
  326. m.Expires = time.Now().Add(2 * time.Hour).Unix()
  327. m.Attachment = &attachment{
  328. Name: "car.jpg",
  329. Type: "image/jpeg",
  330. Expires: 0, // Unknown!
  331. URL: "https://somedomain.com/car.jpg",
  332. }
  333. require.Nil(t, c.AddMessage(m))
  334. m = newDefaultMessage("mytopic2", "message with expired attachment")
  335. m.ID = "m4"
  336. m.Expires = time.Now().Add(2 * time.Hour).Unix()
  337. m.Attachment = &attachment{
  338. Name: "expired-car.jpg",
  339. Type: "image/jpeg",
  340. Size: 20000,
  341. Expires: time.Now().Add(-1 * time.Hour).Unix(),
  342. URL: "https://ntfy.sh/file/aCaRURL.jpg",
  343. }
  344. require.Nil(t, c.AddMessage(m))
  345. ids, err := c.AttachmentsExpired()
  346. require.Nil(t, err)
  347. require.Equal(t, 1, len(ids))
  348. require.Equal(t, "m4", ids[0])
  349. }
  350. func TestSqliteCache_Migration_From0(t *testing.T) {
  351. filename := newSqliteTestCacheFile(t)
  352. db, err := sql.Open("sqlite3", filename)
  353. require.Nil(t, err)
  354. // Create "version 0" schema
  355. _, err = db.Exec(`
  356. BEGIN;
  357. CREATE TABLE IF NOT EXISTS messages (
  358. id VARCHAR(20) PRIMARY KEY,
  359. time INT NOT NULL,
  360. topic VARCHAR(64) NOT NULL,
  361. message VARCHAR(1024) NOT NULL
  362. );
  363. CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
  364. COMMIT;
  365. `)
  366. require.Nil(t, err)
  367. // Insert a bunch of messages
  368. for i := 0; i < 10; i++ {
  369. _, err = db.Exec(`INSERT INTO messages (id, time, topic, message) VALUES (?, ?, ?, ?)`,
  370. fmt.Sprintf("abcd%d", i), time.Now().Unix(), "mytopic", fmt.Sprintf("some message %d", i))
  371. require.Nil(t, err)
  372. }
  373. require.Nil(t, db.Close())
  374. // Create cache to trigger migration
  375. c := newSqliteTestCacheFromFile(t, filename, "")
  376. checkSchemaVersion(t, c.db)
  377. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  378. require.Nil(t, err)
  379. require.Equal(t, 10, len(messages))
  380. require.Equal(t, "some message 5", messages[5].Message)
  381. require.Equal(t, "", messages[5].Title)
  382. require.Nil(t, messages[5].Tags)
  383. require.Equal(t, 0, messages[5].Priority)
  384. }
  385. func TestSqliteCache_Migration_From1(t *testing.T) {
  386. filename := newSqliteTestCacheFile(t)
  387. db, err := sql.Open("sqlite3", filename)
  388. require.Nil(t, err)
  389. // Create "version 1" schema
  390. _, err = db.Exec(`
  391. CREATE TABLE IF NOT EXISTS messages (
  392. id VARCHAR(20) PRIMARY KEY,
  393. time INT NOT NULL,
  394. topic VARCHAR(64) NOT NULL,
  395. message VARCHAR(512) NOT NULL,
  396. title VARCHAR(256) NOT NULL,
  397. priority INT NOT NULL,
  398. tags VARCHAR(256) NOT NULL
  399. );
  400. CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
  401. CREATE TABLE IF NOT EXISTS schemaVersion (
  402. id INT PRIMARY KEY,
  403. version INT NOT NULL
  404. );
  405. INSERT INTO schemaVersion (id, version) VALUES (1, 1);
  406. `)
  407. require.Nil(t, err)
  408. // Insert a bunch of messages
  409. for i := 0; i < 10; i++ {
  410. _, err = db.Exec(`INSERT INTO messages (id, time, topic, message, title, priority, tags) VALUES (?, ?, ?, ?, ?, ?, ?)`,
  411. fmt.Sprintf("abcd%d", i), time.Now().Unix(), "mytopic", fmt.Sprintf("some message %d", i), "", 0, "")
  412. require.Nil(t, err)
  413. }
  414. require.Nil(t, db.Close())
  415. // Create cache to trigger migration
  416. c := newSqliteTestCacheFromFile(t, filename, "")
  417. checkSchemaVersion(t, c.db)
  418. // Add delayed message
  419. delayedMessage := newDefaultMessage("mytopic", "some delayed message")
  420. delayedMessage.Time = time.Now().Add(time.Minute).Unix()
  421. require.Nil(t, c.AddMessage(delayedMessage))
  422. // 10, not 11!
  423. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  424. require.Nil(t, err)
  425. require.Equal(t, 10, len(messages))
  426. // 11!
  427. messages, err = c.Messages("mytopic", sinceAllMessages, true)
  428. require.Nil(t, err)
  429. require.Equal(t, 11, len(messages))
  430. }
  431. func TestSqliteCache_Migration_From9(t *testing.T) {
  432. // This primarily tests the awkward migration that introduces the "expires" column.
  433. // The migration logic has to update the column, using the existing "cache-duration" value.
  434. filename := newSqliteTestCacheFile(t)
  435. db, err := sql.Open("sqlite3", filename)
  436. require.Nil(t, err)
  437. // Create "version 8" schema
  438. _, err = db.Exec(`
  439. BEGIN;
  440. CREATE TABLE IF NOT EXISTS messages (
  441. id INTEGER PRIMARY KEY AUTOINCREMENT,
  442. mid TEXT NOT NULL,
  443. time INT NOT NULL,
  444. topic TEXT NOT NULL,
  445. message TEXT NOT NULL,
  446. title TEXT NOT NULL,
  447. priority INT NOT NULL,
  448. tags TEXT NOT NULL,
  449. click TEXT NOT NULL,
  450. icon TEXT NOT NULL,
  451. actions TEXT NOT NULL,
  452. attachment_name TEXT NOT NULL,
  453. attachment_type TEXT NOT NULL,
  454. attachment_size INT NOT NULL,
  455. attachment_expires INT NOT NULL,
  456. attachment_url TEXT NOT NULL,
  457. sender TEXT NOT NULL,
  458. encoding TEXT NOT NULL,
  459. published INT NOT NULL
  460. );
  461. CREATE INDEX IF NOT EXISTS idx_mid ON messages (mid);
  462. CREATE INDEX IF NOT EXISTS idx_time ON messages (time);
  463. CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
  464. CREATE TABLE IF NOT EXISTS schemaVersion (
  465. id INT PRIMARY KEY,
  466. version INT NOT NULL
  467. );
  468. INSERT INTO schemaVersion (id, version) VALUES (1, 9);
  469. COMMIT;
  470. `)
  471. require.Nil(t, err)
  472. // Insert a bunch of messages
  473. insertQuery := `
  474. INSERT INTO messages (mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, published)
  475. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  476. `
  477. for i := 0; i < 10; i++ {
  478. _, err = db.Exec(
  479. insertQuery,
  480. fmt.Sprintf("abcd%d", i),
  481. time.Now().Unix(),
  482. "mytopic",
  483. fmt.Sprintf("some message %d", i),
  484. "", // title
  485. 0, // priority
  486. "", // tags
  487. "", // click
  488. "", // icon
  489. "", // actions
  490. "", // attachment_name
  491. "", // attachment_type
  492. 0, // attachment_size
  493. 0, // attachment_type
  494. "", // attachment_url
  495. "9.9.9.9", // sender
  496. "", // encoding
  497. 1, // published
  498. )
  499. require.Nil(t, err)
  500. }
  501. // Create cache to trigger migration
  502. cacheDuration := 17 * time.Hour
  503. c, err := newSqliteCache(filename, "", cacheDuration, 0, 0, false)
  504. require.Nil(t, err)
  505. checkSchemaVersion(t, c.db)
  506. // Check version
  507. rows, err := db.Query(`SELECT version FROM main.schemaVersion WHERE id = 1`)
  508. require.Nil(t, err)
  509. require.True(t, rows.Next())
  510. var version int
  511. require.Nil(t, rows.Scan(&version))
  512. require.Equal(t, currentSchemaVersion, version)
  513. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  514. require.Nil(t, err)
  515. require.Equal(t, 10, len(messages))
  516. for _, m := range messages {
  517. require.True(t, m.Expires > time.Now().Add(cacheDuration-5*time.Second).Unix())
  518. require.True(t, m.Expires < time.Now().Add(cacheDuration+5*time.Second).Unix())
  519. }
  520. }
  521. func TestSqliteCache_StartupQueries_WAL(t *testing.T) {
  522. filename := newSqliteTestCacheFile(t)
  523. startupQueries := `pragma journal_mode = WAL;
  524. pragma synchronous = normal;
  525. pragma temp_store = memory;`
  526. db, err := newSqliteCache(filename, startupQueries, time.Hour, 0, 0, false)
  527. require.Nil(t, err)
  528. require.Nil(t, db.AddMessage(newDefaultMessage("mytopic", "some message")))
  529. require.FileExists(t, filename)
  530. require.FileExists(t, filename+"-wal")
  531. require.FileExists(t, filename+"-shm")
  532. }
  533. func TestSqliteCache_StartupQueries_None(t *testing.T) {
  534. filename := newSqliteTestCacheFile(t)
  535. startupQueries := ""
  536. db, err := newSqliteCache(filename, startupQueries, time.Hour, 0, 0, false)
  537. require.Nil(t, err)
  538. require.Nil(t, db.AddMessage(newDefaultMessage("mytopic", "some message")))
  539. require.FileExists(t, filename)
  540. require.NoFileExists(t, filename+"-wal")
  541. require.NoFileExists(t, filename+"-shm")
  542. }
  543. func TestSqliteCache_StartupQueries_Fail(t *testing.T) {
  544. filename := newSqliteTestCacheFile(t)
  545. startupQueries := `xx error`
  546. _, err := newSqliteCache(filename, startupQueries, time.Hour, 0, 0, false)
  547. require.Error(t, err)
  548. }
  549. func TestSqliteCache_Sender(t *testing.T) {
  550. testSender(t, newSqliteTestCache(t))
  551. }
  552. func TestMemCache_Sender(t *testing.T) {
  553. testSender(t, newMemTestCache(t))
  554. }
  555. func testSender(t *testing.T, c *messageCache) {
  556. m1 := newDefaultMessage("mytopic", "mymessage")
  557. m1.Sender = netip.MustParseAddr("1.2.3.4")
  558. require.Nil(t, c.AddMessage(m1))
  559. m2 := newDefaultMessage("mytopic", "mymessage without sender")
  560. require.Nil(t, c.AddMessage(m2))
  561. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  562. require.Nil(t, err)
  563. require.Equal(t, 2, len(messages))
  564. require.Equal(t, messages[0].Sender, netip.MustParseAddr("1.2.3.4"))
  565. require.Equal(t, messages[1].Sender, netip.Addr{})
  566. }
  567. func checkSchemaVersion(t *testing.T, db *sql.DB) {
  568. rows, err := db.Query(`SELECT version FROM schemaVersion`)
  569. require.Nil(t, err)
  570. require.True(t, rows.Next())
  571. var schemaVersion int
  572. require.Nil(t, rows.Scan(&schemaVersion))
  573. require.Equal(t, currentSchemaVersion, schemaVersion)
  574. require.Nil(t, rows.Close())
  575. }
  576. func TestMemCache_NopCache(t *testing.T) {
  577. c, _ := newNopCache()
  578. assert.Nil(t, c.AddMessage(newDefaultMessage("mytopic", "my message")))
  579. messages, err := c.Messages("mytopic", sinceAllMessages, false)
  580. assert.Nil(t, err)
  581. assert.Empty(t, messages)
  582. topics, err := c.Topics()
  583. assert.Nil(t, err)
  584. assert.Empty(t, topics)
  585. }
  586. func newSqliteTestCache(t *testing.T) *messageCache {
  587. c, err := newSqliteCache(newSqliteTestCacheFile(t), "", time.Hour, 0, 0, false)
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. return c
  592. }
  593. func newSqliteTestCacheFile(t *testing.T) string {
  594. return filepath.Join(t.TempDir(), "cache.db")
  595. }
  596. func newSqliteTestCacheFromFile(t *testing.T, filename, startupQueries string) *messageCache {
  597. c, err := newSqliteCache(filename, startupQueries, time.Hour, 0, 0, false)
  598. if err != nil {
  599. t.Fatal(err)
  600. }
  601. return c
  602. }
  603. func newMemTestCache(t *testing.T) *messageCache {
  604. c, err := newMemCache()
  605. if err != nil {
  606. t.Fatal(err)
  607. }
  608. return c
  609. }