Skip to content

Commit

Permalink
Merge pull request openwallet-foundation#261 from dkulic/fix/sqlite_e…
Browse files Browse the repository at this point in the history
…xpire_check

fix: Incorrect SQLite expire check
  • Loading branch information
andrewwhitehead authored Jun 5, 2024
2 parents 1ec2110 + 8a45f93 commit b8f9237
Showing 1 changed file with 27 additions and 4 deletions.
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

0 comments on commit b8f9237

Please sign in to comment.