TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
configurers_utils.test.cpp
Go to the documentation of this file.
1 /**
2  * @file configurers_utils.test.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  *
6  * @brief Test configurers utilitary functions
7  *
8  * @date 06/2021
9  *
10  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
11  *
12  */
13 
14 #include <ros/ros.h>
15 #include <gtest/gtest.h>
16 
18 
19 /*****************************************
20  * Test functions
21  *****************************************/
22 
23 TEST(configurers_utils, ipv4_string_to_uint)
24 {
25  uint ip_uint[4] = {0};
26 
27  EXPECT_TRUE(travesim::ipv4_string_to_uint("1.1.1.1", ip_uint));
28  EXPECT_TRUE(testing::internal::ArrayEq(ip_uint, {1, 1, 1, 1}));
29 
30  ip_uint[0] = 0;
31  ip_uint[1] = 0;
32  ip_uint[2] = 0;
33  ip_uint[3] = 0;
34 
35  EXPECT_TRUE(travesim::ipv4_string_to_uint("127.220.4.32", ip_uint));
36  EXPECT_TRUE(testing::internal::ArrayEq(ip_uint, {127, 220, 4, 32}));
37 
38  ip_uint[0] = 0;
39  ip_uint[1] = 0;
40  ip_uint[2] = 0;
41  ip_uint[3] = 0;
42 
43  EXPECT_FALSE(travesim::ipv4_string_to_uint("@.b.C.3", ip_uint));
44  EXPECT_TRUE(testing::internal::ArrayEq(ip_uint, {0, 0, 0, 0}));
45 
46  ip_uint[0] = 0;
47  ip_uint[1] = 0;
48  ip_uint[2] = 0;
49  ip_uint[3] = 0;
50 
51  EXPECT_FALSE(travesim::ipv4_string_to_uint("123,123,123,123", ip_uint));
52  EXPECT_TRUE(testing::internal::ArrayEq(ip_uint, {123, 0, 0, 0}));
53 
54  ip_uint[0] = 0;
55  ip_uint[1] = 0;
56  ip_uint[2] = 0;
57  ip_uint[3] = 0;
58 
59  EXPECT_FALSE(travesim::ipv4_string_to_uint("Test", ip_uint));
60  EXPECT_TRUE(testing::internal::ArrayEq(ip_uint, {0, 0, 0, 0}));
61 
62  ip_uint[0] = 0;
63  ip_uint[1] = 0;
64  ip_uint[2] = 0;
65  ip_uint[3] = 0;
66 
67  EXPECT_FALSE(travesim::ipv4_string_to_uint("239.64.73.Q", ip_uint));
68  EXPECT_TRUE(testing::internal::ArrayEq(ip_uint, {239, 64, 73, 0}));
69 }
70 
71 TEST(configurers_utils, check_valid_ip)
72 {
73  using namespace travesim;
74 
75  EXPECT_EQ(check_valid_ip("127.0.0.2", "127.0.0.0", "127.255.255.0"), IPValidation::VALID);
76  EXPECT_EQ(check_valid_ip("225.0.64.2", "224.0.0.0", "239.255.34.255"), IPValidation::VALID);
77  EXPECT_EQ(check_valid_ip("227.0.0.2", "227.0.0.0", "A.255.255.0"), IPValidation::INVALID_FORMAT);
78  EXPECT_EQ(check_valid_ip("127.0.0,2", "127.0.0.0", "127.255.255.0"), IPValidation::INVALID_FORMAT);
79  EXPECT_EQ(check_valid_ip("127.0.512.2", "127.0.0.0", "127.255.255.0"), IPValidation::INVALID_NUMBERS);
80  EXPECT_EQ(check_valid_ip("1024.0.0.2", "127.0.0.0", "127.255.255.0"), IPValidation::INVALID_NUMBERS);
81  EXPECT_EQ(check_valid_ip("127.0.0.2", "127.0.0.255", "127.255.255.0"), IPValidation::OUT_OF_RANGE);
82  EXPECT_EQ(check_valid_ip("224.0.0.2", "127.0.0.0", "127.255.255.0"), IPValidation::OUT_OF_RANGE);
83 }
84 
85 TEST(configurers_utils, get_error_msg)
86 {
87  using namespace travesim;
88 
89  EXPECT_EQ(get_error_msg(IPValidation::VALID), "No error.");
90  EXPECT_EQ(get_error_msg(IPValidation::INVALID_FORMAT), "The IP string is wrong formatted.");
92  "The numbers on the ip are not representable by 8 bits.");
94  "The IP is not in the specified range. Hover over the parameterto see the range.");
95  EXPECT_EQ(get_error_msg(IPValidation(42)), "No error.");
96 }
97 
98 // Run all the tests that were declared with TEST()
99 int main(int argc, char** argv) {
100  testing::InitGoogleTest(&argc, argv);
101  return RUN_ALL_TESTS();
102 }
bool ipv4_string_to_uint(std::string ip_string, uint *ip_uint)
Converts a IPv4 in a string to a array of unsigned integers, where the most significant byte of the I...
IPValidation
Type used to validate a IP string.
std::string get_error_msg(IPValidation error)
Get the error msg based on the validation type.
IPValidation check_valid_ip(std::string ip, std::string min_ip, std::string max_ip)
Checks if a IP is valid and is in the specified range.
Configurers utilitary funtions.
TEST(configurers_utils, ipv4_string_to_uint)
int main(int argc, char **argv)