QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/http2/hpack/hpack_string.h" |
| 6 | |
| 7 | // Tests of HpackString. |
| 8 | |
| 9 | #include <utility> |
| 10 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/http2/platform/api/http2_logging.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/http2/platform/api/http2_test_helpers.h" |
| 14 | |
| 15 | using ::testing::AssertionFailure; |
| 16 | using ::testing::AssertionResult; |
| 17 | using ::testing::AssertionSuccess; |
| 18 | |
| 19 | namespace http2 { |
| 20 | namespace test { |
| 21 | namespace { |
| 22 | |
| 23 | const char kStr0[] = "s0: Some string to be copied into another string."; |
| 24 | const char kStr1[] = "S1 - some string to be copied into yet another string."; |
| 25 | |
| 26 | class HpackStringTest : public ::testing::Test { |
| 27 | protected: |
| 28 | AssertionResult VerifyNotEqual(HpackString* actual, |
| 29 | const Http2String& not_expected_str) { |
| 30 | const char* not_expected_ptr = not_expected_str.c_str(); |
| 31 | Http2StringPiece not_expected_sp(not_expected_str); |
| 32 | |
| 33 | VERIFY_NE(*actual, not_expected_ptr); |
| 34 | VERIFY_NE(*actual, not_expected_sp); |
| 35 | VERIFY_NE(*actual, not_expected_str); |
| 36 | VERIFY_NE(actual->ToStringPiece(), not_expected_sp); |
| 37 | |
| 38 | if (!(not_expected_ptr != *actual)) { |
| 39 | return AssertionFailure(); |
| 40 | } |
| 41 | if (!(not_expected_sp != *actual)) { |
| 42 | return AssertionFailure(); |
| 43 | } |
| 44 | if (!(not_expected_str != *actual)) { |
| 45 | return AssertionFailure(); |
| 46 | } |
| 47 | if (!(not_expected_sp != actual->ToStringPiece())) { |
| 48 | return AssertionFailure(); |
| 49 | } |
| 50 | |
| 51 | return AssertionSuccess(); |
| 52 | } |
| 53 | |
| 54 | AssertionResult VerifyEqual(HpackString* actual, |
| 55 | const Http2String& expected_str) { |
| 56 | VERIFY_EQ(actual->size(), expected_str.size()); |
| 57 | |
| 58 | const char* expected_ptr = expected_str.c_str(); |
| 59 | const Http2StringPiece expected_sp(expected_str); |
| 60 | |
| 61 | VERIFY_EQ(*actual, expected_ptr); |
| 62 | VERIFY_EQ(*actual, expected_sp); |
| 63 | VERIFY_EQ(*actual, expected_str); |
| 64 | VERIFY_EQ(actual->ToStringPiece(), expected_sp); |
| 65 | |
| 66 | if (!(expected_sp == *actual)) { |
| 67 | return AssertionFailure(); |
| 68 | } |
| 69 | if (!(expected_ptr == *actual)) { |
| 70 | return AssertionFailure(); |
| 71 | } |
| 72 | if (!(expected_str == *actual)) { |
| 73 | return AssertionFailure(); |
| 74 | } |
| 75 | if (!(expected_sp == actual->ToStringPiece())) { |
| 76 | return AssertionFailure(); |
| 77 | } |
| 78 | |
| 79 | return AssertionSuccess(); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | TEST_F(HpackStringTest, CharArrayConstructor) { |
| 84 | HpackString hs0(kStr0); |
| 85 | EXPECT_TRUE(VerifyEqual(&hs0, kStr0)); |
| 86 | EXPECT_TRUE(VerifyNotEqual(&hs0, kStr1)); |
| 87 | |
| 88 | HpackString hs1(kStr1); |
| 89 | EXPECT_TRUE(VerifyEqual(&hs1, kStr1)); |
| 90 | EXPECT_TRUE(VerifyNotEqual(&hs1, kStr0)); |
| 91 | } |
| 92 | |
| 93 | TEST_F(HpackStringTest, StringPieceConstructor) { |
| 94 | Http2StringPiece sp0(kStr0); |
| 95 | HpackString hs0(sp0); |
| 96 | EXPECT_TRUE(VerifyEqual(&hs0, kStr0)); |
| 97 | EXPECT_TRUE(VerifyNotEqual(&hs0, kStr1)); |
| 98 | |
| 99 | Http2StringPiece sp1(kStr1); |
| 100 | HpackString hs1(sp1); |
| 101 | EXPECT_TRUE(VerifyEqual(&hs1, kStr1)); |
| 102 | EXPECT_TRUE(VerifyNotEqual(&hs1, kStr0)); |
| 103 | } |
| 104 | |
| 105 | TEST_F(HpackStringTest, MoveStringConstructor) { |
| 106 | Http2String str0(kStr0); |
| 107 | HpackString hs0(str0); |
| 108 | EXPECT_TRUE(VerifyEqual(&hs0, kStr0)); |
| 109 | EXPECT_TRUE(VerifyNotEqual(&hs0, kStr1)); |
| 110 | |
| 111 | Http2String str1(kStr1); |
| 112 | HpackString hs1(str1); |
| 113 | EXPECT_TRUE(VerifyEqual(&hs1, kStr1)); |
| 114 | EXPECT_TRUE(VerifyNotEqual(&hs1, kStr0)); |
| 115 | } |
| 116 | |
| 117 | TEST_F(HpackStringTest, CopyConstructor) { |
| 118 | Http2StringPiece sp0(kStr0); |
| 119 | HpackString hs0(sp0); |
| 120 | HpackString hs1(hs0); |
| 121 | EXPECT_EQ(hs0, hs1); |
| 122 | |
| 123 | EXPECT_TRUE(VerifyEqual(&hs0, kStr0)); |
| 124 | EXPECT_TRUE(VerifyEqual(&hs1, kStr0)); |
| 125 | |
| 126 | EXPECT_TRUE(VerifyNotEqual(&hs0, kStr1)); |
| 127 | EXPECT_TRUE(VerifyNotEqual(&hs1, kStr1)); |
| 128 | } |
| 129 | |
| 130 | TEST_F(HpackStringTest, MoveConstructor) { |
| 131 | Http2StringPiece sp0(kStr0); |
| 132 | HpackString hs0(sp0); |
| 133 | EXPECT_TRUE(VerifyEqual(&hs0, kStr0)); |
| 134 | EXPECT_TRUE(VerifyNotEqual(&hs0, "")); |
| 135 | |
| 136 | HpackString hs1(std::move(hs0)); |
| 137 | EXPECT_NE(hs0, hs1); |
| 138 | |
| 139 | EXPECT_TRUE(VerifyEqual(&hs1, kStr0)); |
| 140 | EXPECT_TRUE(VerifyEqual(&hs0, "")); |
| 141 | EXPECT_TRUE(VerifyNotEqual(&hs1, "")); |
| 142 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 143 | HTTP2_LOG(INFO) << hs0; |
| 144 | HTTP2_LOG(INFO) << hs1; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace |
| 148 | } // namespace test |
| 149 | } // namespace http2 |