aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/test/test-ctrl.cc
blob: f0d3e7c37eeb36a2aa582f66b52fbaaf648495a3 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 2021 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 <gtest/gtest.h>

extern "C" {
#include <hicn/util/log.h>
#include <hicn/ctrl.h>
#include <hicn/ctrl/parse.h>
#include <hicn/ctrl/route.h>
#include <hicn/util/sstrncpy.h>
}

class CtrlTest : public ::testing::Test {
 protected:
  CtrlTest() {
    log_conf.log_level = LOG_INFO;
    s_ = hc_sock_create_forwarder(FORWARDER_TYPE_HICNLIGHT);
  }
  virtual ~CtrlTest() { hc_sock_free(s_); }

  hc_sock_t *s_ = nullptr;
  hc_command_t command_ = {};
};

/**
 * The parse() function is used to easily create the command.
 * Here we test the serialization of the commands i.e. from command
 * to message sent to the forwarder.
 */
#if 0
TEST_F(CtrlTest, AddValidListener) {
  std::string cmd = "add listener udp udp0 10.0.0.1 9695 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_TRUE(success);
}

TEST_F(CtrlTest, AddListenerInvalidProtocol) {
  // Set invalid protocol (icmp)
  std::string cmd = "add listener icmp udp0 10.0.0.1 9696 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddListenerInvalidLocalPort) {
  std::string cmd = "add listener udp udp0 10.0.0.1 9695 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  // Override with invalid port
  command_.object.listener.local_port = 0;

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddListenerInvalidLocalAddress) {
  std::string cmd = "add listener udp udp0 10.0.0.1 9695 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  // Override with invalid family
  command_.object.listener.family = -1;

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_EQ(success, false);
}

TEST_F(CtrlTest, AddListenerEmptyLocalAddress) {
  std::string cmd = "add listener udp udp0 10.0.0.1 9695 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  // Override with invalid address
  command_.object.listener.local_addr = IP_ADDRESS_EMPTY;

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddListenerInvalidSymbolicName) {
  std::string cmd = "add listener udp 0udp 10.0.0.1 9695 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddListenerInvalidSymbolicName2) {
  std::string cmd = "add listener udp udp! 10.0.0.1 9695 eth0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddListenerInvalidInterfaceName) {
  std::string cmd = "add listener udp udp0 10.0.0.1 9695 eth/0";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  hc_result_t *result = hc_listener_create_conf(s_, &command_.object.listener);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddValidRoute) {
  std::string cmd = "add route conn0 c001::/64 1";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  hc_result_t *result = hc_route_create_conf(s_, &command_.object.route);
  bool success = hc_result_get_success(s_, result);
  EXPECT_TRUE(success);
}

TEST_F(CtrlTest, AddRouteInvalidLength) {
  std::string cmd = "add route conn0 c001::/64 1";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  // Override with invalid prfix len
  command_.object.route.len = MAX_IPV6_PREFIX_LEN + 1;

  hc_result_t *result = hc_route_create_conf(s_, &command_.object.route);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, AddRouteInvalidCost) {
  std::string cmd = "add route conn0 c001::/64 1";
  ASSERT_EQ(parse(cmd.c_str(), &command_), 0);

  // Override with invalid cost
  command_.object.route.cost = MAX_ROUTE_COST + 1;

  hc_result_t *result = hc_route_create_conf(s_, &command_.object.route);
  bool success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);

  // Override with invalid cost
  command_.object.route.cost = MIN_ROUTE_COST - 1;

  result = hc_route_create_conf(s_, &command_.object.route);
  success = hc_result_get_success(s_, result);
  EXPECT_FALSE(success);
}

TEST_F(CtrlTest, RouteNameOrID) {
  hc_route_t route = {
      .face_id = (face_id_t)INVALID_FACE_ID,
      .family = AF_INET6,
      .remote_addr = IPV6_LOOPBACK,
      .len = 64,
      .cost = 1,
  };

  // At least one between name (symbolic or ID) and face_id
  // should be set to make the route valid

  // Valid name (symbolic)
  snprintf(route.name, SYMBOLIC_NAME_LEN, "%s", "test");
  EXPECT_EQ(hc_route_validate(&route), 0);

  // Valid name (ID)
  snprintf(route.name, SYMBOLIC_NAME_LEN, "%s", "conn0");
  EXPECT_EQ(hc_route_validate(&route), 0);

  // Valid face_id
  route.face_id = 1;
  snprintf(route.name, SYMBOLIC_NAME_LEN, "%s", "");
  EXPECT_EQ(hc_route_validate(&route), 0);

  // Invalid name stating with number
  // (face_id is only checked if empty name)
  route.face_id = 1;
  snprintf(route.name, SYMBOLIC_NAME_LEN, "%s", "1test");
  EXPECT_EQ(hc_route_validate(&route), -1);
}
#endif