5 * Created by Victor Grishchenko on 3/12/09.
6 * Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
11 #include <gtest/gtest.h>
14 using namespace swift;
16 char hash123[] = "a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0";
17 char rooth123[] = "a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0";
19 char hash456a[] = "4d38c7459a659d769bb956c2d758d266008199a4";
20 char hash456b[] = "a923e4b60d2a2a2a5ede87479e0314b028e3ae60";
21 char rooth456[] = "5b53677d3a695f29f1b4e18ab6d705312ef7f8c3";
24 TEST(Sha1HashTest,Trivial) {
25 Sha1Hash hash("123\n");
26 EXPECT_STREQ(hash123,hash.hex().c_str());
30 TEST(Sha1HashTest,OfferDataTest) {
31 Sha1Hash roothash123(true,hash123);
32 //for(bin_t pos(0,0); !pos.is_all(); pos=pos.parent())
33 // roothash123 = Sha1Hash(roothash123,Sha1Hash::ZERO);
35 EXPECT_STREQ(rooth123,roothash123.hex().c_str());
36 HashTree tree("123",roothash123);
37 tree.OfferHash(bin_t(0,0),Sha1Hash(true,hash123));
38 ASSERT_EQ(1,tree.size_in_chunks());
39 ASSERT_TRUE(tree.OfferData(bin_t(0,0), "123\n", 4));
41 ASSERT_EQ(4,tree.size());
45 TEST(Sha1HashTest,SubmitTest) {
46 FILE* f123 = fopen("123","wb+");
47 fprintf(f123, "123\n");
49 HashTree ht123("123");
50 EXPECT_STREQ(hash123,ht123.hash(bin_t(0,0)).hex().c_str());
51 EXPECT_STREQ(rooth123,ht123.root_hash().hex().c_str());
52 EXPECT_EQ(4,ht123.size());
56 TEST(Sha1HashTest,OfferDataTest2) {
57 char data456a[1024]; // 2 chunks with cs 1024, 3 nodes in tree
58 for (int i=0; i<1024; i++)
61 for (int i=0; i<4; i++)
64 FILE* f456 = fopen("456","wb");
65 fwrite(data456a,1,1024,f456);
66 fwrite(data456b,1,4,f456);
69 Sha1Hash roothash456(Sha1Hash(true,hash456a),Sha1Hash(true,hash456b));
71 EXPECT_STREQ(rooth456,roothash456.hex().c_str());
72 HashTree tree("456",roothash456);
73 tree.OfferHash(bin_t(1,0),roothash456);
74 tree.OfferHash(bin_t(0,0),Sha1Hash(true,hash456a));
75 tree.OfferHash(bin_t(0,1),Sha1Hash(true,hash456b));
76 ASSERT_EQ(2,tree.size_in_chunks());
77 ASSERT_TRUE(tree.OfferData(bin_t(0,0), data456a, 1024));
78 ASSERT_TRUE(tree.OfferData(bin_t(0,1), data456b, 4));
80 ASSERT_EQ(1028,tree.size());
84 /*TEST(Sha1HashTest,HashFileTest) {
85 uint8_t a [1024], b[1024], c[1024];
89 Sha1Hash aaahash(a,1024), bbbhash(b,1024), ccchash(c,1024);
90 Sha1Hash abhash(aaahash,bbbhash), c0hash(ccchash,Sha1Hash::ZERO);
91 Sha1Hash aabbccroot(abhash,c0hash);
92 for(bin pos=bin(7); pos<bin::ALL; pos=pos.parent())
93 aabbccroot = Sha1Hash(aabbccroot,Sha1Hash::ZERO);
94 int f = open("testfile",O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
100 ASSERT_TRUE(aabbccroot==filetree.root);
101 EXPECT_EQ(2,filetree.peaks.size());
102 EXPECT_TRUE(aaahash==filetree[1]);
103 HashTree bootstree(filetree.root);
104 EXPECT_EQ( HashTree::DUNNO, bootstree.offer(filetree.peaks[0].first,filetree.peaks[0].second) );
105 EXPECT_EQ( HashTree::PEAK_ACCEPT, bootstree.offer(filetree.peaks[1].first,filetree.peaks[1].second) );
106 EXPECT_EQ( 3, bootstree.length );
107 EXPECT_EQ( 4, bootstree.mass );
108 EXPECT_EQ( HashTree::DUNNO, bootstree.offer(1,aaahash) );
109 EXPECT_EQ( HashTree::ACCEPT, bootstree.offer(2,bbbhash) );
110 EXPECT_TRUE ( bootstree.bits[3]==abhash );
111 EXPECT_TRUE ( bootstree.bits[1]==aaahash );
112 EXPECT_TRUE ( bootstree.bits[2]==bbbhash );
113 EXPECT_FALSE ( bootstree.bits[2]==aaahash );
117 int main (int argc, char** argv) {
120 testing::InitGoogleTest(&argc, argv);
121 return RUN_ALL_TESTS();