From 5ed4180798e268bd7f38d8bf23acec81c27ccfc9 Mon Sep 17 00:00:00 2001 From: andrejlevkovitch Date: Fri, 22 May 2020 11:53:24 +0300 Subject: [PATCH] add missing binary-validation.cpp file for test case --- test/binary-validation.cpp | 135 +++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 test/binary-validation.cpp diff --git a/test/binary-validation.cpp b/test/binary-validation.cpp new file mode 100644 index 0000000..29e707a --- /dev/null +++ b/test/binary-validation.cpp @@ -0,0 +1,135 @@ +// bson-validate.cpp + +#include +#include +#include + +int error_count = 0; + +#define EXPECT_EQ(a, b) \ + do { \ + if (a != b) { \ + std::cerr << "Failed: '" << a << "' != '" << b << "'\n"; \ + error_count++; \ + } \ + } while (0) + +using json = nlohmann::json; +using validator = nlohmann::json_schema::json_validator; + +// check binary data validation +const json bson_schema = json::parse(R"( +{ + "type": "object", + "properties": { + "standard_string": { + "type": "string" + }, + "binary_data": { + "type": "string", + "contentEncoding": "binary" + } + }, + "additionalProperties": false +} +)"); + +const json array_of_types = json::parse(R"( +{ + "type": "object", + "properties": { + "something": { + "type": ["string", "number", "boolean"], + "contentEncoding": "binary" + } + } +} +)"); + +const json array_of_types_without_binary = json::parse(R"( +{ + "type": "object", + "properties": { + "something": { + "type": ["string", "number", "boolean"] + } + } +} +)"); + +class store_ptr_err_handler : public nlohmann::json_schema::basic_error_handler +{ + void error(const nlohmann::json::json_pointer &ptr, const json &, const std::string &message) override + { + nlohmann::json_schema::basic_error_handler::error(ptr, "", message); + std::cerr << "ERROR: '" << ptr << "' - '" + << "" + << "': " << message << "\n"; + failed_pointers.push_back(ptr); + } + +public: + std::vector failed_pointers; + + void reset() override + { + nlohmann::json_schema::basic_error_handler::reset(); + failed_pointers.clear(); + } +}; + +int main() +{ + validator val; + val.set_root_schema(bson_schema); + + // create some bson doc + json::binary_t arr; + std::string as_binary = "hello world"; + std::copy(as_binary.begin(), as_binary.end(), std::back_inserter(arr)); + + json binary = json::binary(arr); + + // all right + store_ptr_err_handler err{}; + val.validate({{"standard_string", "some string"}, {"binary_data", binary}}, err); + EXPECT_EQ(err.failed_pointers.size(), 0); + err.reset(); + + // invalid binary data + val.validate({{"binary_data", "string, but expect binary data"}}, err); + EXPECT_EQ(err.failed_pointers.size(), 1); + EXPECT_EQ(err.failed_pointers[0].to_string(), "/binary_data"); + err.reset(); + + // also check that simple string not accept binary data + val.validate({{"standard_string", binary}, {"binary_data", binary}}, err); + EXPECT_EQ(err.failed_pointers.size(), 1); + EXPECT_EQ(err.failed_pointers[0].to_string(), "/standard_string"); + err.reset(); + + ///////////////////////////////////// + // check with array of types + + // check simple types + val.set_root_schema(array_of_types); + val.validate({{"something", "string"}}, err); + val.validate({{"something", 1}}, err); + val.validate({{"something", false}}, err); + EXPECT_EQ(err.failed_pointers.size(), 0); + err.reset(); + + // check binary data + val.validate({{"something", binary}}, err); + EXPECT_EQ(err.failed_pointers.size(), 0); + err.reset(); + + // and check that you can't set binary data if contentEncoding don't set + val.set_root_schema(array_of_types_without_binary); + val.validate({{"something", binary}}, err); + EXPECT_EQ(err.failed_pointers.size(), 1); + EXPECT_EQ(err.failed_pointers[0], "/something"); + err.reset(); + + return error_count; +}