selfvalidate_test.cxx 5.8 KB

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