1 // Copyright (c) 2020 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 #ifndef QUICHE_COMMON_TEST_TOOLS_QUICHE_TEST_UTILS_H_
6 #define QUICHE_COMMON_TEST_TOOLS_QUICHE_TEST_UTILS_H_
7
8 #include <string>
9
10 #include "absl/status/status.h"
11 #include "absl/status/statusor.h"
12 #include "absl/strings/string_view.h"
13 #include "quiche/common/platform/api/quiche_iovec.h"
14 #include "quiche/common/platform/api/quiche_test.h"
15
16 namespace quiche {
17 namespace test {
18
19 void CompareCharArraysWithHexError(const std::string& description,
20 const char* actual, const int actual_len,
21 const char* expected,
22 const int expected_len);
23
24 // Create iovec that points to that data that `str` points to.
25 iovec MakeIOVector(absl::string_view str);
26
27 // Due to binary size considerations, googleurl library can be built with or
28 // without IDNA support, meaning that we have to adjust our tests accordingly.
29 // This function checks if IDNAs are supported.
30 bool GoogleUrlSupportsIdnaForTest();
31
32 // Takes either a Status or StatusOr<T>, and returns just the Status.
ExtractStatus(const absl::Status & status)33 inline const absl::Status& ExtractStatus(const absl::Status& status) {
34 return status;
35 }
36 template <typename T>
ExtractStatus(const absl::StatusOr<T> & status_or)37 const absl::Status& ExtractStatus(const absl::StatusOr<T>& status_or) {
38 return status_or.status();
39 }
40
41 // Abseil does not provide absl::Status-related macros, so we have to provide
42 // those instead.
43 MATCHER(IsOk, "Checks if an instance of absl::Status is ok.") {
44 if (arg.ok()) {
45 return true;
46 }
47 *result_listener << "Expected status OK, got " << ExtractStatus(arg);
48 return false;
49 }
50
51 MATCHER_P(IsOkAndHolds, matcher,
52 "Matcher against the inner value of absl::StatusOr") {
53 if (!arg.ok()) {
54 *result_listener << "Expected status OK, got " << arg.status();
55 return false;
56 }
57 return ::testing::ExplainMatchResult(matcher, arg.value(), result_listener);
58 }
59
60 MATCHER_P(StatusIs, code, "Matcher against only a specific status code") {
61 if (ExtractStatus(arg).code() != code) {
62 *result_listener << "Expected status " << absl::StatusCodeToString(code)
63 << ", got " << ExtractStatus(arg);
64 return false;
65 }
66 return true;
67 }
68
69 MATCHER_P2(StatusIs, code, matcher, "Matcher against a specific status code") {
70 if (ExtractStatus(arg).code() != code) {
71 *result_listener << "Expected status " << absl::StatusCodeToString(code)
72 << ", got " << ExtractStatus(arg);
73 return false;
74 }
75 return ::testing::ExplainMatchResult(matcher, ExtractStatus(arg).message(),
76 result_listener);
77 }
78
79 #define QUICHE_EXPECT_OK(arg) EXPECT_THAT((arg), ::quiche::test::IsOk())
80 #define QUICHE_ASSERT_OK(arg) ASSERT_THAT((arg), ::quiche::test::IsOk())
81
82 } // namespace test
83 } // namespace quiche
84
85 #endif // QUICHE_COMMON_TEST_TOOLS_QUICHE_TEST_UTILS_H_
86