Exemple #1
0
static void memcmp_func() {
	char* src;
	char* dst;

	for(int i = 1; i < 4000; i++) {
		src = malloc(i);
		dst = malloc(i);

		memset(src, 0, i);		
		memset(dst, 0, i);		
		
		int rtn = __memcmp(src, dst, i);
		assert_int_equal(0, rtn);	

		free(src);
		src = NULL;
		free(dst);
		dst = NULL;
	}

	for(int i = 1; i < 4000; i++) {
		src = malloc(i);
		dst = malloc(i);

		memset(src, 1, i);
		memset(dst, 2, i);

		int rtn = __memcmp(dst, src, i);
		if(*src > *dst) {
			assert_in_range(rtn, 1, INT32_MAX);		
		} else if(*src < *dst) { 
			assert_in_range(rtn, INT32_MIN, -1);
		} 	
	}
}
Exemple #2
0
static void calloc_func() {
	size_t pool_size = 0x40000;	
	size_t malloc_size;

	/* element size 1 */	
	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		malloc_size = i;
		
		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);

		char* mem = __calloc(malloc_size, 1, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);
	
		for(size_t j = 0; j < malloc_size; j++) 
			assert_int_equal(0, (int)mem[j]);

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}

	/* element size 4 */
	for(size_t i = 1; i < (pool_size - 0x3e001) / 4; i++) {
		malloc_size = i;
		
		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);

		int* mem = __calloc(malloc_size, 4, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);
	
		for(size_t j = 0; j < malloc_size; j++) 
			assert_int_equal(0, (int)mem[j]);

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}


	/* element size 8 */
	for(size_t i = 1; i < (pool_size - 0x3e001) / 8; i++) {
		malloc_size = i;
		
		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);

		uint64_t* mem = __calloc(malloc_size, 8, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);
	
		for(size_t j = 0; j < malloc_size; j++) 
			assert_int_equal(0, mem[j]);

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}

}
Exemple #3
0
static void strncmp_func() {
	char text[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	char equal[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	/* Difference in head */
	char not_eqaul_1[1024] = "11234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	/* Difference in middle */
	char not_eqaul_2[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,.!~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	/* Difference in tail */
	char not_eqaul_3[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNMM?!\0";

	/* Comparing from head */
	for(size_t i = 1; i < strlen(text); i++) {
		assert_int_equal(0, __strncmp(text, equal, i));			
	}

	/* Comparing subset from head and forwarding to tail */
	for(size_t i = 1; i < strlen(text); i++) {
		assert_int_equal(0, __strncmp(text + i, equal + i, strlen(text) - i));			
	}

	/* When return value is over zero */
	assert_in_range(__strncmp(text, not_eqaul_1, strlen(text)), 1, INT32_MAX);
	assert_in_range(__strncmp(text, not_eqaul_2, strlen(text)), 1, INT32_MAX);
	/* when retrun value is under zero */	
	assert_in_range(__strncmp(text, not_eqaul_3, strlen(text)), INT32_MIN, -1);

}
Exemple #4
0
static void malloc_func(void** state) {
	size_t pool_size = 0x40000;
/*
	void* malloc_pool = malloc(pool_size);
	init_memory_pool(pool_size, malloc_pool, 0);
	
	size_t mem_size = 0x3e000;
	void* mem = __malloc(mem_size, malloc_pool);
	assert_in_range(mem, malloc_pool, malloc_pool + pool_size);	

	printf("extra malloc\n");
	void* mem2 = __malloc(1800, malloc_pool);
	assert_in_range(mem2, malloc_pool, malloc_pool + pool_size);	

	destroy_memory_pool(malloc_pool);
	free(malloc_pool);
	malloc_pool = NULL;

	return;
*/

	/*
	 * 0x3e000 is max size that won't make failure of malloc
	 * when pool size is set to 0x4000
	 * If malloc size over the 0x3e000(may be 0x3e001), malloc failed.
	 * more problems is descirbed in Packetngin App Test Sheet.
	 */

	/* use private malloc_pool */
	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);
		
		size_t mem_size = i;
		void* mem = __malloc(mem_size, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);	

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}	

	/* use __malloc_pool */
	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		__malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, __malloc_pool, 0);
		
		size_t mem_size = i;
		void* mem = __malloc(mem_size, NULL);

		assert_in_range(mem, __malloc_pool, __malloc_pool + pool_size);	

		destroy_memory_pool(__malloc_pool);
		free(__malloc_pool);
		__malloc_pool = NULL;
	}	
}
Exemple #5
0
static void torture_timeout_update(void **state){
    struct ssh_timestamp ts;
    (void) state;
    ssh_timestamp_init(&ts);
    usleep(50000);
    assert_int_equal(ssh_timeout_update(&ts,25), 0);
    assert_in_range(ssh_timeout_update(&ts,30000),29000,29960);
    assert_in_range(ssh_timeout_update(&ts,75),1,40);
    assert_int_equal(ssh_timeout_update(&ts,0),0);
    assert_int_equal(ssh_timeout_update(&ts,-1),-1);
}
Exemple #6
0
/* Should work until Apnic decides to assign it :) */
#define BLACKHOLE "1.1.1.1"

