diff options
author | Andy Schwerin <schwerin@mongodb.com> | 2014-06-30 17:06:55 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@mongodb.com> | 2014-07-02 19:18:23 -0400 |
commit | 484a5bb3542a73f4a10bc962e2b1b0f05a402659 (patch) | |
tree | 1cd4f3a9491a9ef5f723617b1bcd3a3d12fe4d2f | |
parent | e8b6969143e1d218a105c64716203fd7b74137d4 (diff) | |
download | mongo-484a5bb3542a73f4a10bc962e2b1b0f05a402659.tar.gz |
SERVER-14419 Introduce unittest of HostAndPort; make ":123" unparseable due to lack of host component.
-rw-r--r-- | src/mongo/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport.cpp | 11 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport.h | 3 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport_test.cpp | 81 |
4 files changed, 96 insertions, 2 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index a8770e620d9..c2364c0c426 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -243,6 +243,9 @@ env.Library('hostandport', ['util/net/hostandport.cpp'], 'server_options_core', ]) +env.CppUnitTest('hostandport_test', ['util/net/hostandport_test.cpp'], + LIBDEPS=['hostandport']) + env.Library('network', [ "util/net/sock.cpp", "util/net/socket_poll.cpp", diff --git a/src/mongo/util/net/hostandport.cpp b/src/mongo/util/net/hostandport.cpp index 74190a05b10..31be1408847 100644 --- a/src/mongo/util/net/hostandport.cpp +++ b/src/mongo/util/net/hostandport.cpp @@ -110,11 +110,14 @@ namespace mongo { } void HostAndPort::init(const char *p) { - massert(13110, "HostAndPort: host is empty", *p); + uassert(13110, "HostAndPort: host is empty", *p); const char *colon = strrchr(p, ':'); if( colon ) { + uassert(18522, + "HostAndPort: no host component in " + std::string(p), + colon != p); int port = atoi(colon+1); - massert(13095, "HostAndPort: bad port #", port > 0); + uassert(13095, "HostAndPort: bad port #", port > 0); _host = std::string(p,colon-p); _port = port; } @@ -125,4 +128,8 @@ namespace mongo { } } + std::ostream& operator<<(std::ostream& os, const HostAndPort& hp) { + return os << hp.toString(); + } + } // namespace mongo diff --git a/src/mongo/util/net/hostandport.h b/src/mongo/util/net/hostandport.h index e7aec204e6a..b7021764ce3 100644 --- a/src/mongo/util/net/hostandport.h +++ b/src/mongo/util/net/hostandport.h @@ -27,6 +27,7 @@ #pragma once +#include <iosfwd> #include <string> #include "bson/util/builder.h" @@ -106,4 +107,6 @@ namespace mongo { int _port; // -1 indicates unspecified }; + std::ostream& operator<<(std::ostream& os, const HostAndPort& hp); + } // namespace mongo diff --git a/src/mongo/util/net/hostandport_test.cpp b/src/mongo/util/net/hostandport_test.cpp new file mode 100644 index 00000000000..50b1c7523ad --- /dev/null +++ b/src/mongo/util/net/hostandport_test.cpp @@ -0,0 +1,81 @@ +/* Copyright 2014 MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects + * for all of the code used other than as permitted herein. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you do not + * wish to do so, delete this exception statement from your version. If you + * delete this exception statement from all source files in the program, + * then also delete it in the license file. + */ + +#include "mongo/db/server_options.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/assert_util.h" +#include "mongo/util/net/hostandport.h" + +namespace mongo { +namespace { + + TEST(HostAndPort, BasicLessThanComparison) { + // Not less than self. + ASSERT_FALSE(HostAndPort("a", 1) < HostAndPort("a", 1)); + + // Lex order by name. + ASSERT_LESS_THAN(HostAndPort("a", 1), HostAndPort("b", 1)); + ASSERT_FALSE(HostAndPort("b", 1) < HostAndPort("a", 1)); + + // Then, order by port number. + ASSERT_LESS_THAN(HostAndPort("a", 1), HostAndPort("a", 2)); + ASSERT_FALSE(HostAndPort("a", 2) < HostAndPort("a", 1)); + } + + TEST(HostAndPort, BasicEquality) { + // Comparison on host field + ASSERT_EQUALS(HostAndPort("a", 1), HostAndPort("a", 1)); + ASSERT_FALSE(HostAndPort("b", 1) == HostAndPort("a", 1)); + ASSERT_FALSE(HostAndPort("a", 1) != HostAndPort("a", 1)); + ASSERT_NOT_EQUALS(HostAndPort("b", 1), HostAndPort("a", 1)); + + // Comparison on port field + ASSERT_FALSE(HostAndPort("a", 1) == HostAndPort("a", 2)); + ASSERT_NOT_EQUALS(HostAndPort("a", 1), HostAndPort("a", 2)); + } + + TEST(HostAndPort, ImplicitPortSelection) { + ASSERT_EQUALS(HostAndPort("a", -1), + HostAndPort("a", int(ServerGlobalParams::DefaultDBPort))); + ASSERT_EQUALS(int(ServerGlobalParams::DefaultDBPort), HostAndPort("a", -1).port()); + ASSERT_FALSE(HostAndPort("a", -1).empty()); + } + + TEST(HostAndPort, ConstructorParsing) { + ASSERT_THROWS(HostAndPort(""), AssertionException); + ASSERT_THROWS(HostAndPort("a:"), AssertionException); + ASSERT_THROWS(HostAndPort("a:0xa"), AssertionException); + ASSERT_THROWS(HostAndPort(":123"), AssertionException); + + ASSERT_EQUALS(HostAndPort("abc"), HostAndPort("abc", -1)); + ASSERT_EQUALS(HostAndPort("abc"), HostAndPort("abc", -1)); + + ASSERT_EQUALS(HostAndPort("abc.def:3421"), HostAndPort("abc.def", 3421)); + } + +} // namespace +} // namespace mongo |