aboutsummaryrefslogtreecommitdiffstats
path: root/src/scvpp/tests/scvpp_ip_test.c
blob: 5e281231aa1dbbb01123b2bc2cd0d6ef4e921378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2019 Cisco
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <setjmp.h>
#include <stdarg.h>
#include <cmocka.h>

#include "scvpp_test.h"

#include <scvpp/ip.h>
#include <scvpp/interface.h>
#include <scvpp/comm.h>

static void test_add_remove_ipv4(void **state)
{
    UNUSED(state);
    const char ip_q[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.144";
    char ip_r[VPP_IP4_ADDRESS_STRING_LEN];
    u8 prefix_q = 24;
    u8 prefix_r;
    int rc;

    //add ipv4 on tap0
    rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, true);
    assert_int_equal(rc, SCVPP_OK);

    //dump ipv4 on tap0 and check if it mach
    rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false);
    assert_int_equal(rc, SCVPP_OK);
    assert_string_equal(ip_q, ip_r);
    assert_int_equal(prefix_q, prefix_r);

    //remove ipv4 on tap0
    rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, false);
    assert_int_equal(rc, SCVPP_OK);

    //dump ipv4 after removal and check if equals 0.0.0.0
    rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false);
    assert_int_equal(rc, SCVPP_OK);
    assert_string_equal(ip_r, "0.0.0.0");
}

static void test_ipv4_add_del_route_no_iface(void **state)
{
    UNUSED(state);
    int rc;
    char dst_address[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.0";
    uint8_t prefix_len = 24;
    char next_hop[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.100";
    uint32_t table_id = 0;
    fib_dump_t *dump;
    struct elt *stack;
    bool route_found = false;

    /* Must fail, can not have both interface and next hop IP null */
    rc = ipv46_config_add_del_route(dst_address, prefix_len, NULL, true,
                                    table_id, NULL);
    assert_int_equal(rc, -SCVPP_EINVAL);

    rc = ipv46_config_add_del_route(dst_address, prefix_len, next_hop, true,
                                    table_id, NULL);
    assert_int_equal(rc, SCVPP_OK);

    /* Dump all FIB routes and check if we find ours */
    stack = ipv4_fib_dump_all();
    assert_non_null(stack);
    foreach_stack_elt(stack) {
        dump = (fib_dump_t *) data;

        if (strncmp(sc_ntoa(dump->address), dst_address,
                    VPP_IP4_ADDRESS_STRING_LEN) == 0 &&
            dump->address_length == prefix_len) {
            route_found = true;
            assert_int_equal(dump->table_id, table_id);
            assert_int_equal(dump->count, 1);
            assert_string_equal(sc_ntoa(dump->path[0].next_hop), next_hop);
        }
        free(dump);
    }
    assert_true(route_found);

    /* Delete previously set route */
    rc = ipv46_config_add_del_route(dst_address, prefix_len, next_hop, false,
                                    table_id, NULL);
    assert_int_equal(rc, SCVPP_OK);

    /* Check our route has been deleted */
    route_found = false;
    stack = ipv4_fib_dump_all();
    assert_non_null(stack);
    foreach_stack_elt(stack) {
        dump = (fib_dump_t *) data;
        if (strncmp(sc_ntoa(dump->address), dst_address,
                    VPP_IP4_ADDRESS_STRING_LEN) == 0 &&
            dump->address_length == prefix_len) {
            route_found = true;
        }
        free(dump);
    }
    assert_false(route_found);
}

static void test_ipv4_add_del_route_no_next_hop_ip(void **state)
{
    UNUSED(state);
    int rc;
    char dst_address[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.0";
    uint8_t prefix_len = 24;
    char interface[VPP_IP4_ADDRESS_STRING_LEN] = "tap0";
    uint32_t table_id = 0;
    uint32_t sw_if_index;
    fib_dump_t *dump;
    struct elt *stack;
    bool route_found = false;

    //Add a new route
    rc = ipv46_config_add_del_route(dst_address, prefix_len, NULL, true,
                                    table_id, interface);
    assert_int_equal(rc, SCVPP_OK);

    //Dump all FIB routes and check we find ours
    stack = ipv4_fib_dump_all();
    assert_non_null(stack);

    rc = get_interface_id(interface, &sw_if_index);
    assert_int_equal(rc, SCVPP_OK);

    foreach_stack_elt(stack) {
        dump = (fib_dump_t *) data;

        if (strncmp(sc_ntoa(dump->address), dst_address,
                    VPP_IP4_ADDRESS_STRING_LEN) == 0 &&
            dump->address_length == prefix_len) {
            route_found = true;
            assert_int_equal(dump->table_id, table_id);
            assert_int_equal(dump->count, 1);
            assert_int_equal(dump->path[0].sw_if_index, sw_if_index);
        }
        free(dump);
    }
    assert_true(route_found);

    //Delete route
    rc = ipv46_config_add_del_route(dst_address, prefix_len, NULL, false,
                                    table_id, interface);
    assert_int_equal(rc, SCVPP_OK);

    //Check our route has been deleted
    route_found = false;
    stack = ipv4_fib_dump_all();
    assert_non_null(stack);

    foreach_stack_elt(stack) {
        dump = (fib_dump_t *) data;
        if (strncmp(sc_ntoa(dump->address), dst_address,
                    VPP_IP4_ADDRESS_STRING_LEN) == 0 &&
            dump->address_length == prefix_len) {
            route_found = true;
        }
        free(dump);
    }
    assert_false(route_found);
}


const struct CMUnitTest ip_tests[IP_TEST_SIZE] = {
    cmocka_unit_test(test_add_remove_ipv4),
    cmocka_unit_test(test_ipv4_add_del_route_no_next_hop_ip),
    cmocka_unit_test(test_ipv4_add_del_route_no_iface),
};