static int sshd_setup(void **state)
{
    torture_setup_sshd_server(state);

    return 0;
}

static int sshd_teardown(void **state) {
    torture_teardown_sshd_server(state);

    return 0;
}

static int session_setup(void **state)
{
    struct torture_state *s = *state;
    int verbosity = torture_libssh_verbosity();
    struct passwd *pwd;
    int rc;

    pwd = getpwnam("bob");
    assert_non_null(pwd);

    rc = setuid(pwd->pw_uid);
    assert_return_code(rc, errno);

    s->ssh.session = ssh_new();
    assert_non_null(s->ssh.session);

    ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

    return 0;
}

static int session_teardown(void **state)
{
    struct torture_state *s = *state;

    ssh_disconnect(s->ssh.session);
    ssh_free(s->ssh.session);

    return 0;
}

static void torture_connect_nonblocking(void **state) {
    struct torture_state *s = *state;
    ssh_session session = s->ssh.session;
    int rc;

    rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
    assert_true(rc == SSH_OK);
    ssh_set_blocking(session,0);

    do {
        rc = ssh_connect(session);
        assert_true(rc != SSH_ERROR);
    } while(rc == SSH_AGAIN);

    assert_true(rc == SSH_OK);
}

#if 0 /* This does not work with socket_wrapper */
static void torture_connect_timeout(void **state) {
    struct torture_state *s = *state;
    ssh_session session = s->ssh.session;
    struct timeval before, after;
    int rc;
    long timeout = 2;
    time_t sec;
    suseconds_t usec;

    rc = ssh_options_set(session, SSH_OPTIONS_HOST, BLACKHOLE);
    assert_true(rc == SSH_OK);
    rc = ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
    assert_true(rc == SSH_OK);

    rc = gettimeofday(&before, NULL);
    assert_true(rc == 0);
    rc = ssh_connect(session);
    assert_true(rc == SSH_ERROR);
    rc = gettimeofday(&after, NULL);
    assert_true(rc == 0);
    sec = after.tv_sec - before.tv_sec;
    usec = after.tv_usec - before.tv_usec;
    /* Borrow a second for the missing usecs, but don't bother calculating */
    if (usec < 0)
      sec--;
    assert_in_range(sec, 1, 3);
}
Exemple #7
0
    time_t date_time::to_time(const tm_type& tm, bool check_wday) {
        assert_in_range(tm, check_wday);
        time_t t;
        tm_type tt = tm;
#ifdef _WIN32
        t = _mkgmtime(&tt);
#else
        t = timegm(&tt);
#endif
        if (t == NULL_TIME)
            throw std::invalid_argument("invalid time structure on conversion to date and time");
        // Check that tm is canonical.
        std::string err;
        if (check_wday && tm.tm_wday != tt.tm_wday)
            err = std::string("week day (1-7) incorrect: ") + uripp::convert(tm.tm_wday + 1);
        else if (tm.tm_mday != tt.tm_mday)
            err = std::string("month day (1-31) incorrect: ") + uripp::convert(tm.tm_mday);
        else if (tm.tm_sec != tt.tm_sec)
            err = std::string("second (0-60) incorrect: ") + uripp::convert(tm.tm_sec);
        if (!err.empty()) {
            std::ostringstream oss;
            oss << "time structure for date (YYY-MM-DD) "
                << (tm.tm_year + 1900) << "-"
                << std::setw(2) << std::setfill('0') << (tm.tm_mon + 1) << "-"
                << std::setw(2) << std::setfill('0') << tm.tm_mday << ": "
                << err;
            throw std::invalid_argument(oss.str());
        }
        return t;
    }
