selfvalidate_test.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. for (std::string_view str : testing::internal::GetArgvs()) {
  81. RecursiveTestFilter * ptr;
  82. if (str.starts_with("--json_suite_filter=")) {
  83. str.remove_prefix(20);
  84. ptr = &s_suite_filter;
  85. } else if (str.starts_with("--json_case_filter=")) {
  86. str.remove_prefix(19);
  87. ptr = &s_case_filter;
  88. } else {
  89. continue;
  90. }
  91. size_t const pos_end = str[0] == '-' ? 0 : str.find(":-");
  92. size_t const neg_end =
  93. pos_end == 0 ? 1 : (pos_end == std::string::npos ? pos_end : pos_end + 2);
  94. if (pos_end > 0) {
  95. std::vector<std::string> tokens;
  96. testing::internal::SplitString(std::string(str.substr(0, pos_end)), ':', &tokens);
  97. ptr->whitelist.insert(tokens.begin(), tokens.end());
  98. }
  99. if (neg_end != std::string::npos) {
  100. std::vector<std::string> tokens;
  101. testing::internal::SplitString(std::string(str.substr(neg_end)), ':', &tokens);
  102. ptr->blacklist.insert(tokens.begin(), tokens.end());
  103. }
  104. }
  105. }
  106. };
  107. RecursiveTestFilter JsonSchema::s_suite_filter;
  108. RecursiveTestFilter JsonSchema::s_case_filter;
  109. TEST_P(JsonSchema, TestSuite) {
  110. auto const & [version, file] = GetParam();
  111. Json::Value spec;
  112. EXPECT_TRUE(load_file(file, spec));
  113. for (auto const & suite : spec) {
  114. if (skip_suite(suite["description"].asString())) {
  115. continue;
  116. }
  117. std::cout << "\033[0;32m[ SUITE ] \033[0;0m" << suite["description"].asString() << std::endl;
  118. try {
  119. jvalidate::Schema schema(suite["schema"], version, &load_external_for_test);
  120. for (auto const & test : suite["tests"]) {
  121. if (skip_case(test["description"].asString())) {
  122. continue;
  123. }
  124. try {
  125. std::cout << "\033[0;32m[ CASE ] \033[0;0m " << test["description"].asString()
  126. << std::endl;
  127. EXPECT_THAT(test["data"], ValidatesAgainst(schema, test)) << suite["schema"];
  128. } catch (std::exception const & ex) { ADD_FAILURE() << ex.what() << "\n" << test; }
  129. }
  130. } catch (std::exception const & ex) {
  131. ADD_FAILURE() << "when parsing schema: " << ex.what() << "\n" << suite["schema"];
  132. }
  133. }
  134. }
  135. INSTANTIATE_TEST_SUITE_P(Draft7, JsonSchema, SchemaTests(Version::Draft07), SchemaTestName);
  136. int main(int argc, char ** argv) {
  137. testing::InitGoogleMock(&argc, argv);
  138. return RUN_ALL_TESTS();
  139. }