static void test_parse_array_bad_nested_elems(void) { { const char *data = "[ \"foo\" [\"baz\"], \"bar\" ]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[\"foo\"[\"baz\"],\"bar\"]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[ \"foo\" {\"boing\": \"baz\"}, \"bar\" ]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[\"foo\"{\"boing\":\"baz\"},\"bar\"]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void test_multipleHubsFound(void) { List *list = NULL; hostcount = 2; assert_int_equal(ListHubs(&list), 3); assert_int_not_equal(list, NULL); ListIterator *i = NULL; i = ListIteratorGet(list); HostProperties *host1 = (HostProperties *)ListIteratorData(i); assert_int_not_equal(ListIteratorNext(i), -1); HostProperties *host2 = (HostProperties *)ListIteratorData(i); assert_int_not_equal(ListIteratorNext(i), -1); HostProperties *host3 = (HostProperties *)ListIteratorData(i); assert_int_equal(host1->Port, 5308); assert_string_equal(host1->Hostname, "host1"); assert_string_equal(host1->IPAddress, "10.0.0.100"); assert_int_equal(host2->Port, 1234); assert_string_equal(host2->Hostname, "host2"); assert_string_equal(host2->IPAddress, "10.0.0.100"); assert_int_equal(host3->Port, 4321); assert_string_equal(host3->Hostname, "host3"); assert_string_equal(host3->IPAddress, "10.0.0.100"); ListIteratorDestroy(&i); ListDestroy(&list); }
static void test_int_find(void **state) { intheap *heap = *state; intheap_add(heap, 923, "d"); intheap_add(heap, 467, "b"); intheap_add(heap, 23, "a"); intheap_add(heap, 500, "c"); int k; char *v; k = intheap_find(heap, &intfind, "a"); assert_int_not_equal(k, -1); assert_true(intheap_remove(heap, k, &k, &v)); k = intheap_find(heap, &intfind, "c"); assert_int_not_equal(k, -1); assert_true(intheap_remove(heap, k, &k, &v)); k = intheap_find(heap, &intfind, "x"); assert_int_equal(k, -1); assert_false(intheap_remove(heap, k, &k, &v)); assert_true(intheap_pop(heap, &k, &v)); assert_int_equal(k, 467); assert_string_equal(v, "b"); assert_true(intheap_pop(heap, &k, &v)); assert_int_equal(k, 923); assert_string_equal(v, "d"); assert_false(intheap_pop(heap, &k, &v)); }
static void test_parse_array_garbage(void) { { const char *data = "[1, garbage]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[1 garbage]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[garbage]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[garbage, 1]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void test_parse_object_garbage(void) { { const char *data = "{ \"first\": 1, garbage \"second\": 2 }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "{ \"first\": 1 garbage \"second\": 2 }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "{ \"first\": garbage, \"second\": 2 }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "{ \"first\": garbage \"second\": 2 }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void test_read_ready_eof(void **state) { FillTmpFile(); size_t contents_length = strlen(TMP_FILE_CONTENTS); FILE *fp = fopen(TMP_FILE, "r"); assert_true(fp != NULL); int fd = fileno(fp); assert_int_not_equal(fd, -1); char buf[2]; for(int i = 0; i < contents_length; i++) { bool ready = IsReadReady(fd, 5); assert_true(ready); assert_int_not_equal(fgets(buf, sizeof(buf), fp), 0); } // EOF check assert_int_equal(fgets(buf, sizeof(buf), fp), 0); assert_int_not_equal(feof(fp), 0); bool ready = IsReadReady(fd, 5); assert_true(ready); assert_int_equal(fclose(fp), 0); RemoveTmpFile(); }
/* test with a file size that is not a multiply of the slize size. */ static void test_hbf_splitlist_odd( void **state ){ hbf_transfer_t *list = NULL; const char *dest_url = "http://localhost/ocm/remote.php/webdav/big/church.jpg"; int prev_id = 0; int i, fd; Hbf_State hbf_state; (void) state; /* open a file */ fd = open(test_file("church.jpg"), O_RDONLY); assert_true(fd >= 0); /* do a smoke test for uniqueness */ for( i=0; i < 10000; i++) { list = hbf_init_transfer(dest_url); assert_non_null(list); usleep(1); hbf_state = hbf_splitlist(list, fd); assert_int_not_equal(list->transfer_id, prev_id); prev_id = list->transfer_id; hbf_free_transfer(list); } list = hbf_init_transfer(dest_url); assert_non_null(list); hbf_state = hbf_splitlist(list, fd); assert_non_null(list); #ifndef NDEBUG assert_int_equal(list->calc_size, list->stat_size); #endif assert_int_not_equal(list->block_cnt, 0); assert_true( hbf_state == HBF_SUCCESS); /* checks on the block list */ if( 1 ) { int seen_zero_seq = 0; int prev_seq = -1; int64_t prev_block_end = -1; for( i=0; i < list->block_cnt; i++) { hbf_block_t *blk = list->block_arr[i]; assert_non_null(blk); if( blk->seq_number == 0 ) seen_zero_seq++; assert_int_equal(prev_seq, blk->seq_number -1 ); prev_seq = blk->seq_number; assert_true((prev_block_end+1) == (blk->start)); prev_block_end = blk->start + blk->size; } /* Make sure we exactly saw blk->seq_number == 0 exactly one times */ assert_int_equal( seen_zero_seq, 1 ); } hbf_free_transfer( list ); }
static void test_prepend_item(void) { Item *ip = NULL, *list = NULL; ip = PrependItem(&list, "hello", "classes"); assert_int_not_equal(ip, NULL); assert_int_not_equal(list, NULL); DeleteItem(&list, ip); assert_int_equal(list, NULL); }
static bool FsSupportsSparseFiles(const char *filename) { #ifdef __hpux Log(LOG_LEVEL_NOTICE, "HP-UX detected, skipping sparseness tests!" " Not sure why, but on HP-UX with 'vxfs' filesystem," " the sparse files generated have /sometimes/ greater" " 'disk usage' than their true size, and this is verified by du"); return false; #endif #ifdef __APPLE__ Log(LOG_LEVEL_NOTICE, "OS X detected, skipping sparseness tests!"); return false; #endif int fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0700); assert_int_not_equal(fd, -1); /* 8MB for our temporary sparse file sounds good. */ const int sparse_file_size = 8 * 1024 * 1024; off_t s_ret = lseek(fd, sparse_file_size, SEEK_CUR); assert_int_equal(s_ret, sparse_file_size); /* Make sure the file is not truncated by writing one byte and taking it back. */ ssize_t w_ret = write(fd, "", 1); assert_int_equal(w_ret, 1); int tr_ret = ftruncate(fd, sparse_file_size); assert_int_equal(tr_ret, 0); /* On ZFS the file needs to be synced, else stat() reports a temporary value for st_blocks! */ fsync(fd); int c_ret = close(fd); assert_int_equal(c_ret, 0); struct stat statbuf; int st_ret = stat(filename, &statbuf); assert_int_not_equal(st_ret, -1); int u_ret = unlink(filename); /* clean up */ assert_int_equal(u_ret, 0); /* ACTUAL TEST: IS THE FILE SPARSE? */ if (ST_NBYTES(statbuf) < statbuf.st_size) { return true; } else { return false; } }
void unit_message_serialize_request(UNUSED(void **state)) { msgpack_sbuffer sbuf; msgpack_packer pk; struct message_request request; array params; params.size = 1; params.obj = CALLOC(1, struct message_object); params.obj[0].type = OBJECT_TYPE_UINT; params.obj[0].data.uinteger = 1234; msgpack_sbuffer_init(&sbuf); /* positiv test */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); request.msgid = 1234; request.method = (string) {.str = "test method", .length = sizeof("test method") - 1}; request.params = params; assert_int_equal(0, message_serialize_request(&request, &pk)); msgpack_sbuffer_clear(&sbuf); /* no valid string */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); request.msgid = 1234; request.method = (string) STRING_INIT; request.params = params; assert_int_not_equal(0, message_serialize_request(&request, &pk)); msgpack_sbuffer_clear(&sbuf); free_params(request.params); params.size = 1; params.obj = CALLOC(1, struct message_object); params.obj[0].type = 1000; params.obj[0].data.uinteger = 1234; /* no valid params */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); request.msgid = 1234; request.method = (string) {.str = "test method", .length = sizeof("test method") - 1}; request.params = params; assert_int_not_equal(0, message_serialize_request(&request, &pk)); msgpack_sbuffer_clear(&sbuf); free_params(request.params); /* null check */ assert_int_not_equal(0, message_serialize_request(NULL, NULL)); msgpack_sbuffer_destroy(&sbuf); }
static void session_init_fails_with_no_dh2k() { zrtp_profile_t profile; zrtp_status_t s; zrtp_zid_t zid = "123456789012"; zrtp_session_t *new_session; /* Let's initialize ZRTP session with default profile first */ zrtp_profile_defaults(&profile, zrtp); new_session = NULL; s = zrtp_session_init(zrtp, &profile, zid, ZRTP_SIGNALING_ROLE_INITIATOR, &new_session); assert_int_equal(zrtp_status_ok, s); assert_non_null(new_session); /* Then disable DH2K and leave just mandatory parameters */ profile.pk_schemes[0] = ZRTP_PKTYPE_DH3072; profile.pk_schemes[1] = ZRTP_PKTYPE_MULT; profile.pk_schemes[2] = 0; new_session = NULL; s = zrtp_session_init(zrtp, &profile, zid, ZRTP_SIGNALING_ROLE_INITIATOR, &new_session); assert_int_equal(zrtp_status_ok, s); assert_non_null(new_session); /* Let's try to disable Multi key exchange, it should produce an error. */ profile.pk_schemes[0] = ZRTP_PKTYPE_DH3072; profile.pk_schemes[1] = 0; new_session = NULL; s = zrtp_session_init(zrtp, &profile, zid, ZRTP_SIGNALING_ROLE_INITIATOR, &new_session); assert_int_not_equal(zrtp_status_ok, s); assert_null(new_session); /* Profile checking with one of mandatory components missing should return error too. */ s = zrtp_profile_check(&profile, zrtp); assert_int_not_equal(zrtp_status_ok, s); /* NOTE: we ignore memory leaks and don't destroy ZRTP sessions to make test sources cleaner */ }
/* Fill a buffer with non-NULL garbage. */ static void WriteBufferToFile(const char *name, const void *buf, size_t count) { int fd = open(name, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0700); assert_int_not_equal(fd, -1); ssize_t written = FullWrite(fd, buf, count); assert_int_equal(written, count); int close_ret = close(fd); assert_int_not_equal(close_ret, -1); }
static void test_socket_getsockname(void **state) { struct torture_address addr = { .sa_socklen = sizeof(struct sockaddr_in), }; int rc; int s; (void) state; /* unused */ s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); assert_int_not_equal(s, -1); rc = getsockname(s, &addr.sa.in, &addr.sa_socklen); assert_return_code(rc, errno); assert_int_equal(addr.sa.in.sin_family, AF_INET); } #ifdef HAVE_IPV6 static void test_socket_getsockname6(void **state) { struct torture_address addr = { .sa_socklen = sizeof(struct sockaddr_in), }; int rc; int s; (void) state; /* unused */ s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); assert_int_not_equal(s, -1); rc = getsockname(s, &addr.sa.s, &addr.sa_socklen); assert_return_code(rc, errno); assert_int_equal(addr.sa.in6.sin6_family, AF_INET6); } #endif int main(void) { int rc; const UnitTest tests[] = { unit_test(test_socket_getsockname), #ifdef HAVE_IPV6 unit_test(test_socket_getsockname6), #endif }; rc = run_tests(tests); return rc; }
static void setup() { zrtp_status_t s; zrtp_test_endpoint_cfg_t endpoint_cfg; zrtp_test_endpoint_config_defaults(&endpoint_cfg); s = zrtp_test_endpoint_create(&endpoint_cfg, "Alice", &g_alice); assert_int_equal(zrtp_status_ok, s); assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_alice); s = zrtp_test_endpoint_create(&endpoint_cfg, "Bob", &g_bob); assert_int_equal(zrtp_status_ok, s); assert_int_not_equal(ZRTP_TEST_UNKNOWN_ID, g_bob); }
static void test_zonecut_params(void **state) { /* NULL args */ struct kr_zonecut cut; assert_int_not_equal(kr_zonecut_init(NULL, NULL, NULL), 0); assert_int_not_equal(kr_zonecut_init(&cut, NULL, NULL), 0); kr_zonecut_deinit(NULL); kr_zonecut_set(NULL, NULL); kr_zonecut_set(&cut, NULL); assert_int_not_equal(kr_zonecut_add(NULL, NULL, NULL), 0); assert_null((void *)kr_zonecut_find(NULL, NULL)); assert_null((void *)kr_zonecut_find(&cut, NULL)); assert_int_not_equal(kr_zonecut_set_sbelt(NULL, NULL), 0); assert_int_not_equal(kr_zonecut_find_cached(NULL, NULL, NULL, NULL, 0), 0); }
static void test_read_ready_timeout(void **state) { // NOTE: assuming no STDIN right now int fd = dup(STDIN_FILENO); assert_int_not_equal(fd, -1); bool ready; // tests fail here, so the assumption that there is no STDIN // right now seems to be incorrect. Commenting out the asserts until // clarified - other tests pass, so API seems to work as designed otherwise. // Maybe better to make sure that we read all data from the fd // first to bring it into a known state? ready = IsReadReady(fd, 1); // assert_false(ready); ready = IsReadReady(fd, 0); // assert_false(ready); // if we reach this we didn't crash or block // so consider basic acceptance test successful - // remove once above asserts are in place again ready = true; assert_true(ready); }
static void test_parse_object_nested_garbage(void) { { const char *data = "{ \"first\": { garbage } }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "{ \"first\": [ garbage ] }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void stat_local_file( csync_stat_t *sb, const char *file ) { const _TCHAR *mpath = NULL; mpath = c_multibyte(file); assert_int_not_equal(_tstat(mpath, sb), -1); c_free_multibyte(mpath); }
static void test_df1(void **state) { struct state *st = (*state); struct lyd_node *node; const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\">" "<bar><ho>1</ho><hi>42</hi></bar>" "<foo>42</foo><b1_1>42</b1_1>" "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" "<foo>42</foo><baz>42</baz></hidden>"; st->dt = lyd_new(NULL, st->mod, "df"); assert_ptr_not_equal(st->dt, NULL); /* presence container */ assert_ptr_not_equal((node = lyd_new(st->dt, NULL, "bar")), NULL); assert_int_not_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG), 0); assert_string_equal(ly_errmsg(), "Missing required element \"ho\" in \"bar\"."); /* manadatory node in bar */ assert_ptr_not_equal(lyd_new_leaf(node, NULL, "ho", "1"), NULL); assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG), 0); assert_int_equal(lyd_wd_add(NULL, &(st->dt), LYD_OPT_CONFIG | LYD_WD_ALL), 0); assert_int_equal(lyd_print_mem(&(st->xml), st->dt, LYD_XML, LYP_WITHSIBLINGS), 0); assert_ptr_not_equal(st->xml, NULL); assert_string_equal(st->xml, xml); }
/** Tests comparing non-equal JIDs. */ void test_cmp9(void **state) { struct jid *a = jid_new_from_str("aaa@bbb/ccc"); struct jid *b = jid_new_from_str("aaa@ddd"); assert_int_not_equal(jid_cmp(a, b), 0); jid_del(a); jid_del(b); }
static void sysrepo_daemon_test(void **state) { sr_conn_ctx_t *conn = NULL; int rc = SR_ERR_OK, ret = 0; /* print version */ ret = system("../src/sysrepod -v"); assert_int_equal(ret, 0); /* print help */ ret = system("../src/sysrepod -h"); assert_int_equal(ret, 0); /* start the daemon */ ret = system("../src/sysrepod"); assert_int_equal(ret, 0); /* connect to sysrepo, force daemon connection */ rc = sr_connect("daemon_test", SR_CONN_DAEMON_REQUIRED, &conn); assert_true(SR_ERR_OK == rc); /* disconnect */ sr_disconnect(conn); /* 2nd attempt to start the daemon - should fail since the daemon is running already */ ret = system("../src/sysrepod -l4"); assert_int_not_equal(ret, 0); }
/** Tests comparing non-equal JIDs. */ void test_cmp12(void **state) { struct jid *a = jid_new_from_str("bbb"); struct jid *b = jid_new_from_str("ccc"); assert_int_not_equal(jid_cmp(a, b), 0); jid_del(a); jid_del(b); }
/* TODO isolate important code and move to files_lib.c. */ static bool FileIsSparse(const char *filename) { MAYBE_SYNC_NOW; struct stat statbuf; int ret = stat(filename, &statbuf); assert_int_not_equal(ret, -1); Log(LOG_LEVEL_DEBUG, " st_size=%ju ST_NBYTES=%ju ST_NBLOCKS=%ju ST_BLKSIZE=%ju DEV_BSIZE=%ju", (uint64_t) statbuf.st_size, (uint64_t) ST_NBYTES(statbuf), (uint64_t) ST_NBLOCKS(statbuf), (uint64_t) ST_BLKSIZE(statbuf), (uint64_t) DEV_BSIZE); if (statbuf.st_size <= ST_NBYTES(statbuf)) { Log(LOG_LEVEL_DEBUG, "File is probably non-sparse"); return false; } else { /* We definitely know the file is sparse, since the allocated bytes * are less than the real size. */ Log(LOG_LEVEL_DEBUG, "File is definitely sparse"); return true; } }
/** Tests comparing non-equal JIDs with wildcards. */ void test_cmp_wildcards28(void **state) { struct jid *a = jid_new_from_str("*@*/ddd"); struct jid *b = jid_new_from_str("*@*/ccc"); assert_int_not_equal(jid_cmp_wildcards(a, b), 0); jid_del(a); jid_del(b); }
static void test_parse_array_comma_after_brace(void) { { const char *data = "[ , \"foo\", \"bar\" ]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[,\"foo\",\"bar\"]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void test_parse_array_nested_garbage(void **state) { { const char *data = "[1, [garbage]]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "[1, { garbage }]"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void test_parse_object_double_colon(void) { { const char *data = "{ \"foo\":: \"bar\" }"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } { const char *data = "{\"foo\"::\"bar\"}"; JsonElement *json = NULL; assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json)); assert_false(json); } }
static void test_process_start_time(void) { /* Wait a couple of seconds so that process start time differs. */ printf("Sleeping 2 seconds...\n"); sleep(2); pid_t new_pid = fork(); assert_true(new_pid >= 0); if (new_pid == 0) /* child */ { execl("/bin/sleep", "/bin/sleep", "30", NULL); assert_true(false); /* unreachable */ } SPAWNED_PID = new_pid; time_t newproc_starttime = GetProcessStartTime(new_pid); printf("Spawned a \"sleep\" child with PID %jd and start_time %jd\n", (intmax_t) new_pid, (intmax_t) newproc_starttime); // We might have slipped by a few seconds, but shouldn't be much. assert_int_not_equal(newproc_starttime, PROCESS_START_TIME_UNKNOWN); assert_true(newproc_starttime >= THIS_STARTTIME + 1); assert_true(newproc_starttime <= THIS_STARTTIME + 15); kill(new_pid, SIGKILL); wait(NULL); SPAWNED_PID = 0; }
//find angles between vectors static void vector_find_angle_VectorWithCoordThreeAndFourAndVectorsWithCoordinatesFourAndFive_notMinusOne(){ vector_t * vec1 = vector_test(3,4); vector_t * vec2 = vector_test(4,5); assert_int_not_equal(vector_find_angle(vec1,vec2),-1); vector_free(vec1); vector_free(vec2); }
int __wrap_sr_get_schema(sr_session_ctx_t *session, const char *module_name, const char *revision, const char *submodule_name, sr_schema_format_t format, char **schema_content) { int fd; struct stat st; (void)session; (void)revision; (void)submodule_name; if (format != SR_SCHEMA_YIN) { fail(); } if (!strcmp(module_name, "iana-if-type")) { fd = open(TESTS_DIR "/files/iana-if-type.yin", O_RDONLY); } else if (!strcmp(module_name, "ietf-interfaces")) { fd = open(TESTS_DIR "/files/ietf-interfaces.yin", O_RDONLY); } else if (!strcmp(module_name, "ietf-ip")) { fd = open(TESTS_DIR "/files/ietf-ip.yin", O_RDONLY); } else { return SR_ERR_NOT_FOUND; } assert_int_not_equal(fd, -1); assert_int_equal(fstat(fd, &st), 0); *schema_content = malloc((st.st_size + 1) * sizeof(char)); assert_int_equal(read(fd, *schema_content, st.st_size), st.st_size); close(fd); (*schema_content)[st.st_size] = '\0'; return SR_ERR_OK; }