7b9f97b851706078be471a5eac19e806c70680b0
[ric-plt/sdl.git] / tst / hostandport_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #include <arpa/inet.h>
18 #include <gtest/gtest.h>
19 #include "private/hostandport.hpp"
20
21 using namespace shareddatalayer;
22 using namespace testing;
23
24 TEST(HostAndPortTest, UseDefaultPortNumber)
25 {
26     const HostAndPort hostAndPort("host", htons(100));
27     EXPECT_EQ("host", hostAndPort.getHost());
28     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
29     EXPECT_EQ("host:100", hostAndPort.getString());
30 }
31
32 TEST(HostAndPortTest, UseExplicitPortNumber)
33 {
34     const HostAndPort hostAndPort("host:999", htons(100));
35     EXPECT_EQ("host", hostAndPort.getHost());
36     EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
37     EXPECT_EQ("host:999", hostAndPort.getString());
38 }
39
40 TEST(HostAndPortTest, UseExplicitPortName)
41 {
42     const HostAndPort hostAndPort("host:ssh", htons(100));
43     EXPECT_EQ("host", hostAndPort.getHost());
44     EXPECT_EQ(22, ntohs(hostAndPort.getPort()));
45     EXPECT_EQ("host:22", hostAndPort.getString());
46 }
47
48 TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
49 {
50     const HostAndPort hostAndPort("[2016::dead:beef]:999", htons(100));
51     EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
52     EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
53     EXPECT_EQ("[2016::dead:beef]:999", hostAndPort.getString());
54 }
55
56 TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
57 {
58     const HostAndPort hostAndPort("2016::dead:beef", htons(100));
59     EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
60     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
61     EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
62 }
63
64 TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
65 {
66     const HostAndPort hostAndPort("[host]:999", htons(100));
67     EXPECT_EQ("host", hostAndPort.getHost());
68     EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
69     EXPECT_EQ("host:999", hostAndPort.getString());
70 }
71
72 TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
73 {
74     const HostAndPort hostAndPort("[host]", htons(100));
75     EXPECT_EQ("host", hostAndPort.getHost());
76     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
77     EXPECT_EQ("host:100", hostAndPort.getString());
78 }
79
80 TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
81 {
82     const HostAndPort hostAndPort("[2016::dead:beef]", htons(100));
83     EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
84     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
85     EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
86 }
87
88 TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
89 {
90     try
91     {
92         throw HostAndPort::InvalidPort("foo");
93     }
94     catch (const std::exception& e)
95     {
96         EXPECT_STREQ("invalid port: foo", e.what());
97     }
98 }
99
100 TEST(HostAndPortTest, InvalidPortThrows)
101 {
102     EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
103 }
104
105 TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
106 {
107     try
108     {
109         throw HostAndPort::EmptyPort();
110     }
111     catch (const std::exception& e)
112     {
113         EXPECT_STREQ("empty port", e.what());
114     }
115 }
116
117 TEST(HostAndPortTest, EmptyPortThrows)
118 {
119     EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
120 }
121
122 TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
123 {
124     try
125     {
126         throw HostAndPort::EmptyHost();
127     }
128     catch (const std::exception& e)
129     {
130         EXPECT_STREQ("empty host", e.what());
131     }
132 }
133
134 TEST(HostAndPortTest, EmptyHostThrows)
135 {
136     EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);
137 }