Skip to content

Commit

Permalink
Merge pull request #369 from openzim/fix_aarch64_test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Sep 22, 2023
2 parents dc703e0 + b0a2e0f commit 7ffb64a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
16 changes: 6 additions & 10 deletions src/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ namespace
const bool MANDATORY = true;
const bool OPTIONAL = false;

const std::string LANGS_REGEXP = "\\w{3}(,\\w{3})*";
const std::string DATE_REGEXP = R"(\d\d\d\d-\d\d-\d\d)";
const std::string LANGS_REGEXP = "^\\w{3}(,\\w{3})*$";
const std::string DATE_REGEXP = R"(^\d\d\d\d-\d\d-\d\d$)";
const std::string PNG_REGEXP = "^\x89\x50\x4e\x47\x0d\x0a\x1a\x0a";

// PNG regexp has to be defined in such a tricky way because it includes
// a NUL character
const char PNG_REGEXP_DATA[] = "^\x89\x50\x4e\x47\x0d\x0a\x1a\x0a(.|\\s|\0)+";
const std::string PNG_REGEXP(PNG_REGEXP_DATA, sizeof(PNG_REGEXP_DATA)-1);

bool matchRegex(const std::string& regexStr, const std::string& text)
bool searchRegex(const std::string& regexStr, const std::string& text)
{
const std::regex regex(regexStr);
return std::regex_match(text.begin(), text.end(), regex);
return std::regex_search(text.begin(), text.end(), regex);
}

size_t getTextLength(const std::string& utf8EncodedString)
Expand Down Expand Up @@ -210,7 +206,7 @@ Metadata::Errors Metadata::checkSimpleConstraints() const
oss << name << " must contain at most " << rmr.maxLength << " characters";
errors.push_back(oss.str());
}
if ( !rmr.regex.empty() && !matchRegex(rmr.regex, value) ) {
if ( !rmr.regex.empty() && !searchRegex(rmr.regex, value) ) {
const std::string regex = escapeNonPrintableChars(rmr.regex);
errors.push_back(name + " doesn't match regex: " + regex);
}
Expand Down
8 changes: 4 additions & 4 deletions test/metadata-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TEST(Metadata, regexpConstraints)
ASSERT_FALSE(m.valid());
ASSERT_EQ(m.check(),
zim::Metadata::Errors({
"Date doesn't match regex: \\d\\d\\d\\d-\\d\\d-\\d\\d"
"Date doesn't match regex: ^\\d\\d\\d\\d-\\d\\d-\\d\\d$"
})
);
m.set("Date", "1234-56-78"); // Yes, such a date is considered valid
Expand All @@ -125,7 +125,7 @@ TEST(Metadata, regexpConstraints)
ASSERT_FALSE(m.valid());
ASSERT_EQ(m.check(),
zim::Metadata::Errors({
"Language doesn't match regex: \\w{3}(,\\w{3})*"
"Language doesn't match regex: ^\\w{3}(,\\w{3})*$"
})
);

Expand All @@ -135,7 +135,7 @@ TEST(Metadata, regexpConstraints)
m.set("Illustration_48x48@1", "zimdata/favicon.png");
ASSERT_EQ(m.check(),
zim::Metadata::Errors({
"Illustration_48x48@1 doesn't match regex: ^\\x89PNG\\x0d\\x0a\\x1a\\x0a(.|\\s|\\x00)+"
"Illustration_48x48@1 doesn't match regex: ^\\x89PNG\\x0d\\x0a\\x1a\\x0a"
})
);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ TEST(Metadata, mandatoryMetadataAndSimpleChecksAreRunUnconditionally)
zim::Metadata::Errors({
"Missing mandatory metadata: Illustration_48x48@1",
"Language must contain at least 3 characters",
"Language doesn't match regex: \\w{3}(,\\w{3})*",
"Language doesn't match regex: ^\\w{3}(,\\w{3})*$",
"Title must contain at most 30 characters"
})
);
Expand Down
8 changes: 4 additions & 4 deletions test/zimcheck-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ TEST(zimcheck, metadata_poorzimfile)
" Missing mandatory metadata: Description" "\n"
" Missing mandatory metadata: Illustration_48x48@1" "\n"
" Language must contain at least 3 characters" "\n"
" Language doesn't match regex: \\w{3}(,\\w{3})*" "\n"
" Language doesn't match regex: ^\\w{3}(,\\w{3})*$" "\n"
"[INFO] Overall Test Status: Fail" "\n"
"[INFO] Total time taken by zimcheck: <3 seconds." "\n"
);
Expand Down Expand Up @@ -728,7 +728,7 @@ const std::string ALL_CHECKS_OUTPUT_ON_POORZIMFILE(
" Missing mandatory metadata: Description" "\n"
" Missing mandatory metadata: Illustration_48x48@1" "\n"
" Language must contain at least 3 characters" "\n"
" Language doesn't match regex: \\w{3}(,\\w{3})*" "\n"
" Language doesn't match regex: ^\\w{3}(,\\w{3})*$" "\n"
"[ERROR] Favicon:" "\n"
" Favicon is missing" "\n"
"[ERROR] Missing mainpage:" "\n"
Expand Down Expand Up @@ -866,8 +866,8 @@ TEST(zimcheck, json_poorzimfile)
" {" "\n"
" \"check\" : \"metadata\"," "\n"
" \"level\" : \"ERROR\"," "\n"
" \"message\" : \"Language doesn't match regex: \\\\w{3}(,\\\\w{3})*\"," "\n"
" \"error\" : \"Language doesn't match regex: \\\\w{3}(,\\\\w{3})*\"" "\n"
" \"message\" : \"Language doesn't match regex: ^\\\\w{3}(,\\\\w{3})*$\"," "\n"
" \"error\" : \"Language doesn't match regex: ^\\\\w{3}(,\\\\w{3})*$\"" "\n"
" }," "\n"
" {" "\n"
" \"check\" : \"favicon\"," "\n"
Expand Down

0 comments on commit 7ffb64a

Please sign in to comment.