void test_rpc_in_write(void **state) { int status; status = rpc_in_write(NULL, NULL, 64); assert_int_equal(status, 64); }
/* * Test the case of a SRV record query where the * fake hosts file entry is minimal in the sense * that it omits the priority and weight entries. * The server then fills in some default values. */ static void test_res_fake_srv_query_minimal(void **state) { int rv; struct __res_state dnsstate; unsigned char answer[ANSIZE]; ns_msg handle; ns_rr rr; /* expanded resource record */ const uint8_t *rrdata; int prio; int weight; int port; char hostname[MAXDNAME]; char addr[INET_ADDRSTRLEN]; (void) state; /* unused */ memset(&dnsstate, 0, sizeof(struct __res_state)); rv = res_ninit(&dnsstate); assert_int_equal(rv, 0); rv = res_nquery(&dnsstate, "_krb5._tcp.cwrap.org", ns_c_in, ns_t_srv, answer, sizeof(answer)); assert_in_range(rv, 1, 256); ns_initparse(answer, sizeof(answer), &handle); /* * The query must finish w/o an error, have one answer and the answer * must be a parseable RR of type SRV and have the priority, weight, * port and hostname as in the fake hosts file */ assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror); assert_int_equal(ns_msg_count(handle, ns_s_an), 1); assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_srv); rrdata = ns_rr_rdata(rr); NS_GET16(prio, rrdata); NS_GET16(weight, rrdata); NS_GET16(port, rrdata); rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, hostname, MAXDNAME); assert_int_not_equal(rv, -1); assert_int_equal(prio, 1); assert_int_equal(weight, 100); assert_int_equal(port, 88); assert_string_equal(hostname, "krb5.cwrap.org"); /* The additional section contains the A record of krb5.cwrap.org */ assert_int_equal(ns_msg_count(handle, ns_s_ar), 1); assert_int_equal(ns_parserr(&handle, ns_s_ar, 0, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_a); assert_string_equal(ns_rr_name(rr), "krb5.cwrap.org"); assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), addr, sizeof(addr))); assert_string_equal(addr, "127.0.0.23"); }
static void test_res_fake_cname_query(void **state) { int rv; struct __res_state dnsstate; unsigned char answer[ANSIZE]; ns_msg handle; ns_rr rr; /* expanded resource record */ const uint8_t *rrdata; char cname[MAXDNAME]; char addr[INET_ADDRSTRLEN]; (void) state; /* unused */ memset(&dnsstate, 0, sizeof(struct __res_state)); rv = res_ninit(&dnsstate); assert_int_equal(rv, 0); rv = res_nquery(&dnsstate, "rwrap.org", ns_c_in, ns_t_cname, answer, sizeof(answer)); assert_in_range(rv, 1, 256); ns_initparse(answer, 256, &handle); ns_initparse(answer, sizeof(answer), &handle); /* * The query must finish w/o an error, have one answer and the answer * must be a parseable RR of type CNAME and have the cname as in the * fake hosts file */ assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror); assert_int_equal(ns_msg_count(handle, ns_s_an), 1); assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_cname); rrdata = ns_rr_rdata(rr); rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, cname, MAXDNAME); assert_int_not_equal(rv, -1); assert_string_equal(cname, "web.cwrap.org"); /* The CNAME points to an A record that's present in the additional * section */ assert_int_equal(ns_msg_count(handle, ns_s_ar), 2); assert_int_equal(ns_parserr(&handle, ns_s_ar, 0, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_cname); assert_string_equal(ns_rr_name(rr), "web.cwrap.org"); rrdata = ns_rr_rdata(rr); rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, cname, MAXDNAME); assert_int_not_equal(rv, -1); assert_string_equal(cname, "www.cwrap.org"); assert_int_equal(ns_parserr(&handle, ns_s_ar, 1, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_a); assert_string_equal(ns_rr_name(rr), "www.cwrap.org"); assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), addr, sizeof(addr))); assert_string_equal(addr, "127.0.0.22"); }
static void curl_perform(mangusta_ctx_t * ctx, const char *url, long ver, const char *method) { CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl != NULL) { struct curl_slist *chunk = NULL; struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; curl_easy_setopt(curl, CURLOPT_USERAGENT, "Test suite"); /*curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); */ curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, ver); curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curl_dummy_write); if (!strcmp(method, "GET")) { /* Nothing to do */ } else if (!strcmp(method, "POST")) { curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, __FILE__, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, __FILE__, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); } else if (!strcmp(method, "PUT")) { curl_easy_setopt(curl, CURLOPT_PUT, 1L); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_READFUNCTION, curl_dummy_read); } else if (!strcmp(method, "DELETE")) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_READFUNCTION, curl_dummy_read); } else { assert_non_null(1); } if (strstr(url, "test2") != NULL) { chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked"); } res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); res = curl_easy_perform(curl); if (CURLE_OK == res) { char *ct; long rc; res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc); res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct); printf("CLIENT STATUS: %ld\n", rc); assert_int_equal(rc, 200); } else { assert_int_equal(res, CURLE_OK); } if (must_shutdown) { mangusta_context_stop(ctx); } curl_formfree(formpost); curl_slist_free_all(chunk); /* always cleanup */ curl_easy_cleanup(curl); } return; }
static void test_rehash(void ** UNUSED(state)) { void *hash_table = RESTc_new(RESTc_HashTable, 2); assert_non_null(hash_table); struct test_value_object *elementf = (struct test_value_object *) RESTc_new(_test_value_object, 7, 7); struct test_value_object *elementa = (struct test_value_object *) RESTc_new(_test_value_object, 2, 2); struct test_value_object *elementb = (struct test_value_object *) RESTc_new(_test_value_object, 4, 4); struct test_value_object *elementc = (struct test_value_object *) RESTc_new(_test_value_object, 8, 8); struct test_value_object *elementd = (struct test_value_object *) RESTc_new(_test_value_object, 16, 16); struct test_value_object *elemente = (struct test_value_object *) RESTc_new(_test_value_object, 1, 1); assert_non_null(elementa); assert_non_null(elementb); assert_non_null(elementc); assert_non_null(elementd); assert_non_null(elemente); assert_non_null(elementf); assert_int_equal(RESTc_HashTable.insert(hash_table, elementa), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementb), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementc), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementd), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elemente), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementf), true); struct test_value_object const* helementa = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementa); struct test_value_object const* helementb = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementb); struct test_value_object const* helementc = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementc); struct test_value_object const* helementd = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementd); struct test_value_object const* helemente = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elemente); struct test_value_object const* helementf = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementf); assert_non_null(helementa); assert_non_null(helementb); assert_non_null(helementc); assert_non_null(helementd); assert_non_null(helemente); assert_non_null(helementf); assert_true(elementa != helementa); assert_true(elementb != helementb); assert_true(elementc != helementc); assert_true(elementd != helementd); assert_true(elemente != helemente); assert_true(elementf != helementf); assert_int_equal(elementa->value, helementa->value); assert_int_equal(elementb->value, helementb->value); assert_int_equal(elementc->value, helementc->value); assert_int_equal(elementd->value, helementd->value); assert_int_equal(elemente->value, helemente->value); assert_int_equal(elementf->value, helementf->value); RESTc_delete(elementa); RESTc_delete(elementb); RESTc_delete(elementc); RESTc_delete(elementd); RESTc_delete(elemente); RESTc_delete(elementf); RESTc_delete(hash_table); }
/* This test will fail since the mock connect_to_database() will attempt to * retrieve a value for the parameter port which isn't specified by this * test function. */ static void test_connect_to_product_database_missing_parameter(void **state) { expect_string(connect_to_database, url, "products.abcd.org"); will_return(connect_to_database, 0xDA7ABA53); assert_int_equal((uintptr_t)connect_to_product_database(), 0xDA7ABA53); }
static void test_parse_good_numbers(void **state) { { const char *data = "[0.1]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[0.1234567890123456789]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[0.1234e10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[0.1234e+10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[0.1234e-10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[1203e10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[1203e+10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[123e-10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[0e-10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[0.0e-10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } { const char *data = "[-0.0e-10]"; JsonElement *json = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_true(json); JsonElementDestroy(json); } }
static void Create__new__queue(void **queue){ queue_t * q = queue_new(); assert_int_equal(queue_size(q),0); queue_remove(&q); }
static void strtoll_func() { /* Without integner prefix(e.g. 0x, 0b...) */ char text[1024] = "12345670qwert\0"; char* non_intger; long long int rtn = 0; int base = 0; /* base 0 */ rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 12345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* base 8 */ base = 8; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 012345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* base 10 */ base = 10; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 12345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* base 16 */ base = 16; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* With prefix 0(octet)*/ memset(text, 0, 1024); strncpy(text, "012345670qwert", strlen("012345670qwert")); /* base 0 */ base = 0; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 012345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* base 8 */ base = 8; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 012345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* base 10 */ base = 10; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 12345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* base 16 */ base = 16; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* With prefix 0x(hex)*/ memset(text, 0, 1024); strncpy(text, "0x12345670qwert", strlen("0x12345670qwert")); /* base 0 */ base = 0; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 10, strlen(text + 10)); /* base 8 */ base = 8; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0); assert_memory_equal(non_intger, text + 1, strlen(text + 1)); /* base 10 */ base = 10; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0); assert_memory_equal(non_intger, text + 1, strlen(text + 1)); /* base 16 */ base = 16; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 10, strlen(text + 10)); }
static void check_csync_pathes(void **state) { CSYNC *csync = *state; int rc; _csync_exclude_add( &(csync->excludes), "/exclude" ); /* Check toplevel dir, the pattern only works for toplevel dir. */ rc = csync_excluded(csync, "/exclude", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "/foo/exclude", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); /* check for a file called exclude. Must still work */ rc = csync_excluded(csync, "/exclude", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "/foo/exclude", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); /* Add an exclude for directories only: excl/ */ _csync_exclude_add( &(csync->excludes), "excl/" ); rc = csync_excluded(csync, "/excl", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "meep/excl", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "meep/excl/file", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "/excl", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); _csync_exclude_add(&csync->excludes, "/excludepath/withsubdir"); rc = csync_excluded(csync, "/excludepath/withsubdir", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "/excludepath/withsubdir", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "/excludepath/withsubdir2", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, "/excludepath/withsubdir/foo", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); }
static void check_csync_excluded(void **state) { CSYNC *csync = *state; int rc; rc = csync_excluded(csync, "", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, "/", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, "krawel_krawel", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, ".kde/share/config/kwin.eventsrc", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, ".htaccess/cache-maximegalon/cache1.txt", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "mozilla/.htaccess", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); /* * Test for patterns in subdirs. '.beagle' is defined as a pattern and has * to be found in top dir as well as in directories underneath. */ rc = csync_excluded(csync, ".apdisk", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "foo/.apdisk", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "foo/bar/.apdisk", CSYNC_FTW_TYPE_DIR); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, ".java", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); /* Files in the ignored dir .java will also be ignored. */ rc = csync_excluded(csync, ".apdisk/totally_amazing.jar", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); /* and also in subdirs */ rc = csync_excluded(csync, "projects/.apdisk/totally_amazing.jar", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); /* csync-journal is ignored in general silently. */ rc = csync_excluded(csync, ".csync_journal.db", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED); rc = csync_excluded(csync, ".csync_journal.db.ctmp", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED); rc = csync_excluded(csync, "subdir/.csync_journal.db", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED); /* pattern ]*.directory - ignore and remove */ rc = csync_excluded(csync, "my.~directory", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_AND_REMOVE); rc = csync_excluded(csync, "/a_folder/my.~directory", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_AND_REMOVE); /* Not excluded because the pattern .netscape/cache requires directory. */ rc = csync_excluded(csync, ".netscape/cache", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); /* Not excluded */ rc = csync_excluded(csync, "unicode/中文.hé", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); /* excluded */ rc = csync_excluded(csync, "unicode/пятницы.txt", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "unicode/中文.💩", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); /* path wildcards */ rc = csync_excluded(csync, "foobar/my_manuscript.out", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "latex_tmp/my_manuscript.run.xml", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); rc = csync_excluded(csync, "word_tmp/my_manuscript.run.xml", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, "latex/my_manuscript.tex.tmp", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_NOT_EXCLUDED); rc = csync_excluded(csync, "latex/songbook/my_manuscript.tex.tmp", CSYNC_FTW_TYPE_FILE); assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST); }
static void test_string_to_long(void **state) { assert_int_equal(1234567, StringToLong("1234567")); }
static void test_substring_evil(void **state) { char *new_string = StringSubstring("abcdef", 6, 4, -4); assert_int_equal(new_string, NULL); }
static void test_embedded_tag(void **state) { assert_int_equal(1, cbor_encode_tag(1, buffer, 512)); assert_memory_equal(buffer, ((unsigned char[]) { 0xC1 }), 1);
static void check_c_compare_file(void **state) { int rc; (void) state; rc = c_copy(check_src_file, check_dst_file, 0644); assert_int_equal(rc, 0); rc = c_compare_file( check_src_file, check_dst_file ); assert_int_equal(rc, 1); /* Check error conditions */ rc = c_compare_file( NULL, check_dst_file ); assert_int_equal(rc, -1); rc = c_compare_file( check_dst_file, NULL ); assert_int_equal(rc, -1); rc = c_compare_file( NULL, NULL ); assert_int_equal(rc, -1); rc = c_compare_file( check_src_file, "/I_do_not_exist_in_the_filesystem.dummy"); assert_int_equal(rc, -1); rc = c_compare_file( "/I_do_not_exist_in_the_filesystem.dummy", check_dst_file); assert_int_equal(rc, -1); rc = system("echo \"hallo42\" > /tmp/check/foo.txt"); assert_int_equal(rc, 0); rc = system("echo \"hallo52\" > /tmp/check/bar.txt"); assert_int_equal(rc, 0); rc = c_compare_file( check_src_file, check_dst_file ); assert_int_equal(rc, 0); /* Create two 1MB random files */ rc = system("dd if=/dev/urandom of=/tmp/check/foo.txt bs=1024 count=1024"); assert_int_equal(rc, 0); rc = system("dd if=/dev/urandom of=/tmp/check/bar.txt bs=1024 count=1024"); assert_int_equal(rc, 0); rc = c_compare_file( check_src_file, check_dst_file ); assert_int_equal(rc, 0); /* Create two 1MB random files with different size */ rc = system("dd if=/dev/urandom of=/tmp/check/foo.txt bs=1024 count=1024"); assert_int_equal(rc, 0); rc = system("dd if=/dev/urandom of=/tmp/check/bar.txt bs=1024 count=1020"); assert_int_equal(rc, 0); rc = c_compare_file( check_src_file, check_dst_file ); assert_int_equal(rc, 0); /* compare two big files which are equal */ rc = c_copy(check_src_file, check_dst_file, 0644); assert_int_equal(rc, 0); rc = c_compare_file( check_src_file, check_dst_file ); assert_int_equal(rc, 1); }
void test_connect_to_customer_database(void **state) { will_return(connect_to_database, 0x0DA7ABA53); assert_int_equal((int)connect_to_customer_database(), 0x0DA7ABA53); }
/* This test will fail since the expected URL is different to the URL that is * passed to connect_to_database() by connect_to_product_database(). */ static void test_connect_to_product_database_bad_url(void **state) { expect_string(connect_to_database, url, "products.abcd.com"); expect_value(connect_to_database, port, 322); will_return(connect_to_database, 0xDA7ABA53); assert_int_equal((uintptr_t)connect_to_product_database(), 0xDA7ABA53); }
static void test_ipv6(void) { /* * This test will check the ipv6 parser. We will directly call to it, we will not check * the generic frontend. * Cases to test: * 0000:0000:0000:0000:0000:0000:0000:0000 Ok * 0:0:0:0:0:0:0:0 Ok * a:b:c:d::1 Ok * a:b:c:d:0:1:2:3 Ok * [a::b] Ok * [a:b:c:d:e:f:0:1]:8080 Ok * 0:1:2::4 Ok * 0::2:3:4 Ok * ::3:4 Ok * ::::::: Fail * A:B:C:D:E:F:0:1 Fail */ struct IPV6Address ipv6; memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("0000:0000:0000:0000:0000:0000:0000:0000", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0); assert_int_equal(ipv6.sixteen[1], 0); assert_int_equal(ipv6.sixteen[2], 0); assert_int_equal(ipv6.sixteen[3], 0); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 0); assert_int_equal(ipv6.sixteen[6], 0); assert_int_equal(ipv6.sixteen[7], 0); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("0:0:0:0:0:0:0:0", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0); assert_int_equal(ipv6.sixteen[1], 0); assert_int_equal(ipv6.sixteen[2], 0); assert_int_equal(ipv6.sixteen[3], 0); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 0); assert_int_equal(ipv6.sixteen[6], 0); assert_int_equal(ipv6.sixteen[7], 0); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("a:b:c:d::1", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0x0a); assert_int_equal(ipv6.sixteen[1], 0x0b); assert_int_equal(ipv6.sixteen[2], 0x0c); assert_int_equal(ipv6.sixteen[3], 0x0d); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 0); assert_int_equal(ipv6.sixteen[6], 0); assert_int_equal(ipv6.sixteen[7], 1); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("a:b:c:d:0:1:2:3", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0x0a); assert_int_equal(ipv6.sixteen[1], 0x0b); assert_int_equal(ipv6.sixteen[2], 0x0c); assert_int_equal(ipv6.sixteen[3], 0x0d); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 1); assert_int_equal(ipv6.sixteen[6], 2); assert_int_equal(ipv6.sixteen[7], 3); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("[a::b]", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0x0a); assert_int_equal(ipv6.sixteen[1], 0); assert_int_equal(ipv6.sixteen[2], 0); assert_int_equal(ipv6.sixteen[3], 0); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 0); assert_int_equal(ipv6.sixteen[6], 0); assert_int_equal(ipv6.sixteen[7], 0x0b); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("[a:b:c:d:e:f:0:1]:8080", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0x0a); assert_int_equal(ipv6.sixteen[1], 0x0b); assert_int_equal(ipv6.sixteen[2], 0x0c); assert_int_equal(ipv6.sixteen[3], 0x0d); assert_int_equal(ipv6.sixteen[4], 0x0e); assert_int_equal(ipv6.sixteen[5], 0x0f); assert_int_equal(ipv6.sixteen[6], 0); assert_int_equal(ipv6.sixteen[7], 1); assert_int_equal(ipv6.port, 8080); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("0:1:2::4", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0); assert_int_equal(ipv6.sixteen[1], 1); assert_int_equal(ipv6.sixteen[2], 2); assert_int_equal(ipv6.sixteen[3], 0); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 0); assert_int_equal(ipv6.sixteen[6], 0); assert_int_equal(ipv6.sixteen[7], 4); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("0::2:3:4", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0); assert_int_equal(ipv6.sixteen[1], 0); assert_int_equal(ipv6.sixteen[2], 0); assert_int_equal(ipv6.sixteen[3], 0); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 2); assert_int_equal(ipv6.sixteen[6], 3); assert_int_equal(ipv6.sixteen[7], 4); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(0, IPV6_parser("::3:4", &ipv6)); assert_int_equal(ipv6.sixteen[0], 0); assert_int_equal(ipv6.sixteen[1], 0); assert_int_equal(ipv6.sixteen[2], 0); assert_int_equal(ipv6.sixteen[3], 0); assert_int_equal(ipv6.sixteen[4], 0); assert_int_equal(ipv6.sixteen[5], 0); assert_int_equal(ipv6.sixteen[6], 3); assert_int_equal(ipv6.sixteen[7], 4); assert_int_equal(ipv6.port, 0); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(-1, IPV6_parser(":::::::", &ipv6)); memset(&ipv6, 0, sizeof(struct IPV6Address)); assert_int_equal(-1, IPV6_parser("A:B:C:D:E:F:0:1", &ipv6)); }
static void test_basic_blocks(void **state) { uc_engine *uc = *state; uc_hook trace1; #define BASEADDR 0x1000000 uint64_t address = BASEADDR; const uint8_t code[] = { 0x33, 0xC0, // xor eax, eax 0x90, // nop 0x90, // nop 0xEB, 0x00, // jmp $+2 0x90, // nop 0x90, // nop 0x90, // nop }; static const struct bb blocks[] = { {BASEADDR, 6}, {BASEADDR+ 6, 3}, }; struct bbtest bbtest = { .blocks = blocks, .blocknum = 0, }; #undef BASEADDR // map 2MB memory for this emulation OK(uc_mem_map(uc, address, 2 * 1024 * 1024, UC_PROT_ALL)); // write machine code to be emulated to memory OK(uc_mem_write(uc, address, code, sizeof(code))); // trace all basic blocks OK(uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, test_basic_blocks_hook, &bbtest, 1, 0)); OK(uc_emu_start(uc, address, address+sizeof(code), 0, 0)); } /******************************************************************************/ // callback for tracing basic blocks static void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *user_data) { //printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size); } // callback for tracing instruction static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data) { //int eflags; //printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size); //uc_reg_read(uc, UC_X86_REG_EFLAGS, &eflags); //printf(">>> --- EFLAGS is 0x%x\n", eflags); // Uncomment below code to stop the emulation using uc_emu_stop() // if (address == 0x1000009) // uc_emu_stop(uc); } static void test_i386(void **state) { uc_engine *uc; uc_err err; uint32_t tmp; uc_hook trace1, trace2; const uint8_t code[] = "\x41\x4a"; // INC ecx; DEC edx const uint64_t address = 0x1000000; int r_ecx = 0x1234; // ECX register int r_edx = 0x7890; // EDX register // Initialize emulator in X86-32bit mode err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc); uc_assert_success(err); // map 2MB memory for this emulation err = uc_mem_map(uc, address, 2 * 1024 * 1024, UC_PROT_ALL); uc_assert_success(err); // write machine code to be emulated to memory err = uc_mem_write(uc, address, code, sizeof(code)-1); uc_assert_success(err); // initialize machine registers err = uc_reg_write(uc, UC_X86_REG_ECX, &r_ecx); uc_assert_success(err); err = uc_reg_write(uc, UC_X86_REG_EDX, &r_edx); uc_assert_success(err); // tracing all basic blocks with customized callback err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0); uc_assert_success(err); // tracing all instruction by having @begin > @end err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0); uc_assert_success(err); // emulate machine code in infinite time err = uc_emu_start(uc, address, address+sizeof(code)-1, 0, 0); uc_assert_success(err); // now print out some registers //printf(">>> Emulation done. Below is the CPU context\n"); uc_reg_read(uc, UC_X86_REG_ECX, &r_ecx); uc_reg_read(uc, UC_X86_REG_EDX, &r_edx); assert_int_equal(r_ecx, 0x1235); assert_int_equal(r_edx, 0x788F); // read from memory err = uc_mem_read(uc, address, (uint8_t *)&tmp, 4); uc_assert_success(err); //printf(">>> Read 4 bytes from [0x%"PRIX64"] = 0x%x\n", address, tmp); uc_close(uc); }
static void test_generic_interface(void) { /* * This test might seem short, but it is intentional. * All the parsing tests should be implemented directly * on the corresponding parser test. Keep this test as * lean as possible. */ IPAddress *address = NULL; Buffer *buffer = NULL; buffer = BufferNew(); assert_true(buffer != NULL); BufferSet(buffer, "127.0.0.1", strlen("127.0.0.1")); address = IPAddressNew(buffer); assert_true(address != NULL); assert_int_equal(IP_ADDRESS_TYPE_IPV4, IPAddressType(address)); assert_string_equal("127.0.0.1", BufferData(IPAddressGetAddress(address))); assert_int_equal(0, IPAddressGetPort(address)); assert_int_equal(0, IPAddressDestroy(&address)); BufferSet(buffer, "127.0.0.1:8080", strlen("127.0.0.1:8080")); address = IPAddressNew(buffer); assert_true(address != NULL); assert_int_equal(IP_ADDRESS_TYPE_IPV4, IPAddressType(address)); assert_string_equal("127.0.0.1", BufferData(IPAddressGetAddress(address))); assert_int_equal(8080, IPAddressGetPort(address)); assert_int_equal(0, IPAddressDestroy(&address)); BufferSet(buffer, "0:1:2:3:4:5:6:7", strlen("0:1:2:3:4:5:6:7")); address = IPAddressNew(buffer); assert_true(address != NULL); assert_int_equal(IP_ADDRESS_TYPE_IPV6, IPAddressType(address)); assert_string_equal("0:1:2:3:4:5:6:7", BufferData(IPAddressGetAddress(address))); assert_int_equal(0, IPAddressGetPort(address)); assert_int_equal(0, IPAddressDestroy(&address)); BufferSet(buffer, "[0:1:2:3:4:5:6:7]:9090", strlen("[0:1:2:3:4:5:6:7]:9090")); address = IPAddressNew(buffer); assert_true(address != NULL); assert_int_equal(IP_ADDRESS_TYPE_IPV6, IPAddressType(address)); assert_string_equal("0:1:2:3:4:5:6:7", BufferData(IPAddressGetAddress(address))); assert_int_equal(9090, IPAddressGetPort(address)); assert_int_equal(0, IPAddressDestroy(&address)); BufferDestroy(buffer); }
static void test_array_remove_range(void **state) { { // remove whole JsonElement *arr = JsonArrayCreate(5); JsonArrayAppendString(arr, "one"); JsonArrayAppendString(arr, "two"); JsonArrayAppendString(arr, "three"); JsonArrayRemoveRange(arr, 0, 2); assert_int_equal(JsonElementLength(arr), 0); JsonElementDestroy(arr); } { // remove middle JsonElement *arr = JsonArrayCreate(5); JsonArrayAppendString(arr, "one"); JsonArrayAppendString(arr, "two"); JsonArrayAppendString(arr, "three"); JsonArrayRemoveRange(arr, 1, 1); assert_int_equal(JsonElementLength(arr), 2); assert_string_equal(JsonArrayGetAsString(arr, 0), "one"); assert_string_equal(JsonArrayGetAsString(arr, 1), "three"); JsonElementDestroy(arr); } { // remove rest JsonElement *arr = JsonArrayCreate(5); JsonArrayAppendString(arr, "one"); JsonArrayAppendString(arr, "two"); JsonArrayAppendString(arr, "three"); JsonArrayRemoveRange(arr, 1, 2); assert_int_equal(JsonElementLength(arr), 1); assert_string_equal(JsonArrayGetAsString(arr, 0), "one"); JsonElementDestroy(arr); } { // remove but last JsonElement *arr = JsonArrayCreate(5); JsonArrayAppendString(arr, "one"); JsonArrayAppendString(arr, "two"); JsonArrayAppendString(arr, "three"); JsonArrayRemoveRange(arr, 0, 1); assert_int_equal(JsonElementLength(arr), 1); assert_string_equal(JsonArrayGetAsString(arr, 0), "three"); JsonElementDestroy(arr); } }
static void test_ipv4_address_comparison(void) { /* * We test different IPV4 combinations: * 1.1.1.1 vs 1.1.1.1 -> equal * 1.2.3.4 vs 1.1.1.1 -> not equal * 1.2.3.4 vs 1.2.1.1 -> not equal * 1.2.3.4 vs 1.2.3.1 -> not equal * 2.2.3.4 vs 1.2.3.4 -> not equal * 1.2.3.4 vs 1.2.3.4 -> equal * 1.2.3.4 vs NULL -> error * 1.2.3.4 vs 1:2:3:4:5:6:7:8 -> error */ IPAddress *a = NULL; IPAddress *b = NULL; Buffer *bufferA = NULL; Buffer *bufferB = NULL; bufferA = BufferNew(); assert_true(bufferA != NULL); BufferSet(bufferA, "1.1.1.1", strlen("1.1.1.1")); a = IPAddressNew(bufferA); assert_true(a != NULL); bufferB = BufferNew(); assert_true(bufferB != NULL); BufferSet(bufferB, "1.1.1.1", strlen("1.1.1.1")); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_true(IPAddressIsEqual(a, b)); BufferSet(bufferA, "1.2.3.4", strlen("1.2.3.4")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_false(IPAddressIsEqual(a, b)); BufferSet(bufferB, "1.2.1.1", strlen("1.2.1.1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); BufferSet(bufferB, "1.2.3.1", strlen("1.2.3.1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); BufferSet(bufferA, "2.2.3.4", strlen("2.2.3.4")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); BufferSet(bufferB, "1.2.3.4", strlen("1.2.3.4")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); BufferSet(bufferA, "1.2.3.4", strlen("1.2.3.4")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_true(IPAddressIsEqual(a, b)); assert_int_equal(IPAddressIsEqual(a, NULL), -1); assert_int_equal(IPAddressIsEqual(NULL, a), -1); BufferSet(bufferA, "1:2:3:4:5:6:7:8", strlen("1:2:3:4:5:6:7:8")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_int_equal(IPAddressIsEqual(a, b), -1); assert_int_equal(IPAddressIsEqual(b, a), -1); }
static void test_remove(void ** UNUSED(state)) { struct test_value_object const *helementa, *helementb, *helementc, *helementd; void *hash_table = RESTc_new(RESTc_HashTable, 10); assert_non_null(hash_table); struct test_value_object *elementa = (struct test_value_object *) RESTc_new(_test_value_object, 99, 99); struct test_value_object *elementb = (struct test_value_object *) RESTc_new(_test_value_object, 19, 19); struct test_value_object *elementc = (struct test_value_object *) RESTc_new(_test_value_object, 0, 0); struct test_value_object *elementd = (struct test_value_object *) RESTc_new(_test_value_object, 36, 36); assert_non_null(elementa); assert_non_null(elementb); assert_non_null(elementc); assert_non_null(elementd); assert_int_equal(RESTc_HashTable.insert(hash_table, elementa), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementb), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementc), true); assert_int_equal(RESTc_HashTable.insert(hash_table, elementd), true); assert_int_equal(RESTc_HashTable.remove(hash_table, elementd), true); helementa = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementa); helementb = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementb); helementc = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementc); helementd = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementd); assert_non_null(helementa); assert_non_null(helementb); assert_non_null(helementc); assert_null(helementd); assert_true(elementa != helementa); assert_true(elementb != helementb); assert_true(elementc != helementc); assert_int_equal(elementa->value, helementa->value); assert_int_equal(elementb->value, helementb->value); assert_int_equal(elementc->value, helementc->value); assert_int_equal(RESTc_HashTable.remove(hash_table, elementd), false); assert_int_equal(RESTc_HashTable.remove(hash_table, elementa), true); helementa = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementa); helementb = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementb); helementc = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementc); helementd = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementd); assert_null(helementa); assert_non_null(helementb); assert_non_null(helementc); assert_null(helementd); assert_true(elementb != helementb); assert_true(elementc != helementc); assert_int_equal(elementb->value, helementb->value); assert_int_equal(elementc->value, helementc->value); assert_int_equal(RESTc_HashTable.remove(hash_table, elementa), false); assert_int_equal(RESTc_HashTable.remove(hash_table, elementd), false); assert_int_equal(RESTc_HashTable.remove(hash_table, elementc), true); helementa = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementa); helementb = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementb); helementc = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementc); helementd = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementd); assert_null(helementa); assert_non_null(helementb); assert_null(helementc); assert_null(helementd); assert_true(elementb != helementb); assert_int_equal(elementb->value, helementb->value); assert_int_equal(RESTc_HashTable.remove(hash_table, elementd), false); assert_int_equal(RESTc_HashTable.remove(hash_table, elementc), false); assert_int_equal(RESTc_HashTable.remove(hash_table, elementa), false); assert_int_equal(RESTc_HashTable.remove(hash_table, elementb), true); helementa = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementa); helementb = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementb); helementc = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementc); helementd = (struct test_value_object const*) RESTc_HashTable.find(hash_table, elementd); assert_null(helementa); assert_null(helementb); assert_null(helementc); assert_null(helementd); RESTc_delete(elementa); RESTc_delete(elementb); RESTc_delete(elementc); RESTc_delete(elementd); RESTc_delete(hash_table); }
static void test_ipv6_address_comparison(void) { /* * We test different IPV6 combinations: * 1:1:1:1:1:1:1:1 vs 1:1:1:1:1:1:1:1 -> equal * 1:2:3:4:5:6:7:8 vs 1:1:1:1:1:1:1:1 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:1:1:1:1:1:1 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:3:1:1:1:1:1 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:3:4:1:1:1:1 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:1:1:1 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:1:1 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:1 -> not equal * 2:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:8 -> not equal * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:8 -> equal * Exotic variants * 1:0:0:0:0:0:0:1 vs 1::1 -> equal * 1:1:0:0:0:0:0:1 vs 1::1 -> not equal * 1:1:0:0:0:0:0:1 vs 1:1::1 -> equal * 1:0:0:0:0:0:1:1 vs 1::1:1 -> equal * Error conditions * 1::1:1 vs NULL -> error * 1::1:1 vs 1.2.3.4 -> error */ IPAddress *a = NULL; IPAddress *b = NULL; Buffer *bufferA = NULL; Buffer *bufferB = NULL; bufferA = BufferNew(); assert_true(bufferA != NULL); BufferSet(bufferA, "1:1:1:1:1:1:1:1", strlen("1:1:1:1:1:1:1:1")); a = IPAddressNew(bufferA); assert_true(a != NULL); bufferB = BufferNew(); assert_true(bufferB != NULL); BufferSet(bufferB, "1:1:1:1:1:1:1:1", strlen("1:1:1:1:1:1:1:1")); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_true(IPAddressIsEqual(a, b)); assert_true(bufferA != NULL); BufferSet(bufferA, "1:2:3:4:5:6:7:8", strlen("1:1:1:1:1:1:1:1")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:2:1:1:1:1:1:1", strlen("1:2:1:1:1:1:1:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:2:3:1:1:1:1:1", strlen("1:2:3:1:1:1:1:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:2:3:4:1:1:1:1", strlen("1:2:3:4:1:1:1:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:2:3:4:5:1:1:1", strlen("1:2:3:4:5:1:1:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:2:3:4:5:6:1:1", strlen("1:2:3:4:5:6:1:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:2:3:4:5:6:7:1", strlen("1:2:3:4:5:6:7:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "2:2:3:4:5:6:7:8", strlen("2:2:3:4:5:6:7:8")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferA != NULL); BufferSet(bufferA, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_true(bufferB != NULL); BufferSet(bufferB, "1::1", strlen("1::1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_true(IPAddressIsEqual(a, b)); assert_true(bufferA != NULL); BufferSet(bufferA, "1:1:0:0:0:0:0:1", strlen("1:1:0:0:0:0:0:1")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_false(IPAddressIsEqual(a, b)); assert_true(bufferB != NULL); BufferSet(bufferB, "1:1::1", strlen("1:1::1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_true(IPAddressIsEqual(a, b)); assert_true(bufferA != NULL); BufferSet(bufferA, "1::1:1", strlen("1::1:1")); assert_int_equal(IPAddressDestroy(&a), 0); a = IPAddressNew(bufferA); assert_true(a != NULL); assert_true(bufferB != NULL); BufferSet(bufferB, "1:0:0:0:0:0:1:1", strlen("1:0:0:0:0:0:1:1")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_true(IPAddressIsEqual(a, b)); assert_int_equal(IPAddressIsEqual(a, NULL), -1); assert_true(bufferB != NULL); BufferSet(bufferB, "1.2.3.4", strlen("1.2.3.4")); assert_int_equal(IPAddressDestroy(&b), 0); b = IPAddressNew(bufferB); assert_true(b != NULL); assert_int_equal(IPAddressIsEqual(a, b), -1); }
static void test_pdu_to_host_byte_order(void **state) { struct pdu_serial_notify pdu_serial; struct pdu_end_of_data_v1 pdu_eod; UNUSED(state); pdu_serial.ver = 1; pdu_serial.type = SERIAL_NOTIFY; pdu_serial.session_id = 0xDDFF; pdu_serial.len = 0xC; pdu_serial.sn = 0xDF; rtr_pdu_footer_to_host_byte_order(&pdu_serial); rtr_pdu_header_to_host_byte_order(&pdu_serial); assert_int_equal(pdu_serial.ver, 1); assert_int_equal(pdu_serial.type, SERIAL_NOTIFY); assert_int_equal(pdu_serial.len, 0xC000000); assert_int_equal(pdu_serial.sn, 0xDF000000); pdu_eod.ver = 1; pdu_eod.type = EOD; pdu_eod.session_id = 0xFDDF; pdu_eod.len = 0x18; pdu_eod.sn = 0xFEDCBA; pdu_eod.refresh_interval = 0xAF; pdu_eod.retry_interval = 0xDC; pdu_eod.expire_interval = 0xCCF; rtr_pdu_header_to_host_byte_order(&pdu_eod); rtr_pdu_footer_to_host_byte_order(&pdu_eod); assert_int_equal(pdu_eod.ver, 1); assert_int_equal(pdu_eod.type, EOD); assert_int_equal(pdu_eod.session_id, 0xDFFD); assert_int_equal(pdu_eod.len, 0x18000000); assert_int_equal(pdu_eod.sn, 0xBADCFE00); assert_int_equal(pdu_eod.refresh_interval, 0xAF000000); assert_int_equal(pdu_eod.retry_interval, 0xDC000000); assert_int_equal(pdu_eod.expire_interval, 0xCF0C0000); }
static void test_isipaddress(void) { /* * This test is just a summary of the other tests. * We just check that this interface works accordingly, most of the * functionality has already been tested. * 1.2.3.4 -> ok * 1.2..3 -> not * 1.a.2.3 -> not * 256.255.255.255 -> not * 255.255.255.255 -> ok * 1:0:0:0:0:0:0:1 -> ok * 1:1:1:1:0:1:1:1 -> ok * a:b:c:d:e:f:0:1 -> ok * a:b:c:d:e:f:g:h -> not * ffff:ffff:fffff:0:0:0:0:1 -> not */ IPAddress *address = NULL; Buffer *bufferAddress = NULL; bufferAddress = BufferNew(); assert_true (bufferAddress != NULL); BufferSet(bufferAddress, "1.2.3.4", strlen("1.2.3.4")); assert_true(IPAddressIsIPAddress(bufferAddress, NULL)); assert_true(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address != NULL); assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4); BufferClear(bufferAddress); assert_int_equal(IPAddressDestroy(&address), 0); BufferSet(bufferAddress, "1.2..3", strlen("1.2..3")); assert_false(IPAddressIsIPAddress(bufferAddress, NULL)); assert_false(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address == NULL); BufferClear(bufferAddress); BufferSet(bufferAddress, "1.a.2.3", strlen("1.a.2.3")); assert_false(IPAddressIsIPAddress(bufferAddress, NULL)); assert_false(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address == NULL); BufferClear(bufferAddress); BufferSet(bufferAddress, "256.255.255.255", strlen("256.255.255.255")); assert_false(IPAddressIsIPAddress(bufferAddress, NULL)); assert_false(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address == NULL); BufferClear(bufferAddress); BufferSet(bufferAddress, "255.255.255.255", strlen("255.255.255.255")); assert_true(IPAddressIsIPAddress(bufferAddress, NULL)); assert_true(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address != NULL); assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4); BufferClear(bufferAddress); assert_int_equal(IPAddressDestroy(&address), 0); BufferSet(bufferAddress, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1")); assert_true(IPAddressIsIPAddress(bufferAddress, NULL)); assert_true(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address != NULL); assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6); BufferClear(bufferAddress); assert_int_equal(IPAddressDestroy(&address), 0); BufferSet(bufferAddress, "1:1:1:1:0:1:1:1", strlen("1:1:1:1:0:1:1:1")); assert_true(IPAddressIsIPAddress(bufferAddress, NULL)); assert_true(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address != NULL); assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6); BufferClear(bufferAddress); assert_int_equal(IPAddressDestroy(&address), 0); BufferSet(bufferAddress, "a:b:c:d:e:f:0:1", strlen("a:b:c:d:e:f:0:1")); assert_true(IPAddressIsIPAddress(bufferAddress, NULL)); assert_true(IPAddressIsIPAddress(bufferAddress, &address)); assert_true(address != NULL); assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6); BufferClear(bufferAddress); assert_int_equal(IPAddressDestroy(&address), 0); BufferSet(bufferAddress, "a:b:c:d:e:f:g:h", strlen("a:b:c:d:e:f:g:h")); assert_false(IPAddressIsIPAddress(bufferAddress, NULL)); assert_false(IPAddressIsIPAddress(bufferAddress, &address)); BufferClear(bufferAddress); BufferSet(bufferAddress, "ffff:ffff:fffff:0:0:0:0:1", strlen("ffff:ffff:fffff:0:0:0:0:1")); assert_false(IPAddressIsIPAddress(bufferAddress, NULL)); assert_false(IPAddressIsIPAddress(bufferAddress, &address)); BufferClear(bufferAddress); }
static void test_res_fake_soa_query(void **state) { int rv; struct __res_state dnsstate; unsigned char answer[ANSIZE]; ns_msg handle; ns_rr rr; /* expanded resource record */ const uint8_t *rrdata; char nameser[MAXDNAME]; char admin[MAXDNAME]; int serial; int refresh; int retry; int expire; int minimum; (void) state; /* unused */ memset(&dnsstate, 0, sizeof(struct __res_state)); rv = res_ninit(&dnsstate); assert_int_equal(rv, 0); rv = res_nquery(&dnsstate, "cwrap.org", ns_c_in, ns_t_soa, answer, sizeof(answer)); assert_in_range(rv, 1, 100); ns_initparse(answer, sizeof(answer), &handle); /* * The query must finish w/o an error, have one answer and the answer * must be a parseable RR of type SOA and have the data as in the fake * hosts file */ assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror); assert_int_equal(ns_msg_count(handle, ns_s_an), 1); assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_soa); rrdata = ns_rr_rdata(rr); rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, nameser, MAXDNAME); assert_int_not_equal(rv, -1); rrdata += rv; rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, admin, MAXDNAME); assert_int_not_equal(rv, -1); rrdata += rv; NS_GET32(serial, rrdata); NS_GET32(refresh, rrdata); NS_GET32(retry, rrdata); NS_GET32(expire, rrdata); NS_GET32(minimum, rrdata); assert_string_equal(nameser, "ns1.cwrap.org"); assert_string_equal(admin, "admin.cwrap.org"); assert_int_equal(serial, 2014100457); assert_int_equal(refresh, 3600); assert_int_equal(retry, 300); assert_int_equal(expire, 1814400); assert_int_equal(minimum, 600); }
static void test_ipv4(void) { /* * This test will check the ipv4 parser. We will directly call to it, we will not check * the generic frontend. * Cases to test: * 0.0.0.0 Ok * 255.255.255.255 Ok * 1.1.1.1 Ok * 1.1.1.1:1 Ok * 1.2.3.4 Ok * 5.6.7.8:9 Ok * 10.0.0.9:0 Ok * 10.0.0.10:5308 Ok * 192.168.56.10:65535 Ok * 0 Fail * 0.1 Fail * 0.1.2 Fail * 0:0 Fail * 1.1.1.260 Fail * 1.1.260.1 Fail * 1.260.1.1 Fail * 260.1.1.1 Fail * 260.260.260.260 Fail * 1.1.1.1: Fail * 2.3.4.5:65536 Fail * a.b.c.d Fail */ struct IPV4Address ipv4; memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("0.0.0.0", &ipv4)); assert_int_equal(ipv4.octets[0], 0); assert_int_equal(ipv4.octets[1], 0); assert_int_equal(ipv4.octets[2], 0); assert_int_equal(ipv4.octets[3], 0); assert_int_equal(ipv4.port, 0); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("255.255.255.255", &ipv4)); assert_int_equal(ipv4.octets[0], 255); assert_int_equal(ipv4.octets[1], 255); assert_int_equal(ipv4.octets[2], 255); assert_int_equal(ipv4.octets[3], 255); assert_int_equal(ipv4.port, 0); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("1.1.1.1", &ipv4)); assert_int_equal(ipv4.octets[0], 1); assert_int_equal(ipv4.octets[1], 1); assert_int_equal(ipv4.octets[2], 1); assert_int_equal(ipv4.octets[3], 1); assert_int_equal(ipv4.port, 0); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("1.1.1.1:1", &ipv4)); assert_int_equal(ipv4.octets[0], 1); assert_int_equal(ipv4.octets[1], 1); assert_int_equal(ipv4.octets[2], 1); assert_int_equal(ipv4.octets[3], 1); assert_int_equal(ipv4.port, 1); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("1.2.3.4", &ipv4)); assert_int_equal(ipv4.octets[0], 1); assert_int_equal(ipv4.octets[1], 2); assert_int_equal(ipv4.octets[2], 3); assert_int_equal(ipv4.octets[3], 4); assert_int_equal(ipv4.port, 0); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("5.6.7.8:9", &ipv4)); assert_int_equal(ipv4.octets[0], 5); assert_int_equal(ipv4.octets[1], 6); assert_int_equal(ipv4.octets[2], 7); assert_int_equal(ipv4.octets[3], 8); assert_int_equal(ipv4.port, 9); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("10.0.0.9:0", &ipv4)); assert_int_equal(ipv4.octets[0], 10); assert_int_equal(ipv4.octets[1], 0); assert_int_equal(ipv4.octets[2], 0); assert_int_equal(ipv4.octets[3], 9); assert_int_equal(ipv4.port, 0); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("10.0.0.10:5308", &ipv4)); assert_int_equal(ipv4.octets[0], 10); assert_int_equal(ipv4.octets[1], 0); assert_int_equal(ipv4.octets[2], 0); assert_int_equal(ipv4.octets[3], 10); assert_int_equal(ipv4.port, 5308); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(0, IPV4_parser("192.168.56.10:65535", &ipv4)); assert_int_equal(ipv4.octets[0], 192); assert_int_equal(ipv4.octets[1], 168); assert_int_equal(ipv4.octets[2], 56); assert_int_equal(ipv4.octets[3], 10); assert_int_equal(ipv4.port, 65535); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("0", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("0.1", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("0.1.2", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("0:0", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("1.1.1.260", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("1.1.260.1", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("1.260.1.1", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("260.1.1.1", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("260.260.260.260", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("1.1.1.1:", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("2.3.4.5:65536", &ipv4)); memset(&ipv4, 0, sizeof(struct IPV4Address)); assert_int_equal(-1, IPV4_parser("a.b.c.d", &ipv4)); }
static void test_res_fake_a_via_cname(void **state) { int rv; struct __res_state dnsstate; unsigned char answer[ANSIZE]; ns_msg handle; ns_rr rr; /* expanded resource record */ const uint8_t *rrdata; char cname[MAXDNAME]; char addr[INET_ADDRSTRLEN]; (void) state; /* unused */ memset(&dnsstate, 0, sizeof(struct __res_state)); rv = res_ninit(&dnsstate); assert_int_equal(rv, 0); /* Query for A record, but the key is a CNAME. The expected result is * that the whole chain of CNAMEs will be included in the answer section * along with the resulting A */ rv = res_nquery(&dnsstate, "rwrap.org", ns_c_in, ns_t_a, answer, sizeof(answer)); assert_in_range(rv, 1, 256); ns_initparse(answer, sizeof(answer), &handle); /* * The query must finish w/o an error, have three answers and the answers * must be a parseable RR of type CNAME and have the cname as in the * fake hosts file */ assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror); assert_int_equal(ns_msg_count(handle, ns_s_an), 3); assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_cname); rrdata = ns_rr_rdata(rr); rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, cname, MAXDNAME); assert_int_not_equal(rv, -1); assert_string_equal(cname, "web.cwrap.org"); assert_int_equal(ns_parserr(&handle, ns_s_an, 1, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_cname); rrdata = ns_rr_rdata(rr); rv = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), rrdata, cname, MAXDNAME); assert_int_not_equal(rv, -1); assert_string_equal(cname, "www.cwrap.org"); assert_int_equal(ns_parserr(&handle, ns_s_an, 2, &rr), 0); assert_int_equal(ns_rr_type(rr), ns_t_a); assert_string_equal(ns_rr_name(rr), "www.cwrap.org"); assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), addr, sizeof(addr))); assert_string_equal(addr, "127.0.0.22"); }
/* Ensure add() adds two integers correctly. */ static void test_add(void **state) { (void) state; /* unused */ assert_int_equal(add(3, 3), 6); assert_int_equal(add(3, -3), 0); }