Exemple #8
0
void test_processes_monitor(void)
{
    double cf_this[100] = { 0.0 };
    MonProcessesGatherData(cf_this);
    MonProcessesGatherData(cf_this);
    MonProcessesGatherData(cf_this);

    int usr, rusr, ousr;
    usr = rusr = ousr = 0;

    bool res = GetSysUsers(&usr, &rusr, &ousr);
    if (!res)
    {
        Log(LOG_LEVEL_NOTICE, "TEST SKIPPED!");
        return;
    }

    usr  = 3*usr;
    rusr = 3*rusr;
    ousr = 3*ousr;

    Log(LOG_LEVEL_NOTICE, "Counted %d/3 different users on the process table,"
        " while CFEngine counted %f/3", usr, cf_this[ob_users]);
    Log(LOG_LEVEL_NOTICE, "This is a non-deterministic test,"
        " the two numbers should be *about* the same since the 'ps'"
        " commands run very close to each other");

    int upper = (int) ((double) usr*1.10);
    int lower = (int) ((double) usr*0.90);
    assert_in_range((long long) cf_this[ob_users], lower, upper);
}
Exemple #9
0
void test_processes_monitor(void)
{
# ifdef __sun
  return; //redmine 6316
# endif
    double cf_this[100] = { 0.0 };
    MonProcessesGatherData(cf_this);
    MonProcessesGatherData(cf_this);
    MonProcessesGatherData(cf_this);
    int usr, rusr, ousr;

    usr = rusr = ousr = 0;
    bool res = GetSysUsers(&usr, &rusr, &ousr);
    if (res == false )
    {
        assert_true(1);
        return;
    }

    usr  = 3*usr;
    rusr = 3*rusr;
    ousr = 3*ousr;
    int upper = (int)((double)usr*1.10);
    int lower = (int)((double)usr*0.90);
    assert_in_range((long long)cf_this[ob_users], lower, upper);
}
Exemple #10
0
  void debug_assert_in_range( size_type offset, size_type size_in ) const
    {
      (void)offset; (void)size_in;
#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
      assert_in_range(offset, size_in);
#endif
    }
