Ejemplo n.º 1
0
static void
test_parse_packet_igmp_query_v2_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/igmp_query_v2.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_IPV4_IGMP );

  u_char macda[] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x01 };
  u_char macsa[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );

  assert_int_equal( packet_info->l2_payload_length, 32 );

  assert_int_equal( packet_info->igmp_type, IGMP_MEMBERSHIP_QUERY );
  assert_int_equal( packet_info->igmp_code, 100 );
  assert_int_equal( packet_info->igmp_checksum, 0xee9b );
  assert_int_equal( packet_info->igmp_group, 0 );

  assert_true( packet_type_igmp_membership_query( buffer ) );

  free_buffer( buffer );
}
Ejemplo n.º 2
0
static void test_bytes_node_element_at(void **state)
{
	struct bytes_node *head = bytes_node_init_from_str("It");
	struct bytes *word_2 = bytes_init_from_str("was");
	struct bytes *word_3 = bytes_init_from_str("the");
	struct bytes *word_4 = bytes_init_from_str("best");

	bytes_node_add(head, word_2);
	bytes_node_add(head, word_3);
	bytes_node_add(head, word_4);

	assert_memory_equal(bytes_node_element_at(head, 1)->bytes->data,
			    word_2->data, word_2->len *
			    sizeof(word_2->data[0]));
	assert_memory_equal(bytes_node_element_at(head, 2)->bytes->data,
			    word_3->data, word_3->len *
			    sizeof(word_3->data[0]));
	assert_memory_equal(bytes_node_element_at(head, 3)->bytes->data,
			    word_4->data, word_4->len *
			    sizeof(word_4->data[0]));

	bytes_put(word_4);
	bytes_put(word_3);
	bytes_put(word_2);
	bytes_node_put(head);
}
Ejemplo n.º 3
0
static void test_sysdb_attrs_add_base64_blob(void **state)
{
    struct sysdb_attrs *attrs;
    struct ldb_message_element *el;
    char zero[] = { '\1', '\2', '\3' };
    int ret;

    attrs = sysdb_new_attrs(NULL);
    assert_non_null(attrs);

    ret = sysdb_attrs_add_base64_blob(attrs, "testAttrABC", TEST_BASE64_ABC);
    assert_int_equal(ret, EOK);

    ret = sysdb_attrs_add_base64_blob(attrs, "testAttr000", TEST_BASE64_123);
    assert_int_equal(ret, EOK);

    ret = sysdb_attrs_get_el(attrs, "testAttrABC", &el);
    assert_int_equal(ret, EOK);
    assert_int_equal(el->num_values, 1);
    assert_non_null(el->values);
    assert_non_null(el->values[0].data);
    assert_int_equal(el->values[0].length, 3);
    assert_memory_equal(el->values[0].data, "abc", 3);

    ret = sysdb_attrs_get_el(attrs, "testAttr000", &el);
    assert_int_equal(ret, EOK);
    assert_int_equal(el->num_values, 1);
    assert_non_null(el->values);
    assert_non_null(el->values[0].data);
    assert_int_equal(el->values[0].length, 3);
    assert_memory_equal(el->values[0].data, zero, 3);
}
Ejemplo n.º 4
0
/* Basic behavior tests */
static void
test_irange_basic(void **state)
{
	IndexRange	irange;
	List	   *irange_list;

	/* test irb_pred() */
	assert_int_equal(99, irb_pred(100));
	assert_int_equal(0, irb_pred(1));
	assert_int_equal(0, irb_pred(0));

	/* test irb_succ() */
	assert_int_equal(100, irb_succ(99));
	assert_int_equal(IRANGE_BOUNDARY_MASK, irb_succ(IRANGE_BOUNDARY_MASK));
	assert_int_equal(IRANGE_BOUNDARY_MASK, irb_succ(IRANGE_BOUNDARY_MASK + 1));

	/* test convenience macros */
	irange = make_irange(0, IRANGE_BOUNDARY_MASK, IR_LOSSY);
	assert_int_equal(irange_lower(irange), 0);
	assert_int_equal(irange_upper(irange), IRANGE_BOUNDARY_MASK);
	assert_true(is_irange_lossy(irange));
	assert_true(is_irange_valid(irange));

	/* test allocation */
	irange_list = NIL;
	irange_list = lappend_irange(irange_list, irange);
	assert_memory_equal(&irange, &linitial_irange(irange_list), sizeof(IndexRange));
	assert_memory_equal(&irange, &llast_irange(irange_list), sizeof(IndexRange));
}
Ejemplo n.º 5
0
static void
test_parse_packet_arp_request_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/arp_req.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );
  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_ARP );

  u_char macda[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  u_char macsa[] = { 0x8c, 0x89, 0xa5, 0x16, 0x22, 0x09 };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
  assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_ARP );

  assert_int_equal( packet_info->l2_payload_length, 46 );

  assert_int_equal( packet_info->arp_ar_hrd, 0x0001 );
  assert_int_equal( packet_info->arp_ar_pro, 0x0800 );
  assert_int_equal( packet_info->arp_ar_hln, 6 );
  assert_int_equal( packet_info->arp_ar_pln, 4 );
  assert_int_equal( packet_info->arp_ar_op, 1 );
  assert_int_equal( packet_info->arp_spa, 0xc0a8642c );
  assert_memory_equal( packet_info->arp_sha, macsa, ETH_ADDRLEN );
  assert_int_equal( packet_info->arp_tpa, 0xc0a8642b );
  u_char maczero[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  assert_memory_equal( packet_info->arp_tha, maczero, ETH_ADDRLEN );

  assert_true( packet_type_arp_request( buffer ) );
  assert_false( packet_type_arp_reply( buffer ) );

  free_buffer( buffer );
}
Ejemplo n.º 6
0
static void
test_parse_packet_ipv6_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/icmp6_echo_req.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->ipv6_version, 6 );
  assert_int_equal( packet_info->ipv6_tc, 0 );
  assert_int_equal( packet_info->ipv6_flowlabel, 0 );
  assert_int_equal( packet_info->ipv6_plen, 0x40 );
  assert_int_equal( packet_info->ipv6_nexthdr, 0x3a );
  assert_int_equal( packet_info->ipv6_hoplimit, 0x40 );

  u_char saddr[] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                     0x8e, 0x89, 0xa5, 0xff, 0xfe, 0x16, 0x22, 0x09 };
  u_char daddr[] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                     0x8e, 0x89, 0xa5, 0xff, 0xfe, 0x15, 0x84, 0xcb };
  assert_memory_equal( packet_info->ipv6_saddr, saddr, IPV6_ADDRLEN );
  assert_memory_equal( packet_info->ipv6_daddr, daddr, IPV6_ADDRLEN );

  free_buffer( buffer );
}
Ejemplo n.º 7
0
static void brigade_flatten_leftover(void **state)
{
  char buf[80];
  char bufcmp[80];
  char buf2[2];
  char bufcmp2[2];
  size_t len = sizeof(buf);
  size_t len2 = sizeof(buf2);
  sln_brigade_t *bb;
  sln_bucket_t *e1;
  sln_bucket_t *e2;

  SLN_ERR(sln_brigade_create(&bb));
  SLN_ERR(sln_bucket_create_empty(&e1, 40));
  memset(e1->data, 'A', e1->size);
  SLN_BRIGADE_INSERT_TAIL(bb, e1);
  SLN_ERR(sln_bucket_create_empty(&e2, 42));
  memset(e2->data, 'B', e2->size);
  SLN_BRIGADE_INSERT_TAIL(bb, e2);
  SLN_ERR(sln_brigade_flatten(bb, &buf[0], &len));
  assert_int_equal(len, 80);
  assert_int_equal(sln_brigade_size(bb), 2);
  SLN_ERR(sln_brigade_flatten(bb, &buf2[0], &len2));
  assert_int_equal(len2, 2);
  sln_brigade_destroy(bb);

  memset(&bufcmp[0], 'A', 40);
  memset(&bufcmp[0]+40, 'B', 40);
  assert_memory_equal(buf, bufcmp, 80);

  memset(&bufcmp2[0], 'B', 2);
  assert_memory_equal(buf2, bufcmp2, 2);
}
Ejemplo n.º 8
0
static void brigade_pread_simple(void **state)
{
  sln_brigade_t *bb;
  sln_bucket_t *e;
  char buf[5];
  size_t len = 0;

  SLN_ERR(sln_brigade_create(&bb));
  SLN_ERR(sln_bucket_create_empty(&e, 20));
  SLN_BRIGADE_INSERT_TAIL(bb, e);
  memset(e->data, 'B', e->size);
  memset(e->data, 'A', 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 1, &buf[0], &len));
  assert_int_equal(len, 1);
  assert_memory_equal(buf, "A", 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 1, 1, &buf[0], &len));
  assert_int_equal(len, 1);
  assert_memory_equal(buf, "B", 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 2, &buf[0], &len));
  assert_int_equal(len, 2);
  assert_memory_equal(buf, "AB", 2);

  SLN_ERR(sln_brigade_pread_bytes(bb, 2, 2, &buf[0], &len));
  assert_int_equal(len, 2);
  assert_memory_equal(buf, "BB", 2);

  sln_brigade_destroy(bb);
}
Ejemplo n.º 9
0
static void test_bytestostr(void **state)
{
	char *ans_1 = "this is how the world ends...";
	char *ans_2 = "the quick brown fox jumps over the lazy dog";
	char *ans_3 = "that's all folks";

	struct bytes *b_1 = bytes_init_from_str(ans_1);
	struct bytes *b_2 = bytes_init_from_str(ans_2);
	struct bytes *b_3 = bytes_init_from_str(ans_3);

	char *res_1 = malloc(b_1->len * sizeof(*res_1) + 1);
	char *res_2 = malloc(b_2->len * sizeof(*res_2) + 1);
	char *res_3 = malloc(b_3->len * sizeof(*res_3) + 1);

	bytestostr(res_1, b_1);
	bytestostr(res_2, b_2);
	bytestostr(res_3, b_3);

	assert_memory_equal(res_1, ans_1, strlen(ans_1));
	assert_memory_equal(res_2, ans_2, strlen(ans_2));
	assert_memory_equal(res_3, ans_3, strlen(ans_3));

	free(res_1);
	free(res_2);
	free(res_3);
	bytes_put(b_1);
	bytes_put(b_2);
	bytes_put(b_3);
}
Ejemplo n.º 10
0
static void brigade_copy_into(void **state)
{
  sln_brigade_t *source;
  sln_brigade_t *dest;
  sln_bucket_t *e1;
  char buf[20];
  size_t len = 0;

  SLN_ERR(sln_brigade_create(sln_test_alloc, &source));
  SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10));
  SLN_BRIGADE_INSERT_TAIL(source, e1);
  memset(e1->data, 'A', e1->size);

  SLN_ERR(sln_brigade_create(sln_test_alloc, &dest));
  SLN_ERR(sln_brigade_copy_into(source, 0, 10, dest));
  assert_int_equal(sln_brigade_size(dest), 10);
  SLN_ERR(sln_brigade_pread_bytes(dest, 0, 10, &buf[0], &len));
  assert_memory_equal(buf, "AAAAAAAAAA", 2);
  sln_brigade_clear(dest);
  sln_brigade_destroy(dest);

  SLN_ERR(sln_brigade_create(sln_test_alloc, &dest));
  SLN_ERR(sln_brigade_copy_into(source, 2, 4, dest));
  assert_int_equal(sln_brigade_size(dest), 4);
  SLN_ERR(sln_brigade_pread_bytes(dest, 0, 4, &buf[0], &len));
  assert_memory_equal(buf, "AAAA", 4);
  sln_brigade_clear(dest);
  sln_brigade_destroy(dest);

  sln_brigade_destroy(source);
}
Ejemplo n.º 11
0
static void
test_parse_packet_snap_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/ipx.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_8023_SNAP );

  u_char macda[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  u_char macsa[] = { 0x00, 0x19, 0xdb, 0x17, 0xb9, 0x6f };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
  assert_true( packet_info->eth_type < ETH_MTU );

  u_char llc[] = { 0xe0, 0xe0, 0x03 };
  u_char oui[] = { 0xff, 0xff, 0x00 };
  assert_memory_equal( packet_info->snap_llc, llc, SNAP_LLC_LENGTH );
  assert_memory_equal( packet_info->snap_oui, oui, SNAP_OUI_LENGTH );
  assert_int_equal( packet_info->snap_type, 0xb700 );

  uint16_t sample = ntohs( *( uint16_t * ) packet_info->l2_payload );
  assert_int_equal( sample, 0x0400 );

  assert_int_equal( packet_info->l2_payload_length, 0xb2 );

  free_buffer( buffer );
}
Ejemplo n.º 12
0
void test_migrate_unqualified_names(void)
{
    DBHandle *db = setup(true);
    assert_int_equal(WriteDB(db, "foo", &dummy_event, sizeof(dummy_event)), true);
    assert_int_equal(WriteDB(db, "q.bar", &dummy_event, sizeof(dummy_event)), true);
    CloseDB(db);

    assert_int_equal(OpenDB(&db, dbid_bundles), true);

    /* Old entry migrated */
    assert_int_equal(HasKeyDB(db, "foo", strlen("foo") + 1), false);
    assert_int_equal(HasKeyDB(db, "default.foo", strlen("default.foo") + 1), true);
    Event read_value = { 0 };
    ReadDB(db, "default.foo", &read_value, sizeof(read_value));
    assert_memory_equal(&read_value, &dummy_event, sizeof(dummy_event));

    /* New entry preserved */
    assert_int_equal(HasKeyDB(db, "q.bar", strlen("q.bar") + 1), true);
    memset(&read_value, 0, sizeof(read_value));
    ReadDB(db, "q.bar", &read_value, sizeof(read_value));
    assert_memory_equal(&read_value, &dummy_event, sizeof(dummy_event));

    /* Version marker */
    assert_int_equal(HasKeyDB(db, "version", strlen("version") + 1), true);
    CloseDB(db);
}
Ejemplo n.º 13
0
void opt_test_get(void **state)
{
    int ret;
    struct sss_test_ctx *tctx;
    struct dp_option *opts;
    struct sss_test_conf_param params[] = {
        { "string_nodefault", "stringval2" },
        { "blob_nodefault", "blobval2" },
        { "int_nodefault", "456" },
        { "bool_true", "false" },
        { NULL, NULL },             /* Sentinel */
    };
    char *s;
    struct dp_opt_blob b;
    int i;
    bool bo;

    tctx = create_dom_test_ctx(global_talloc_context, TESTS_PATH, TEST_CONF_DB,
                               TEST_DOM_NAME, TEST_ID_PROVIDER, params);
    assert_non_null(tctx);

    ret = dp_get_options(global_talloc_context, tctx->confdb, tctx->conf_dom_path,
                         test_def_opts, OPT_NUM_OPTS, &opts);
    assert_int_equal(ret, EOK);

    /* Options that were not specified explicitly should only have the default
     * value, those that have been specified explicitly should carry that
     * value
     */
    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
    assert_non_null(s);
    assert_string_equal(s, "stringval2");

    s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
    assert_non_null(s);
    assert_string_equal(s, STRING_DEFAULT);

    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
    assert_non_null(b.data);
    assert_int_equal(b.length, strlen("blobval2"));
    assert_memory_equal(b.data, "blobval2", strlen("blobval2"));

    b = dp_opt_get_blob(opts, OPT_BLOB_DEFAULT);
    assert_non_null(b.data);
    assert_int_equal(b.length, strlen(BLOB_DEFAULT));
    assert_memory_equal(b.data, BLOB_DEFAULT, strlen(BLOB_DEFAULT));

    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
    assert_int_equal(i, 456);

    i = dp_opt_get_int(opts, OPT_INT_DEFAULT);
    assert_int_equal(i, INT_DEFAULT);

    bo = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
    assert_true(bo == false);

    bo = dp_opt_get_bool(opts, OPT_BOOL_FALSE);
    assert_true(bo == false);
}
Ejemplo n.º 14
0
static void
test_parse_packet_vtag_icmpv4_echo_reply_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/vtag_icmp_echo_rep.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_VTAG_IPV4_ICMPV4 );

  u_char macda[] = { 0x00, 0x1f, 0x3c, 0x48, 0xad, 0x3a };
  u_char macsa[] = { 0x00, 0x13, 0xd3, 0x40, 0x2e, 0x22 };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );

  assert_int_equal( packet_info->vlan_tci, 0x6f9f );
  assert_int_equal( packet_info->vlan_tpid, ETH_ETHTYPE_TPID );
  assert_int_equal( packet_info->vlan_prio, 3 );
  assert_int_equal( packet_info->vlan_cfi, 0 );
  assert_int_equal( packet_info->vlan_vid, 0x0f9f );

  assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );

  assert_int_equal( packet_info->l2_payload_length, 60 );

  assert_int_equal( packet_info->ipv4_version, 4 );
  assert_int_equal( packet_info->ipv4_ihl, 5 );
  assert_int_equal( packet_info->ipv4_tos, 0 );
  assert_int_equal( packet_info->ipv4_tot_len, 0x3c );
  assert_int_equal( packet_info->ipv4_id, 0x1652 );
  assert_int_equal( packet_info->ipv4_frag_off, 0 );
  assert_int_equal( packet_info->ipv4_ttl, 0x40 );
  assert_int_equal( packet_info->ipv4_protocol, IPPROTO_ICMP );
  assert_int_equal( packet_info->ipv4_checksum, 0xa2d3 );
  assert_int_equal( packet_info->ipv4_saddr, 0xc0a82001 );
  assert_int_equal( packet_info->ipv4_daddr, 0xc0a8204a );

  assert_int_equal( packet_info->l3_payload_length, 40 );

  assert_int_equal( packet_info->icmpv4_type, ICMP_TYPE_ECHOREP );
  assert_int_equal( packet_info->icmpv4_code, 0 );
  assert_int_equal( packet_info->icmpv4_id, 1024 );
  assert_int_equal( packet_info->icmpv4_seq, 24576 );

  uint16_t sample = ntohs( *( uint16_t * ) packet_info->l4_payload );
  assert_int_equal( sample, 0x6162 );
  assert_int_equal( packet_info->l4_payload_length, 32 );

  assert_false( packet_type_icmpv4_echo_request( buffer ) );
  assert_false( packet_type_icmpv4_dst_unreach( buffer ) );
  assert_false( packet_type_icmpv4_redirect( buffer ) );
  assert_true( packet_type_icmpv4_echo_reply( buffer ) );

  free_buffer( buffer );
}
Ejemplo n.º 15
0
static void arp_process_func(void** state) {
	// Timer setting
	timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz");

	// Nic initialization
	void* malloc_pool = malloc(POOL_SIZE);
	init_memory_pool(POOL_SIZE, malloc_pool, 0);

	__nics[0] = malloc(sizeof(NIC));
	__nic_count++;

	__nics[0]->mac = 0x74d4358f66cb;
	__nics[0]->pool_size = POOL_SIZE;
	__nics[0]->pool = malloc_pool;
	__nics[0]->output_buffer = fifo_create(8, malloc_pool);
	__nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool);


	// Arp request
	Packet* packet = nic_alloc(__nics[0], sizeof(arp_request_packet));
	memcpy(packet->buffer + packet->start, arp_request_packet, sizeof(arp_request_packet));

	Ether* ether = (Ether*)(packet->buffer + packet->start);
	ARP* arp = (ARP*)ether->payload;
	uint32_t addr = endian32(arp->tpa);
	nic_ip_add(__nics[0], addr);

	assert_true(arp_process(packet));
	packet = fifo_pop(__nics[0]->output_buffer); 
	assert_memory_equal(packet->buffer + packet->start, arp_reply_packet, 42);

	nic_free(packet);
	packet = NULL;
	
	// Arp response
	packet = nic_alloc(__nics[0], sizeof(arp_reply_packet));
	memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet));

	ether = (Ether*)(packet->buffer + packet->start);
	arp = (ARP*)ether->payload;
	addr = endian32(arp->tpa);
	nic_ip_add(__nics[0], addr);

	uint8_t comp_mac[6] = { 0xcb, 0x66, 0x8f, 0x35, 0xd4, 0x74 }; 
	uint32_t sip = endian32(arp->spa);
	Map* arp_table = nic_config_get(packet->nic, "net.arp.arptable");
	assert_true(arp_process(packet));
	ARPEntity* entity = map_get(arp_table, (void*)(uintptr_t)sip);

	assert_memory_equal((uint8_t*)&entity->mac, comp_mac, 6);

	destroy_memory_pool(malloc_pool);
	free(malloc_pool);
	malloc_pool = NULL;
}
Ejemplo n.º 16
0
static void strchr_func() {
	char text[1024] = "`11223344567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?\0";
	int len = strlen(text);

	for(int i = 0; i < len; i++) {
		if(i < 10 && i != 0 && i % 2 == 0) {
			char* str = __strchr(text, text[i]);				
			assert_memory_equal(str, text + i - 1, strlen(text + i - 1));		
		} else {
			char* str = __strchr(text, text[i]);				
			assert_memory_equal(str, text + i, strlen(text + i));		
		}
	}
}
Ejemplo n.º 17
0
static void
test_parse_packet_tcp_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/tcp.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_IPV4_TCP );

  u_char macda[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
  u_char macsa[] = { 0x00, 0x16, 0x17, 0x00, 0x43, 0xf3 };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
  assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );

  assert_int_equal( packet_info->l2_payload_length, 82 );

  assert_int_equal( packet_info->ipv4_version, 4 );
  assert_int_equal( packet_info->ipv4_ihl, 5 );
  assert_int_equal( packet_info->ipv4_tos, 0 );
  assert_int_equal( packet_info->ipv4_tot_len, 0x01dd );
  assert_int_equal( packet_info->ipv4_id, 0x0399 );
  assert_int_equal( packet_info->ipv4_frag_off, 0x4000 );
  assert_int_equal( packet_info->ipv4_ttl, 0x40 );
  assert_int_equal( packet_info->ipv4_protocol, IPPROTO_TCP );
  assert_int_equal( packet_info->ipv4_checksum, 0xeb24 );
  assert_int_equal( packet_info->ipv4_saddr, 0xc0a864e1 );
  assert_int_equal( packet_info->ipv4_daddr, 0xc0a8642b );

  assert_int_equal( packet_info->l3_payload_length, 62 );

  assert_int_equal( packet_info->tcp_src_port, 0x0050 );
  assert_int_equal( packet_info->tcp_dst_port, 0xad49 );
  assert_int_equal( packet_info->tcp_seq_no, 0x20656b68 );
  assert_int_equal( packet_info->tcp_ack_no, 0x51de986e );
  assert_int_equal( packet_info->tcp_offset, 0x8 );
  assert_int_equal( packet_info->tcp_flags, 0x18 );
  assert_int_equal( packet_info->tcp_window, 0x2086 );
  assert_int_equal( packet_info->tcp_checksum, 0x4c2d );
  assert_int_equal( packet_info->tcp_urgent, 0 );

  uint16_t sample = ntohs( *( uint16_t * ) packet_info->l4_payload );
  assert_int_equal( sample, 0x4854 );

  assert_int_equal( packet_info->l4_payload_length, 30 );

  free_buffer( buffer );
}
Ejemplo n.º 18
0
static void
test_parse_packet_icmpv4_echo_request_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/icmp_echo_req.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_IPV4_ICMPV4 );

  u_char macda[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
  u_char macsa[] = { 0x8c, 0x89, 0xa5, 0x16, 0x22, 0x09 };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
  assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );

  assert_int_equal( packet_info->l2_payload_length, 84 );

  assert_int_equal( packet_info->ipv4_version, 4 );
  assert_int_equal( packet_info->ipv4_ihl, 5 );
  assert_int_equal( packet_info->ipv4_tos, 0 );
  assert_int_equal( packet_info->ipv4_tot_len, 0x54 );
  assert_int_equal( packet_info->ipv4_id, 0 );
  assert_int_equal( packet_info->ipv4_frag_off, 0x4000 );
  assert_int_equal( packet_info->ipv4_ttl, 0x40 );
  assert_int_equal( packet_info->ipv4_protocol, IPPROTO_ICMP );
  assert_int_equal( packet_info->ipv4_checksum, 0xf100 );
  assert_int_equal( packet_info->ipv4_saddr, 0xc0a8642c );
  assert_int_equal( packet_info->ipv4_daddr, 0xc0a8642b );

  assert_int_equal( packet_info->l3_payload_length, 64 );

  assert_int_equal( packet_info->icmpv4_type, ICMP_TYPE_ECHOREQ );
  assert_int_equal( packet_info->icmpv4_code, 0 );
  assert_int_equal( packet_info->icmpv4_id, 1076 );
  assert_int_equal( packet_info->icmpv4_seq, 1 );

  uint16_t sample = ntohs( *( uint16_t * ) packet_info->l4_payload );
  assert_int_equal( sample, 0xa0a9 );

  assert_int_equal( packet_info->l4_payload_length, 56 );

  assert_true( packet_type_icmpv4_echo_request( buffer ) );
  assert_false( packet_type_icmpv4_dst_unreach( buffer ) );
  assert_false( packet_type_icmpv4_redirect( buffer ) );
  assert_false( packet_type_icmpv4_echo_reply( buffer ) );

  free_buffer( buffer );
}
Ejemplo n.º 19
0
static void strrchr_func() {
	char text[1024] = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:1ZXCVBNM<>?\0";
	int len = strlen(text);

	for(int i = 0; i < len; i++) {
		char* str;
		if(text[i] == '1') {
			str = __strrchr(text, '1');		
			assert_memory_equal(str, "1ZXCVBNM<>?", strlen("1ZXCVBNM<>?"));
		} else {
			str = __strrchr(text, text[i]);				
			assert_memory_equal(str, text + i, strlen(text + i));		
		}
	}	
}
Ejemplo n.º 20
0
/*
 * GUC array: all non-PGC_USERSET, return the same array
 */
