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.
17 #include <arpa/inet.h>
18 #include <gtest/gtest.h>
19 #include "private/hostandport.hpp"
22 using namespace shareddatalayer;
23 using namespace testing;
25 TEST(HostAndPortTest, UseDefaultPortNumber)
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());
33 TEST(HostAndPortTest, UseExplicitPortNumber)
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());
41 TEST(HostAndPortTest, UseExplicitPortName)
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());
49 TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
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());
57 TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
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());
65 TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
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());
73 TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
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());
81 TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
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());
89 TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
93 throw HostAndPort::InvalidPort("foo");
95 catch (const std::exception& e)
97 EXPECT_STREQ("invalid port: foo", e.what());
101 TEST(HostAndPortTest, InvalidPortThrows)
103 EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
106 TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
110 throw HostAndPort::EmptyPort();
112 catch (const std::exception& e)
114 EXPECT_STREQ("empty port", e.what());
118 TEST(HostAndPortTest, EmptyPortThrows)
120 EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
123 TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
127 throw HostAndPort::EmptyHost();
129 catch (const std::exception& e)
131 EXPECT_STREQ("empty host", e.what());
135 TEST(HostAndPortTest, EmptyHostThrows)
137 EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);
140 TEST(HostAndPortTest, CanOutput)
142 std::string expectedOutput("somehost.somesubdomain.somedomain:1234");
143 std::stringstream ss;
144 HostAndPort hostAndPort("somehost.somesubdomain.somedomain", 1234);
146 EXPECT_EQ(expectedOutput, ss.str());