From 26d16db0e5e9d82694c0670dd6233277e129eb23 Mon Sep 17 00:00:00 2001 From: victor Date: Sat, 10 Oct 2009 17:54:17 +0000 Subject: [PATCH] now it builds git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@397 e16421f0-f15b-0410-abcd-98678b794739 --- SConstruct | 4 +- datagram.cpp | 4 +- tests/SConscript | 18 ++++---- tests/hashtest.cpp | 5 +- tests/ledbattest.cpp | 30 ++++++------ tests/ledbattest2.cpp | 30 ++++++------ tests/transfertest.cpp | 102 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 148 insertions(+), 45 deletions(-) create mode 100644 tests/transfertest.cpp diff --git a/SConstruct b/SConstruct index f485110..cc59e73 100644 --- a/SConstruct +++ b/SConstruct @@ -7,8 +7,8 @@ env = Environment(CPPPATH = ['.']) env.SharedLibrary ( target='p2tp', - source = [ 'bin.cpp','hashtree.cpp','datagram.cpp', - 'bins.cpp' ], + source = [ 'bin64.cpp','hashtree.cpp','datagram.cpp', + 'bins.cpp', 'transfer.cpp' ], LIBS=['stdc++','gtest','glog','crypto'] ) SConscript('tests/SConscript') diff --git a/datagram.cpp b/datagram.cpp index 3c2794a..a81622d 100644 --- a/datagram.cpp +++ b/datagram.cpp @@ -38,8 +38,8 @@ int Datagram::Recv () { int Datagram::Wait (int sockcnt, int* sockets, tint usec) { LOG(INFO)<<"waiting for "<>4, dev_avg = NEVER>>4; + tint DELAY_BIN = TINT_SEC*30; + tint min_delay = TINT_NEVER; + tint rtt_avg = TINT_NEVER>>4, dev_avg = TINT_NEVER>>4; tint last_bin_time = 0; tint last_drop_time = 0; int delay_bin = 0; deque history, delay_history; - tint min_delay_bins[4] = {NEVER,NEVER, - NEVER,NEVER}; - tint cur_delays[4] = {NEVER,NEVER, - NEVER,NEVER}; + tint min_delay_bins[4] = {TINT_NEVER,TINT_NEVER, + TINT_NEVER,TINT_NEVER}; + tint cur_delays[4] = {TINT_NEVER,TINT_NEVER, + TINT_NEVER,TINT_NEVER}; tint last_sec = 0; int sec_ackd = 0; @@ -51,7 +51,7 @@ TEST(Datagram,LedbatTest) { uint8_t* garbage = (uint8_t*) malloc(1024); int socks[2] = {send_sock,ack_sock}; int sock2read; - tint wait_time = 100*MSEC; + tint wait_time = 100*TINT_MSEC; while (sock2read = Datagram::Wait(2,socks,wait_time)) { tint now = Datagram::Time(); @@ -64,7 +64,7 @@ TEST(Datagram,LedbatTest) { ack.Push64(now); if (4+8!=ack.Send()) fprintf(stderr,"short write\n"); - fprintf(stderr,"%lli rcvd%i\n",now/SEC,seq); + fprintf(stderr,"%lli rcvd%i\n",now/TINT_SEC,seq); // TODO: peer cwnd !!! continue; } @@ -102,8 +102,8 @@ TEST(Datagram,LedbatTest) { if (send_time/DELAY_BIN != last_bin_time) { last_bin_time = send_time/DELAY_BIN; delay_bin = (delay_bin+1) % 4; - min_delay_bins[delay_bin] = NEVER; - min_delay = NEVER; + min_delay_bins[delay_bin] = TINT_NEVER; + min_delay = TINT_NEVER; for(int i=0;i<4;i++) if (min_delay_bins[i] cur_delays[i]) current_delay = cur_delays[i]; // FIXME avg @@ -125,7 +125,7 @@ TEST(Datagram,LedbatTest) { fprintf(stderr,"ackd cwnd%f cur%lli min%lli seq%i off%i\n", cwnd,current_delay,min_delay,seq_off+seq,seq); - if (now/SEC!=last_sec/SEC) { + if (now/TINT_SEC!=last_sec/TINT_SEC) { fprintf(stderr,"%i KB/sec\n",sec_ackd); sec_ackd = 0; last_sec = now; // FIXME @@ -162,7 +162,7 @@ TEST(Datagram,LedbatTest) { if (history.size()>4, dev_avg = NEVER>>4; + tint DELAY_BIN = TINT_SEC*30; + tint min_delay = TINT_NEVER; + tint rtt_avg = TINT_NEVER>>4, dev_avg = TINT_NEVER>>4; tint last_bin_time = 0; tint last_drop_time = 0; int delay_bin = 0; deque history, delay_history; - tint min_delay_bins[4] = {NEVER,NEVER, - NEVER,NEVER}; - tint cur_delays[4] = {NEVER,NEVER, - NEVER,NEVER}; + tint min_delay_bins[4] = {TINT_NEVER,TINT_NEVER, + TINT_NEVER,TINT_NEVER}; + tint cur_delays[4] = {TINT_NEVER,TINT_NEVER, + TINT_NEVER,TINT_NEVER}; tint last_sec = 0; int sec_ackd = 0; @@ -58,7 +58,7 @@ TEST(Datagram,LedbatTest) { uint8_t* garbage = (uint8_t*) malloc(1024); int socks[2] = {send_sock,ack_sock}; int sock2read; - tint wait_time = 100*MSEC; + tint wait_time = 100*TINT_MSEC; while (sock2read = Datagram::Wait(2,socks,wait_time)) { tint now = Datagram::Time(); @@ -71,7 +71,7 @@ TEST(Datagram,LedbatTest) { ack.Push64(now); if (4+8!=ack.Send()) fprintf(stderr,"short write\n"); - fprintf(stderr,"%lli rcvd%i\n",now/SEC,seq); + fprintf(stderr,"%lli rcvd%i\n",now/TINT_SEC,seq); // TODO: peer cwnd !!! continue; } @@ -109,8 +109,8 @@ TEST(Datagram,LedbatTest) { if (send_time/DELAY_BIN != last_bin_time) { last_bin_time = send_time/DELAY_BIN; delay_bin = (delay_bin+1) % 4; - min_delay_bins[delay_bin] = NEVER; - min_delay = NEVER; + min_delay_bins[delay_bin] = TINT_NEVER; + min_delay = TINT_NEVER; for(int i=0;i<4;i++) if (min_delay_bins[i] cur_delays[i]) current_delay = cur_delays[i]; // FIXME avg @@ -132,7 +132,7 @@ TEST(Datagram,LedbatTest) { fprintf(stderr,"ackd cwnd%f cur%lli min%lli seq%i off%i\n", cwnd,current_delay,min_delay,seq_off+seq,seq); - if (now/SEC!=last_sec/SEC) { + if (now/TINT_SEC!=last_sec/TINT_SEC) { fprintf(stderr,"%i KB/sec\n",sec_ackd); sec_ackd = 0; last_sec = now; // FIXME @@ -169,7 +169,7 @@ TEST(Datagram,LedbatTest) { if (history.size() +#include "p2tp.h" + +using namespace p2tp; + +const char* BTF = "big_test_file"; + +Sha1Hash A,B,C,D,E,AB,CD,ABCD,E0,E000,ABCDE000,ROOT; + + +TEST(TransferTest,TransferFile) { + + AB = Sha1Hash(A,B); + CD = Sha1Hash(C,D); + ABCD = Sha1Hash(AB,CD); + E0 = Sha1Hash(E,Sha1Hash::ZERO); + E000 = Sha1Hash(E0,Sha1Hash::ZERO); + ABCDE000 = Sha1Hash(ABCD,E000); + ROOT = ABCDE000; + for (int i=0; i<60; i++) + ROOT = Sha1Hash(ROOT,Sha1Hash::ZERO); + + // submit a new file + FileTransfer* seed = new FileTransfer(Sha1Hash::ZERO,BTF); + EXPECT_TRUE(ROOT==seed->root_hash); + EXPECT_EQ(4100,seed->size); + EXPECT_EQ(5,seed->sizek); + EXPECT_EQ(4100,seed->complete); + EXPECT_EQ(4100,seed->seq_complete); + + // retrieve it + FileTransfer* leech = new FileTransfer(seed->root_hash,"copy"); + // transfer peak hashes + for(int i=0; ipeak_count; i++) + leech->OfferHash(seed->peaks[i],seed->peak_hashes[i]); + ASSERT_EQ(1<<12,leech->size); + ASSERT_EQ(4,leech->sizek); + ASSERT_EQ(0,leech->complete); + // transfer data and hashes + // ABCDHASH + // ABHASH CDHASH + // AAAA BBBB CCCC DDDD E + leech->OfferHash(bin64_t(1,0), seed->hashes[bin64_t(1,0)]); + leech->OfferHash(bin64_t(1,1), seed->hashes[bin64_t(1,1)]); + for (int i=0; i<4; i++) { + /*if (leech->complete()==trap) { + delete leech; + FileTransfer* leech = new FileTransfer(seed,"copy"); + }*/ + bin64_t next = leech->picker->Pick(seed->ack_out,0); + uint8_t buf[1024]; //size_t len = seed->storer->ReadData(next,&buf); + size_t len = pread(seed->fd,buf,1024,next.base_offset()); + bin64_t sibling = next.sibling(); + leech->OfferHash(sibling, seed->hashes[sibling]); + leech->OfferData(next, buf, len); + } + EXPECT_EQ(4100,leech->size); + EXPECT_EQ(5,leech->sizek); + EXPECT_EQ(4100,leech->complete); + EXPECT_EQ(4100,leech->seq_complete); + + unlink("copy"); + +} + + +int main (int argc, char** argv) { + + int f = open(BTF,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); + uint8_t buf[1024]; + memset(buf,'A',1024); + A = Sha1Hash(buf,1024); + write(f,buf,1024); + memset(buf,'B',1024); + B = Sha1Hash(buf,1024); + write(f,buf,1024); + memset(buf,'C',1024); + C = Sha1Hash(buf,1024); + write(f,buf,1024); + memset(buf,'D',1024); + D = Sha1Hash(buf,1024); + write(f,buf,1024); + memset(buf,'E',4); + E = Sha1Hash(buf,4); + write(f,buf,4); + close(f); + + testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + + unlink(BTF); + + return ret; +} -- 2.20.1