void
test__GUCArrayReset__all_non_userset_guc(void **state)
{
	ArrayType  *in;
	ArrayType  *out;
	Datum		d;
	List	   *guc_list;
	bool		isnull;
	int			i;
	int			elems;

	build_guc_variables();
	will_return(superuser, false);

	/* construct text array */
	elems = 3;
	guc_list = list_make3("log_error_verbosity=terse", "gp_log_format=csv", "maintenance_mode=true");
	in = create_guc_array(guc_list, elems);

	out = GUCArrayReset(in);
	assert_not_null(out);
	assert_int_equal(ARR_DIMS(out)[0], elems);

	/* check element 1 */
	i = 1;
	d = array_ref(out, 1, &i, -1, TEXT_TYPLEN, TEXT_TYPBYVAL, TEXT_TYPALIGN, &isnull);
	assert_false(isnull);
	assert_int_equal(strlen("log_error_verbosity=terse"), VARLEN(d));
	assert_memory_equal(VARDATA(d), "log_error_verbosity=terse", VARLEN(d));

	/* check element 2 */
	i = 2;
	d = array_ref(out, 1, &i, -1, TEXT_TYPLEN, TEXT_TYPBYVAL, TEXT_TYPALIGN, &isnull);
	assert_false(isnull);
	assert_int_equal(strlen("gp_log_format=csv"), VARLEN(d));
	assert_memory_equal(VARDATA(d), "gp_log_format=csv", VARLEN(d));

	/* check element 3 */
	i = 3;
	d = array_ref(out, 1, &i, -1, TEXT_TYPLEN, TEXT_TYPBYVAL, TEXT_TYPALIGN, &isnull);
	assert_false(isnull);
	assert_int_equal(strlen("maintenance_mode=true"), VARLEN(d));
	assert_memory_equal(VARDATA(d), "maintenance_mode=true", VARLEN(d));

	list_free(guc_list);
	pfree(in);
	pfree(out);
}
Ejemplo n.º 21
0
static void
test_parse_packet_tcp_syn_succeeds() {
  const char filename[] = "./unittests/lib/test_packets/tcp_syn.cap";
  buffer *buffer = store_packet_to_buffer( filename );

  assert_true( parse_packet( buffer ) );

  packet_info *packet_info = buffer->user_data;

  assert_int_equal( packet_info->format, ETH_IPV4_TCP );

  u_char macda[] = { 0x00, 0x16, 0x17, 0x00, 0x43, 0xf3 };
  u_char macsa[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
  assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
  assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
  assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );

  assert_int_equal( packet_info->l2_payload_length, 60 );

  assert_int_equal( packet_info->ipv4_version, 4 );
  assert_int_equal( packet_info->ipv4_ihl, 5 );
  assert_int_equal( packet_info->ipv4_tos, 0x10 );
  assert_int_equal( packet_info->ipv4_tot_len, 0x003c );
  assert_int_equal( packet_info->ipv4_id, 0x5551 );
  assert_int_equal( packet_info->ipv4_frag_off, 0x4000 );
  assert_int_equal( packet_info->ipv4_ttl, 0x40 );
  assert_int_equal( packet_info->ipv4_protocol, IPPROTO_TCP );
  assert_int_equal( packet_info->ipv4_checksum, 0x9afd );
  assert_int_equal( packet_info->ipv4_saddr, 0xc0a8642b );
  assert_int_equal( packet_info->ipv4_daddr, 0xc0a864e1 );

  assert_int_equal( packet_info->l3_payload_length, 40 );

  assert_int_equal( packet_info->tcp_src_port, 0xad49 );
  assert_int_equal( packet_info->tcp_dst_port, 0x0050 );
  assert_int_equal( packet_info->tcp_seq_no, 0x51de9851 );
  assert_int_equal( packet_info->tcp_ack_no, 0 );
  assert_int_equal( packet_info->tcp_offset, 0xa );
  assert_int_equal( packet_info->tcp_flags, 0x02 );
  assert_int_equal( packet_info->tcp_window, 0x16d0 );
  assert_int_equal( packet_info->tcp_checksum, 0x76bb );
  assert_int_equal( packet_info->tcp_urgent, 0 );

  assert_int_equal( packet_info->l4_payload, NULL );
  assert_int_equal( packet_info->l4_payload_length, 0 );

  free_buffer( buffer );
}
Ejemplo n.º 22
0
/**
 * A basic test showing mapping of memory, and reading/writing it
 */
