blob: d078a5962230eecc5de59be236eaec0dea3a4866 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
Rich Laned983aa52013-06-13 11:48:37 -070028:: include('_copyright.c')
Rich Lanea06d0c32013-03-25 08:52:03 -070029
30/**
31 *
32 * Test utility functions
33 *
34 */
35
36#include <locitest/test_common.h>
37#include <loci/of_utils.h>
38
39/**
40 * Test has output port utility function
41 */
42int
43test_has_outport(void)
44{
45 of_list_action_t *list;
Rich Lanee499bd12014-10-28 15:25:09 -070046 of_object_t set_dl_src;
47 of_object_t output;
Rich Lanea06d0c32013-03-25 08:52:03 -070048
49 list = of_list_action_new(OF_VERSION_1_0);
50 TEST_ASSERT(list != NULL);
51
52 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
53 TEST_ASSERT(!of_action_list_has_out_port(list, 1));
54
55 /* Add some other action */
Rich Lanee499bd12014-10-28 15:25:09 -070056 of_action_set_dl_src_init(&set_dl_src, OF_VERSION_1_0, -1, 1);
57 TEST_OK(of_list_action_append_bind(list, &set_dl_src));
Rich Lanea06d0c32013-03-25 08:52:03 -070058
59 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
60 TEST_ASSERT(!of_action_list_has_out_port(list, 1));
61
62 /* Add port 2 */
Rich Lanee499bd12014-10-28 15:25:09 -070063 of_action_output_init(&output, OF_VERSION_1_0, -1, 1);
64 TEST_OK(of_list_action_append_bind(list, &output));
65 of_action_output_port_set(&output, 2);
Rich Lanea06d0c32013-03-25 08:52:03 -070066
67 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
68 TEST_ASSERT(!of_action_list_has_out_port(list, 1));
69 TEST_ASSERT(of_action_list_has_out_port(list, 2));
70
71 /* Add port 1 */
Rich Lanee499bd12014-10-28 15:25:09 -070072 of_action_output_init(&output, OF_VERSION_1_0, -1, 1);
73 TEST_OK(of_list_action_append_bind(list, &output));
74 of_action_output_port_set(&output, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -070075
76 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
77 TEST_ASSERT(of_action_list_has_out_port(list, 1));
78 TEST_ASSERT(of_action_list_has_out_port(list, 2));
79
80 /* Start over and put action at front of list */
81 of_list_action_delete(list);
82
83 list = of_list_action_new(OF_VERSION_1_0);
84 TEST_ASSERT(list != NULL);
85
86 /* Add port 2 */
Rich Lanee499bd12014-10-28 15:25:09 -070087 of_action_output_init(&output, OF_VERSION_1_0, -1, 1);
88 TEST_OK(of_list_action_append_bind(list, &output));
89 of_action_output_port_set(&output, 2);
Rich Lanea06d0c32013-03-25 08:52:03 -070090
91 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
92 TEST_ASSERT(!of_action_list_has_out_port(list, 1));
93 TEST_ASSERT(of_action_list_has_out_port(list, 2));
94
95 /* Add some other action */
Rich Lanee499bd12014-10-28 15:25:09 -070096 of_action_set_dl_src_init(&set_dl_src, OF_VERSION_1_0, -1, 1);
97 TEST_OK(of_list_action_append_bind(list, &set_dl_src));
Rich Lanea06d0c32013-03-25 08:52:03 -070098
99 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
100 TEST_ASSERT(!of_action_list_has_out_port(list, 1));
101 TEST_ASSERT(of_action_list_has_out_port(list, 2));
102
103 /* Add port 1 */
Rich Lanee499bd12014-10-28 15:25:09 -0700104 of_action_output_init(&output, OF_VERSION_1_0, -1, 1);
105 TEST_OK(of_list_action_append_bind(list, &output));
106 of_action_output_port_set(&output, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700107
108 TEST_ASSERT(of_action_list_has_out_port(list, OF_PORT_DEST_WILDCARD));
109 TEST_ASSERT(of_action_list_has_out_port(list, 1));
110 TEST_ASSERT(of_action_list_has_out_port(list, 2));
111
112 of_list_action_delete(list);
113
114 return TEST_PASS;
115}
116
Rich Lanec73680c2014-02-22 10:44:28 -0800117static int
Rich Lane85aaa652014-03-05 10:12:55 -0800118test_of_object_new_from_message(void)
119{
120 /* v1 OFPT_HELLO, xid=0x12345678 */
121 uint8_t data[] = { 0x01, 0x00, 0x00, 0x08, 0x12, 0x34, 0x56, 0x78 };
122 uint8_t *buf = malloc(sizeof(data));
123 memcpy(buf, data, sizeof(data));
124
125 of_object_t *obj = of_object_new_from_message(buf, sizeof(data));
126
127 TEST_ASSERT(obj != NULL);
128 TEST_ASSERT(obj->version == OF_VERSION_1_0);
129 TEST_ASSERT(obj->object_id == OF_HELLO);
130
131 uint32_t xid;
132 of_hello_xid_get(obj, &xid);
133 TEST_ASSERT(xid == 0x12345678);
134
135 of_object_delete(obj);
136
137 return TEST_PASS;
138}
139
140static int
Rich Lanec73680c2014-02-22 10:44:28 -0800141test_of_object_new_from_message_preallocated(void)
142{
143 /* v1 OFPT_HELLO, xid=0x12345678 */
144 uint8_t buf[] = { 0x01, 0x00, 0x00, 0x08, 0x12, 0x34, 0x56, 0x78 };
145
146 of_object_storage_t storage;
147 of_object_t *obj = of_object_new_from_message_preallocated(
148 &storage, buf, sizeof(buf));
149
150 TEST_ASSERT(obj != NULL);
Rich Lane85aaa652014-03-05 10:12:55 -0800151 TEST_ASSERT(obj->version == OF_VERSION_1_0);
152 TEST_ASSERT(obj->object_id == OF_HELLO);
Rich Lanec73680c2014-02-22 10:44:28 -0800153
154 uint32_t xid;
155 of_hello_xid_get(obj, &xid);
156 TEST_ASSERT(xid == 0x12345678);
157
158 return TEST_PASS;
159}
160
Rich Lanea06d0c32013-03-25 08:52:03 -0700161int
162run_utility_tests(void)
163{
164 RUN_TEST(has_outport);
Rich Lane85aaa652014-03-05 10:12:55 -0800165 RUN_TEST(of_object_new_from_message);
Rich Lanec73680c2014-02-22 10:44:28 -0800166 RUN_TEST(of_object_new_from_message_preallocated);
Rich Lanea06d0c32013-03-25 08:52:03 -0700167 RUN_TEST(dump_objs);
168
169 return TEST_PASS;
170}