Add unit test infrastructure for LISP protocol
[vpp.git] / vnet / test / lisp-cp / test_lisp_types.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
18 #include <vnet/lisp-cp/lisp_types.h>
19 #include <vnet/lisp-cp/lisp_cp_messages.h>
20
21 #define _assert(e)                    \
22   error = CLIB_ERROR_ASSERT (e);      \
23   if (error)                          \
24     goto done;
25
26 static clib_error_t * test_locator_type (void)
27 {
28   clib_error_t * error = 0;
29   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
30   gid_address_type(gid_addr) = IP_PREFIX;
31   gid_address_ippref_len(gid_addr) = 24;
32   ip_prefix_version(&gid_addr->ippref) = IP4;
33   gid_addr->ippref.addr.ip.v4.as_u32 = 0x20304050;
34
35   /* local locator */
36   locator_t loc1, loc2 = {
37     .local = 1,
38     .state = 2,
39     .sw_if_index = 8,
40     .priority = 3,
41     .weight = 100,
42     .mpriority = 4,
43     .mweight = 101
44   };
45   locator_copy (&loc1, &loc2);
46   _assert (0 == locator_cmp (&loc1, &loc2));
47
48   /* remote locator */
49   loc2.local = 0;
50   loc2.address = gid_addr[0];
51   locator_copy(&loc1, &loc2);
52
53   _assert (0 == locator_cmp (&loc1, &loc2));
54
55 done:
56   return error;
57 }
58
59 static clib_error_t * test_gid_parse ()
60 {
61   clib_error_t * error = 0;
62   return error;
63 }
64
65 static clib_error_t * test_format_unformat_gid_address (void)
66 {
67   u8 * s = 0;
68   clib_error_t * error = 0;
69   unformat_input_t _input;
70   unformat_input_t * input = &_input;
71   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
72   gid_address_t unformated_gid;
73
74   /* format/unformat IPv4 global ID address */
75   gid_address_type(gid_addr) = IP_PREFIX;
76   gid_address_ippref_len(gid_addr) = 24;
77   ip_prefix_version(&gid_addr->ippref) = IP4;
78   gid_addr->ippref.addr.ip.v4.as_u32 = 0x20304050;
79
80   s = format(0, "%U", format_gid_address, gid_addr);
81   vec_add1(s, 0);
82   unformat_init_string(input, (char *)s, vec_len(s));
83
84   _assert (unformat(input, "%U",
85         unformat_gid_address, &unformated_gid));
86   _assert (0 == gid_address_cmp (&unformated_gid, gid_addr));
87
88   unformat_free(input);
89   vec_free(s);
90   s = 0;
91
92   /* format/unformat IPv6 global ID address */
93   gid_address_type(gid_addr) = IP_PREFIX;
94   gid_address_ippref_len(gid_addr) = 64;
95   ip_prefix_version(&gid_addr->ippref) = IP6;
96   u8 ipv6[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
97   memcpy(gid_addr->ippref.addr.ip.v6.as_u8, ipv6, sizeof(ipv6));
98
99   s = format(0, "%U", format_gid_address, gid_addr);
100   vec_add1(s, 0);
101   unformat_init_string(input, (char *)s, vec_len(s));
102
103   _assert (unformat (input, "%U", unformat_gid_address,
104         &unformated_gid));
105   _assert (0 == gid_address_cmp(&unformated_gid, gid_addr));
106
107   /* test address copy */
108   gid_address_t gid_addr_copy;
109   gid_address_copy(&gid_addr_copy, gid_addr);
110   _assert (0 == gid_address_cmp (&gid_addr_copy, gid_addr));
111
112 done:
113   unformat_free(input);
114   vec_free(s);
115   return error;
116 }
117
118 #define foreach_test_case                 \
119   _(format_unformat_gid_address)          \
120   _(locator_type)                         \
121   _(gid_parse)
122
123 int run_tests (void)
124 {
125   clib_error_t * error;
126
127 #define _(_test_name)                   \
128   error = test_ ## _test_name ();       \
129   if (error)                            \
130     {                                   \
131       clib_error_report (error);        \
132       return 0;                         \
133     }
134
135   foreach_test_case
136 #undef _
137
138   return 0;
139 }
140
141 int main()
142 {
143   return run_tests ();
144 }
145