blob: d0888ca2d345041760833f27783d120f8f775812 [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include <gtest/gtest.h>
2#include <netinet/in.h>
3#include "util.hpp"
4
5namespace phosphor
6{
7namespace network
8{
9
10class TestUtil : public testing::Test
11{
12 public:
13 TestUtil()
14 {
15 // Empty
16 }
17
18};
19
20TEST_F(TestUtil, ConvertV4MasktoPrefix)
21{
22 std::string mask = "255.255.255.0";
23 uint8_t prefix = toCidr(AF_INET, mask);
24 EXPECT_EQ(prefix, 24);
25
26 mask = "255.255.0.0";
27 prefix = toCidr(AF_INET, mask);
28 EXPECT_EQ(prefix, 16);
29
30 mask = "255.0.0.0";
31 prefix = toCidr(AF_INET, mask);
32 EXPECT_EQ(prefix, 8);
33
34 mask = "255.224.0.0";
35 prefix = toCidr(AF_INET, mask);
36 EXPECT_EQ(prefix, 11);
37
38 //Invalid Mask
39 mask = "255.0.255.0";
40 prefix = toCidr(AF_INET, mask);
41 EXPECT_EQ(prefix, 0);
42}
43
44TEST_F(TestUtil, convertV6MasktoPrefix)
45{
46 std::string mask = "ffff:ffff::";
47 uint8_t prefix = toCidr(AF_INET6, mask);
48 EXPECT_EQ(prefix, 32);
49
50 mask = "ffff:ffff:ffff::";
51 prefix = toCidr(AF_INET6, mask);
52 EXPECT_EQ(prefix, 48);
53
54 mask = "ffff:ffff:fc00::";
55 prefix = toCidr(AF_INET6, mask);
56 EXPECT_EQ(prefix, 38);
57
58 //Invalid Mask
59 mask = "ffff:0fff::";
60 prefix = toCidr(AF_INET6, mask);
61 EXPECT_EQ(prefix, 0);
62}
63
64TEST_F(TestUtil, isLinLocaladdress)
65{
66 std::string ipaddress = "fe80:fec0::";
67 EXPECT_TRUE(isLinkLocal(ipaddress));
68
69 ipaddress = "2000:4567:789::";
70 EXPECT_FALSE(isLinkLocal(ipaddress));
71
72 ipaddress = "2000:fe80::";
73 EXPECT_FALSE(isLinkLocal(ipaddress));
74}
75
76TEST_F(TestUtil, convertPrefixToMask)
77{
78 std::string mask = toMask(AF_INET, 24);
79 EXPECT_EQ(mask, "255.255.255.0");
80
81 mask = toMask(AF_INET, 8);
82 EXPECT_EQ(mask, "255.0.0.0");
83
84 mask = toMask(AF_INET, 27);
85 EXPECT_EQ(mask, "255.255.255.224");
86}
87
88}// namespce network
89}// namespace phosphor