aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/libhicnctrl/src/test/test_hicnlight_route.cc
blob: d48066ba2455d2645fd05e6da0e3d6a4f133b43f (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
/*
 * Copyright (c) 2021-2022 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>
#include <sstream>

extern "C" {
#include <hicn/ctrl/object.h>
#include "../modules/hicn_light/route.h"
}

#include "common.h"

namespace {

const hc_object_t valid_route = {
    .route = {.face_id = 1,
              .face_name = {0},  // NULL, use face_id instead
              .family = AF_INET,
              .remote_addr = IPV4_LOOPBACK,
              .len = 16,
              .cost = 1,
              .face = {0}}};

const std::vector<uint8_t> valid_route_create_payload = {
    /* uint8_t message_type = REQUEST_LIGHT */
    0xc0,
    /* uint8_t command_id = COMMAND_TYPE_ROUTE_ADD */
    0x08,
    /* uint16_t length = 1 */
    0x01, 0x00,
    /* uint32_t seq_num = 0 */
    0x00, 0x00, 0x00, 0x00,
    /* char symbolic_or_connid[16] = "1\0" */
    0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    /* hicn_ip_address_t address = {0, 0, 0, 127.0.0.1} */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x7f, 0x00, 0x00, 0x01,
    /* */
    0x01,
    /* */
    0x00,
    /* */
    0x02,
    /* */
    0x10};

const std::vector<uint8_t> valid_route_delete_payload = {
    /* uint8_t message_type = REQUEST_LIGHT */
    0xc0,
    /* uint8_t command_id = COMMAND_TYPE_ROUTE_REMOVE */
    0x09,
    /* uint16_t length = 1 */
    0x01, 0x00,
    /* uint32_t seq_num = 0 */
    0x00, 0x00, 0x00, 0x00,

    /* char symbolic_or_connid[16] = "1\0" */
    0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    /* hicn_ip_address_t address = {0, 0, 0, 127.0.0.1} */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x7f, 0x00, 0x00, 0x01,
    /* uint8_t family = AF_INET (2) */
    0x02,
    /* uint8_t len = 16 */
    0x10,
    /* 2-byte padding */
    0x00, 0x00};

const std::vector<uint8_t> valid_route_list_payload = {0xc0, 0x0a, 0x00, 0x00,
                                                       0x00, 0x00, 0x00, 0x00};

TEST_F(TestHicnLightSerialize, TestHicnLightSerializeRouteCreate) {
  uint8_t buf[BUFSIZE];

  hc_object_t obj;
  memset(&obj, 0, sizeof(hc_object_t));
  memcpy(&obj.route, &valid_route, sizeof(hc_route_t));

  hc_serialize_t fn = hicnlight_route_module_ops.serialize[ACTION_CREATE];
  size_t n = fn(&obj, buf);

  EXPECT_EQ(n, valid_route_create_payload.size());
  EXPECT_PAYLOAD_EQ(buf, n, valid_route_create_payload);
}

// TODO
// - create with id != 0
// - create with invalid fields, non zero-terminated strings, etc.

TEST_F(TestHicnLightSerialize, TestHicnLightSerializeRouteDelete) {
  uint8_t buf[BUFSIZE];

  hc_object_t obj;
  memset(&obj, 0, sizeof(hc_object_t));
  memcpy(&obj.route, &valid_route, sizeof(hc_route_t));

  hc_serialize_t fn = hicnlight_route_module_ops.serialize[ACTION_DELETE];
  size_t n = fn(&obj, buf);

  EXPECT_EQ(n, valid_route_delete_payload.size());
  EXPECT_PAYLOAD_EQ(buf, n, valid_route_delete_payload);
}

TEST_F(TestHicnLightSerialize, TestHicnLightSerializeRouteList) {
  uint8_t buf[BUFSIZE];

  hc_object_t obj;
  memset(&obj, 0, sizeof(hc_object_t));
  memcpy(&obj.route, &valid_route, sizeof(hc_route_t));

  hc_serialize_t fn = hicnlight_route_module_ops.serialize[ACTION_LIST];
  size_t n = fn(&obj, buf);

  EXPECT_EQ(n, valid_route_list_payload.size());
  EXPECT_PAYLOAD_EQ(buf, n, valid_route_list_payload);
}

}  // namespace