static void test_res_fake_a_query_case_insensitive(void **state)
{
    int rv;
    struct __res_state dnsstate;
    unsigned char answer[ANSIZE];
    char addr[INET_ADDRSTRLEN];
    ns_msg handle;
    ns_rr rr;   /* expanded resource record */

    (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_a,
                    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 A and have the address that our
     * fake hosts file contains. Case does not matter.
     */
    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_a);
    assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr),
                              addr, sizeof(addr)));
    assert_string_equal(addr, "127.0.0.21");

    res_nclose(&dnsstate);
}
static void test_res_fake_aaaa_query(void **state)
{
    int rv;
    struct __res_state dnsstate;
    unsigned char answer[ANSIZE];
    char addr[INET6_ADDRSTRLEN];
    ns_msg handle;
    ns_rr rr;   /* expanded resource record */

    (void) state; /* unused */

    memset(&dnsstate, 0, sizeof(struct __res_state));
    rv = res_ninit(&dnsstate);
    assert_int_equal(rv, 0);

    rv = res_nquery(&dnsstate, "cwrap6.org", ns_c_in, ns_t_aaaa,
                    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 AAAA and have the address that our
     * fake hosts file contains
     */
    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_aaaa);
    assert_non_null(inet_ntop(AF_INET6, ns_rr_rdata(rr),
                              addr, sizeof(addr)));
    assert_string_equal(addr, "2a00:1450:4013:c01::63");
}
Exemple #13
0
static void interface_alloc_func(void** state) {
	void* malloc_pool = malloc(POOL_SIZE);
	init_memory_pool(POOL_SIZE, malloc_pool, 0);

	IPv4Interface* interface = interface_alloc(malloc_pool);
	assert_in_range(interface, malloc_pool, malloc_pool + POOL_SIZE);

	destroy_memory_pool(malloc_pool);
	free(malloc_pool);
	malloc_pool = NULL;
}
Exemple #14
0
static void realloc_func() {
	size_t pool_size = 0x40000;
	size_t malloc_size;

	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		malloc_size = i;

		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);

		void* mem = __malloc(malloc_size, malloc_pool);		
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);

		mem = __realloc(mem, 0x3e000, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}
}
Exemple #15
0
static void strcmp_func() {
	char text[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	char equal[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	/* Difference in head that is smaller than origin text */
	char not_eqaul_1[1024] = "11234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	/* Difference in middle that is bigger than origin text */
	char not_eqaul_2[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,.!~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	/* Difference in tail that is biggeer than origin text */
	char not_eqaul_3[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNMM?!\0";

	assert_int_equal(0, __strcmp(text, equal));	

	/* When return value is over zero */
	assert_in_range(__strcmp(text, not_eqaul_1), 1, INT32_MAX);
	assert_in_range(__strcmp(text, not_eqaul_2), 1, INT32_MAX);
	/* when retrun value is under zero */	
	assert_in_range(__strcmp(text, not_eqaul_3), INT32_MIN, -1);
	assert_int_not_equal(0, __strcmp(text, not_eqaul_1));	
	assert_int_not_equal(0, __strcmp(text, not_eqaul_2));	
	assert_int_not_equal(0, __strcmp(text, not_eqaul_3));	
}
Exemple #16
0
void 
test__AOSegmentFilePathNameLen(void **state) 
{
	RelationData reldata;
	char* basepath = "base/21381/123";

	expect_any(relpath, &rnode);
	will_return(relpath, strdup(basepath));

	int r = AOSegmentFilePathNameLen(&reldata);

	assert_in_range(r, strlen(basepath) + 3, strlen(basepath) + 10);
}
static void test_res_fake_srv_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;
    int prio;
    int weight;
    int port;
    char hostname[MAXDNAME];

    (void) state; /* unused */

    memset(&dnsstate, 0, sizeof(struct __res_state));
    rv = res_ninit(&dnsstate);
    assert_int_equal(rv, 0);

    rv = res_nquery(&dnsstate, "_ldap._tcp.cwrap.org", ns_c_in, ns_t_srv,
                    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 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, 5);
    assert_int_equal(port, 389);
    assert_string_equal(hostname, "ldap.cwrap.org");
}
static void test_res_fake_aaaa_query_notfound(void **state)
{
    int rv;
    struct __res_state dnsstate;
    unsigned char answer[ANSIZE];
    ns_msg handle;

    (void) state; /* unused */

    memset(&dnsstate, 0, sizeof(struct __res_state));
    rv = res_ninit(&dnsstate);
    assert_int_equal(rv, 0);

    rv = res_nquery(&dnsstate, "nosuchentry.org", ns_c_in, ns_t_aaaa,
                    answer, sizeof(answer));
    assert_in_range(rv, 1, 100);

    ns_initparse(answer, sizeof(answer), &handle);
    /* The query must finish w/o an error and have no answer */
    assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
    assert_int_equal(ns_msg_count(handle, ns_s_an), 0);
}
/*
 * 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_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");
}
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");
}
Exemple #22
0
static void test_value_range(void **state)
{
    int a = *((int *)*state);

    assert_in_range(a, 0, 100);
}
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 newRand_void_sizeInRangeZeroToTen (void **state)
{
    array_t array = Array_newRand();
    assert_in_range (Array_getSize(array),0, 10);
    Array_delete(array);
}