Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incorrect SQLite expire check #261

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions askar-storage/src/backend/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ const COUNT_QUERY: &str = "SELECT COUNT(*) FROM items i
WHERE profile_id = ?1
AND (kind = ?2 OR ?2 IS NULL)
AND (category = ?3 OR ?3 IS NULL)
AND (expiry IS NULL OR expiry > DATETIME('now'))";
AND (expiry IS NULL OR DATETIME(expiry) > DATETIME('now'))";
const DELETE_QUERY: &str = "DELETE FROM items
WHERE profile_id = ?1 AND kind = ?2 AND category = ?3 AND name = ?4";
const FETCH_QUERY: &str = "SELECT i.id, i.value,
(SELECT GROUP_CONCAT(it.plaintext || ':' || HEX(it.name) || ':' || HEX(it.value))
FROM items_tags it WHERE it.item_id = i.id) AS tags
FROM items i WHERE i.profile_id = ?1 AND i.kind = ?2
AND i.category = ?3 AND i.name = ?4
AND (i.expiry IS NULL OR i.expiry > DATETIME('now'))";
AND (i.expiry IS NULL OR DATETIME(i.expiry) > DATETIME('now'))";
const INSERT_QUERY: &str =
"INSERT OR IGNORE INTO items (profile_id, kind, category, name, value, expiry)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)";
Expand All @@ -59,7 +59,7 @@ const SCAN_QUERY: &str = "SELECT i.id, i.kind, i.category, i.name, i.value,
FROM items i WHERE i.profile_id = ?1
AND (i.kind = ?2 OR ?2 IS NULL)
AND (i.category = ?3 OR ?3 IS NULL)
AND (i.expiry IS NULL OR i.expiry > DATETIME('now'))";
AND (i.expiry IS NULL OR DATETIME(i.expiry) > DATETIME('now'))";
const DELETE_ALL_QUERY: &str = "DELETE FROM items AS i
WHERE i.profile_id = ?1
AND (i.kind = ?2 OR ?2 IS NULL)
Expand Down Expand Up @@ -763,7 +763,7 @@ mod tests {
.provision(StoreKeyMethod::RawKey, key, None, false)
.await?;
let ts = expiry_timestamp(1000).unwrap();
let check = sqlx::query("SELECT datetime('now'), ?1, ?1 > datetime('now')")
let check = sqlx::query("SELECT datetime('now'), ?1, datetime(?1) > datetime('now')")
.bind(ts)
.fetch_one(&db.conn_pool)
.await?;
Expand All @@ -778,6 +778,29 @@ mod tests {
.unwrap();
}

#[test]
fn sqlite_check_expiry_timestamp_expired() {
block_on(async {
let key = generate_raw_store_key(None)?;
let db = SqliteStoreOptions::in_memory()
.provision(StoreKeyMethod::RawKey, key, None, false)
.await?;
let ts = expiry_timestamp(-1000).unwrap(); // put it to be already expired
let check = sqlx::query("SELECT datetime('now'), ?1, datetime(?1) > datetime('now')")
.bind(ts)
.fetch_one(&db.conn_pool)
.await?;
let now: String = check.try_get(0)?;
let cmp_ts: String = check.try_get(1)?;
let cmp: bool = check.try_get(2)?;
if cmp {
panic!("now ({}) < expiry timestamp ({})", now, cmp_ts);
}
Result::<_, Error>::Ok(())
})
.unwrap();
}

#[test]
fn sqlite_query_placeholders() {
assert_eq!(
Expand Down
Loading