blob: 907e57b00864da2bbc149e911a9deee5ff9cc864 [file] [log] [blame]
David Benjamin06b94de2015-05-09 22:46:47 -04001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include "file_test.h"
16
17#include <ctype.h>
18#include <errno.h>
19#include <stdarg.h>
20#include <string.h>
21
22#include "stl_compat.h"
23
24
25FileTest::FileTest(const char *path) {
26 file_ = fopen(path, "r");
27 if (file_ == nullptr) {
28 fprintf(stderr, "Could not open file %s: %s.\n", path, strerror(errno));
29 }
30}
31
32FileTest::~FileTest() {
33 if (file_ != nullptr) {
34 fclose(file_);
35 }
36}
37
38// FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
39// if there is none.
40static const char *FindDelimiter(const char *str) {
41 while (*str) {
42 if (*str == ':' || *str == '=') {
43 return str;
44 }
45 str++;
46 }
47 return nullptr;
48}
49
50// StripSpace returns a string containing up to |len| characters from |str| with
51// leading and trailing whitespace removed.
52static std::string StripSpace(const char *str, size_t len) {
53 // Remove leading space.
54 while (len > 0 && isspace(*str)) {
55 str++;
56 len--;
57 }
58 while (len > 0 && isspace(str[len-1])) {
59 len--;
60 }
61 return std::string(str, len);
62}
63
64FileTest::ReadResult FileTest::ReadNext() {
65 // If the previous test had unused attributes or block, it is an error.
66 if (!unused_attributes_.empty()) {
67 for (const std::string &key : unused_attributes_) {
68 PrintLine("Unused attribute: %s", key.c_str());
69 }
70 return kReadError;
71 }
72 if (!block_.empty() && !used_block_) {
73 PrintLine("Unused block");
74 return kReadError;
75 }
76
77 ClearTest();
78
79 bool in_block = false;
80 while (true) {
81 // Read the next line.
82 char buf[4096];
83 if (fgets(buf, sizeof(buf), file_) == nullptr) {
84 if (feof(file_)) {
85 if (in_block) {
86 fprintf(stderr, "Unterminated block.\n");
87 return kReadError;
88 }
89 // EOF is a valid terminator for a test.
90 return start_line_ > 0 ? kReadSuccess : kReadEOF;
91 }
92 fprintf(stderr, "Error reading from input.\n");
93 return kReadError;
94 }
95
96 line_++;
97 size_t len = strlen(buf);
98 // Check for truncation.
99 if (len > 0 && buf[len - 1] != '\n' && !feof(file_)) {
100 fprintf(stderr, "Line %u too long.\n", line_);
101 return kReadError;
102 }
103
104 bool is_delimiter = strncmp(buf, "---", 3) == 0;
105 if (in_block) {
106 block_ += buf;
107 if (is_delimiter) {
108 // Ending the block completes the test.
109 return kReadSuccess;
110 }
111 } else if (is_delimiter) {
112 if (start_line_ == 0) {
113 fprintf(stderr, "Line %u: Unexpected block.\n", line_);
114 return kReadError;
115 }
116 in_block = true;
117 block_ += buf;
118 } else if (buf[0] == '\n' || buf[0] == '\0') {
119 // Empty lines delimit tests.
120 if (start_line_ > 0) {
121 return kReadSuccess;
122 }
123 } else if (buf[0] != '#') { // Comment lines are ignored.
124 // Parse the line as an attribute.
125 const char *delimiter = FindDelimiter(buf);
126 if (delimiter == nullptr) {
127 fprintf(stderr, "Line %u: Could not parse attribute.\n", line_);
128 }
129 std::string key = StripSpace(buf, delimiter - buf);
130 std::string value = StripSpace(delimiter + 1,
131 buf + len - delimiter - 1);
132
133 unused_attributes_.insert(key);
134 attributes_[key] = value;
135 if (start_line_ == 0) {
136 // This is the start of a test.
137 type_ = key;
138 parameter_ = value;
139 start_line_ = line_;
140 }
141 }
142 }
143}
144
145void FileTest::PrintLine(const char *format, ...) {
146 va_list args;
147 va_start(args, format);
148
149 fprintf(stderr, "Line %u: ", start_line_);
150 vfprintf(stderr, format, args);
151 fprintf(stderr, "\n");
152
153 va_end(args);
154}
155
156const std::string &FileTest::GetType() {
157 OnKeyUsed(type_);
158 return type_;
159}
160
161const std::string &FileTest::GetParameter() {
162 OnKeyUsed(type_);
163 return parameter_;
164}
165
166const std::string &FileTest::GetBlock() {
167 used_block_ = true;
168 return block_;
169}
170
171bool FileTest::HasAttribute(const std::string &key) {
172 OnKeyUsed(key);
173 return attributes_.count(key) > 0;
174}
175
176bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
177 OnKeyUsed(key);
178 auto iter = attributes_.find(key);
179 if (iter == attributes_.end()) {
180 PrintLine("Missing attribute '%s'.", key.c_str());
181 return false;
182 }
183 *out_value = iter->second;
184 return true;
185}
186
187static bool FromHexDigit(uint8_t *out, char c) {
188 if ('0' <= c && c <= '9') {
189 *out = c - '0';
190 return true;
191 }
192 if ('a' <= c && c <= 'f') {
193 *out = c - 'a' + 10;
194 return true;
195 }
196 if ('A' <= c && c <= 'F') {
197 *out = c - 'A' + 10;
198 return true;
199 }
200 return false;
201}
202
203bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
204 std::string value;
205 if (!GetAttribute(&value, key)) {
206 return false;
207 }
208
209 if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
210 out->assign(value.begin() + 1, value.end() - 1);
211 return true;
212 }
213
214 if (value.size() % 2 != 0) {
215 PrintLine("Error decoding value: %s", value.c_str());
216 return false;
217 }
218 out->reserve(value.size() / 2);
219 for (size_t i = 0; i < value.size(); i += 2) {
220 uint8_t hi, lo;
221 if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i+1])) {
222 PrintLine("Error decoding value: %s", value.c_str());
223 return false;
224 }
225 out->push_back((hi << 4) | lo);
226 }
227 return true;
228}
229
230static std::string EncodeHex(const uint8_t *in, size_t in_len) {
231 static const char kHexDigits[] = "0123456789abcdef";
232 std::string ret;
233 ret.reserve(in_len * 2);
234 for (size_t i = 0; i < in_len; i++) {
235 ret += kHexDigits[in[i] >> 4];
236 ret += kHexDigits[in[i] & 0xf];
237 }
238 return ret;
239}
240
241bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
242 const uint8_t *actual, size_t actual_len) {
243 if (expected_len == actual_len &&
244 memcmp(expected, actual, expected_len) == 0) {
245 return true;
246 }
247
248 std::string expected_hex = EncodeHex(expected, expected_len);
249 std::string actual_hex = EncodeHex(actual, actual_len);
250 PrintLine("Expected: %s", expected_hex.c_str());
251 PrintLine("Actual: %s", actual_hex.c_str());
252 return false;
253}
254
255void FileTest::ClearTest() {
256 start_line_ = 0;
257 type_.clear();
258 parameter_.clear();
259 attributes_.clear();
260 block_.clear();
261 unused_attributes_.clear();
262 used_block_ = false;
263}
264
265void FileTest::OnKeyUsed(const std::string &key) {
266 unused_attributes_.erase(key);
267}
268
269int FileTestMain(bool (*run_test)(FileTest *t), const char *path) {
270 FileTest t(path);
271 if (!t.is_open()) {
272 return 1;
273 }
274
275 bool failed = false;
276 while (true) {
277 FileTest::ReadResult ret = t.ReadNext();
278 if (ret == FileTest::kReadError) {
279 return 1;
280 } else if (ret == FileTest::kReadEOF) {
281 break;
282 }
283
284 if (!run_test(&t)) {
285 failed = true;
286 }
287 }
288
289 if (failed) {
290 return 1;
291 }
292
293 printf("PASS\n");
294 return 0;
295}