Add first version
[ric-plt/sdl.git] / tst / hostandport_test.cpp
diff --git a/tst/hostandport_test.cpp b/tst/hostandport_test.cpp
new file mode 100644 (file)
index 0000000..7b9f97b
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+   Copyright (c) 2018-2019 Nokia.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#include <arpa/inet.h>
+#include <gtest/gtest.h>
+#include "private/hostandport.hpp"
+
+using namespace shareddatalayer;
+using namespace testing;
+
+TEST(HostAndPortTest, UseDefaultPortNumber)
+{
+    const HostAndPort hostAndPort("host", htons(100));
+    EXPECT_EQ("host", hostAndPort.getHost());
+    EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("host:100", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, UseExplicitPortNumber)
+{
+    const HostAndPort hostAndPort("host:999", htons(100));
+    EXPECT_EQ("host", hostAndPort.getHost());
+    EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("host:999", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, UseExplicitPortName)
+{
+    const HostAndPort hostAndPort("host:ssh", htons(100));
+    EXPECT_EQ("host", hostAndPort.getHost());
+    EXPECT_EQ(22, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("host:22", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, IPv6AddressWithExplicitPortNumber)
+{
+    const HostAndPort hostAndPort("[2016::dead:beef]:999", htons(100));
+    EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
+    EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("[2016::dead:beef]:999", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, IPv6AddressWithDefaultPortNumber)
+{
+    const HostAndPort hostAndPort("2016::dead:beef", htons(100));
+    EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
+    EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, HostnameInBracketsWithExplicitPort)
+{
+    const HostAndPort hostAndPort("[host]:999", htons(100));
+    EXPECT_EQ("host", hostAndPort.getHost());
+    EXPECT_EQ(999, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("host:999", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, HostnameInBracketsWithDefaultPort)
+{
+    const HostAndPort hostAndPort("[host]", htons(100));
+    EXPECT_EQ("host", hostAndPort.getHost());
+    EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("host:100", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, IPv6AddressInBracketsWithDefaultPort)
+{
+    const HostAndPort hostAndPort("[2016::dead:beef]", htons(100));
+    EXPECT_EQ("2016::dead:beef", hostAndPort.getHost());
+    EXPECT_EQ(100, ntohs(hostAndPort.getPort()));
+    EXPECT_EQ("[2016::dead:beef]:100", hostAndPort.getString());
+}
+
+TEST(HostAndPortTest, CanThrowAndCatchInvalidPort)
+{
+    try
+    {
+        throw HostAndPort::InvalidPort("foo");
+    }
+    catch (const std::exception& e)
+    {
+        EXPECT_STREQ("invalid port: foo", e.what());
+    }
+}
+
+TEST(HostAndPortTest, InvalidPortThrows)
+{
+    EXPECT_THROW(HostAndPort("host:definitely_invalid_port", htons(100)), HostAndPort::InvalidPort);
+}
+
+TEST(HostAndPortTest, CanThrowAndCatchEmptyPort)
+{
+    try
+    {
+        throw HostAndPort::EmptyPort();
+    }
+    catch (const std::exception& e)
+    {
+        EXPECT_STREQ("empty port", e.what());
+    }
+}
+
+TEST(HostAndPortTest, EmptyPortThrows)
+{
+    EXPECT_THROW(HostAndPort("host:", htons(100)), HostAndPort::EmptyPort);
+}
+
+TEST(HostAndPortTest, CanThrowAndCatchEmptyHost)
+{
+    try
+    {
+        throw HostAndPort::EmptyHost();
+    }
+    catch (const std::exception& e)
+    {
+        EXPECT_STREQ("empty host", e.what());
+    }
+}
+
+TEST(HostAndPortTest, EmptyHostThrows)
+{
+    EXPECT_THROW(HostAndPort(":1234", htons(100)), HostAndPort::EmptyHost);
+}