-#include<test_utils.h>
-
-#include<bencode.h>
-
-int main();
-static void bdecode_integer_basic_test();
-static void bdecode_string_basic_test();
-static void bdecode_type_basic_test();
-static void bencode_dict_basic_test();
-static void bencode_list_basic_test();
-static void bencode_size_basic_test();
-static void bencode_size_int_basic_test();
-static void bencode_string_basic_test();
-
-int main() {
- setup_env();
-
- bdecode_integer_basic_test();
- bdecode_string_basic_test();
- bdecode_type_basic_test();
- bencode_dict_basic_test();
- bencode_list_basic_test();
- bencode_size_basic_test();
- bencode_size_int_basic_test();
- bencode_string_basic_test();
-
- clean_env();
-
- return EXIT_SUCCESS;
-}
-
-static void bdecode_integer_basic_test() {
- uint8_t str1[] = "i10e";
- uint8_t str2[] = "i0e";
- uint8_t str3[] = "i0023423e";
- uint8_t str4[] = "i10928390128301e";
- long long int i;
-
- assert(bdecode_int(NULL,sizeof(str1)-1,&i)==-1);
- assert(bdecode_int(str1,0,&i)==-1);
- assert(bdecode_int(str1,sizeof(str1)-1,NULL)==-1);
- assert(bdecode_int(str1,sizeof(str1)-1,&i)==4);
- assert(i==10);
-
- assert(bdecode_int(str2,sizeof(str2)-1,&i)==3);
- assert(i==0);
-
- assert(bdecode_int(str3,sizeof(str3)-1,&i)==-1);
-
- assert(bdecode_int(str4,sizeof(str4)-1,&i)==16);
- assert(i==10928390128301);
-}
-
-static void bdecode_string_basic_test() {
- uint8_t buf[1024];
- uint8_t str1[] = "4:spam";
-
- assert(bdecode_string(NULL,sizeof(str1)-1,buf,1024)==-1);
- assert(bdecode_string(str1,0,buf,1024)==-1);
- assert(bdecode_string(str1,sizeof(str1)-1,NULL,1024)==-1);
- assert(bdecode_string(str1,sizeof(str1)-1,buf,0)==-1);
- assert(bdecode_string(str1,sizeof(str1)-1,buf,3)==-1);
- assert(bdecode_string(str1,sizeof(str1)-1,buf,4)==4);
- assert(bdecode_string(str1,sizeof(str1)-1,buf,1024)==4);
- assert(memcmp(buf,"spam",4)==0);
-}
-
-static void bdecode_type_basic_test() {
- uint8_t str1[] = "4:spam";
- uint8_t str2[] = "i12093e";
- uint8_t str3[] = "l4:spam4:eggse";
- uint8_t str4[] = "d3:cow3:moo4:spam4:eggse";
-
- assert(BENCODE_STRING==bdecode_type(str1,sizeof(str1)-1));
- assert(BENCODE_INTEGER==bdecode_type(str2,sizeof(str2)-1));
- assert(BENCODE_LIST==bdecode_type(str3,sizeof(str3)-1));
- assert(BENCODE_DICTIONARY==bdecode_type(str4,sizeof(str4)-1));
-}
-
-static void bencode_dict_basic_test() {
- uint8_t buf[1024];
- uint8_t *p;
-
- memset(buf,0,1024);
-
- p = buf;
- assert(bencode_dict_start(NULL,1024)==-1);
- assert(bencode_dict_start(p,0)==-1);
- assert(bencode_dict_start(p,1024)==1);
- p++;
-
- uint8_t str1[] = "cow";
- assert(bencode_string(str1,3,p,1023)==5);
- p += 5;
-
- uint8_t str2[] = "moo";
- assert(bencode_string(str2,3,p,1018)==5);
- p += 5;
-
- uint8_t str3[] = "spam";
- assert(bencode_string(str3,4,p,1013)==6);
- p += 6;
-
- uint8_t str4[] = "eggs";
- assert(bencode_string(str4,4,p,1007)==6);
- p += 6;
-
- assert(bencode_dict_end(NULL,1001)==-1);
- assert(bencode_dict_end(p,0)==-1);
- assert(bencode_dict_end(p,1001)==1);
-
- assert(memcmp(buf,"d3:cow3:moo4:spam4:eggse",24)==0);
-}
-
-static void bencode_list_basic_test() {
- uint8_t buf[1024];
- uint8_t *p;
-
- memset(buf,0,1024);
-
- p = buf;
- assert(bencode_list_start(NULL,1024)==-1);
- assert(bencode_list_start(p,0)==-1);
- assert(bencode_list_start(p,1024)==1);
- p++;
-
- uint8_t str1[] = "spam";
- assert(bencode_string(str1,4,p,1023)==6);
- p += 6;
-
- uint8_t str2[] = "eggs";
- assert(bencode_string(str2,4,p,1017)==6);
- p += 6;
-
- assert(bencode_list_end(NULL,1011)==-1);
- assert(bencode_list_end(p,0)==-1);
- assert(bencode_list_end(p,1011)==1);
-
- assert(memcmp(buf,"l4:spam4:eggse",14)==0);
-}
-
-static void bencode_size_basic_test() {
- uint8_t buf[1024];
-
- assert(bencode_size(0,buf,1)==-1);
- assert(bencode_size(0,buf,2)==2);
-
- assert(bencode_size(1234567890,buf,1024)==11);
-
- assert(bencode_size(SIZE_MAX,buf,1024)==-1);
- assert(bencode_size(INTMAX_MAX,buf,1024)>0);
-}
-
-static void bencode_size_int_basic_test() {
- assert(bencode_size_int(0)==1);
- assert(bencode_size_int(-1)==2);
- assert(bencode_size_int(109)==3);
- assert(bencode_size_int(23984723984)==11);
- assert(bencode_size_int(922337203685477580)==18);
- assert(bencode_size_int(INTMAX_MIN)>0);
- assert(bencode_size_int(INTMAX_MAX)>0);
-}
-
-static void bencode_string_basic_test() {
- uint8_t buf[1024];
-
- memset(buf,0,1024);
-
- uint8_t str1[] = "testlkajslfkdjasdfl test string";
- assert(bencode_string(NULL,31,buf,1024)==-1);
- assert(bencode_string(str1,31,NULL,1024)==-1);
- assert(bencode_string(str1,31,buf,0)==-1);
-
- assert(bencode_string(str1,0,buf,1024)==2);
- assert(memcmp("0:",buf,2)==0);
-
- assert(bencode_string(str1,31,buf,1024)==34);
- assert(memcmp("31:testlkajslfkdjasdfl test string",buf,34)==0);
-}