SQLite: expand expiration test

This commit is contained in:
Tim Wojtulewicz 2025-02-28 15:14:26 -07:00
parent ac4aef2d94
commit a99a13dc4c
2 changed files with 48 additions and 27 deletions

View file

@ -1,6 +1,10 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
open result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<opaque of BackendHandleVal>] open result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<opaque of BackendHandleVal>]
put result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>] put result 1, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>]
get result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=value7890] put result 2, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>]
get result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=value1234]
get result same as inserted, T get result same as inserted, T
get result, [code=Storage::KEY_NOT_FOUND, error_str=<uninitialized>, value=<uninitialized>] get result 2, [code=Storage::SUCCESS, error_str=<uninitialized>, value=value2345]
get result 2 same as inserted, T
get result 1 after expiration, [code=Storage::KEY_NOT_FOUND, error_str=<uninitialized>, value=<uninitialized>]
get result 2 after expiration, [code=Storage::SUCCESS, error_str=<uninitialized>, value=value2345]

View file

@ -12,43 +12,60 @@ redef exit_only_after_terminate = T;
# Create a typename here that can be passed down into get(). # Create a typename here that can be passed down into get().
type str: string; type str: string;
global backend: opaque of Storage::BackendHandle; global b: opaque of Storage::BackendHandle;
global key: string = "key1234"; global key1: string = "key1234";
global value: string = "value7890"; global value1: string = "value1234";
event check_removed() { global key2: string = "key2345";
# This should return an error from the sqlite backend that there aren't any more global value2: string = "value2345";
# rows available.
local res2 = Storage::Sync::get(backend, key);
if ( res2$code != Storage::SUCCESS )
print "get result", res2;
Storage::Sync::close_backend(backend); event check_removed()
{
local res = Storage::Sync::get(b, key1);
print "get result 1 after expiration", res;
res = Storage::Sync::get(b, key2);
print "get result 2 after expiration", res;
Storage::Sync::close_backend(b);
terminate(); terminate();
} }
event setup_test() { event setup_test()
{
local opts : Storage::BackendOptions; local opts : Storage::BackendOptions;
opts$sqlite = [$database_path = "storage-test.sqlite", $table_name = "testing"]; opts$sqlite = [$database_path = "storage-test.sqlite", $table_name = "testing"];
local open_res = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str); local open_res = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
print "open result", open_res; print "open result", open_res;
backend = open_res$value;
local res = Storage::Sync::put(backend, [$key=key, $value=value, $expire_time=2 secs]); b = open_res$value;
print "put result", res;
local res2 = Storage::Sync::get(backend, key); # Insert a key that will expire in the time allotted
print "get result", res2; local res = Storage::Sync::put(b, [ $key=key1, $value=value1, $expire_time=2secs ]);
if ( res2$code == Storage::SUCCESS && res2?$value ) print "put result 1", res;
print "get result same as inserted", value == (res2$value as string);
schedule 5 secs { check_removed() }; # Insert a key that won't expire
} res = Storage::Sync::put(b, [ $key=key2, $value=value2, $expire_time=20secs ]);
print "put result 2", res;
event zeek_init() { res = Storage::Sync::get(b, key1);
print "get result", res;
if ( res$code == Storage::SUCCESS && res?$value )
print "get result same as inserted", value1 == ( res$value as string );
res = Storage::Sync::get(b, key2);
print "get result 2", res;
if ( res$code == Storage::SUCCESS && res?$value )
print "get result 2 same as inserted", value2 == ( res$value as string );
schedule 5secs { check_removed() };
}
event zeek_init()
{
# We need network time to be set to something other than zero for the # We need network time to be set to something other than zero for the
# expiration time to be set correctly. Schedule an event on a short # expiration time to be set correctly. Schedule an event on a short
# timer so packets start getting read and do the setup there. # timer so packets start getting read and do the setup there.
schedule 100 msecs { setup_test() }; schedule 100msecs { setup_test() };
} }