Splits nghttp2-specific test utilities into nghttp2_test_utils.{h,cc}.
PiperOrigin-RevId: 386327383
diff --git a/http2/adapter/callback_visitor_test.cc b/http2/adapter/callback_visitor_test.cc
index 695ad7f..0e3ad30 100644
--- a/http2/adapter/callback_visitor_test.cc
+++ b/http2/adapter/callback_visitor_test.cc
@@ -1,6 +1,7 @@
#include "http2/adapter/callback_visitor.h"
#include "http2/adapter/mock_nghttp2_callbacks.h"
+#include "http2/adapter/nghttp2_test_utils.h"
#include "http2/adapter/test_utils.h"
#include "common/platform/api/quiche_test.h"
diff --git a/http2/adapter/nghttp2_adapter_test.cc b/http2/adapter/nghttp2_adapter_test.cc
index 091f6e4..34ecebe 100644
--- a/http2/adapter/nghttp2_adapter_test.cc
+++ b/http2/adapter/nghttp2_adapter_test.cc
@@ -1,6 +1,7 @@
#include "http2/adapter/nghttp2_adapter.h"
#include "http2/adapter/mock_http2_visitor.h"
+#include "http2/adapter/nghttp2_test_utils.h"
#include "http2/adapter/test_frame_sequence.h"
#include "http2/adapter/test_utils.h"
#include "common/platform/api/quiche_test.h"
diff --git a/http2/adapter/nghttp2_test.cc b/http2/adapter/nghttp2_test.cc
index e489a99..9623487 100644
--- a/http2/adapter/nghttp2_test.cc
+++ b/http2/adapter/nghttp2_test.cc
@@ -1,6 +1,8 @@
#include "third_party/nghttp2/src/lib/includes/nghttp2/nghttp2.h"
+
#include "absl/strings/str_cat.h"
#include "http2/adapter/mock_nghttp2_callbacks.h"
+#include "http2/adapter/nghttp2_test_utils.h"
#include "http2/adapter/nghttp2_util.h"
#include "http2/adapter/test_frame_sequence.h"
#include "http2/adapter/test_utils.h"
diff --git a/http2/adapter/nghttp2_test_utils.cc b/http2/adapter/nghttp2_test_utils.cc
new file mode 100644
index 0000000..6543da7
--- /dev/null
+++ b/http2/adapter/nghttp2_test_utils.cc
@@ -0,0 +1,406 @@
+#include "http2/adapter/nghttp2_test_utils.h"
+
+#include "http2/adapter/nghttp2_util.h"
+#include "common/quiche_endian.h"
+
+namespace http2 {
+namespace adapter {
+namespace test {
+
+namespace {
+
+// Custom gMock matcher, used to implement HasFrameHeader().
+class FrameHeaderMatcher
+ : public testing::MatcherInterface<const nghttp2_frame_hd*> {
+ public:
+ FrameHeaderMatcher(int32_t streamid, uint8_t type,
+ const testing::Matcher<int> flags)
+ : stream_id_(streamid), type_(type), flags_(flags) {}
+
+ bool MatchAndExplain(const nghttp2_frame_hd* frame,
+ testing::MatchResultListener* listener) const override {
+ bool matched = true;
+ if (stream_id_ != frame->stream_id) {
+ *listener << "; expected stream " << stream_id_ << ", saw "
+ << frame->stream_id;
+ matched = false;
+ }
+ if (type_ != frame->type) {
+ *listener << "; expected frame type " << type_ << ", saw "
+ << static_cast<int>(frame->type);
+ matched = false;
+ }
+ if (!flags_.MatchAndExplain(frame->flags, listener)) {
+ matched = false;
+ }
+ return matched;
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a frame header with stream " << stream_id_ << ", type "
+ << type_ << ", ";
+ flags_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a frame header with stream " << stream_id_
+ << ", type " << type_ << ", ";
+ flags_.DescribeNegationTo(os);
+ }
+
+ private:
+ const int32_t stream_id_;
+ const int type_;
+ const testing::Matcher<int> flags_;
+};
+
+class DataMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ DataMatcher(const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<size_t> length,
+ const testing::Matcher<int> flags)
+ : stream_id_(stream_id), length_(length), flags_(flags) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_DATA) {
+ *listener << "; expected DATA frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ bool matched = true;
+ if (!stream_id_.MatchAndExplain(frame->hd.stream_id, listener)) {
+ matched = false;
+ }
+ if (!length_.MatchAndExplain(frame->hd.length, listener)) {
+ matched = false;
+ }
+ if (!flags_.MatchAndExplain(frame->hd.flags, listener)) {
+ matched = false;
+ }
+ return matched;
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a DATA frame, ";
+ stream_id_.DescribeTo(os);
+ length_.DescribeTo(os);
+ flags_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a DATA frame, ";
+ stream_id_.DescribeNegationTo(os);
+ length_.DescribeNegationTo(os);
+ flags_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<uint32_t> stream_id_;
+ const testing::Matcher<size_t> length_;
+ const testing::Matcher<int> flags_;
+};
+
+class HeadersMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ HeadersMatcher(const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<int> flags,
+ const testing::Matcher<int> category)
+ : stream_id_(stream_id), flags_(flags), category_(category) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_HEADERS) {
+ *listener << "; expected HEADERS frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ bool matched = true;
+ if (!stream_id_.MatchAndExplain(frame->hd.stream_id, listener)) {
+ matched = false;
+ }
+ if (!flags_.MatchAndExplain(frame->hd.flags, listener)) {
+ matched = false;
+ }
+ if (!category_.MatchAndExplain(frame->headers.cat, listener)) {
+ matched = false;
+ }
+ return matched;
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a HEADERS frame, ";
+ stream_id_.DescribeTo(os);
+ flags_.DescribeTo(os);
+ category_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a HEADERS frame, ";
+ stream_id_.DescribeNegationTo(os);
+ flags_.DescribeNegationTo(os);
+ category_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<uint32_t> stream_id_;
+ const testing::Matcher<int> flags_;
+ const testing::Matcher<int> category_;
+};
+
+class RstStreamMatcher
+ : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ RstStreamMatcher(const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<uint32_t> error_code)
+ : stream_id_(stream_id), error_code_(error_code) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_RST_STREAM) {
+ *listener << "; expected RST_STREAM frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ bool matched = true;
+ if (!stream_id_.MatchAndExplain(frame->hd.stream_id, listener)) {
+ matched = false;
+ }
+ if (!error_code_.MatchAndExplain(frame->rst_stream.error_code, listener)) {
+ matched = false;
+ }
+ return matched;
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a RST_STREAM frame, ";
+ stream_id_.DescribeTo(os);
+ error_code_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a RST_STREAM frame, ";
+ stream_id_.DescribeNegationTo(os);
+ error_code_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<uint32_t> stream_id_;
+ const testing::Matcher<uint32_t> error_code_;
+};
+
+class SettingsMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ SettingsMatcher(const testing::Matcher<std::vector<Http2Setting>> values)
+ : values_(values) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_SETTINGS) {
+ *listener << "; expected SETTINGS frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ std::vector<Http2Setting> settings;
+ settings.reserve(frame->settings.niv);
+ for (int i = 0; i < frame->settings.niv; ++i) {
+ const auto& p = frame->settings.iv[i];
+ settings.push_back({static_cast<uint16_t>(p.settings_id), p.value});
+ }
+ return values_.MatchAndExplain(settings, listener);
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a SETTINGS frame, ";
+ values_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a SETTINGS frame, ";
+ values_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<std::vector<Http2Setting>> values_;
+};
+
+class PingMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ PingMatcher(const testing::Matcher<uint64_t> id, bool is_ack)
+ : id_(id), is_ack_(is_ack) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_PING) {
+ *listener << "; expected PING frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ bool matched = true;
+ bool frame_ack = frame->hd.flags & NGHTTP2_FLAG_ACK;
+ if (is_ack_ != frame_ack) {
+ *listener << "; expected is_ack=" << is_ack_ << ", saw " << frame_ack;
+ matched = false;
+ }
+ uint64_t data;
+ std::memcpy(&data, frame->ping.opaque_data, sizeof(data));
+ data = quiche::QuicheEndian::HostToNet64(data);
+ if (!id_.MatchAndExplain(data, listener)) {
+ matched = false;
+ }
+ return matched;
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a PING frame, ";
+ id_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a PING frame, ";
+ id_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<uint64_t> id_;
+ const bool is_ack_;
+};
+
+class GoAwayMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ GoAwayMatcher(const testing::Matcher<uint32_t> last_stream_id,
+ const testing::Matcher<uint32_t> error_code,
+ const testing::Matcher<absl::string_view> opaque_data)
+ : last_stream_id_(last_stream_id),
+ error_code_(error_code),
+ opaque_data_(opaque_data) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_GOAWAY) {
+ *listener << "; expected GOAWAY frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ bool matched = true;
+ if (!last_stream_id_.MatchAndExplain(frame->goaway.last_stream_id,
+ listener)) {
+ matched = false;
+ }
+ if (!error_code_.MatchAndExplain(frame->goaway.error_code, listener)) {
+ matched = false;
+ }
+ auto opaque_data =
+ ToStringView(frame->goaway.opaque_data, frame->goaway.opaque_data_len);
+ if (!opaque_data_.MatchAndExplain(opaque_data, listener)) {
+ matched = false;
+ }
+ return matched;
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a GOAWAY frame, ";
+ last_stream_id_.DescribeTo(os);
+ error_code_.DescribeTo(os);
+ opaque_data_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a GOAWAY frame, ";
+ last_stream_id_.DescribeNegationTo(os);
+ error_code_.DescribeNegationTo(os);
+ opaque_data_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<uint32_t> last_stream_id_;
+ const testing::Matcher<uint32_t> error_code_;
+ const testing::Matcher<absl::string_view> opaque_data_;
+};
+
+class WindowUpdateMatcher
+ : public testing::MatcherInterface<const nghttp2_frame*> {
+ public:
+ WindowUpdateMatcher(const testing::Matcher<uint32_t> delta) : delta_(delta) {}
+
+ bool MatchAndExplain(const nghttp2_frame* frame,
+ testing::MatchResultListener* listener) const override {
+ if (frame->hd.type != NGHTTP2_WINDOW_UPDATE) {
+ *listener << "; expected WINDOW_UPDATE frame, saw frame of type "
+ << static_cast<int>(frame->hd.type);
+ return false;
+ }
+ return delta_.MatchAndExplain(frame->window_update.window_size_increment,
+ listener);
+ }
+
+ void DescribeTo(std::ostream* os) const override {
+ *os << "contains a WINDOW_UPDATE frame, ";
+ delta_.DescribeTo(os);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const override {
+ *os << "does not contain a WINDOW_UPDATE frame, ";
+ delta_.DescribeNegationTo(os);
+ }
+
+ private:
+ const testing::Matcher<uint32_t> delta_;
+};
+
+} // namespace
+
+testing::Matcher<const nghttp2_frame_hd*> HasFrameHeader(
+ uint32_t streamid, uint8_t type, const testing::Matcher<int> flags) {
+ return MakeMatcher(new FrameHeaderMatcher(streamid, type, flags));
+}
+
+testing::Matcher<const nghttp2_frame*> IsData(
+ const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<size_t> length, const testing::Matcher<int> flags) {
+ return MakeMatcher(new DataMatcher(stream_id, length, flags));
+}
+
+testing::Matcher<const nghttp2_frame*> IsHeaders(
+ const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<int> flags, const testing::Matcher<int> category) {
+ return MakeMatcher(new HeadersMatcher(stream_id, flags, category));
+}
+
+testing::Matcher<const nghttp2_frame*> IsRstStream(
+ const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<uint32_t> error_code) {
+ return MakeMatcher(new RstStreamMatcher(stream_id, error_code));
+}
+
+testing::Matcher<const nghttp2_frame*> IsSettings(
+ const testing::Matcher<std::vector<Http2Setting>> values) {
+ return MakeMatcher(new SettingsMatcher(values));
+}
+
+testing::Matcher<const nghttp2_frame*> IsPing(
+ const testing::Matcher<uint64_t> id) {
+ return MakeMatcher(new PingMatcher(id, false));
+}
+
+testing::Matcher<const nghttp2_frame*> IsPingAck(
+ const testing::Matcher<uint64_t> id) {
+ return MakeMatcher(new PingMatcher(id, true));
+}
+
+testing::Matcher<const nghttp2_frame*> IsGoAway(
+ const testing::Matcher<uint32_t> last_stream_id,
+ const testing::Matcher<uint32_t> error_code,
+ const testing::Matcher<absl::string_view> opaque_data) {
+ return MakeMatcher(
+ new GoAwayMatcher(last_stream_id, error_code, opaque_data));
+}
+
+testing::Matcher<const nghttp2_frame*> IsWindowUpdate(
+ const testing::Matcher<uint32_t> delta) {
+ return MakeMatcher(new WindowUpdateMatcher(delta));
+}
+
+} // namespace test
+} // namespace adapter
+} // namespace http2
diff --git a/http2/adapter/nghttp2_test_utils.h b/http2/adapter/nghttp2_test_utils.h
new file mode 100644
index 0000000..029e2c9
--- /dev/null
+++ b/http2/adapter/nghttp2_test_utils.h
@@ -0,0 +1,96 @@
+#ifndef QUICHE_HTTP2_ADAPTER_NGHTTP2_TEST_UTILS_H_
+#define QUICHE_HTTP2_ADAPTER_NGHTTP2_TEST_UTILS_H_
+
+#include <vector>
+
+#include "absl/strings/string_view.h"
+#include "http2/adapter/http2_protocol.h"
+#include "third_party/nghttp2/src/lib/includes/nghttp2/nghttp2.h"
+#include "common/platform/api/quiche_test.h"
+
+namespace http2 {
+namespace adapter {
+namespace test {
+
+// A simple class that can easily be adapted to act as a nghttp2_data_source.
+class TestDataSource {
+ public:
+ explicit TestDataSource(absl::string_view data) : data_(std::string(data)) {}
+
+ absl::string_view ReadNext(size_t size) {
+ const size_t to_send = std::min(size, remaining_.size());
+ auto ret = remaining_.substr(0, to_send);
+ remaining_.remove_prefix(to_send);
+ return ret;
+ }
+
+ size_t SelectPayloadLength(size_t max_length) {
+ return std::min(max_length, remaining_.size());
+ }
+
+ nghttp2_data_provider MakeDataProvider() {
+ return nghttp2_data_provider{
+ .source = {.ptr = this},
+ .read_callback = [](nghttp2_session*, int32_t, uint8_t*, size_t length,
+ uint32_t* data_flags, nghttp2_data_source* source,
+ void*) -> ssize_t {
+ *data_flags |= NGHTTP2_DATA_FLAG_NO_COPY;
+ auto* s = static_cast<TestDataSource*>(source->ptr);
+ if (!s->is_data_available()) {
+ return NGHTTP2_ERR_DEFERRED;
+ }
+ const ssize_t ret = s->SelectPayloadLength(length);
+ if (ret < length) {
+ *data_flags |= NGHTTP2_DATA_FLAG_EOF;
+ }
+ return ret;
+ }};
+ }
+
+ bool is_data_available() const { return is_data_available_; }
+ void set_is_data_available(bool value) { is_data_available_ = value; }
+
+ private:
+ const std::string data_;
+ absl::string_view remaining_ = data_;
+ bool is_data_available_ = true;
+};
+
+// Matchers for nghttp2 data types.
+testing::Matcher<const nghttp2_frame_hd*> HasFrameHeader(
+ uint32_t streamid, uint8_t type, const testing::Matcher<int> flags);
+
+testing::Matcher<const nghttp2_frame*> IsData(
+ const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<size_t> length, const testing::Matcher<int> flags);
+
+testing::Matcher<const nghttp2_frame*> IsHeaders(
+ const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<int> flags, const testing::Matcher<int> category);
+
+testing::Matcher<const nghttp2_frame*> IsRstStream(
+ const testing::Matcher<uint32_t> stream_id,
+ const testing::Matcher<uint32_t> error_code);
+
+testing::Matcher<const nghttp2_frame*> IsSettings(
+ const testing::Matcher<std::vector<Http2Setting>> values);
+
+testing::Matcher<const nghttp2_frame*> IsPing(
+ const testing::Matcher<uint64_t> id);
+
+testing::Matcher<const nghttp2_frame*> IsPingAck(
+ const testing::Matcher<uint64_t> id);
+
+testing::Matcher<const nghttp2_frame*> IsGoAway(
+ const testing::Matcher<uint32_t> last_stream_id,
+ const testing::Matcher<uint32_t> error_code,
+ const testing::Matcher<absl::string_view> opaque_data);
+
+testing::Matcher<const nghttp2_frame*> IsWindowUpdate(
+ const testing::Matcher<uint32_t> delta);
+
+} // namespace test
+} // namespace adapter
+} // namespace http2
+
+#endif // QUICHE_HTTP2_ADAPTER_NGHTTP2_TEST_UTILS_H_
diff --git a/http2/adapter/nghttp2_util_test.cc b/http2/adapter/nghttp2_util_test.cc
index f6610ae..fda0063 100644
--- a/http2/adapter/nghttp2_util_test.cc
+++ b/http2/adapter/nghttp2_util_test.cc
@@ -1,5 +1,6 @@
#include "http2/adapter/nghttp2_util.h"
+#include "http2/adapter/nghttp2_test_utils.h"
#include "http2/adapter/test_utils.h"
#include "common/platform/api/quiche_test.h"
diff --git a/http2/adapter/test_utils.cc b/http2/adapter/test_utils.cc
index e0dfec6..defd56e 100644
--- a/http2/adapter/test_utils.cc
+++ b/http2/adapter/test_utils.cc
@@ -1,6 +1,5 @@
#include "http2/adapter/test_utils.h"
-#include "http2/adapter/nghttp2_util.h"
#include "common/quiche_endian.h"
#include "spdy/core/spdy_frame_reader.h"
@@ -185,346 +184,6 @@
const std::vector<TypeAndOptionalLength> expected_types_and_lengths_;
};
-// Custom gMock matcher, used to implement HasFrameHeader().
-class FrameHeaderMatcher
- : public testing::MatcherInterface<const nghttp2_frame_hd*> {
- public:
- FrameHeaderMatcher(int32_t streamid,
- uint8_t type,
- const testing::Matcher<int> flags)
- : stream_id_(streamid), type_(type), flags_(flags) {}
-
- bool MatchAndExplain(const nghttp2_frame_hd* frame,
- testing::MatchResultListener* listener) const override {
- bool matched = true;
- if (stream_id_ != frame->stream_id) {
- *listener << "; expected stream " << stream_id_ << ", saw "
- << frame->stream_id;
- matched = false;
- }
- if (type_ != frame->type) {
- *listener << "; expected frame type " << type_ << ", saw "
- << static_cast<int>(frame->type);
- matched = false;
- }
- if (!flags_.MatchAndExplain(frame->flags, listener)) {
- matched = false;
- }
- return matched;
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a frame header with stream " << stream_id_ << ", type "
- << type_ << ", ";
- flags_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a frame header with stream " << stream_id_
- << ", type " << type_ << ", ";
- flags_.DescribeNegationTo(os);
- }
-
- private:
- const int32_t stream_id_;
- const int type_;
- const testing::Matcher<int> flags_;
-};
-
-class DataMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- DataMatcher(const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<size_t> length,
- const testing::Matcher<int> flags)
- : stream_id_(stream_id), length_(length), flags_(flags) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_DATA) {
- *listener << "; expected DATA frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- bool matched = true;
- if (!stream_id_.MatchAndExplain(frame->hd.stream_id, listener)) {
- matched = false;
- }
- if (!length_.MatchAndExplain(frame->hd.length, listener)) {
- matched = false;
- }
- if (!flags_.MatchAndExplain(frame->hd.flags, listener)) {
- matched = false;
- }
- return matched;
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a DATA frame, ";
- stream_id_.DescribeTo(os);
- length_.DescribeTo(os);
- flags_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a DATA frame, ";
- stream_id_.DescribeNegationTo(os);
- length_.DescribeNegationTo(os);
- flags_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<uint32_t> stream_id_;
- const testing::Matcher<size_t> length_;
- const testing::Matcher<int> flags_;
-};
-
-class HeadersMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- HeadersMatcher(const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<int> flags,
- const testing::Matcher<int> category)
- : stream_id_(stream_id), flags_(flags), category_(category) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_HEADERS) {
- *listener << "; expected HEADERS frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- bool matched = true;
- if (!stream_id_.MatchAndExplain(frame->hd.stream_id, listener)) {
- matched = false;
- }
- if (!flags_.MatchAndExplain(frame->hd.flags, listener)) {
- matched = false;
- }
- if (!category_.MatchAndExplain(frame->headers.cat, listener)) {
- matched = false;
- }
- return matched;
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a HEADERS frame, ";
- stream_id_.DescribeTo(os);
- flags_.DescribeTo(os);
- category_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a HEADERS frame, ";
- stream_id_.DescribeNegationTo(os);
- flags_.DescribeNegationTo(os);
- category_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<uint32_t> stream_id_;
- const testing::Matcher<int> flags_;
- const testing::Matcher<int> category_;
-};
-
-class RstStreamMatcher
- : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- RstStreamMatcher(const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<uint32_t> error_code)
- : stream_id_(stream_id), error_code_(error_code) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_RST_STREAM) {
- *listener << "; expected RST_STREAM frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- bool matched = true;
- if (!stream_id_.MatchAndExplain(frame->hd.stream_id, listener)) {
- matched = false;
- }
- if (!error_code_.MatchAndExplain(frame->rst_stream.error_code, listener)) {
- matched = false;
- }
- return matched;
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a RST_STREAM frame, ";
- stream_id_.DescribeTo(os);
- error_code_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a RST_STREAM frame, ";
- stream_id_.DescribeNegationTo(os);
- error_code_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<uint32_t> stream_id_;
- const testing::Matcher<uint32_t> error_code_;
-};
-
-class SettingsMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- SettingsMatcher(const testing::Matcher<std::vector<Http2Setting>> values)
- : values_(values) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_SETTINGS) {
- *listener << "; expected SETTINGS frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- std::vector<Http2Setting> settings;
- settings.reserve(frame->settings.niv);
- for (int i = 0; i < frame->settings.niv; ++i) {
- const auto& p = frame->settings.iv[i];
- settings.push_back({static_cast<uint16_t>(p.settings_id), p.value});
- }
- return values_.MatchAndExplain(settings, listener);
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a SETTINGS frame, ";
- values_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a SETTINGS frame, ";
- values_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<std::vector<Http2Setting>> values_;
-};
-
-class PingMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- PingMatcher(const testing::Matcher<uint64_t> id, bool is_ack)
- : id_(id), is_ack_(is_ack) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_PING) {
- *listener << "; expected PING frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- bool matched = true;
- bool frame_ack = frame->hd.flags & NGHTTP2_FLAG_ACK;
- if (is_ack_ != frame_ack) {
- *listener << "; expected is_ack=" << is_ack_ << ", saw " << frame_ack;
- matched = false;
- }
- uint64_t data;
- std::memcpy(&data, frame->ping.opaque_data, sizeof(data));
- data = quiche::QuicheEndian::HostToNet64(data);
- if (!id_.MatchAndExplain(data, listener)) {
- matched = false;
- }
- return matched;
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a PING frame, ";
- id_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a PING frame, ";
- id_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<uint64_t> id_;
- const bool is_ack_;
-};
-
-class GoAwayMatcher : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- GoAwayMatcher(const testing::Matcher<uint32_t> last_stream_id,
- const testing::Matcher<uint32_t> error_code,
- const testing::Matcher<absl::string_view> opaque_data)
- : last_stream_id_(last_stream_id),
- error_code_(error_code),
- opaque_data_(opaque_data) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_GOAWAY) {
- *listener << "; expected GOAWAY frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- bool matched = true;
- if (!last_stream_id_.MatchAndExplain(frame->goaway.last_stream_id,
- listener)) {
- matched = false;
- }
- if (!error_code_.MatchAndExplain(frame->goaway.error_code, listener)) {
- matched = false;
- }
- auto opaque_data =
- ToStringView(frame->goaway.opaque_data, frame->goaway.opaque_data_len);
- if (!opaque_data_.MatchAndExplain(opaque_data, listener)) {
- matched = false;
- }
- return matched;
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a GOAWAY frame, ";
- last_stream_id_.DescribeTo(os);
- error_code_.DescribeTo(os);
- opaque_data_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a GOAWAY frame, ";
- last_stream_id_.DescribeNegationTo(os);
- error_code_.DescribeNegationTo(os);
- opaque_data_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<uint32_t> last_stream_id_;
- const testing::Matcher<uint32_t> error_code_;
- const testing::Matcher<absl::string_view> opaque_data_;
-};
-
-class WindowUpdateMatcher
- : public testing::MatcherInterface<const nghttp2_frame*> {
- public:
- WindowUpdateMatcher(const testing::Matcher<uint32_t> delta) : delta_(delta) {}
-
- bool MatchAndExplain(const nghttp2_frame* frame,
- testing::MatchResultListener* listener) const override {
- if (frame->hd.type != NGHTTP2_WINDOW_UPDATE) {
- *listener << "; expected WINDOW_UPDATE frame, saw frame of type "
- << static_cast<int>(frame->hd.type);
- return false;
- }
- return delta_.MatchAndExplain(frame->window_update.window_size_increment,
- listener);
- }
-
- void DescribeTo(std::ostream* os) const override {
- *os << "contains a WINDOW_UPDATE frame, ";
- delta_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "does not contain a WINDOW_UPDATE frame, ";
- delta_.DescribeNegationTo(os);
- }
-
- private:
- const testing::Matcher<uint32_t> delta_;
-};
-
} // namespace
testing::Matcher<absl::string_view> EqualsFrames(
@@ -544,61 +203,6 @@
return MakeMatcher(new SpdyControlFrameMatcher(std::move(types_and_lengths)));
}
-testing::Matcher<const nghttp2_frame_hd*> HasFrameHeader(
- uint32_t streamid,
- uint8_t type,
- const testing::Matcher<int> flags) {
- return MakeMatcher(new FrameHeaderMatcher(streamid, type, flags));
-}
-
-testing::Matcher<const nghttp2_frame*> IsData(
- const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<size_t> length,
- const testing::Matcher<int> flags) {
- return MakeMatcher(new DataMatcher(stream_id, length, flags));
-}
-
-testing::Matcher<const nghttp2_frame*> IsHeaders(
- const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<int> flags,
- const testing::Matcher<int> category) {
- return MakeMatcher(new HeadersMatcher(stream_id, flags, category));
-}
-
-testing::Matcher<const nghttp2_frame*> IsRstStream(
- const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<uint32_t> error_code) {
- return MakeMatcher(new RstStreamMatcher(stream_id, error_code));
-}
-
-testing::Matcher<const nghttp2_frame*> IsSettings(
- const testing::Matcher<std::vector<Http2Setting>> values) {
- return MakeMatcher(new SettingsMatcher(values));
-}
-
-testing::Matcher<const nghttp2_frame*> IsPing(
- const testing::Matcher<uint64_t> id) {
- return MakeMatcher(new PingMatcher(id, false));
-}
-
-testing::Matcher<const nghttp2_frame*> IsPingAck(
- const testing::Matcher<uint64_t> id) {
- return MakeMatcher(new PingMatcher(id, true));
-}
-
-testing::Matcher<const nghttp2_frame*> IsGoAway(
- const testing::Matcher<uint32_t> last_stream_id,
- const testing::Matcher<uint32_t> error_code,
- const testing::Matcher<absl::string_view> opaque_data) {
- return MakeMatcher(
- new GoAwayMatcher(last_stream_id, error_code, opaque_data));
-}
-
-testing::Matcher<const nghttp2_frame*> IsWindowUpdate(
- const testing::Matcher<uint32_t> delta) {
- return MakeMatcher(new WindowUpdateMatcher(delta));
-}
-
} // namespace test
} // namespace adapter
} // namespace http2
diff --git a/http2/adapter/test_utils.h b/http2/adapter/test_utils.h
index 1d58669..cea7d61 100644
--- a/http2/adapter/test_utils.h
+++ b/http2/adapter/test_utils.h
@@ -8,7 +8,6 @@
#include "http2/adapter/data_source.h"
#include "http2/adapter/http2_protocol.h"
#include "http2/adapter/mock_http2_visitor.h"
-#include "third_party/nghttp2/src/lib/includes/nghttp2/nghttp2.h"
#include "common/platform/api/quiche_test.h"
#include "spdy/core/spdy_protocol.h"
@@ -67,50 +66,6 @@
bool is_data_available_ = true;
};
-// A simple class that can easily be adapted to act as a nghttp2_data_source.
-class TestDataSource {
- public:
- explicit TestDataSource(absl::string_view data) : data_(std::string(data)) {}
-
- absl::string_view ReadNext(size_t size) {
- const size_t to_send = std::min(size, remaining_.size());
- auto ret = remaining_.substr(0, to_send);
- remaining_.remove_prefix(to_send);
- return ret;
- }
-
- size_t SelectPayloadLength(size_t max_length) {
- return std::min(max_length, remaining_.size());
- }
-
- nghttp2_data_provider MakeDataProvider() {
- return nghttp2_data_provider{
- .source = {.ptr = this},
- .read_callback = [](nghttp2_session*, int32_t, uint8_t*, size_t length,
- uint32_t* data_flags, nghttp2_data_source* source,
- void*) -> ssize_t {
- *data_flags |= NGHTTP2_DATA_FLAG_NO_COPY;
- auto* s = static_cast<TestDataSource*>(source->ptr);
- if (!s->is_data_available()) {
- return NGHTTP2_ERR_DEFERRED;
- }
- const ssize_t ret = s->SelectPayloadLength(length);
- if (ret < length) {
- *data_flags |= NGHTTP2_DATA_FLAG_EOF;
- }
- return ret;
- }};
- }
-
- bool is_data_available() const { return is_data_available_; }
- void set_is_data_available(bool value) { is_data_available_ = value; }
-
- private:
- const std::string data_;
- absl::string_view remaining_ = data_;
- bool is_data_available_ = true;
-};
-
// These matchers check whether a string consists entirely of HTTP/2 frames of
// the specified ordered sequence. This is useful in tests where we want to show
// that one or more particular frame types are serialized for sending to the
@@ -126,42 +81,6 @@
testing::Matcher<absl::string_view> EqualsFrames(
std::vector<spdy::SpdyFrameType> types);
-testing::Matcher<const nghttp2_frame_hd*> HasFrameHeader(
- uint32_t streamid,
- uint8_t type,
- const testing::Matcher<int> flags);
-
-testing::Matcher<const nghttp2_frame*> IsData(
- const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<size_t> length,
- const testing::Matcher<int> flags);
-
-testing::Matcher<const nghttp2_frame*> IsHeaders(
- const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<int> flags,
- const testing::Matcher<int> category);
-
-testing::Matcher<const nghttp2_frame*> IsRstStream(
- const testing::Matcher<uint32_t> stream_id,
- const testing::Matcher<uint32_t> error_code);
-
-testing::Matcher<const nghttp2_frame*> IsSettings(
- const testing::Matcher<std::vector<Http2Setting>> values);
-
-testing::Matcher<const nghttp2_frame*> IsPing(
- const testing::Matcher<uint64_t> id);
-
-testing::Matcher<const nghttp2_frame*> IsPingAck(
- const testing::Matcher<uint64_t> id);
-
-testing::Matcher<const nghttp2_frame*> IsGoAway(
- const testing::Matcher<uint32_t> last_stream_id,
- const testing::Matcher<uint32_t> error_code,
- const testing::Matcher<absl::string_view> opaque_data);
-
-testing::Matcher<const nghttp2_frame*> IsWindowUpdate(
- const testing::Matcher<uint32_t> delta);
-
} // namespace test
} // namespace adapter
} // namespace http2