blob: beab275377098cecd79ce7e34cff8d0ab21882c7 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
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
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#include <arpa/inet.h>
23#include <gtest/gtest.h>
24#include "private/hostandport.hpp"
Rolf Badorek2dcf9402019-10-01 18:33:58 +030025#include <sstream>
Rolf Badorekef2bf512019-08-20 11:17:15 +030026
27using namespace shareddatalayer;
28using namespace testing;
29
30TEST(HostAndPortTest, UseDefaultPortNumber)
31{
32 const HostAndPort hostAndPort("host", htons(100));
33 EXPECT_EQ("host", hostAndPort.getHost());
34 EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
35 EXPECT_EQ("host:100", hostAndPort.getString());
36}
37
38TEST(HostAndPortTest, UseExplicitPortNumber)
39{
40 const HostAndPort hostAndPort("host:999", htons(100));
41 EXPECT_EQ("host", hostAndPort.getHost());
42 EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
43 EXPECT_EQ("host:999", hostAndPort.getString());
44}
45
46TEST(HostAndPortTest, UseExplicitPortName)
47{
48 const HostAndPort hostAndPort("host:ssh", htons(100));
49 EXPECT_EQ("host", hostAndPort.getHost());
50 EXPECT_EQ(22, ntohs(hostAndPort.getPort()));
51 EXPECT_EQ("host:22", hostAndPort.getString());
52}
53
54TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
55{
56 const HostAndPort hostAndPort("[2016::dead:beef]:999", htons(100));
57 EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
58 EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
59 EXPECT_EQ("[2016::dead:beef]:999", hostAndPort.getString());
60}
61
62TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
63{
64 const HostAndPort hostAndPort("2016::dead:beef", htons(100));
65 EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
66 EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
67 EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
68}
69
70TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
71{
72 const HostAndPort hostAndPort("[host]:999", htons(100));
73 EXPECT_EQ("host", hostAndPort.getHost());
74 EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
75 EXPECT_EQ("host:999", hostAndPort.getString());
76}
77
78TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
79{
80 const HostAndPort hostAndPort("[host]", htons(100));
81 EXPECT_EQ("host", hostAndPort.getHost());
82 EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
83 EXPECT_EQ("host:100", hostAndPort.getString());
84}
85
86TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
87{
88 const HostAndPort hostAndPort("[2016::dead:beef]", htons(100));
89 EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
90 EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
91 EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
92}
93
94TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
95{
96 try
97 {
98 throw HostAndPort::InvalidPort("foo");
99 }
100 catch (const std::exception& e)
101 {
102 EXPECT_STREQ("invalid port: foo", e.what());
103 }
104}
105
106TEST(HostAndPortTest, InvalidPortThrows)
107{
108 EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
109}
110
111TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
112{
113 try
114 {
115 throw HostAndPort::EmptyPort();
116 }
117 catch (const std::exception& e)
118 {
119 EXPECT_STREQ("empty port", e.what());
120 }
121}
122
123TEST(HostAndPortTest, EmptyPortThrows)
124{
125 EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
126}
127
128TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
129{
130 try
131 {
132 throw HostAndPort::EmptyHost();
133 }
134 catch (const std::exception& e)
135 {
136 EXPECT_STREQ("empty host", e.what());
137 }
138}
139
140TEST(HostAndPortTest, EmptyHostThrows)
141{
142 EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);
143}
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300144
145TEST(HostAndPortTest, CanOutput)
146{
147 std::string expectedOutput("somehost.somesubdomain.somedomain:1234");
148 std::stringstream ss;
149 HostAndPort hostAndPort("somehost.somesubdomain.somedomain", 1234);
150 ss << hostAndPort;
151 EXPECT_EQ(expectedOutput, ss.str());
152}