TEST(ValidatorTest, ArrayCompactTooShort5) { std::string const value("\x13\x80\x05\x18", 4); Validator validator; ASSERT_VELOCYPACK_EXCEPTION(validator.validate(value.c_str(), value.size()), Exception::ValidatorInvalidLength); }
TEST(ValidatorTest, Illegal) { std::string const value("\x17", 1); Validator validator; ASSERT_TRUE(validator.validate(value.c_str(), value.size())); }
TEST(ValidatorTest, ArrayCompactWithExtra) { std::string const value("\x13\x04\x18\x01\x41", 5); Validator validator; ASSERT_VELOCYPACK_EXCEPTION(validator.validate(value.c_str(), value.size()), Exception::ValidatorInvalidLength); }
TEST(ValidatorTest, ArrayCompact) { std::string const value("\x13\x04\x18\x01", 4); Validator validator; ASSERT_TRUE(validator.validate(value.c_str(), value.size())); }
TEST(ValidatorTest, ArrayOneByteIndexedMultipleMembers) { std::string const value("\x06\x09\x02\x18\x18\x18\x03\x04\x05", 9); Validator validator; ASSERT_TRUE(validator.validate(value.c_str(), value.size())); }
ParsedArgument ArgumentParser::parse(int argc, char** argv) { // put all the arguments into vector for easy manipulation vector<string> v; for (int i = 1; i < argc; i++) { v.push_back(string(argv[i])); } ParsedArgument pa; // ignore the first argument since the first argument is a program name for (size_t i = 0; i < v.size(); ++i) { string s = v[i]; bool shortArg = cppargparser::isShortArg(s); bool longArg = cppargparser::isLongArg(s); if (!shortArg && !longArg) { throw InvalidArgumentException(s + " is an invalid argument"); } string arg = s; if (longArg) { // if the argument has =, e.g. ---ccc=123 split it by = and then // modify the vector by modifying --ccc=123 with --ccc and adding // a new element 123 size_t index = arg.find("="); if (index != string::npos) { string key = arg.substr(0, index); string value = arg.substr(index+1); arg = key; v[i] = key; v.insert(v.begin()+i+1, value); } } map<string, Argument>::iterator it = args.find(arg); if (it == args.end()) { throw InvalidArgumentException(arg + " is an invalid argument"); } Argument argument = it->second; if (argument.getNumArgs() == Argument::INFINITY) { string value = ""; do { value = v[++i]; if (!cppargparser::isShortArg(value)) { pa.putArgument(argument.getShortArg(), value); } if (!cppargparser::isLongArg(value)) { pa.putArgument(argument.getLongArg(), value); } } while (!isShortArg(value) && !isLongArg(value)); } else { i = i + 1; size_t n = i + argument.getNumArgs(); // this condition means there's the argument doesn't need any value, // i.e. the numArgs is 0, thus there's no need to iterate each // argument value if (i == n) { pa.putArgument(argument.getShortArg(), ""); pa.putArgument(argument.getLongArg(), ""); } else { for (; i < n; ++i) { if (i >= v.size()) { throw InvalidArgumentException( argument.getArg() + " requires " + cppargparser::toString(argument.getNumArgs()) + " argument(s)"); } string value = v[i]; pa.putArgument(argument.getShortArg(), value); pa.putArgument(argument.getLongArg(), value); } } } Validator* validator = argument.getValidator(); if (validator != NULL) { if (argument.isShortArg()) { vector<string> values = pa.getValues(argument.getShortArg()); if (!validator->validate(values)) { throw InvalidArgumentException(cppargparser::toString(values) + " is an invalid argument value"); } } else { vector<string> values = pa.getValues(argument.getLongArg()); if (!validator->validate(values)) { throw InvalidArgumentException(cppargparser::toString(values) + " is an invalid argument value"); } } } args.erase(argument.getShortArg()); args.erase(argument.getLongArg()); // need to decrement i here because both inner and outer loops // increment i by 1 --i; } // check if the there are still mandatory arguments in the args map // if there are, throw an InvalidArgumentException for (map<string, Argument>::const_iterator i = args.begin(); i != args.end(); ++i) { if (i->second.isMandatory()) { throw InvalidArgumentException(i->second.getArg() + " is a mandatory argument"); } } return pa; }
TEST_F(TestValidationErrors, AllOfConstraintFailure) { // Load schema document rapidjson::Document schemaDocument; ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/schemas/allof_integers_and_numbers.schema.json", schemaDocument) ); RapidJsonAdapter schemaAdapter(schemaDocument); // Parse schema document Schema schema; SchemaParser schemaParser; ASSERT_NO_THROW( schemaParser.populateSchema(schemaAdapter, schema) ); // Load test document rapidjson::Document testDocument; ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/documents/array_doubles_1_2_3.json", testDocument) ); RapidJsonAdapter testAdapter(testDocument); Validator validator; ValidationResults results; EXPECT_FALSE( validator.validate(schema, testAdapter, &results) ); ValidationResults::Error error; EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(2), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "[0]", error.context[1] ); EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate item #0 in array.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(2), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "[1]", error.context[1] ); EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate item #1 in array.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(2), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "[2]", error.context[1] ); EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate item #2 in array.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate against child schema #0.", error.description ); EXPECT_FALSE( results.popError(error) ); while (results.popError(error)) { //std::cerr << error.context << std::endl; std::cerr << error.description << std::endl; } }