Commit e5cfe692 authored by bnc's avatar bnc Committed by Commit bot

Implement StringPieceHasSubstrMatcher.

This makes it possible to define a HasSubstr() that is compatible with
base::StringPiece arguments, thus reducing divergence between upstream and
Chromium code.  (In the upstream codebase, base::StringPiece can be implicitly
converted into std::string, so there is no need for this workaround.)

Review-Url: https://codereview.chromium.org/2615383002
Cr-Commit-Position: refs/heads/master@{#442914}
parent 09a39242
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
// Tests of HpackWholeEntryBuffer: does it buffer correctly, and does it // Tests of HpackWholeEntryBuffer: does it buffer correctly, and does it
// detect Huffman decoding errors and oversize string errors? // detect Huffman decoding errors and oversize string errors?
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -15,7 +17,6 @@ using ::testing::AllOf; ...@@ -15,7 +17,6 @@ using ::testing::AllOf;
using ::testing::HasSubstr; using ::testing::HasSubstr;
using ::testing::InSequence; using ::testing::InSequence;
using ::testing::Property; using ::testing::Property;
using ::testing::SaveArg;
using ::testing::StrictMock; using ::testing::StrictMock;
using ::testing::_; using ::testing::_;
...@@ -25,6 +26,15 @@ namespace { ...@@ -25,6 +26,15 @@ namespace {
constexpr size_t kMaxStringSize = 20; constexpr size_t kMaxStringSize = 20;
// Define HasSubstr() for base::StringPiece arguments.
// This shadows ::testing::HasSubstr(), which only works on argument types
// that can be implicilty converted to a std::string.
inline ::testing::PolymorphicMatcher<StringPieceHasSubstrMatcher> HasSubstr(
const std::string& substring) {
return ::testing::MakePolymorphicMatcher(
StringPieceHasSubstrMatcher(substring));
}
class MockHpackWholeEntryListener : public HpackWholeEntryListener { class MockHpackWholeEntryListener : public HpackWholeEntryListener {
public: public:
~MockHpackWholeEntryListener() override {} ~MockHpackWholeEntryListener() override {}
...@@ -157,21 +167,15 @@ TEST_F(HpackWholeEntryBufferTest, OnLiteralNameAndValue) { ...@@ -157,21 +167,15 @@ TEST_F(HpackWholeEntryBufferTest, OnLiteralNameAndValue) {
// Verify that a name longer than the allowed size generates an error. // Verify that a name longer than the allowed size generates an error.
TEST_F(HpackWholeEntryBufferTest, NameTooLong) { TEST_F(HpackWholeEntryBufferTest, NameTooLong) {
entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 0); entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 0);
StringPiece error_message; EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry name")));
EXPECT_CALL(listener_, OnHpackDecodeError(_))
.WillOnce(SaveArg<0>(&error_message));
entry_buffer_.OnNameStart(false, kMaxStringSize + 1); entry_buffer_.OnNameStart(false, kMaxStringSize + 1);
EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry name"));
} }
// Verify that a name longer than the allowed size generates an error. // Verify that a name longer than the allowed size generates an error.
TEST_F(HpackWholeEntryBufferTest, ValueTooLong) { TEST_F(HpackWholeEntryBufferTest, ValueTooLong) {
entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 1); entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 1);
StringPiece error_message; EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry value")));
EXPECT_CALL(listener_, OnHpackDecodeError(_))
.WillOnce(SaveArg<0>(&error_message));
entry_buffer_.OnValueStart(false, kMaxStringSize + 1); entry_buffer_.OnValueStart(false, kMaxStringSize + 1);
EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry value"));
} }
// Verify that a Huffman encoded name with an explicit EOS generates an error // Verify that a Huffman encoded name with an explicit EOS generates an error
...@@ -183,12 +187,9 @@ TEST_F(HpackWholeEntryBufferTest, NameHuffmanError) { ...@@ -183,12 +187,9 @@ TEST_F(HpackWholeEntryBufferTest, NameHuffmanError) {
entry_buffer_.OnNameStart(true, 4); entry_buffer_.OnNameStart(true, 4);
entry_buffer_.OnNameData(data, 3); entry_buffer_.OnNameData(data, 3);
StringPiece error_message; EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry name")));
EXPECT_CALL(listener_, OnHpackDecodeError(_))
.WillOnce(SaveArg<0>(&error_message));
entry_buffer_.OnNameData(data, 1); entry_buffer_.OnNameData(data, 1);
EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry name"));
// After an error is reported, the listener is not called again. // After an error is reported, the listener is not called again.
EXPECT_CALL(listener_, OnDynamicTableSizeUpdate(8096)).Times(0); EXPECT_CALL(listener_, OnDynamicTableSizeUpdate(8096)).Times(0);
...@@ -204,12 +205,9 @@ TEST_F(HpackWholeEntryBufferTest, ValueeHuffmanError) { ...@@ -204,12 +205,9 @@ TEST_F(HpackWholeEntryBufferTest, ValueeHuffmanError) {
entry_buffer_.OnValueStart(true, 3); entry_buffer_.OnValueStart(true, 3);
entry_buffer_.OnValueData(data, 3); entry_buffer_.OnValueData(data, 3);
StringPiece error_message; EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry value")));
EXPECT_CALL(listener_, OnHpackDecodeError(_))
.WillOnce(SaveArg<0>(&error_message));
entry_buffer_.OnValueEnd(); entry_buffer_.OnValueEnd();
EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry value"));
// After an error is reported, the listener is not called again. // After an error is reported, the listener is not called again.
EXPECT_CALL(listener_, OnIndexedHeader(17)).Times(0); EXPECT_CALL(listener_, OnIndexedHeader(17)).Times(0);
......
...@@ -7,9 +7,14 @@ ...@@ -7,9 +7,14 @@
#ifndef NET_TEST_GTEST_UTIL_H_ #ifndef NET_TEST_GTEST_UTIL_H_
#define NET_TEST_GTEST_UTIL_H_ #define NET_TEST_GTEST_UTIL_H_
#include <string>
#include "base/macros.h"
#include "base/strings/string_piece.h"
#include "base/test/mock_log.h" #include "base/test/mock_log.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/test/scoped_disable_exit_on_dfatal.h" #include "net/test/scoped_disable_exit_on_dfatal.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -36,6 +41,34 @@ MATCHER(IsOk, ...@@ -36,6 +41,34 @@ MATCHER(IsOk,
return arg == net::OK; return arg == net::OK;
} }
// A gMock matcher for base::StringPiece arguments.
// gMock's built-in HasSubstrMatcher does not work,
// because base::StringPiece cannot be implicitly converted to std::string.
class StringPieceHasSubstrMatcher {
public:
explicit StringPieceHasSubstrMatcher(const std::string& substring)
: substring_(substring) {}
bool MatchAndExplain(base::StringPiece s,
::testing::MatchResultListener* listener) const {
return s.as_string().find(substring_) != std::string::npos;
}
// Describe what this matcher matches.
void DescribeTo(std::ostream* os) const {
*os << "has substring " << substring_;
}
void DescribeNegationTo(std::ostream* os) const {
*os << "has no substring " << substring_;
}
private:
const std::string substring_;
DISALLOW_ASSIGN(StringPieceHasSubstrMatcher);
};
// Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL
// macros. Do not use this directly. // macros. Do not use this directly.
#define GTEST_DFATAL_(statement, severity, matcher, fail) \ #define GTEST_DFATAL_(statement, severity, matcher, fail) \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment