From 7541fa63d1546f1a434ec742627d9632dd751959 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Sat, 26 Apr 2014 20:38:48 +0200 Subject: Fix bug with adding first element of empty tree. And tests! --- src/ht.erl | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/ht.erl') diff --git a/src/ht.erl b/src/ht.erl index 2edee82..8b71628 100644 --- a/src/ht.erl +++ b/src/ht.erl @@ -54,7 +54,7 @@ size(Head) -> %% Example: N=6 (110) => l=1, the last and rightmost inner node. -spec append(head(), leaf() | iolist() | binary()) -> head(). append(#head{version = 0}, Leaf) when is_record(Leaf, leaf) -> - mkhead(0, Leaf); + mkhead(1, Leaf); append(Head, Leaf) when is_record(Leaf, leaf) -> N = Head#head.version, %Depth = depth(Head), @@ -167,8 +167,9 @@ rightbranchdepth(Tree, Acc) -> %%%%%%%%%%%%%%%%%%%% %% Internal tests. -define(TEST_VECTOR_TREES, - [<<148,242,40,0,3,172,180,106,111,230,146,161,32,40,128,38,103,8,194, - 102,72,68, 126,70,108,47,8,216,208,146,178,107>>]). + [[<<"a foo is a bar">>, + <<148,242,40,0,3,172,180,106,111,230,146,161,32,40,128,38,103,8,194, + 102,72,68, 126,70,108,47,8,216,208,146,178,107>>]]). %% Test vectors from certificate-transparency/src/python/ct/crypto/merkle_test.py. -define(EMPTY_HASH, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"). -define(TEST_VECTOR_LEAVES, ["", "\x00", "\x10", " !", "01", "@ABC", "PQRSTUVW", "`abcdefghijklmno"]). @@ -185,11 +186,12 @@ basic_helpers_test_() -> [test_bitcount()]. basic_tree_test_() -> - TestVectorTree = #leaf{hash = hd(?TEST_VECTOR_TREES)}, + TVT1 = lists:nth(1, ?TEST_VECTOR_TREES), [?_assertEqual(#head{version = 0, tree = undefined}, create()), - ?_assertEqual(#head{version = 1, tree = TestVectorTree}, - create("a foo is a bar"))]. + ?_assertEqual(#head{version = 1, + tree = #leaf{hash = lists:last(TVT1)}}, + create(hd(TVT1)))]. empty_hash_test_() -> [?_assertEqual(hex:hexstr_to_bin(?EMPTY_HASH), mth([]))]. @@ -202,6 +204,15 @@ mth_test() -> end, lists:seq(1, length(?TEST_VECTOR_LEAVES))). +new_tree_test_() -> + TVT1 = lists:nth(1, ?TEST_VECTOR_TREES), + V0 = create(), + V1 = append(V0, hd(TVT1)), + [?_assertEqual(0, V0#head.version), + ?_assertEqual(undefined, V0#head.tree), + ?_assertEqual(lists:last(TVT1), (V1#head.tree)#leaf.hash), + ?_assertEqual(1, V1#head.version)]. + %% @doc Build trees using append/2 and mth/2 and compare the resulting %% tree hashes. append_test() -> -- cgit v1.1