aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-08-25 14:05:30 -0700
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-08-25 14:05:30 -0700
commit9dee67b5cf52608f1dbe1142c43e8bf197cc34da (patch)
tree6b32dce07d328651b7b59e6b5b9aa5710da2f7cd /test
parent25de92c997117da87cfde68f97400aa32d27d814 (diff)
parent4087ea6f9c543ec79436c45281475fefe4e33a8f (diff)
Merge github.com:grpc/grpc into naming-crisis
Diffstat (limited to 'test')
-rw-r--r--test/core/end2end/dualstack_socket_test.c36
-rw-r--r--test/cpp/util/string_ref_test.cc215
2 files changed, 247 insertions, 4 deletions
diff --git a/test/core/end2end/dualstack_socket_test.c b/test/core/end2end/dualstack_socket_test.c
index 1f64062bf7..fcc12952bf 100644
--- a/test/core/end2end/dualstack_socket_test.c
+++ b/test/core/end2end/dualstack_socket_test.c
@@ -32,12 +32,16 @@
*/
#include <string.h>
-#include "src/core/iomgr/socket_utils_posix.h"
+
#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/host_port.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
+
+#include "src/core/support/string.h"
+#include "src/core/iomgr/socket_utils_posix.h"
+
#include "test/core/end2end/cq_verifier.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
@@ -57,6 +61,7 @@ static void drain_cq(grpc_completion_queue *cq) {
} while (ev.type != GRPC_QUEUE_SHUTDOWN);
}
+static void do_nothing(void *ignored) {}
void test_connect(const char *server_host, const char *client_host, int port,
int expect_ok) {
char *client_hostport;
@@ -109,8 +114,30 @@ void test_connect(const char *server_host, const char *client_host, int port,
/* Create client. */
if (client_host[0] == 'i') {
- /* for ipv4:/ipv6: addresses, just concatenate the port */
- gpr_asprintf(&client_hostport, "%s:%d", client_host, port);
+ /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */
+ size_t i;
+ gpr_slice uri_slice;
+ gpr_slice_buffer uri_parts;
+ char **hosts_with_port;
+
+ uri_slice =
+ gpr_slice_new((char *)client_host, strlen(client_host), do_nothing);
+ gpr_slice_buffer_init(&uri_parts);
+ gpr_slice_split(uri_slice, ",", &uri_parts);
+ hosts_with_port = gpr_malloc(sizeof(char*) * uri_parts.count);
+ for (i = 0; i < uri_parts.count; i++) {
+ char *uri_part_str = gpr_dump_slice(uri_parts.slices[i], GPR_DUMP_ASCII);
+ gpr_asprintf(&hosts_with_port[i], "%s:%d", uri_part_str, port);
+ gpr_free(uri_part_str);
+ }
+ client_hostport = gpr_strjoin_sep((const char **)hosts_with_port,
+ uri_parts.count, ",", NULL);
+ for (i = 0; i < uri_parts.count; i++) {
+ gpr_free(hosts_with_port[i]);
+ }
+ gpr_free(hosts_with_port);
+ gpr_slice_buffer_destroy(&uri_parts);
+ gpr_slice_unref(uri_slice);
} else {
gpr_join_host_port(&client_hostport, client_host, port);
}
@@ -260,7 +287,8 @@ int main(int argc, char **argv) {
test_connect("0.0.0.0", "127.0.0.1", 0, 1);
test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1);
test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1);
- test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1]", 0, 1);
+ test_connect("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1);
+ test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1);
test_connect("0.0.0.0", "localhost", 0, 1);
if (do_ipv6) {
test_connect("::", "::1", 0, 1);
diff --git a/test/cpp/util/string_ref_test.cc b/test/cpp/util/string_ref_test.cc
new file mode 100644
index 0000000000..c4ca4fce84
--- /dev/null
+++ b/test/cpp/util/string_ref_test.cc
@@ -0,0 +1,215 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <grpc++/support/string_ref.h>
+
+#include <string.h>
+
+#include <gtest/gtest.h>
+
+namespace grpc {
+namespace {
+
+const char kTestString[] = "blah";
+const char kTestStringWithEmbeddedNull[] = "blah\0foo";
+const size_t kTestStringWithEmbeddedNullLength = 8;
+const char kTestUnrelatedString[] = "foo";
+
+class StringRefTest : public ::testing::Test {
+};
+
+TEST_F(StringRefTest, Empty) {
+ string_ref s;
+ EXPECT_EQ(0U, s.length());
+ EXPECT_EQ(nullptr, s.data());
+}
+
+TEST_F(StringRefTest, FromCString) {
+ string_ref s(kTestString);
+ EXPECT_EQ(strlen(kTestString), s.length());
+ EXPECT_EQ(kTestString, s.data());
+}
+
+TEST_F(StringRefTest, FromCStringWithLength) {
+ string_ref s(kTestString, 2);
+ EXPECT_EQ(2U, s.length());
+ EXPECT_EQ(kTestString, s.data());
+}
+
+TEST_F(StringRefTest, FromString) {
+ string copy(kTestString);
+ string_ref s(copy);
+ EXPECT_EQ(copy.data(), s.data());
+ EXPECT_EQ(copy.length(), s.length());
+}
+
+TEST_F(StringRefTest, CopyConstructor) {
+ string_ref s1(kTestString);;
+ string_ref s2(s1);
+ EXPECT_EQ(s1.length(), s2.length());
+ EXPECT_EQ(s1.data(), s2.data());
+}
+
+TEST_F(StringRefTest, FromStringWithEmbeddedNull) {
+ string copy(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ string_ref s(copy);
+ EXPECT_EQ(copy.data(), s.data());
+ EXPECT_EQ(copy.length(), s.length());
+ EXPECT_EQ(kTestStringWithEmbeddedNullLength, s.length());
+}
+
+TEST_F(StringRefTest, Assignment) {
+ string_ref s1(kTestString);;
+ string_ref s2;
+ EXPECT_EQ(nullptr, s2.data());
+ s2 = s1;
+ EXPECT_EQ(s1.length(), s2.length());
+ EXPECT_EQ(s1.data(), s2.data());
+}
+
+TEST_F(StringRefTest, Iterator) {
+ string_ref s(kTestString);
+ size_t i = 0;
+ for (char c : s) {
+ EXPECT_EQ(kTestString[i++], c);
+ }
+ EXPECT_EQ(strlen(kTestString), i);
+}
+
+TEST_F(StringRefTest, ReverseIterator) {
+ string_ref s(kTestString);
+ size_t i = strlen(kTestString);
+ for (auto rit = s.crbegin(); rit != s.crend(); ++rit) {
+ EXPECT_EQ(kTestString[--i], *rit);
+ }
+ EXPECT_EQ(0U, i);
+}
+
+TEST_F(StringRefTest, Capacity) {
+ string_ref empty;
+ EXPECT_EQ(0U, empty.length());
+ EXPECT_EQ(0U, empty.size());
+ EXPECT_EQ(0U, empty.max_size());
+ EXPECT_TRUE(empty.empty());
+
+ string_ref s(kTestString);
+ EXPECT_EQ(strlen(kTestString), s.length());
+ EXPECT_EQ(s.length(), s.size());
+ EXPECT_EQ(s.max_size(), s.length());
+ EXPECT_FALSE(s.empty());
+}
+
+TEST_F(StringRefTest, Compare) {
+ string_ref s1(kTestString);
+ string s1_copy(kTestString);
+ string_ref s2(kTestUnrelatedString);
+ string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ EXPECT_EQ(0, s1.compare(s1_copy));
+ EXPECT_NE(0, s1.compare(s2));
+ EXPECT_NE(0, s1.compare(s3));
+}
+
+TEST_F(StringRefTest, StartsWith) {
+ string_ref s1(kTestString);
+ string_ref s2(kTestUnrelatedString);
+ string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ EXPECT_TRUE(s1.starts_with(s1));
+ EXPECT_FALSE(s1.starts_with(s2));
+ EXPECT_FALSE(s2.starts_with(s1));
+ EXPECT_FALSE(s1.starts_with(s3));
+ EXPECT_TRUE(s3.starts_with(s1));
+}
+
+TEST_F(StringRefTest, Endswith) {
+ string_ref s1(kTestString);
+ string_ref s2(kTestUnrelatedString);
+ string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ EXPECT_TRUE(s1.ends_with(s1));
+ EXPECT_FALSE(s1.ends_with(s2));
+ EXPECT_FALSE(s2.ends_with(s1));
+ EXPECT_FALSE(s2.ends_with(s3));
+ EXPECT_TRUE(s3.ends_with(s2));
+}
+
+TEST_F(StringRefTest, Find) {
+ string_ref s1(kTestString);
+ string_ref s2(kTestUnrelatedString);
+ string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ EXPECT_EQ(0U, s1.find(s1));
+ EXPECT_EQ(0U, s2.find(s2));
+ EXPECT_EQ(0U, s3.find(s3));
+ EXPECT_EQ(string_ref::npos,s1.find(s2) );
+ EXPECT_EQ(string_ref::npos,s2.find(s1));
+ EXPECT_EQ(string_ref::npos,s1.find(s3));
+ EXPECT_EQ(0U, s3.find(s1));
+ EXPECT_EQ(5U, s3.find(s2));
+ EXPECT_EQ(string_ref::npos, s1.find('z'));
+ EXPECT_EQ(1U, s2.find('o'));
+}
+
+TEST_F(StringRefTest, SubString) {
+ string_ref s(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ string_ref sub1 = s.substr(0, 4);
+ EXPECT_EQ(string_ref(kTestString), sub1);
+ string_ref sub2 = s.substr(5);
+ EXPECT_EQ(string_ref(kTestUnrelatedString), sub2);
+}
+
+TEST_F(StringRefTest, ComparisonOperators) {
+ string_ref s1(kTestString);
+ string_ref s2(kTestUnrelatedString);
+ string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
+ EXPECT_EQ(s1, s1);
+ EXPECT_EQ(s2, s2);
+ EXPECT_EQ(s3, s3);
+ EXPECT_GE(s1, s1);
+ EXPECT_GE(s2, s2);
+ EXPECT_GE(s3, s3);
+ EXPECT_LE(s1, s1);
+ EXPECT_LE(s2, s2);
+ EXPECT_LE(s3, s3);
+ EXPECT_NE(s1, s2);
+ EXPECT_NE(s1, s3);
+ EXPECT_NE(s2, s3);
+ EXPECT_GT(s3, s1);
+ EXPECT_LT(s1, s3);
+}
+
+} // namespace
+} // namespace grpc
+
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
+