From a7ff9218ab70d006ab6efde687c6e3639dea1c1e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 7 Jan 2008 17:52:50 +0000 Subject: [PATCH] grumble bikeshed grumble --- libtransmission/Makefile.am | 5 +++- libtransmission/test-fastset.c | 15 ++++++----- libtransmission/test-peer-id.c | 47 ++++++++++++++++++++++++++++++++++ libtransmission/transmission.c | 18 +++++-------- 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 libtransmission/test-peer-id.c diff --git a/libtransmission/Makefile.am b/libtransmission/Makefile.am index 402d10f91..d64ff21c1 100644 --- a/libtransmission/Makefile.am +++ b/libtransmission/Makefile.am @@ -73,7 +73,8 @@ noinst_HEADERS = \ noinst_PROGRAMS = \ - test-fastset + test-fastset \ + test-peer-id TESTS = $(noinst_PROGRAMS) @@ -86,6 +87,8 @@ TEST_LDADD = \ test_fastset_SOURCES = test-fastset.c test_fastset_LDADD = $(TEST_LDADD) +test_peer_id_SOURCES = test-peer-id.c +test_peer_id_LDADD = $(TEST_LDADD) diff --git a/libtransmission/test-fastset.c b/libtransmission/test-fastset.c index 8ba1021c7..2ec86f78d 100644 --- a/libtransmission/test-fastset.c +++ b/libtransmission/test-fastset.c @@ -4,19 +4,22 @@ #include "peer-mgr.h" #include "utils.h" +#define VERBOSE 0 #define check(A) { \ ++test; \ - if (A) \ - fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ - else { \ - fprintf( stderr, "FAILPASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ + if (A) { \ + if( VERBOSE ) \ + fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ + } else { \ + if( VERBOSE ) \ + fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ return test; \ } \ } - -int main( void ) +int +main( void ) { uint32_t i; int test = 0; diff --git a/libtransmission/test-peer-id.c b/libtransmission/test-peer-id.c new file mode 100644 index 000000000..b829be7d0 --- /dev/null +++ b/libtransmission/test-peer-id.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include "transmission.h" +#include "utils.h" + +#define VERBOSE 0 + +#define check(A) { \ + ++test; \ + if (A) { \ + if( VERBOSE ) \ + fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ + } else { \ + if( VERBOSE ) \ + fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ + return test; \ + } \ +} + +int +main( void ) +{ + int i; + int test = 0; + + for( i=0; i<100000; ++i ) + { + int j; + int val = 0; + uint8_t * pch = tr_peerIdNew( ); + + check( strlen( (char*)pch ) == 20 ); + check( !memcmp( pch, PEERID_PREFIX, 8 ) ); + + for( j=8; j<20; ++j ) { + char tmp[2] = { pch[j], '\0' }; + val += strtoul( tmp, NULL, 36 ); + } + + check( ( val % 36 ) == 0 ); + + tr_free( pch ); + } + + return 0; +} diff --git a/libtransmission/transmission.c b/libtransmission/transmission.c index 301fa5d71..56fac0d66 100644 --- a/libtransmission/transmission.c +++ b/libtransmission/transmission.c @@ -55,29 +55,25 @@ uint8_t* tr_peerIdNew( void ) { int i; + int val; int total = 0; uint8_t * buf = tr_new( uint8_t, 21 ); const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz"; - const int base = strlen( pool ); + const int base = 36; memcpy( buf, PEERID_PREFIX, 8 ); for( i=8; i<19; ++i ) { - const int val = tr_rand( base ); + val = tr_rand( base ); total += val; buf[i] = pool[val]; } - if( 1 ) { - int val = 0; - while( ( total + val ) % base ) - ++val; - buf[19] = pool[val]; - total += val; - } - - assert( ( total % base ) == 0 ); + val = total % base ? base - (total % base) : 0; + total += val; + buf[19] = pool[val]; buf[20] = '\0'; + return buf; }