diff --git a/.clang_complete b/.clang_complete index 12c483f7..91ab8360 100644 --- a/.clang_complete +++ b/.clang_complete @@ -5,3 +5,4 @@ -Iexternals/utf8proc -Iexternals/json-parser -Iexternals/bandit +-Iexternals/crypto-algorithms diff --git a/.gitmodules b/.gitmodules index fa884d94..72fbdb67 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/externals/crypto-algorithms b/externals/crypto-algorithms new file mode 160000 index 00000000..cfbde484 --- /dev/null +++ b/externals/crypto-algorithms @@ -0,0 +1 @@ +Subproject commit cfbde48414baacf51fc7c74f275190881f037d32 diff --git a/test/helpers/tree_helpers.cc b/test/helpers/tree_helpers.cc index 2d8efb1b..8b6b6623 100644 --- a/test/helpers/tree_helpers.cc +++ b/test/helpers/tree_helpers.cc @@ -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 @@ -48,3 +50,42 @@ bool operator==(const std::vector &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()); + } +} diff --git a/test/helpers/tree_helpers.h b/test/helpers/tree_helpers.h index e91a3c8f..19ae2c70 100644 --- a/test/helpers/tree_helpers.h +++ b/test/helpers/tree_helpers.h @@ -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 &right, const TreeArray &array); +void assert_consistent_tree_sizes(TSNode node); + #endif // HELPERS_TREE_HELPERS_H_ diff --git a/test/integration/fuzzing-examples.cc b/test/integration/fuzzing-examples.cc new file mode 100644 index 00000000..5c76efb9 --- /dev/null +++ b/test/integration/fuzzing-examples.cc @@ -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> 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(base64_input.c_str()), + reinterpret_cast(&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 diff --git a/test/integration/real_grammars.cc b/test/integration/real_grammars.cc index d89c97fd..dc8f82dd 100644 --- a/test/integration/real_grammars.cc +++ b/test/integration/real_grammars.cc @@ -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 -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 diff --git a/tests.gyp b/tests.gyp index dedde1ba..bede6dfe 100644 --- a/tests.gyp +++ b/tests.gyp @@ -12,6 +12,7 @@ 'test', 'externals/bandit', 'externals/utf8proc', + 'externals/crypto-algorithms', ], 'sources': [ 'test/tests.cc',