Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 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 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include <arpa/inet.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include "private/hostandport.hpp" |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 25 | #include <sstream> |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 26 | |
| 27 | using namespace shareddatalayer; |
| 28 | using namespace testing; |
| 29 | |
| 30 | TEST(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 | |
| 38 | TEST(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 | |
| 46 | TEST(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 | |
| 54 | TEST(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 | |
| 62 | TEST(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 | |
| 70 | TEST(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 | |
| 78 | TEST(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 | |
| 86 | TEST(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 | |
| 94 | TEST(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 | |
| 106 | TEST(HostAndPortTest, InvalidPortThrows) |
| 107 | { |
| 108 | EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort); |
| 109 | } |
| 110 | |
| 111 | TEST(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 | |
| 123 | TEST(HostAndPortTest, EmptyPortThrows) |
| 124 | { |
| 125 | EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort); |
| 126 | } |
| 127 | |
| 128 | TEST(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 | |
| 140 | TEST(HostAndPortTest, EmptyHostThrows) |
| 141 | { |
| 142 | EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost); |
| 143 | } |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 144 | |
| 145 | TEST(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 | } |