selfvalidate_test.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <filesystem>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <regex>
  7. #include <unordered_set>
  8. #include <curl/curl.h>
  9. #include <gmock/gmock.h>
  10. #include <gtest/gtest.h>
  11. #include <jvalidate/adapter.h>
  12. #include <jvalidate/adapters/jsoncpp.h>
  13. #include <jvalidate/enum.h>
  14. #include <jvalidate/schema.h>
  15. #include <jvalidate/status.h>
  16. #include <jvalidate/uri.h>
  17. #include <jvalidate/validator.h>
  18. #include <json/reader.h>
  19. #include <json/value.h>
  20. #include <json/writer.h>
  21. #include "./json_schema_test_suite.h"
  22. using jvalidate::schema::Version;
  23. using testing::TestWithParam;
  24. class Glob {
  25. private:
  26. std::regex re_;
  27. public:
  28. explicit Glob(std::string glob) {
  29. for (size_t i = glob.find('.'); i != std::string::npos; i = glob.find('.', i + 2)) {
  30. glob.insert(glob.begin() + i, '\\');
  31. }
  32. for (size_t i = glob.find('*'); i != std::string::npos; i = glob.find('*', i + 2)) {
  33. glob.insert(glob.begin() + i, '.');
  34. }
  35. re_ = std::regex(glob);
  36. }
  37. bool operator==(std::string const & str) const { return std::regex_search(str, re_); }
  38. };
  39. struct RecursiveTestFilter {
  40. std::vector<Glob> whitelist;
  41. std::vector<Glob> blacklist;
  42. bool accepts(std::string const & str) const {
  43. return std::count(blacklist.begin(), blacklist.end(), str) == 0 and
  44. (whitelist.empty() or std::count(whitelist.begin(), whitelist.end(), str) > 0);
  45. }
  46. };
  47. bool load_stream(std::istream & in, Json::Value & out) {
  48. Json::CharReaderBuilder builder;
  49. std::string error;
  50. return Json::parseFromStream(builder, in, &out, &error);
  51. }
  52. bool load_file(std::filesystem::path const & path, Json::Value & out) {
  53. std::ifstream in(path);
  54. return load_stream(in, out);
  55. }
  56. size_t transfer_to_buffer(char * data, size_t size, size_t nmemb, void * userdata) {
  57. std::stringstream & ss = *reinterpret_cast<std::stringstream *>(userdata);
  58. size_t actual_size = size * nmemb;
  59. ss << std::string_view(data, actual_size);
  60. return actual_size;
  61. }
  62. bool load_external_for_test(jvalidate::URI const & uri, Json::Value & out) {
  63. constexpr std::string_view g_fake_url = "localhost:1234/";
  64. if (uri.scheme().starts_with("http") && uri.resource().starts_with(g_fake_url)) {
  65. std::string_view path = uri.resource().substr(g_fake_url.size());
  66. return load_file(JSONSchemaTestSuiteDir() / "remotes" / path, out);
  67. } else if (uri.scheme().starts_with("http")) {
  68. std::stringstream ss;
  69. if (CURL * curl = curl_easy_init(); curl) {
  70. curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
  71. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  72. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ss);
  73. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &transfer_to_buffer);
  74. CURLcode res = curl_easy_perform(curl);
  75. curl_easy_cleanup(curl);
  76. if (res == CURLE_OK) {
  77. return load_stream(ss, out);
  78. }
  79. }
  80. return false;
  81. } else if (uri.scheme() == "file") {
  82. return load_file(uri.resource(), out);
  83. } else {
  84. return false;
  85. }
  86. }
  87. class JsonSchema : public TestWithParam<SchemaParams> {
  88. private:
  89. static RecursiveTestFilter s_suite_filter;
  90. static RecursiveTestFilter s_case_filter;
  91. protected:
  92. bool skip_suite(std::string const & desc) const { return not s_suite_filter.accepts(desc); }
  93. bool skip_case(std::string const & desc) const { return not s_case_filter.accepts(desc); }
  94. static void SetUpTestCase() {
  95. auto tokenize = [](auto & into, std::string const & in) {
  96. std::vector<std::string> tokens;
  97. testing::internal::SplitString(in, ':', &tokens);
  98. for (size_t i = 1; i < tokens.size();) {
  99. if (tokens[i].front() != ' ') {
  100. ++i;
  101. continue;
  102. }
  103. tokens[i - 1] += ":";
  104. tokens[i - 1] += tokens[i];
  105. tokens.erase(tokens.begin() + i);
  106. }
  107. for (auto &tok : tokens) {
  108. into.emplace_back(tok);
  109. }
  110. };
  111. for (std::string_view str : testing::internal::GetArgvs()) {
  112. RecursiveTestFilter * ptr;
  113. if (str.starts_with("--json_suite_filter=")) {
  114. str.remove_prefix(20);
  115. ptr = &s_suite_filter;
  116. } else if (str.starts_with("--json_case_filter=")) {
  117. str.remove_prefix(19);
  118. ptr = &s_case_filter;
  119. } else {
  120. continue;
  121. }
  122. size_t const pos_end = str[0] == '-' ? 0 : str.find(":-");
  123. size_t const neg_start =
  124. pos_end == 0 ? 1 : (pos_end == std::string::npos ? pos_end : pos_end + 2);
  125. if (pos_end > 0 && not str.empty()) {
  126. tokenize(ptr->whitelist, std::string(str.substr(0, pos_end)));
  127. }
  128. if (neg_start != std::string::npos) {
  129. tokenize(ptr->blacklist, std::string(str.substr(neg_start)));
  130. }
  131. }
  132. }
  133. };
  134. RecursiveTestFilter JsonSchema::s_suite_filter;
  135. RecursiveTestFilter JsonSchema::s_case_filter;
  136. TEST_P(JsonSchema, TestSuite) {
  137. auto const & [version, file] = GetParam();
  138. Json::Value spec;
  139. EXPECT_TRUE(load_file(file, spec));
  140. for (auto const & suite : spec) {
  141. if (skip_suite(suite["description"].asString())) {
  142. continue;
  143. }
  144. std::cout << "\033[0;32m[ SUITE ] \033[0;0m" << suite["description"].asString() << std::endl;
  145. try {
  146. jvalidate::Schema schema(suite["schema"], version, &load_external_for_test);
  147. for (auto const & test : suite["tests"]) {
  148. if (skip_case(test["description"].asString())) {
  149. continue;
  150. }
  151. try {
  152. std::cout << "\033[0;32m[ CASE ] \033[0;0m " << test["description"].asString()
  153. << std::endl;
  154. EXPECT_THAT(test["data"], ValidatesAgainst(schema, test)) << suite["schema"];
  155. } catch (std::exception const & ex) { ADD_FAILURE() << ex.what() << "\n" << test; }
  156. }
  157. } catch (std::exception const & ex) {
  158. ADD_FAILURE() << "when parsing schema: " << ex.what() << "\n" << suite["schema"];
  159. }
  160. }
  161. }
  162. INSTANTIATE_TEST_SUITE_P(Draft3, JsonSchema, SchemaTests(Version::Draft03), SchemaTestName);
  163. INSTANTIATE_TEST_SUITE_P(Draft4, JsonSchema, SchemaTests(Version::Draft04), SchemaTestName);
  164. INSTANTIATE_TEST_SUITE_P(Draft6, JsonSchema, SchemaTests(Version::Draft06), SchemaTestName);
  165. INSTANTIATE_TEST_SUITE_P(Draft7, JsonSchema, SchemaTests(Version::Draft07), SchemaTestName);
  166. INSTANTIATE_TEST_SUITE_P(Draft2019_09, JsonSchema, SchemaTests(Version::Draft2019_09),
  167. SchemaTestName);
  168. INSTANTIATE_TEST_SUITE_P(Draft2020_12, JsonSchema, SchemaTests(Version::Draft2020_12),
  169. SchemaTestName);
  170. int main(int argc, char ** argv) {
  171. testing::InitGoogleMock(&argc, argv);
  172. return RUN_ALL_TESTS();
  173. }