selfvalidate_test.cxx 5.6 KB

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