aboutsummaryrefslogtreecommitdiffstats
path: root/src/scvpp/tests/scvpp_nat_test.c
blob: 93541e95b579adf069065305cff4bcf4d032164a (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
/*
 * Copyright (c) 2019 PANTHEON.tech.
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <setjmp.h>
#include <cmocka.h>

#include "scvpp_test.h"

#include <scvpp/comm.h>
#include <scvpp/nat.h>


void test_nat44_static_mapping(void **state)
{
    UNUSED(state);
    nat44_add_del_static_mapping_t map = {0};
    nat44_static_mapping_details_t dump = {0};
    u8 empty_ip[4] = {0};
    int rc;

    /*Configure the static mapping
      Alternative to this CLI command:
      nat44 add static mapping local 172.168.0.1 external 172.168.8.5
     */
    sc_aton("172.168.0.1", map.local_ip_address,
            sizeof(map.local_ip_address));
    sc_aton("172.168.8.5", map.external_ip_address,
            sizeof(map.external_ip_address));
    map.external_sw_if_index = ~0;
    map.is_add = 1;

    rc = nat44_add_del_static_mapping(&map);
    assert_int_equal(rc, SCVPP_OK);

    rc = nat44_static_mapping_dump(&dump);
    assert_int_equal(rc, SCVPP_OK);

    assert_memory_equal(dump.local_ip_address, map.local_ip_address,
                        sizeof(dump.local_ip_address));

    assert_memory_equal(dump.external_ip_address, map.external_ip_address,
                        sizeof(dump.external_ip_address));

    /* Remove previous config*/
    map.is_add = 0;

    rc = nat44_add_del_static_mapping(&map);
    assert_int_equal(rc, SCVPP_OK);

    memset(&dump, 0, sizeof(dump));

    rc = nat44_static_mapping_dump(&dump);
    assert_int_equal(rc, SCVPP_OK);

    assert_memory_equal(dump.local_ip_address, empty_ip,
                        sizeof(dump.local_ip_address));

    assert_memory_equal(dump.external_ip_address, empty_ip,
                        sizeof(dump.external_ip_address));
}

void test_nat44_static_mapping_with_ports(void **state)
{
    UNUSED(state);
    nat44_add_del_static_mapping_t map = {0};
    nat44_static_mapping_details_t dump = {0};
    nat44_add_del_address_range_t range = {0};
    u8 empty_ip[4] = {0};
    const u16 lport = 77;
    const u16 eport = 88;
    int rc;

    /*Configure address pool
     Alternative to this CLI:
     nat44 add address 5.4.4.4
     */
    sc_aton("5.4.4.4", range.first_ip_address,
            sizeof(range.first_ip_address));

    sc_aton("5.4.4.4", range.last_ip_address,
            sizeof(range.last_ip_address));

    range.is_add = 1;

    rc = nat44_add_del_addr_range(&range);
    assert_int_equal(rc, SCVPP_OK);

    /*Configure NAT with ports
     Alternative to this CLI:
     nat44 add static mapping tcp local 172.168.0.1 77 external 5.4.4.4 88
     */

    sc_aton("172.168.0.1", map.local_ip_address,
            sizeof(map.local_ip_address));

    sc_aton("5.4.4.4", map.external_ip_address,
            sizeof(map.external_ip_address));

    map.protocol = 6;

    map.external_sw_if_index = ~0;

    map.external_port = eport;
    map.local_port = lport;

    map.is_add = 1;

    rc = nat44_add_del_static_mapping(&map);
    assert_int_equal(rc, SCVPP_OK);

    rc = nat44_static_mapping_dump(&dump);
    assert_int_equal(rc, SCVPP_OK);

    assert_memory_equal(dump.local_ip_address, map.local_ip_address,
                        sizeof(dump.local_ip_address));

    assert_memory_equal(dump.external_ip_address, map.external_ip_address,
                        sizeof(dump.external_ip_address));

    assert_int_equal(dump.local_port, lport);

    assert_int_equal(dump.external_port, eport);

    /* Remove all previous config*/

    map.is_add = 0;

    rc = nat44_add_del_static_mapping(&map);
    assert_int_equal(rc, SCVPP_OK);

    memset(&dump, 0, sizeof(dump));

    rc = nat44_static_mapping_dump(&dump);
    assert_int_equal(rc, SCVPP_OK);

    assert_memory_equal(dump.local_ip_address, empty_ip,
                        sizeof(dump.local_ip_address));

    assert_memory_equal(dump.external_ip_address, empty_ip,
                        sizeof(dump.external_ip_address));

    assert_int_equal(dump.local_port, 0);

    assert_int_equal(dump.external_port, 0);

    range.is_add = 0;

    rc = nat44_add_del_addr_range(&range);
    assert_int_equal(rc, SCVPP_OK);
}

const struct CMUnitTest nat_tests[] = {
            cmocka_unit_test_setup_teardown(test_nat44_static_mapping, NULL,
                                            NULL),
            cmocka_unit_test_setup_teardown(test_nat44_static_mapping_with_ports,
                                            NULL, NULL),
    };