8f0c2d2532526e5d9ffd65033ccf29e7d7a45b31
[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 #include <sstream>
21
22 using namespace shareddatalayer;
23 using namespace testing;
24
25 TEST(HostAndPortTest, UseDefaultPortNumber)
26 {
27     const HostAndPort hostAndPort("host", htons(100));
28     EXPECT_EQ("host", hostAndPort.getHost());
29     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
30     EXPECT_EQ("host:100", hostAndPort.getString());
31 }
32
33 TEST(HostAndPortTest, UseExplicitPortNumber)
34 {
35     const HostAndPort hostAndPort("host:999", htons(100));
36     EXPECT_EQ("host", hostAndPort.getHost());
37     EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
38     EXPECT_EQ("host:999", hostAndPort.getString());
39 }
40
41 TEST(HostAndPortTest, UseExplicitPortName)
42 {
43     const HostAndPort hostAndPort("host:ssh", htons(100));
44     EXPECT_EQ("host", hostAndPort.getHost());
45     EXPECT_EQ(22, ntohs(hostAndPort.getPort()));
46     EXPECT_EQ("host:22", hostAndPort.getString());
47 }
48
49 TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
50 {
51     const HostAndPort hostAndPort("[2016::dead:beef]:999", htons(100));
52     EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
53     EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
54     EXPECT_EQ("[2016::dead:beef]:999", hostAndPort.getString());
55 }
56
57 TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
58 {
59     const HostAndPort hostAndPort("2016::dead:beef", htons(100));
60     EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
61     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
62     EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
63 }
64
65 TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
66 {
67     const HostAndPort hostAndPort("[host]:999", htons(100));
68     EXPECT_EQ("host", hostAndPort.getHost());
69     EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
70     EXPECT_EQ("host:999", hostAndPort.getString());
71 }
72
73 TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
74 {
75     const HostAndPort hostAndPort("[host]", htons(100));
76     EXPECT_EQ("host", hostAndPort.getHost());
77     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
78     EXPECT_EQ("host:100", hostAndPort.getString());
79 }
80
81 TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
82 {
83     const HostAndPort hostAndPort("[2016::dead:beef]", htons(100));
84     EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
85     EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
86     EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
87 }
88
89 TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
90 {
91     try
92     {
93         throw HostAndPort::InvalidPort("foo");
94     }
95     catch (const std::exception& e)
96     {
97         EXPECT_STREQ("invalid port: foo", e.what());
98     }
99 }
100
101 TEST(HostAndPortTest, InvalidPortThrows)
102 {
103     EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
104 }
105
106 TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
107 {
108     try
109     {
110         throw HostAndPort::EmptyPort();
111     }
112     catch (const std::exception& e)
113     {
114         EXPECT_STREQ("empty port", e.what());
115     }
116 }
117
118 TEST(HostAndPortTest, EmptyPortThrows)
119 {
120     EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
121 }
122
123 TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
124 {
125     try
126     {
127         throw HostAndPort::EmptyHost();
128     }
129     catch (const std::exception& e)
130     {
131         EXPECT_STREQ("empty host", e.what());
132     }
133 }
134
135 TEST(HostAndPortTest, EmptyHostThrows)
136 {
137     EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);
138 }
139
140 TEST(HostAndPortTest, CanOutput)
141 {
142     std::string expectedOutput("somehost.somesubdomain.somedomain:1234");
143     std::stringstream ss;
144     HostAndPort hostAndPort("somehost.somesubdomain.somedomain", 1234);
145     ss << hostAndPort;
146     EXPECT_EQ(expectedOutput, ss.str());
147 }