selfvalidate_test.cxx 5.7 KB

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