/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Insulate these test-classes from the outside world. // // (In particular, distinguish this `CommandLine` from the one in ipc/chromium. // The compiler has no issues here, but the MSVC debugger gets somewhat // confused.) namespace testzilla {
// Auxiliary class to simplify the declaration of test-cases for // EnsureCommandlineSafe. class CommandLine { const size_t size;
std::vector<std::string> _data_8;
std::vector<std::wstring> _data_16; // _should_ be const all the way through, but... charconst** argv_8;
wchar_t const** argv_16;
template <typename Container> explicit CommandLine(Container const& container)
: size{std::size(container) + 1}, // plus one for argv[0]
argv_8(newconstchar*[size + 1]), // plus one again for argv[argc]
argv_16(newconst wchar_t*[size + 1]) {
_data_8.reserve(size + 1);
_data_16.reserve(size + 1);
size_t pos = 0;
constauto append = [&](constchar* val) {
size_t const len = ::strlen(val);
_data_8.emplace_back(val, val + len);
_data_16.emplace_back(val, val + len);
argv_8[pos] = _data_8.back().data();
argv_16[pos] = _data_16.back().data();
++pos;
};
append("GECKOAPP.COM"); // argv[0] may be anything for (constchar* item : container) {
append(item);
}
assert(pos == size);
// C99 §5.1.2.2.1 states "argv[argc] shall be a null pointer", and // `CheckArg` implicitly relies on this.
argv_8[pos] = nullptr;
argv_16[pos] = nullptr;
}
// debug output friend std::ostream& operator<<(std::ostream& o, CommandLine const& cl) { for (constauto& item : cl._data_8) {
o << '"'; for (constchar c : item) { if (c == '"') {
o << "\\\"";
} else {
o << c;
}
}
o << "\", ";
} return o;
}
};
// capital letters are not accepted in the left comparand
{FAIL, {"I", "i"}},
{FAIL, {"I", "i"}},
{FAIL, {"Mozilla", "mozilla"}},
{FAIL, {"Mozilla", "Mozilla"}},
// punctuation other than `-` is rejected
{FAIL, {"*", "*"}},
{FAIL, {"*", "word"}},
{FAIL, {".", "."}},
{FAIL, {".", "a"}},
{FAIL, {"_", "_"}},
// non-ASCII characters are rejected // // (the contents of this test case may differ depending on the source and // execution character sets, but the result should always be failure)
{FAIL, {"à", "a"}},
{FAIL, {"a", "à"}},
{FAIL, {"à", "à"}},
};
#ifdef XP_WIN
using WideTestCase = std::pair<constchar*, const wchar_t*>;
std::pair<TestCaseState, WideTestCase> kStrMatches16[] = { // (Turkish 'İ' may lowercase to 'i' in some locales, but // we explicitly prevent that from being relevant)
{FAIL, {"i", L"İ"}},
{FAIL, {"mozilla", L"ṁozilla"}},
}; #endif
// on Windows, slashes are also permitted
{WIN_ONLY, {"-osint", "/aleph", "http://www.example.com/"}},
{WIN_ONLY, {"--osint", "/aleph", "http://www.example.com/"}}, // - These should pass on Windows because they're well-formed. // - These should pass elsewhere because the first parameter is // just a local filesystem path, not an option.
{PASS, {"/osint", "/aleph", "http://www.example.com/"}},
{PASS, {"/osint", "-aleph", "http://www.example.com/"}},
{PASS, {"/osint", "--aleph", "http://www.example.com/"}},
// the required argument's argument may not itself be a flag
{FAIL, {"-osint", "-aleph", "-anything"}},
{FAIL, {"-osint", "-aleph", "--anything"}}, // - On Windows systems this is a switch and should be rejected. // - On non-Windows systems this is potentially a valid local path.
{!WIN_ONLY, {"-osint", "-aleph", "/anything"}},
// Additional test cases for optional parameters. // // Test cases marked PASS should pass iff the above optional parameters are // permitted. (Test cases marked FAIL should fail regardless, and are only // grouped here for convenience and semantic coherence.)
MOZ_RUNINIT std::pair<TestCaseState, std::vector<constchar*>>
kCommandLinesOpt[] = { // one permitted optional argument
{PASS, {"-osint", "-mozilla", "-aleph", "http://www.example.com/"}},
{PASS, {"-osint", "-allizom", "-aleph", "http://www.example.com/"}},
TEST(CmdLineAndEnvUtils, ArgStartsWith)
{
using mozilla::ArgStartsWith; // Should match and point to '=' auto match = ArgStartsWith("flag=value", "flag");
ASSERT_TRUE(match); // Should match and point to '\0' for exact match auto match2 = ArgStartsWith("flag", "flag");
ASSERT_TRUE(match2); // Should not match auto match3 = ArgStartsWith("flog=value", "flag");
EXPECT_FALSE(match3);
}
TEST(CmdLineAndEnvUtils, flagValue)
{
using mozilla::ARG_BAD;
using mozilla::ARG_FOUND;
using mozilla::ARG_NONE;
using mozilla::CheckArg; int argc; constchar* argv1[] = {"prog", "--flag=value", nullptr};
argc = 2; constchar* param = nullptr;
EXPECT_EQ(CheckArg(argc, const_cast<char**>(argv1), "flag", ¶m),
ARG_FOUND);
ASSERT_TRUE(param);
EXPECT_STREQ(param, "value");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.