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"
21 using namespace shareddatalayer;
22 using namespace testing;
24 TEST(HostAndPortTest, UseDefaultPortNumber)
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());
32 TEST(HostAndPortTest, UseExplicitPortNumber)
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());
40 TEST(HostAndPortTest, UseExplicitPortName)
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());
48 TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
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());
56 TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
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());
64 TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
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());
72 TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
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());
80 TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
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());
88 TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
92 throw HostAndPort::InvalidPort("foo");
94 catch (const std::exception& e)
96 EXPECT_STREQ("invalid port: foo", e.what());
100 TEST(HostAndPortTest, InvalidPortThrows)
102 EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
105 TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
109 throw HostAndPort::EmptyPort();
111 catch (const std::exception& e)
113 EXPECT_STREQ("empty port", e.what());
117 TEST(HostAndPortTest, EmptyPortThrows)
119 EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
122 TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
126 throw HostAndPort::EmptyHost();
128 catch (const std::exception& e)
130 EXPECT_STREQ("empty host", e.what());
134 TEST(HostAndPortTest, EmptyHostThrows)
136 EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);