Add test for an example found during fuzzing

This commit is contained in:
Max Brunsfeld 2017-06-30 21:55:50 -07:00
parent f722923493
commit fcffd4b732
8 changed files with 111 additions and 40 deletions

View file

@ -5,3 +5,4 @@
-Iexternals/utf8proc
-Iexternals/json-parser
-Iexternals/bandit
-Iexternals/crypto-algorithms

3
.gitmodules vendored
View file

@ -10,3 +10,6 @@
[submodule "externals/json-parser"]
path = externals/json-parser
url = https://github.com/udp/json-parser.git
[submodule "externals/crypto-algorithms"]
path = externals/crypto-algorithms
url = https://github.com/B-Con/crypto-algorithms.git

1
externals/crypto-algorithms vendored Submodule

@ -0,0 +1 @@
Subproject commit cfbde48414baacf51fc7c74f275190881f037d32

View file

@ -1,4 +1,6 @@
#include "bandit/bandit.h"
#include "helpers/tree_helpers.h"
#include "helpers/point_helpers.h"
#include "runtime/document.h"
#include "runtime/node.h"
#include <ostream>
@ -48,3 +50,42 @@ bool operator==(const std::vector<Tree *> &vec, const TreeArray &array) {
return false;
return true;
}
void assert_consistent_tree_sizes(TSNode node) {
size_t child_count = ts_node_child_count(node);
size_t start_byte = ts_node_start_byte(node);
size_t end_byte = ts_node_end_byte(node);
TSPoint start_point = ts_node_start_point(node);
TSPoint end_point = ts_node_end_point(node);
bool some_child_has_changes = false;
AssertThat(start_byte, !IsGreaterThan(end_byte));
AssertThat(start_point, !IsGreaterThan(end_point));
size_t last_child_end_byte = start_byte;
TSPoint last_child_end_point = start_point;
for (size_t i = 0; i < child_count; i++) {
TSNode child = ts_node_child(node, i);
size_t child_start_byte = ts_node_start_byte(child);
TSPoint child_start_point = ts_node_start_point(child);
AssertThat(child_start_byte, !IsLessThan(last_child_end_byte));
AssertThat(child_start_point, !IsLessThan(last_child_end_point));
assert_consistent_tree_sizes(child);
if (ts_node_has_changes(child))
some_child_has_changes = true;
last_child_end_byte = ts_node_end_byte(child);
last_child_end_point = ts_node_end_point(child);
}
if (child_count > 0) {
AssertThat(end_byte, !IsLessThan(last_child_end_byte));
AssertThat(end_point, !IsLessThan(last_child_end_point));
}
if (some_child_has_changes) {
AssertThat(ts_node_has_changes(node), IsTrue());
}
}

View file

@ -13,4 +13,6 @@ std::ostream &operator<<(std::ostream &stream, const TSNode &node);
bool operator==(const TSNode &left, const TSNode &right);
bool operator==(const std::vector<Tree *> &right, const TreeArray &array);
void assert_consistent_tree_sizes(TSNode node);
#endif // HELPERS_TREE_HELPERS_H_

View file

@ -0,0 +1,60 @@
#include "test_helper.h"
#include "base64.c"
#include "helpers/load_language.h"
#include "helpers/tree_helpers.h"
#include "helpers/record_alloc.h"
START_TEST
vector<pair<string, string>> examples({
{
"javascript",
"Bi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLXGK0i0vLS0tLS0tLS0tLS0tLS0tLS0tLS0tLXGK0i0vLS0tLS0tLS0tLS0tLS0xLS0tLTYtLfpZAA=="
},
});
describe("examples found via fuzzing", [&]() {
before_each([&]() {
record_alloc::start();
});
after_each([&]() {
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
});
for (unsigned i = 0, n = examples.size(); i < n; i++) {
it(("parses example number " + to_string(i)).c_str(), [&]() {
TSDocument *document = ts_document_new();
// ts_document_print_debugging_graphs(document, true);
const string &language_name = examples[i].first;
ts_document_set_language(document, load_real_language(language_name));
string input;
const string &base64_input = examples[i].second;
input.resize(base64_input.size());
input.resize(base64_decode(
reinterpret_cast<const unsigned char *>(base64_input.c_str()),
reinterpret_cast<unsigned char *>(&input[0]),
base64_input.size()
));
ts_document_set_input_string_with_length(
document,
input.c_str(),
input.size()
);
ts_document_parse(document);
TSNode node = ts_document_root_node(document);
assert_consistent_tree_sizes(node);
ts_document_free(document);
});
}
});
END_TEST

View file

@ -9,47 +9,9 @@
#include "helpers/record_alloc.h"
#include "helpers/random_helpers.h"
#include "helpers/scope_sequence.h"
#include "helpers/tree_helpers.h"
#include <set>
static void assert_consistent_sizes(TSNode node) {
size_t child_count = ts_node_child_count(node);
size_t start_byte = ts_node_start_byte(node);
size_t end_byte = ts_node_end_byte(node);
TSPoint start_point = ts_node_start_point(node);
TSPoint end_point = ts_node_end_point(node);
bool some_child_has_changes = false;
AssertThat(start_byte, !IsGreaterThan(end_byte));
AssertThat(start_point, !IsGreaterThan(end_point));
size_t last_child_end_byte = start_byte;
TSPoint last_child_end_point = start_point;
for (size_t i = 0; i < child_count; i++) {
TSNode child = ts_node_child(node, i);
size_t child_start_byte = ts_node_start_byte(child);
TSPoint child_start_point = ts_node_start_point(child);
AssertThat(child_start_byte, !IsLessThan(last_child_end_byte));
AssertThat(child_start_point, !IsLessThan(last_child_end_point));
assert_consistent_sizes(child);
if (ts_node_has_changes(child))
some_child_has_changes = true;
last_child_end_byte = ts_node_end_byte(child);
last_child_end_point = ts_node_end_point(child);
}
if (child_count > 0) {
AssertThat(end_byte, !IsLessThan(last_child_end_byte));
AssertThat(end_point, !IsLessThan(last_child_end_point));
}
if (some_child_has_changes) {
AssertThat(ts_node_has_changes(node), IsTrue());
}
}
static void assert_correct_tree_size(TSDocument *document, string content) {
TSNode root_node = ts_document_root_node(document);
size_t expected_size = content.size();
@ -65,7 +27,7 @@ static void assert_correct_tree_size(TSDocument *document, string content) {
expected_size = content.find_last_not_of("\n ") + 1;
AssertThat(ts_node_end_byte(root_node), Equals(expected_size));
assert_consistent_sizes(root_node);
assert_consistent_tree_sizes(root_node);
}
START_TEST

View file

@ -12,6 +12,7 @@
'test',
'externals/bandit',
'externals/utf8proc',
'externals/crypto-algorithms',
],
'sources': [
'test/tests.cc',