Add hash clash unit tests

Add unit tests for keys with clashing hashes
This commit is contained in:
martinsumner 2016-11-07 23:53:14 +00:00
parent b47f8963d5
commit 105d3b66f2
2 changed files with 82 additions and 3 deletions

View file

@ -1774,6 +1774,49 @@ state_test() ->
?assertMatch({"Key1", "Value1"}, cdb_get(P1, "Key1")),
ok = cdb_close(P1).
hashclash_test() ->
{ok, P1} = cdb_open_writer("../test/hashclash_test.pnd",
#cdb_options{binary_mode=false}),
Key1 = "Key4184465780",
Key99 = "Key4254669179",
KeyNF = "Key9070567319",
?assertMatch(22, hash(Key1)),
?assertMatch(22, hash(Key99)),
?assertMatch(22, hash(KeyNF)),
ok = cdb_mput(P1, [{Key1, 1}, {Key99, 99}]),
?assertMatch(probably, cdb_keycheck(P1, Key1)),
?assertMatch(probably, cdb_keycheck(P1, Key99)),
?assertMatch(probably, cdb_keycheck(P1, KeyNF)),
?assertMatch({Key1, 1}, cdb_get(P1, Key1)),
?assertMatch({Key99, 99}, cdb_get(P1, Key99)),
?assertMatch(missing, cdb_get(P1, KeyNF)),
{ok, FN} = cdb_complete(P1),
{ok, P2} = cdb_open_reader(FN),
?assertMatch(probably, cdb_keycheck(P2, Key1)),
?assertMatch(probably, cdb_keycheck(P2, Key99)),
?assertMatch(probably, cdb_keycheck(P2, KeyNF)),
?assertMatch({Key1, 1}, cdb_get(P2, Key1)),
?assertMatch({Key99, 99}, cdb_get(P2, Key99)),
?assertMatch(missing, cdb_get(P2, KeyNF)),
ok = cdb_deletepending(P2),
?assertMatch(probably, cdb_keycheck(P2, Key1)),
?assertMatch(probably, cdb_keycheck(P2, Key99)),
?assertMatch(probably, cdb_keycheck(P2, KeyNF)),
?assertMatch({Key1, 1}, cdb_get(P2, Key1)),
?assertMatch({Key99, 99}, cdb_get(P2, Key99)),
?assertMatch(missing, cdb_get(P2, KeyNF)),
ok = cdb_close(P2).
corruptfile_test() ->
file:delete("../test/corrupt_test.pnd"),
{ok, P1} = cdb_open_writer("../test/corrupt_test.pnd",

View file

@ -391,8 +391,6 @@ delete_pending({get_kvrange, StartKey, EndKey, ScanWidth}, _From, State) ->
ScanWidth),
self()),
{reply, Reply, delete_pending, State, ?DELETE_TIMEOUT};
delete_pending(get_maxsqn, _From, State) ->
{reply, State#state.highest_sqn, delete_pending, State, ?DELETE_TIMEOUT};
delete_pending(close, _From, State) ->
leveled_log:log("SFT06", [State#state.filename]),
ok = file:close(State#state.handle),
@ -419,7 +417,6 @@ handle_event(_Msg, StateName, State) ->
handle_info(_Msg, StateName, State) ->
{next_state, StateName, State}.
terminate(Reason, _StateName, State) ->
leveled_log:log("SFT05", [Reason, State#state.filename]).
@ -1980,6 +1977,40 @@ big_iterator_test() ->
ok = file:close(Handle),
ok = file:delete(Filename).
hashclash_test() ->
Filename = "../test/hashclash.sft",
Key1 = {o, "Bucket", "Key838068", null},
Key99 = {o, "Bucket", "Key898982", null},
KeyNF = {o, "Bucket", "Key539122", null},
?assertMatch(4, hash_for_segmentid({keyonly, Key1})),
?assertMatch(4, hash_for_segmentid({keyonly, Key99})),
?assertMatch(4, hash_for_segmentid({keyonly, KeyNF})),
KeyList = lists:foldl(fun(X, Acc) ->
Key = {o,
"Bucket",
"Key8400" ++ integer_to_list(X),
null},
Value = {X, {active, infinity}, null},
Acc ++ [{Key, Value}] end,
[],
lists:seq(10,98)),
KeyListToUse = [{Key1, {1, {active, infinity}, null}}|KeyList]
++ [{Key99, {99, {active, infinity}, null}}],
{InitHandle, InitFileMD} = create_file(Filename),
{Handle, _FileMD, _Rem} = complete_file(InitHandle, InitFileMD,
KeyListToUse, [],
#level{level=1}),
ok = file:close(Handle),
{ok, SFTr, _KeyExtremes} = sft_open(Filename),
?assertMatch({Key1, {1, {active, infinity}, null}},
sft_get(SFTr, Key1)),
?assertMatch({Key99, {99, {active, infinity}, null}},
sft_get(SFTr, Key99)),
?assertMatch(not_present,
sft_get(SFTr, KeyNF)),
ok = sft_clear(SFTr).
filename_test() ->
FN1 = "../tmp/filename",
FN2 = "../tmp/filename.pnd",
@ -1992,4 +2023,9 @@ filename_test() ->
"../tmp/subdir/file_name.sft"},
generate_filenames(FN3)).
nonsense_coverage_test() ->
{ok, Pid} = gen_fsm:start(?MODULE, [], []),
undefined = gen_fsm:sync_send_all_state_event(Pid, nonsense),
ok = gen_fsm:send_all_state_event(Pid, nonsense).
-endif.