Skip to content

Commit

Permalink
locitest: generate tests from the test_data files
Browse files Browse the repository at this point in the history
The "c" section of the test data file should allocate and populate a LOCI
object. The resulting wire buffer is compared to the "binary" section.

This only tests serialization, but given that we have autogenerated tests that
round-trip each object I think this is sufficient.

I added C sections to the OF 1.0 test data files. OF 1.3 tests will come in a
future pull request.
  • Loading branch information
rlane committed Jul 22, 2013
1 parent 9ff6d16 commit ccae031
Show file tree
Hide file tree
Showing 19 changed files with 509 additions and 3 deletions.
16 changes: 16 additions & 0 deletions c_gen/c_test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import loxi_front_end.type_maps as type_maps
import loxi_utils.loxi_utils as loxi_utils
import loxi_front_end.identifiers as identifiers
import util
import test_data

def var_name_map(m_type):
"""
Expand Down Expand Up @@ -340,6 +342,7 @@ def gen_common_test_header(out, name):
extern int run_list_limits_tests(void);
extern int test_ext_objs(void);
extern int test_datafiles(void);
""")

Expand Down Expand Up @@ -1966,3 +1969,16 @@ def gen_ident_tests(out):
}
""")

def gen_datafiles_tests(out, name):
tests = []
for filename in test_data.list_files():
data = test_data.read(filename)
if not 'c' in data:
continue
name = filename[:-5].replace("/", "_")
tests.append(dict(name=name,
filename=filename,
c=data['c'],
binary=data['binary']))

util.render_template(out, "test_data.c", tests=tests)
2 changes: 2 additions & 0 deletions c_gen/templates/locitest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ main(int argc, char *argv[])

RUN_TEST(ext_objs);

TEST_ASSERT(test_datafiles() == TEST_PASS);

return global_error;
}
88 changes: 88 additions & 0 deletions c_gen/templates/locitest/test_data.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University */
/* Copyright (c) 2011, 2012 Open Networking Foundation */
/* Copyright (c) 2012, 2013 Big Switch Networks, Inc. */
/* See the file LICENSE.loci which should have been included in the source distribution */

/**
*
* AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
*
* Data file tests for all versions.
*/

#include <locitest/test_common.h>

<?py
def hexarray(data, indent):
i = 0
text = []
text.append(" " * indent)
for byte in data:
text.append("0x%02x, " % ord(byte))
i += 1
if i == 8:
text.append("\n" + " " * indent)
i = 0
#endif
#endfor
return "".join(text)
#end
?>

static void
hexdump(const uint8_t *data, int len)
{
int i = 0, j;
while (i < len) {
printf("%02x: ", i);
for (j = 0; j < 8 && i < len; j++, i++) {
printf("%02x ", data[i]);
}
printf("\n");
}
}

static void
show_failure(const uint8_t *a, int a_len, const uint8_t *b, int b_len)
{
printf("\n--- Expected: (len=%d)\n", a_len);
hexdump(a, a_len);
printf("\n--- Actual: (len=%d)\n", b_len);
hexdump(b, b_len);
}

:: for test in tests:
/* Generated from ${test['filename']} */
static int
test_${test['name']}(void) {
uint8_t binary[] = {
${hexarray(test['binary'], indent=8)}
};

of_object_t *obj;

${'\n'.join([' ' * 4 + x for x in test['c'].split("\n")])}

if (sizeof(binary) != WBUF_CURRENT_BYTES(OF_OBJECT_TO_WBUF(obj))
|| memcmp(binary, WBUF_BUF(OF_OBJECT_TO_WBUF(obj)), sizeof(binary))) {
show_failure(binary, sizeof(binary),
WBUF_BUF(OF_OBJECT_TO_WBUF(obj)),
WBUF_CURRENT_BYTES(OF_OBJECT_TO_WBUF(obj)));
of_object_delete(obj);
return TEST_FAIL;
}

of_object_delete(obj);
return TEST_PASS;
}

:: #endfor

int
test_datafiles(void)
{
:: for test in tests:
RUN_TEST(${test['name']});
:: #endfor
return TEST_PASS;
}
1 change: 1 addition & 0 deletions lang_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def static(out, name):
'locitest/src/test_msg.c': c_test_gen.gen_msg_test,
'locitest/src/test_scalar_acc.c': c_test_gen.gen_message_scalar_test,
'locitest/src/test_uni_acc.c': c_test_gen.gen_unified_accessor_tests,
'locitest/src/test_data.c': c_test_gen.gen_datafiles_tests,

# Static locitest code
'locitest/inc/locitest/unittest.h': static,
Expand Down
3 changes: 3 additions & 0 deletions test_data/of10/action_bsn_set_tunnel_dst.data
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ ff ff # type
ofp.action.bsn_set_tunnel_dst(dst=0x12345678)
-- python pretty-printer
bsn_set_tunnel_dst { dst = 0x12345678 }
-- c
obj = of_action_bsn_set_tunnel_dst_new(OF_VERSION_1_0);
of_action_bsn_set_tunnel_dst_dst_set(obj, 0x12345678);
24 changes: 24 additions & 0 deletions test_data/of10/desc_stats_reply.data
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,27 @@ ofp.message.desc_stats_reply(
sw_desc="Indigo-2 LRI pre-release",
serial_num="11235813213455",
dp_desc="Indigo-2 LRI forwarding module")
-- c
obj = of_desc_stats_reply_new(OF_VERSION_1_0);
of_desc_stats_reply_xid_set(obj, 3);
of_desc_stats_reply_flags_set(obj, OF_STATS_REPLY_FLAG_REPLY_MORE);
{
of_desc_str_t mfr_desc = "The Indigo-2 Community";
of_desc_stats_reply_mfr_desc_set(obj, mfr_desc);
}
{
of_desc_str_t hw_desc = "Unknown server";
of_desc_stats_reply_hw_desc_set(obj, hw_desc);
}
{
of_desc_str_t sw_desc = "Indigo-2 LRI pre-release";
of_desc_stats_reply_sw_desc_set(obj, sw_desc);
}
{
of_desc_str_t dp_desc = "Indigo-2 LRI forwarding module";
of_desc_stats_reply_dp_desc_set(obj, dp_desc);
}
{
of_serial_num_t serial_num = "11235813213455";
of_desc_stats_reply_serial_num_set(obj, serial_num);
}
7 changes: 7 additions & 0 deletions test_data/of10/echo_request.data
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@
ofp.message.echo_request(xid=0x12345678, data="ab\x01")
-- python pretty-printer
echo_request { xid = 0x12345678, data = 'ab\x01' }
-- c
obj = of_echo_request_new(OF_VERSION_1_0);
of_echo_request_xid_set(obj, 0x12345678);
{
of_octets_t data = { .data=(uint8_t *)"ab\x01", .bytes=3 };
of_echo_request_data_set(obj, &data);
}
52 changes: 49 additions & 3 deletions test_data/of10/flow_add.data
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- binary
01 0e 00 70 12 34 56 78
00 00 00 0c 00 03 01 23
00 10 00 02 00 03 01 23
45 67 89 ab cd ef 01 23
45 67 00 00 00 00 00 00
00 00 00 00 c0 a8 03 7f
Expand All @@ -17,7 +17,7 @@ ff ff 00 10 00 5c 16 c7
ofp.message.flow_add(
xid=0x12345678,
match=ofp.match(
wildcards=ofp.OFPFW_DL_SRC|ofp.OFPFW_DL_DST,
wildcards=ofp.OFPFW_DL_VLAN|ofp.OFPFW_DL_VLAN_PCP,
in_port=3,
ipv4_src=0xc0a8037f,
ipv4_dst=0xffffffff,
Expand All @@ -33,7 +33,7 @@ ofp.message.flow_add(
flow_add {
xid = 0x12345678,
match = match_v1 {
wildcards = OFPFW_DL_SRC|OFPFW_DL_DST,
wildcards = OFPFW_DL_VLAN|OFPFW_DL_VLAN_PCP,
in_port = 3,
eth_src = 01:23:45:67:89:ab,
eth_dst = cd:ef:01:23:45:67,
Expand All @@ -60,3 +60,49 @@ flow_add {
bsn_set_tunnel_dst { dst = 0x0 }
]
}
-- c
obj = of_flow_add_new(OF_VERSION_1_0);
of_flow_add_xid_set(obj, 0x12345678);
of_flow_add_idle_timeout_set(obj, 5);
of_flow_add_flags_set(obj, 2);
{
of_match_t match = { OF_VERSION_1_0 };
match.fields.in_port = 3;
match.fields.eth_src = (of_mac_addr_t) { { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } };
match.fields.eth_dst = (of_mac_addr_t) { { 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 } };
match.fields.ipv4_src = 0xc0a8037f;
match.fields.ipv4_dst = 0xffffffff;
OF_MATCH_MASK_IN_PORT_EXACT_SET(&match);
OF_MATCH_MASK_ETH_SRC_EXACT_SET(&match);
OF_MATCH_MASK_ETH_DST_EXACT_SET(&match);
//OF_MATCH_MASK_VLAN_VID_EXACT_SET(&match);
//OF_MATCH_MASK_VLAN_PCP_EXACT_SET(&match);
OF_MATCH_MASK_ETH_TYPE_EXACT_SET(&match);
OF_MATCH_MASK_IP_DSCP_EXACT_SET(&match);
OF_MATCH_MASK_IP_PROTO_EXACT_SET(&match);
OF_MATCH_MASK_IPV4_SRC_EXACT_SET(&match);
OF_MATCH_MASK_IPV4_DST_EXACT_SET(&match);
OF_MATCH_MASK_TCP_SRC_EXACT_SET(&match);
OF_MATCH_MASK_TCP_DST_EXACT_SET(&match);
of_flow_add_match_set(obj, &match);
}
{
of_list_action_t actions;
of_flow_add_actions_bind(obj, &actions);
{
of_action_t action;
of_action_output_init(&action.output, OF_VERSION_1_0, -1, 1);
of_list_action_append_bind(&actions, &action);
of_action_output_port_set(&action.output, OF_PORT_DEST_FLOOD);
}
{
of_action_t action;
of_action_nicira_dec_ttl_init(&action.nicira_dec_ttl, OF_VERSION_1_0, -1, 1);
of_list_action_append_bind(&actions, &action);
}
{
of_action_t action;
of_action_bsn_set_tunnel_dst_init(&action.bsn_set_tunnel_dst, OF_VERSION_1_0, -1, 1);
of_list_action_append_bind(&actions, &action);
}
}
33 changes: 33 additions & 0 deletions test_data/of10/flow_stats_entry.data
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,36 @@ ofp.flow_stats_entry(
actions=[
ofp.action.output(port=1),
ofp.action.output(port=2)])
-- c
obj = of_flow_stats_entry_new(OF_VERSION_1_0);
{
of_object_t list;
of_flow_stats_entry_actions_bind(obj, &list);
{
of_object_t *obj = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(obj, 0);
of_action_output_port_set(obj, 1);
of_list_append(&list, obj);
of_object_delete(obj);
}
{
of_object_t *obj = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(obj, 0);
of_action_output_port_set(obj, 2);
of_list_append(&list, obj);
of_object_delete(obj);
}
}
of_flow_stats_entry_byte_count_set(obj, 1000);
of_flow_stats_entry_cookie_set(obj, 81985529216486895);
of_flow_stats_entry_duration_nsec_set(obj, 2);
of_flow_stats_entry_duration_sec_set(obj, 1);
of_flow_stats_entry_hard_timeout_set(obj, 10);
of_flow_stats_entry_idle_timeout_set(obj, 5);
{
of_match_t match = { OF_VERSION_1_0 };
of_flow_stats_entry_match_set(obj, &match);
}
of_flow_stats_entry_packet_count_set(obj, 10);
of_flow_stats_entry_priority_set(obj, 100);
of_flow_stats_entry_table_id_set(obj, 3);
90 changes: 90 additions & 0 deletions test_data/of10/flow_stats_reply.data
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,93 @@ ofp.message.flow_stats_reply(
actions=[ofp.action.output(port=1),
ofp.action.output(port=2),
ofp.action.output(port=3)])])
-- c
obj = of_flow_stats_reply_new(OF_VERSION_1_0);
of_flow_stats_reply_flags_set(obj, 0);
of_flow_stats_reply_xid_set(obj, 6);
{
of_object_t *entries = of_list_flow_stats_entry_new(OF_VERSION_1_0);
{
of_object_t *elem = of_flow_stats_entry_new(OF_VERSION_1_0);
of_flow_stats_entry_byte_count_set(elem, 1000);
of_flow_stats_entry_cookie_set(elem, 81985529216486895);
of_flow_stats_entry_duration_nsec_set(elem, 2);
of_flow_stats_entry_duration_sec_set(elem, 1);
of_flow_stats_entry_hard_timeout_set(elem, 10);
of_flow_stats_entry_idle_timeout_set(elem, 5);
of_flow_stats_entry_packet_count_set(elem, 10);
of_flow_stats_entry_priority_set(elem, 100);
of_flow_stats_entry_table_id_set(elem, 3);
{
of_match_t match = { OF_VERSION_1_0 };
of_flow_stats_entry_match_set(elem, &match);
}
{
of_object_t *actions = of_list_action_new(OF_VERSION_1_0);
{
of_object_t *elem = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(elem, 0);
of_action_output_port_set(elem, 1);
of_list_append(actions, elem);
of_object_delete(elem);
}
{
of_object_t *elem = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(elem, 0);
of_action_output_port_set(elem, 2);
of_list_append(actions, elem);
of_object_delete(elem);
}
of_flow_stats_entry_actions_set(elem, actions);
of_object_delete(actions);
}
of_list_append(entries, elem);
of_object_delete(elem);
}
{
of_object_t *elem = of_flow_stats_entry_new(OF_VERSION_1_0);
of_flow_stats_entry_byte_count_set(elem, 1000);
of_flow_stats_entry_cookie_set(elem, 81985529216486895);
of_flow_stats_entry_duration_nsec_set(elem, 2);
of_flow_stats_entry_duration_sec_set(elem, 1);
of_flow_stats_entry_hard_timeout_set(elem, 10);
of_flow_stats_entry_idle_timeout_set(elem, 5);
of_flow_stats_entry_packet_count_set(elem, 10);
of_flow_stats_entry_priority_set(elem, 100);
of_flow_stats_entry_table_id_set(elem, 4);
{
of_match_t match = { OF_VERSION_1_0 };
of_flow_stats_entry_match_set(elem, &match);
}
{
of_object_t *actions = of_list_action_new(OF_VERSION_1_0);
{
of_object_t *elem = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(elem, 0);
of_action_output_port_set(elem, 1);
of_list_append(actions, elem);
of_object_delete(elem);
}
{
of_object_t *elem = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(elem, 0);
of_action_output_port_set(elem, 2);
of_list_append(actions, elem);
of_object_delete(elem);
}
{
of_object_t *elem = of_action_output_new(OF_VERSION_1_0);
of_action_output_max_len_set(elem, 0);
of_action_output_port_set(elem, 3);
of_list_append(actions, elem);
of_object_delete(elem);
}
of_flow_stats_entry_actions_set(elem, actions);
of_object_delete(actions);
}
of_list_append(entries, elem);
of_object_delete(elem);
}
of_flow_stats_reply_entries_set(obj, entries);
of_object_delete(entries);
}
3 changes: 3 additions & 0 deletions test_data/of10/hello.data
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
12 34 56 78 # xid
-- python
ofp.message.hello(xid=0x12345678)
-- c
obj = of_hello_new(OF_VERSION_1_0);
of_hello_xid_set(obj, 305419896);
Loading

0 comments on commit ccae031

Please sign in to comment.