static void test_basic(void **state)
{
    uc_engine *uc = *state;
    const uint64_t mem_start = 0x1000;
    const uint64_t mem_len   = 0x1000;
    const uint64_t test_addr = mem_start + 0x100;

    /* Map a region */
    uc_assert_success(uc_mem_map(uc, mem_start, mem_len, UC_PROT_NONE));

    /* Write some data to it */
    uc_assert_success(uc_mem_write(uc, test_addr, "test", 4));

    uint8_t buf[4];
    memset(buf, 0xCC, sizeof(buf));

    /* Read it back */
    uc_assert_success(uc_mem_read(uc, test_addr, buf, sizeof(buf)));

    /* And make sure it matches what we expect */
    assert_memory_equal(buf, "test", 4);

    /* Unmap the region */
    //uc_assert_success(uc_mem_unmap(uc, mem_start, mem_len));
}
Ejemplo n.º 23
0
/**
 * Move FIFO head and tail at `n`th byte by write and read action.
 * Add different length of records to FIFO till `n`th byte get
 * overwrite. remove and compare all records and check FIFO's length.
 */
void test_case_14(void **fifo)
{
	vfifo_t *e_fifo = *fifo;
	char * const str[] = {"Brazil", "Cuba", "Russia", "Iran"};
	char buf[10];
	int ret;
	int len;
	int i;

	/* Change FIFO's head and tail position to 'n'th byte */
	for (i = 0; i < 2; i++)
		vfifo_add(e_fifo, str[i], strlen(str[i]));

	for (i = 0; i < 2; i++)
		vfifo_remove(e_fifo, buf, sizeof(buf));


	/* Write and read from 'n'th byte */
	for (i = 0; i < ARRAY_LEN(str); i++) {
		vfifo_add(e_fifo, str[i], strlen(str[i]));
		ret = vfifo_len(e_fifo);
		if (i < 3)
			assert_int_equal(ret, i + 1);
		else
			assert_int_equal(ret, 3);
	}
	
	for (len = 2, i = 1; i <= 3; i++, len--) {
		ret = vfifo_remove(e_fifo, buf, strlen(str[i]));
		assert_int_equal(ret, 0);
		assert_memory_equal(str[i], buf, strlen(str[i]));
		ret = vfifo_len(e_fifo);
		assert_int_equal(ret, len);
	}
}
Ejemplo n.º 24
0
/**
 * Move FIFO head and tail at `n`th byte by write and read action.
 * Add different length of records to FIFO up to `n`th byte. remove
 * and compare all records and check FIFO's length.
 */
