/proc/self/cwd/test/test_common.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2016 Google Inc. All Rights Reserved. |
2 | | * |
3 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | * you may not use this file except in compliance with the License. |
5 | | * You may obtain a copy of the License at |
6 | | * |
7 | | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | * |
9 | | * Unless required by applicable law or agreed to in writing, software |
10 | | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | * See the License for the specific language governing permissions and |
13 | | * limitations under the License. |
14 | | */ |
15 | | #ifndef GRPC_TRANSCODING_TEST_COMMON_H_ |
16 | | #define GRPC_TRANSCODING_TEST_COMMON_H_ |
17 | | |
18 | | #include <deque> |
19 | | #include <functional> |
20 | | #include <string> |
21 | | #include <vector> |
22 | | |
23 | | #include "google/api/service.pb.h" |
24 | | #include "google/protobuf/io/zero_copy_stream.h" |
25 | | #include "google/protobuf/text_format.h" |
26 | | #include "grpc_transcoding/transcoder_input_stream.h" |
27 | | #include "gtest/gtest.h" |
28 | | |
29 | | namespace google { |
30 | | namespace grpc { |
31 | | |
32 | | namespace transcoding { |
33 | | namespace testing { |
34 | | |
35 | | // An implementation of ZeroCopyInputStream for testing. |
36 | | // The tests define the chunks that TestZeroCopyInputStream produces. |
37 | | class TestZeroCopyInputStream : public TranscoderInputStream { |
38 | | public: |
39 | | TestZeroCopyInputStream(); |
40 | | |
41 | | // Add an input chunk |
42 | | void AddChunk(std::string chunk); |
43 | | |
44 | | // Ends the stream |
45 | 0 | void Finish() { finished_ = true; } |
46 | | |
47 | | // Returns whether Finish() has been called on this stream or not. |
48 | 1.55M | bool Finished() const { return finished_; } |
49 | | |
50 | | // ZeroCopyInputStream methods |
51 | | bool Next(const void** data, int* size); |
52 | | void BackUp(int count); |
53 | | int64_t BytesAvailable() const; |
54 | 87.4k | int64_t ByteCount() const { return 0; } // Not implemented |
55 | 0 | bool Skip(int) { return false; } // Not implemented |
56 | | |
57 | | private: |
58 | | std::deque<std::string> chunks_; |
59 | | bool finished_; |
60 | | |
61 | | int position_; |
62 | | std::string current_; |
63 | | }; |
64 | | |
65 | | // Test the translation test case with different partitions of the input. The |
66 | | // test will generate combinations of partitioning input into specified number |
67 | | // of chunks (chunk_count). For each of the input partition, test assertion is |
68 | | // verified. The partition is passed to the test assertion as a std::vector of |
69 | | // partitioning points in the input. |
70 | | // Because the number of partitionings is O(N^chunk_count) we use a coefficient |
71 | | // which controls which fraction of partitionings is generated and tested. |
72 | | // The process of generating partitionings is deterministic. |
73 | | // |
74 | | // chunk_count - the number of parts (chunks) in each partition |
75 | | // partitioning_coefficient - a real number in (0, 1] interval that defines how |
76 | | // exhaustive the test should be, i.e. what part of |
77 | | // all partitions of the input string should be |
78 | | // tested (1.0 means all partitions). |
79 | | // input - the input string |
80 | | // test - the test to run |
81 | | bool RunTestForInputPartitions( |
82 | | size_t chunk_count, double partitioning_coefficient, |
83 | | const std::string& input, |
84 | | std::function<bool(const std::vector<size_t>& t)> test); |
85 | | |
86 | | // Generate an input string of the specified size using the specified seed. |
87 | | std::string GenerateInput(const std::string& seed, size_t size); |
88 | | |
89 | | // Load service from a proto text file. Returns true if loading succeeds; |
90 | | // otherwise returns false. |
91 | | bool LoadService(const std::string& config_pb_txt_file, |
92 | | const std::string& testdata_path, |
93 | | ::google::api::Service* service); |
94 | | bool LoadService(const std::string& config_pb_txt_file, |
95 | | ::google::api::Service* service); |
96 | | |
97 | | // Parses the gRPC message delimiter and returns the size of the message. |
98 | | unsigned DelimiterToSize(const unsigned char* delimiter); |
99 | | |
100 | | // Generates a gRPC message delimiter with the given message size. |
101 | | std::string SizeToDelimiter(unsigned size); |
102 | | |
103 | | // Genereate a proto message with the gRPC delimiter from proto text |
104 | | template <class MessageType> |
105 | | std::string GenerateGrpcMessage(const std::string& proto_text) { |
106 | | // Parse the message from text & serialize to binary |
107 | | MessageType message; |
108 | | EXPECT_TRUE( |
109 | | ::google::protobuf::TextFormat::ParseFromString(proto_text, &message)); |
110 | | std::string binary; |
111 | | EXPECT_TRUE(message.SerializeToString(&binary)); |
112 | | |
113 | | // Now prefix the binary with a delimiter and return |
114 | | return SizeToDelimiter(binary.size()) + binary; |
115 | | } |
116 | | |
117 | | // Compares JSON objects |
118 | | bool ExpectJsonObjectEq(const std::string& expected, const std::string& actual); |
119 | | |
120 | | // Compares JSON arrays |
121 | | bool ExpectJsonArrayEq(const std::string& expected, const std::string& actual); |
122 | | |
123 | | // JSON array tester that supports matching partial arrays. |
124 | | class JsonArrayTester { |
125 | | public: |
126 | | // Tests a new element of the array. |
127 | | // expected - the expected new element of the array to match |
128 | | // actual - the actual JSON chunk (which will include "[", "]" or "," if |
129 | | // needed) |
130 | | bool TestElement(const std::string& expected, const std::string& actual); |
131 | | |
132 | | // Tests a new chunk of the array (potentially multiple elements). |
133 | | // expected - the expected new chunk of the array to match (including "[", "]" |
134 | | // or "," if needed) |
135 | | // actual - the actual JSON chunk (including "[", "]" or "," if needed) |
136 | | // closes - indicates whether the chunk closes the array or not. |
137 | | bool TestChunk(const std::string& expected, const std::string& actual, |
138 | | bool closes); |
139 | | |
140 | | // Test that the array is closed after adding the given JSON chunk (i.e. must |
141 | | // be "]" modulo whitespace) |
142 | | bool TestClosed(const std::string& actual); |
143 | | |
144 | | private: |
145 | | std::string expected_so_far_; |
146 | | std::string actual_so_far_; |
147 | | }; |
148 | | |
149 | | } // namespace testing |
150 | | } // namespace transcoding |
151 | | |
152 | | } // namespace grpc |
153 | | } // namespace google |
154 | | |
155 | | #endif // GRPC_TRANSCODING_MESSAGE_READER_H_ |