2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include <arpa/inet.h>
23 #include <gtest/gtest.h>
24 #include "private/hostandport.hpp"
27 using namespace shareddatalayer;
28 using namespace testing;
30 TEST(HostAndPortTest, UseDefaultPortNumber)
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());
38 TEST(HostAndPortTest, UseExplicitPortNumber)
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());
46 TEST(HostAndPortTest, UseExplicitPortName)
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());
54 TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
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());
62 TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
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());
70 TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
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());
78 TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
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());
86 TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
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());
94 TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
98 throw HostAndPort::InvalidPort("foo");
100 catch (const std::exception& e)
102 EXPECT_STREQ("invalid port: foo", e.what());
106 TEST(HostAndPortTest, InvalidPortThrows)
108 EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
111 TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
115 throw HostAndPort::EmptyPort();
117 catch (const std::exception& e)
119 EXPECT_STREQ("empty port", e.what());
123 TEST(HostAndPortTest, EmptyPortThrows)
125 EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
128 TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
132 throw HostAndPort::EmptyHost();
134 catch (const std::exception& e)
136 EXPECT_STREQ("empty host", e.what());
140 TEST(HostAndPortTest, EmptyHostThrows)
142 EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);
145 TEST(HostAndPortTest, CanOutput)
147 std::string expectedOutput("somehost.somesubdomain.somedomain:1234");
148 std::stringstream ss;
149 HostAndPort hostAndPort("somehost.somesubdomain.somedomain", 1234);
151 EXPECT_EQ(expectedOutput, ss.str());