void test_case_13(void **fifo)
{
	vfifo_t *e_fifo = *fifo;
	char * const str[] = {"Brazil", "Cuba", "Russia", "Iran"};
	char buf[3];
	int ret;
	int i;
	int len;

	for (i = 0; i < ARRAY_LEN(str); i++) {
		vfifo_add(e_fifo, str[i], strlen(str[i]));
		ret = vfifo_len(e_fifo);
		if (i < 3)
			assert_int_equal(ret, i + 1);
		else
			assert_int_equal(ret, 3);
	}
	
	
	for (len = 2, i = 1; i <= 3; i++, len--) {
		ret = vfifo_remove(e_fifo, buf, sizeof(buf));
		assert_int_equal(ret, -2);
		assert_memory_equal(str[i], buf, sizeof(buf));
		ret = vfifo_len(e_fifo);
		assert_int_equal(ret, len);
	}
}
Ejemplo n.º 25
0
static void assert_defaults(struct dp_option *opts)
{
    char *s;
    struct dp_opt_blob b;
    int i;
    bool bo;

    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
    assert_null(s);

    s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
    assert_non_null(s);
    assert_string_equal(s, STRING_DEFAULT);

    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
    assert_null(b.data);
    assert_int_equal(b.length, 0);

    b = dp_opt_get_blob(opts, OPT_BLOB_DEFAULT);
    assert_non_null(b.data);
    assert_int_equal(b.length, strlen(BLOB_DEFAULT));
    assert_memory_equal(b.data, BLOB_DEFAULT, strlen(BLOB_DEFAULT));

    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
    assert_int_equal(i, 0);

    i = dp_opt_get_int(opts, OPT_INT_DEFAULT);
    assert_int_equal(i, INT_DEFAULT);

    bo = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
    assert_true(bo == true);

    bo = dp_opt_get_bool(opts, OPT_BOOL_FALSE);
    assert_true(bo == false);
}
Ejemplo n.º 26
0
void write_callback(void* buffer, int size, void* context) {
	char _buffer[1024];
	memset(_buffer, 0, 1024);
	sprintf(_buffer, "Write Test %d", *(int*)context);
	assert_memory_equal(_buffer, buffer, strlen(buffer));
	assert_int_equal(1024, size);	
}
Ejemplo n.º 27
0
/**
 * Make a call to Marshal_UINT16 function that should succeed. The *_setup
 * function is expected to have allocated sufficient buffer to hold a
 * uint16_t. This test just 'marshals' a known uint16_t into this data buffer
 * and then compares the value to the expected result.
 * The workings of the Marshal_UINT16 function is a bit complex, so we
 * assert the expected results as well.
 */
