aboutsummaryrefslogtreecommitdiffstats
path: root/src/scvpp/tests/scvpp_iface_test.c
blob: 6a96694a02fcd3b670d38ad274f8bea554a9217d (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
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * 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/interface.h>
#include <scvpp/v3po.h>

static void test_enable_disable(void **state)
{
    UNUSED(state);
    sw_interface_dump_t dump = {0};
    int rc;

    rc = interface_enable("tap0", 1);
    assert_int_equal(rc, SCVPP_OK);

    rc = interface_dump_iface(&dump, "tap0");
    assert_int_equal(rc, SCVPP_OK);

    assert_int_equal(dump.admin_up_down, true);

    rc = interface_enable("tap0", 0);
    assert_int_equal(rc, SCVPP_OK);
}

static void test_create_tapv2(void **state)
{
    UNUSED(state);
    tapv2_create_t query = {0};
    sw_interface_dump_t dump = {0};
    int rc;

    query.id = 1;
    query.use_random_mac = 1;

    rc = create_tapv2(&query);
    assert_int_equal(rc, SCVPP_OK);

    rc = interface_dump_iface(&dump, "tap1");
    assert_int_equal(rc, SCVPP_OK);
}

static int teardown_tapv2(void **state)
{
    UNUSED(state);
    return delete_tapv2("tap1");
}

static void test_dump_iface_all(void **state)
{
    UNUSED(state);
    struct elt *stack = NULL;
    sw_interface_dump_t *dump;
    bool exist = false;

    stack = interface_dump_all();
    assert_non_null(stack);
    foreach_stack_elt(stack) {
        dump = (sw_interface_dump_t *) data;
        if (!strncmp((char*) dump->interface_name, "tap0", VPP_INTFC_NAME_LEN))
            exist = true;
        free(dump);
    }
    assert_true(exist);
}

static void test_dump_iface_exist(void **state)
{
    UNUSED(state);
    vapi_payload_sw_interface_details details = {0};
    int rc;

    rc = interface_dump_iface(&details, "local0");
    assert_int_equal(rc, SCVPP_OK);

    assert_string_equal(details.interface_name, "local0");
}

static void test_dump_iface_unexist(void **state)
{
    UNUSED(state);
    vapi_payload_sw_interface_details details = {0};
    int rc;

    rc = interface_dump_iface(&details, "unexisting");
    assert_int_equal(rc, -SCVPP_NOT_FOUND);
}

static void test_get_interface_name(void **state)
{
    UNUSED(state);
    char interface_name[VPP_INTFC_NAME_LEN];
    uint32_t tap0_if_index;
    int rc;

    rc = get_interface_id("tap0", &tap0_if_index);
    assert_int_equal(rc, SCVPP_OK);

    rc = get_interface_name(interface_name, tap0_if_index);
    assert_int_equal(rc, SCVPP_OK);

    assert_string_equal(interface_name, "tap0");
}

const struct CMUnitTest iface_tests[IFACE_TEST_SIZE] = {
    cmocka_unit_test_teardown(test_create_tapv2, teardown_tapv2),
    cmocka_unit_test(test_enable_disable),
    cmocka_unit_test(test_dump_iface_all),
    cmocka_unit_test(test_dump_iface_exist),
    cmocka_unit_test(test_dump_iface_unexist),
    cmocka_unit_test(test_get_interface_name),
};