|
| 1 | +/* Copyright 2014 MongoDB Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "mongo/unittest/unittest.h" |
| 17 | + |
| 18 | +#include "mongo/client/dbclientinterface.h" |
| 19 | + |
| 20 | +namespace { |
| 21 | + using mongo::ConnectionString; |
| 22 | + |
| 23 | + struct URLTestCase { |
| 24 | + std::string URL; |
| 25 | + std::string uname; |
| 26 | + std::string password; |
| 27 | + mongo::ConnectionString::ConnectionType type; |
| 28 | + std::string setname; |
| 29 | + size_t numservers; |
| 30 | + size_t numOptions; |
| 31 | + std::string database; |
| 32 | + }; |
| 33 | + |
| 34 | + const mongo::ConnectionString::ConnectionType kMaster = mongo::ConnectionString::MASTER; |
| 35 | + const mongo::ConnectionString::ConnectionType kSet = mongo::ConnectionString::SET; |
| 36 | + |
| 37 | + const URLTestCase cases[] = { |
| 38 | + |
| 39 | + { "mongodb://user:pwd@127.0.0.1", "user", "pwd", kMaster, "", 1, 0, "" }, |
| 40 | + |
| 41 | + { "mongodb://user@127.0.0.1", "user", "", kMaster, "", 1, 0, "" }, |
| 42 | + |
| 43 | + { "mongodb://127.0.0.1/dbName?foo=a&c=b", "", "", kMaster, "", 1, 2, "dbName" }, |
| 44 | + |
| 45 | + { "mongodb://user:pwd@127.0.0.1:1234", "user", "pwd", kMaster, "", 1, 0, "" }, |
| 46 | + |
| 47 | + { "mongodb://user@127.0.0.1:1234", "user", "", kMaster, "", 1, 0, "" }, |
| 48 | + |
| 49 | + { "mongodb://127.0.0.1:1234/dbName?foo=a&c=b", "", "", kMaster, "", 1, 2, "dbName" }, |
| 50 | + |
| 51 | + { "mongodb://user:pwd@127.0.0.1,127.0.0.2/?replicaSet=replName", "user", "pwd", kSet, "replName", 2, 1, "" }, |
| 52 | + |
| 53 | + { "mongodb://user@127.0.0.1,127.0.0.2/?replicaSet=replName", "user", "", kSet, "replName", 2, 1, "" }, |
| 54 | + |
| 55 | + { "mongodb://127.0.0.1,127.0.0.2/dbName?foo=a&c=b&replicaSet=replName", "", "", kSet, "replName", 2, 3, "dbName" }, |
| 56 | + |
| 57 | + { "mongodb://user:pwd@127.0.0.1:1234,127.0.0.2:1234/?replicaSet=replName", "user", "pwd", kSet, "replName", 2, 1, "" }, |
| 58 | + |
| 59 | + { "mongodb://user@127.0.0.1:1234,127.0.0.2:1234/?replicaSet=replName", "user", "", kSet, "replName", 2, 1, "" }, |
| 60 | + |
| 61 | + { "mongodb://127.0.0.1:1234,127.0.0.1:1234/dbName?foo=a&c=b&replicaSet=replName", "", "", kSet, "replName", 2, 3, "dbName" }, |
| 62 | + |
| 63 | + { "mongodb://user:pwd@[::1]", "user", "pwd", kMaster, "", 1, 0, "" }, |
| 64 | + |
| 65 | + { "mongodb://user@[::1]", "user", "", kMaster, "", 1, 0, "" }, |
| 66 | + |
| 67 | + { "mongodb://[::1]/dbName?foo=a&c=b", "", "", kMaster, "", 1, 2, "dbName" }, |
| 68 | + |
| 69 | + { "mongodb://user:pwd@[::1]:1234", "user", "pwd", kMaster, "", 1, 0, "" }, |
| 70 | + |
| 71 | + { "mongodb://user@[::1]:1234", "user", "", kMaster, "", 1, 0, "" }, |
| 72 | + |
| 73 | + { "mongodb://[::1]:1234/dbName?foo=a&c=b", "", "", kMaster, "", 1, 2, "dbName" }, |
| 74 | + |
| 75 | + { "mongodb://user:pwd@[::1],127.0.0.2/?replicaSet=replName", "user", "pwd", kSet, "replName", 2, 1, "" }, |
| 76 | + |
| 77 | + { "mongodb://user@[::1],127.0.0.2/?replicaSet=replName", "user", "", kSet, "replName", 2, 1, "" }, |
| 78 | + |
| 79 | + { "mongodb://[::1],127.0.0.2/dbName?foo=a&c=b&replicaSet=replName", "", "", kSet, "replName", 2, 3, "dbName" }, |
| 80 | + |
| 81 | + { "mongodb://user:pwd@[::1]:1234,127.0.0.2:1234/?replicaSet=replName", "user", "pwd", kSet, "replName", 2, 1, "" }, |
| 82 | + |
| 83 | + { "mongodb://user@[::1]:1234,127.0.0.2:1234/?replicaSet=replName", "user", "", kSet, "replName", 2, 1, "" }, |
| 84 | + |
| 85 | + { "mongodb://[::1]:1234,[::1]:1234/dbName?foo=a&c=b&replicaSet=replName", "", "", kSet, "replName", 2, 3, "dbName" }, |
| 86 | + |
| 87 | + { "mongodb://user:pwd@[::1]", "user", "pwd", kMaster, "", 1, 0, "" }, |
| 88 | + |
| 89 | + { "mongodb://user@[::1]", "user", "", kMaster, "", 1, 0, "" }, |
| 90 | + |
| 91 | + { "mongodb://[::1]/dbName?foo=a&c=b", "", "", kMaster, "", 1, 2, "dbName" }, |
| 92 | + |
| 93 | + { "mongodb://user:pwd@[::1]:1234", "user", "pwd", kMaster, "", 1, 0, "" }, |
| 94 | + |
| 95 | + { "mongodb://user@[::1]:1234", "user", "", kMaster, "", 1, 0, "" }, |
| 96 | + |
| 97 | + { "mongodb://[::1]:1234/dbName?foo=a&c=b", "", "", kMaster, "", 1, 2, "dbName" }, |
| 98 | + |
| 99 | + { "mongodb://user:pwd@[::1],127.0.0.2/?replicaSet=replName", "user", "pwd", kSet, "replName", 2, 1, "" }, |
| 100 | + |
| 101 | + { "mongodb://user@[::1],127.0.0.2/?replicaSet=replName", "user", "", kSet, "replName", 2, 1, "" }, |
| 102 | + |
| 103 | + { "mongodb://[::1],127.0.0.2/dbName?foo=a&c=b&replicaSet=replName", "", "", kSet, "replName", 2, 3, "dbName" }, |
| 104 | + |
| 105 | + { "mongodb://user:pwd@[::1]:1234,127.0.0.2:1234/?replicaSet=replName", "user", "pwd", kSet, "replName", 2, 1, "" }, |
| 106 | + |
| 107 | + { "mongodb://user@[::1]:1234,127.0.0.2:1234/?replicaSet=replName", "user", "", kSet, "replName", 2, 1, "" }, |
| 108 | + |
| 109 | + { "mongodb://[::1]:1234,[::1]:1234/dbName?foo=a&c=b&replicaSet=replName", "", "", kSet, "replName", 2, 3, "dbName"}, |
| 110 | + }; |
| 111 | + |
| 112 | + |
| 113 | + TEST(ConnectionString, GoodTrickyURLs) { |
| 114 | + |
| 115 | + const size_t numCases = sizeof(cases) / sizeof(cases[0]); |
| 116 | + |
| 117 | + for (size_t i = 0; i != numCases; ++i) { |
| 118 | + const URLTestCase testCase = cases[i]; |
| 119 | + std::cout << "Testing URL: " << testCase.URL << '\n'; |
| 120 | + std::string errMsg; |
| 121 | + const ConnectionString result = ConnectionString::parse(testCase.URL, errMsg); |
| 122 | + ASSERT_TRUE(result.isValid()); |
| 123 | + ASSERT_TRUE(errMsg.empty()); |
| 124 | + ASSERT_EQ(testCase.uname, result.getUser()); |
| 125 | + ASSERT_EQ(testCase.password, result.getPassword()); |
| 126 | + ASSERT_EQ(testCase.type, result.type()); |
| 127 | + ASSERT_EQ(testCase.setname, result.getSetName()); |
| 128 | + ASSERT_EQ(testCase.numservers, result.getServers().size()); |
| 129 | + BSONObj options = result.getOptions(); |
| 130 | + std::set<std::string> fieldNames; |
| 131 | + options.getFieldNames(fieldNames); |
| 132 | + ASSERT_EQ(testCase.numOptions, fieldNames.size()); |
| 133 | + ASSERT_EQ(testCase.database, result.getDatabase()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} // namespace |
0 commit comments