void
marshal_TPM2B_NAME_good (void **state)
{
    marshal_simple2b_t *data;
    /**
     * This is what the above TPM2B_NAME should look like when marshalled.
     * Interestingly enough the only thing that changes by order is the size
     * field. The 'name' field is marshalled as a byte buffer so endianness
     * doesn't change.
     */
    data = (marshal_simple2b_t*)*state;
    uint8_t *nextData = data->buffer;

    Marshal_Simple_TPM2B (data->buffer,
                          data->buffer_size,
                          &nextData,
                          (TPM2B*)&name2b,
                          &data->rc);
    /**
     * uint16_t that was marshalled into the data buffer should be equal to
     * the expected value (data converted from host byte order to network
     * byte order).
     */
    assert_memory_equal (data->buffer, name2b_marshalled, marshalled_size);
    /**
     * The Marshal_* functions advance the 'nextData' parameter by the size of
     * the marshalled data.
     */
    assert_int_equal (data->buffer, nextData - marshalled_size);
    /* Finally the return code should indicate success. */
    assert_int_equal (data->rc, TSS2_RC_SUCCESS);
}
Ejemplo n.º 28
0
void
unmarshal_TPM2B_NAME_good (void **state)
{
    /**
     * rc must be initialized to success or the unmarshal function will do
     * nothing.
     */
    TSS2_RC rc = TSS2_RC_SUCCESS;
    TPM2B_NAME name2b_unmarshal = { .b = {0}, };
    /**
     * Unmarshal_Simple_TPM2B compares the size field in the destination
     * structure to the size indicated by the marshalled data in the buffer
     * that's being unmarshalled. So we must set the size to the maximum size
     * possible.
     */
    name2b_unmarshal.t.size = sizeof (name2b_unmarshal.t.name);

    uint8_t *nextData = name2b_marshalled;

    Unmarshal_Simple_TPM2B (name2b_marshalled,
                            marshalled_size,
                            &nextData,
                            &name2b_unmarshal.b,
                            &rc);
    /* The return code should indicate success */
    assert_int_equal (rc, TSS2_RC_SUCCESS);
    /* The size of the unmarshalled structure should match the reference */
    assert_int_equal (name2b_unmarshal.t.size, name2b.t.size);
    /* the contents of the name buffer should match the reference */
    assert_memory_equal (name2b_unmarshal.t.name, name2b.t.name, name2b.t.size);
}
Ejemplo n.º 29
0
static void discovery_with_services_succeeds()
{
    struct await_discovery_args args;
    struct cpn_discovery_results results;
    struct cpn_thread t;

    args.channel = &remote;
    args.identity = &remote_keys.pk;
    args.name = "test";
    args.nservices = 1;
    args.services = &service;

    assert_success(cpn_spawn(&t, await_discovery, &args));
    assert_success(cpn_client_discovery_probe(&local, NULL));
    assert_success(cpn_client_discovery_handle_announce(&results, &local));
    assert_success(cpn_join(&t, NULL));

    assert_success(args.result);
    assert_string_equal(results.name, args.name);
    assert_int_equal(results.version, CPN_PROTOCOL_VERSION);
    assert_memory_equal(&results.identity, &remote_keys.pk, sizeof(struct cpn_sign_pk));
    assert_int_equal(results.nservices, args.nservices);
    assert_string_equal(results.services[0].category, service.plugin->category);
    assert_string_equal(results.services[0].name, service.name);
    assert_int_equal(results.services[0].port, service.port);

    cpn_discovery_results_clear(&results);
}
Ejemplo n.º 30
0
static void
test_request_credentials_returns_raw_bytes(void **state)
{
	daos_iov_t	creds;
	size_t		expected_len;
	uint8_t		*expected_data;

	memset(&creds, 0, sizeof(daos_iov_t));

	/*
	 * Credential bytes == raw bytes of the packed SecurityCredential
	 */
	expected_len = security_credential__get_packed_size(
			drpc_call_resp_return_security_credential);
	D_ALLOC(expected_data, expected_len);
	security_credential__pack(drpc_call_resp_return_security_credential,
			expected_data);

	assert_int_equal(dc_sec_request_creds(&creds), DER_SUCCESS);

	assert_int_equal(creds.iov_buf_len, expected_len);
	assert_int_equal(creds.iov_len, expected_len);
	assert_memory_equal(creds.iov_buf, expected_data, expected_len);

	D_FREE(expected_data);
	daos_iov_free(&creds);
}