| Index: net/cert/internal/verify_name_match_unittest.cc
|
| diff --git a/net/cert/internal/verify_name_match_unittest.cc b/net/cert/internal/verify_name_match_unittest.cc
|
| index 0bdce8a028ac56ee7b8dd92e0d91257a57273100..6aefeb87e05984725cbb1455ffffe97b701f1568 100644
|
| --- a/net/cert/internal/verify_name_match_unittest.cc
|
| +++ b/net/cert/internal/verify_name_match_unittest.cc
|
| @@ -4,17 +4,259 @@
|
|
|
| #include "net/cert/internal/verify_name_match.h"
|
|
|
| +#include "base/base_paths.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/path_service.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/string_util.h"
|
| #include "net/der/input.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace net {
|
| +namespace {
|
|
|
| -TEST(VerifyNameMatchTest, Simple) {
|
| - // TODO(mattm): Use valid Names.
|
| - EXPECT_TRUE(VerifyNameMatch(der::Input("hello"), der::Input("hello")));
|
| - EXPECT_FALSE(VerifyNameMatch(der::Input("aello"), der::Input("hello")));
|
| - EXPECT_FALSE(VerifyNameMatch(der::Input("hello"), der::Input("hello1")));
|
| - EXPECT_FALSE(VerifyNameMatch(der::Input("hello1"), der::Input("hello")));
|
| +std::string LoadTestData(const std::string& prefix,
|
| + const std::string& value_type,
|
| + const std::string& suffix) {
|
| + base::FilePath src_root;
|
| + PathService::Get(base::DIR_SOURCE_ROOT, &src_root);
|
| + std::string filename = prefix + "-" + value_type + "-" + suffix + ".der";
|
| + base::FilePath filepath =
|
| + src_root.Append("net/data/verify_name_match_unittest/names")
|
| + .Append(filename);
|
| + std::string result;
|
| + bool success = base::ReadFileToString(filepath, &result);
|
| + EXPECT_TRUE(success);
|
| + return result;
|
| +}
|
| +
|
| +static const char* kValueTypes[] = {"PRINTABLESTRING",
|
| + "T61STRING",
|
| + "UTF8",
|
| + "BMPSTRING",
|
| + "UNIVERSALSTRING"};
|
| +static const char* kMangleTypes[] = {"unmangled",
|
| + "case_swap",
|
| + "extra_whitespace"};
|
| +
|
| +} // namespace
|
| +
|
| +class VerifyNameMatchSimpleTest
|
| + : public ::testing::TestWithParam<
|
| + ::testing::tuple<const char*, const char*>> {
|
| + public:
|
| + std::string value_type() const { return ::testing::get<0>(GetParam()); }
|
| + std::string suffix() const { return ::testing::get<1>(GetParam()); }
|
| +};
|
| +
|
| +// Compare each input against itself, verifies that all input data is parsed
|
| +// successfully.
|
| +TEST_P(VerifyNameMatchSimpleTest, ExactEquality) {
|
| + std::string der = LoadTestData("ascii", value_type(), suffix());
|
| + EXPECT_TRUE(VerifyNameMatch(der::Input(der), der::Input(der)));
|
| +
|
| + std::string der_extra_attr =
|
| + LoadTestData("ascii", value_type(), suffix() + "-extra_attr");
|
| + EXPECT_TRUE(
|
| + VerifyNameMatch(der::Input(der_extra_attr), der::Input(der_extra_attr)));
|
| +
|
| + std::string der_extra_rdn =
|
| + LoadTestData("ascii", value_type(), suffix() + "-extra_rdn");
|
| + EXPECT_TRUE(
|
| + VerifyNameMatch(der::Input(der_extra_rdn), der::Input(der_extra_rdn)));
|
| +}
|
| +
|
| +// Ensure that a Name does not match another Name which is exactly the same but
|
| +// with an extra attribute in one Relative Distinguished Name.
|
| +TEST_P(VerifyNameMatchSimpleTest, ExtraAttrDoesNotMatch) {
|
| + std::string der = LoadTestData("ascii", value_type(), suffix());
|
| + std::string der_extra_attr =
|
| + LoadTestData("ascii", value_type(), suffix() + "-extra_attr");
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(der), der::Input(der_extra_attr)));
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(der_extra_attr), der::Input(der)));
|
| +}
|
| +
|
| +// Ensure that a Name does not match another Name which is exactly the same but
|
| +// with an extra Relative Distinguished Name.
|
| +TEST_P(VerifyNameMatchSimpleTest, ExtraRdnDoesNotMatch) {
|
| + std::string der = LoadTestData("ascii", value_type(), suffix());
|
| + std::string der_extra_rdn =
|
| + LoadTestData("ascii", value_type(), suffix() + "-extra_rdn");
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(der), der::Input(der_extra_rdn)));
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(der_extra_rdn), der::Input(der)));
|
| +}
|
| +
|
| +INSTANTIATE_TEST_CASE_P(InstantiationName,
|
| + VerifyNameMatchSimpleTest,
|
| + ::testing::Combine(::testing::ValuesIn(kValueTypes),
|
| + ::testing::ValuesIn(kMangleTypes)));
|
| +
|
| +class VerifyNameMatchNormalizationTest
|
| + : public ::testing::TestWithParam<::testing::tuple<bool, const char*>> {
|
| + public:
|
| + bool expected_result() const { return ::testing::get<0>(GetParam()); }
|
| + std::string value_type() const { return ::testing::get<1>(GetParam()); }
|
| +};
|
| +
|
| +// Verify matching is case insensitive (for the types which currently support
|
| +// normalization).
|
| +TEST_P(VerifyNameMatchNormalizationTest, CaseInsensitivity) {
|
| + std::string normal = LoadTestData("ascii", value_type(), "unmangled");
|
| + std::string case_swap = LoadTestData("ascii", value_type(), "case_swap");
|
| + EXPECT_EQ(expected_result(),
|
| + VerifyNameMatch(der::Input(normal), der::Input(case_swap)));
|
| + EXPECT_EQ(expected_result(),
|
| + VerifyNameMatch(der::Input(case_swap), der::Input(normal)));
|
| +}
|
| +
|
| +// Verify matching folds whitespace (for the types which currently support
|
| +// normalization).
|
| +TEST_P(VerifyNameMatchNormalizationTest, CollapseWhitespace) {
|
| + std::string normal = LoadTestData("ascii", value_type(), "unmangled");
|
| + std::string whitespace =
|
| + LoadTestData("ascii", value_type(), "extra_whitespace");
|
| + EXPECT_EQ(expected_result(),
|
| + VerifyNameMatch(der::Input(normal), der::Input(whitespace)));
|
| + EXPECT_EQ(expected_result(),
|
| + VerifyNameMatch(der::Input(whitespace), der::Input(normal)));
|
| +}
|
| +
|
| +// TODO(mattm): Current implementation uses RFC 2459 rules, where only
|
| +// PRINTABLESTRING is normalized. Change the false values below to true when
|
| +// updating to RFC 5280 (at least for UTF8, handling the others is optional).
|
| +INSTANTIATE_TEST_CASE_P(
|
| + InstantiationName,
|
| + VerifyNameMatchNormalizationTest,
|
| + ::testing::Values(::testing::make_tuple(true, "PRINTABLESTRING"),
|
| + ::testing::make_tuple(false, "T61STRING"),
|
| + ::testing::make_tuple(false, "UTF8"),
|
| + ::testing::make_tuple(false, "BMPSTRING"),
|
| + ::testing::make_tuple(false, "UNIVERSALSTRING")));
|
| +
|
| +class VerifyNameMatchDifferingTypesTest
|
| + : public ::testing::TestWithParam<
|
| + ::testing::tuple<const char*, const char*>> {
|
| + public:
|
| + std::string value_type_1() const { return ::testing::get<0>(GetParam()); }
|
| + std::string value_type_2() const { return ::testing::get<1>(GetParam()); }
|
| +};
|
| +
|
| +// TODO(mattm): in RFC 2459, different value types are assumed unequal. In RFC
|
| +// 5280, different types are transcoded to Unicode, so this will need to be
|
| +// updated.
|
| +TEST_P(VerifyNameMatchDifferingTypesTest, DifferentTypesAreNonEqual) {
|
| + std::string der_1 = LoadTestData("ascii", value_type_1(), "unmangled");
|
| + std::string der_2 = LoadTestData("ascii", value_type_2(), "unmangled");
|
| + if (value_type_1() == value_type_2())
|
| + EXPECT_TRUE(VerifyNameMatch(der::Input(der_1), der::Input(der_2)));
|
| + else
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(der_1), der::Input(der_2)));
|
| +}
|
| +
|
| +INSTANTIATE_TEST_CASE_P(InstantiationName,
|
| + VerifyNameMatchDifferingTypesTest,
|
| + ::testing::Combine(::testing::ValuesIn(kValueTypes),
|
| + ::testing::ValuesIn(kValueTypes)));
|
| +
|
| +// Matching should fail if a PrintableString contains invalid characters.
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnInvalidPrintableStringChars) {
|
| + std::string normal = LoadTestData("ascii", "PRINTABLESTRING", "unmangled");
|
| + // Find a known location inside a PrintableString in the DER-encoded data.
|
| + size_t replace_location = normal.find("0123456789");
|
| + ASSERT_NE(std::string::npos, replace_location);
|
| + for (int c = 0; c < 256; ++c) {
|
| + SCOPED_TRACE(base::IntToString(c));
|
| + if (IsAsciiAlpha(c) || IsAsciiDigit(c))
|
| + continue;
|
| + switch (c) {
|
| + case ' ':
|
| + case '\'':
|
| + case '(':
|
| + case ')':
|
| + case '+':
|
| + case ',':
|
| + case '-':
|
| + case '.':
|
| + case '/':
|
| + case ':':
|
| + case '=':
|
| + case '?':
|
| + continue;
|
| + }
|
| + std::string invalid = normal.replace(replace_location, 1, 1, c);
|
| + // Verification should fail due to the invalid character.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| + }
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueExtraData) {
|
| + std::string invalid =
|
| + LoadTestData("invalid", "AttributeTypeAndValue", "extradata");
|
| + // Verification should fail due to extra element in AttributeTypeAndValue
|
| + // sequence.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueShort) {
|
| + std::string invalid =
|
| + LoadTestData("invalid", "AttributeTypeAndValue", "onlyOneElement");
|
| + // Verification should fail due to AttributeTypeAndValue sequence having only
|
| + // one element.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueEmpty) {
|
| + std::string invalid =
|
| + LoadTestData("invalid", "AttributeTypeAndValue", "empty");
|
| + // Verification should fail due to empty AttributeTypeAndValue sequence.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnBadAttributeType) {
|
| + std::string invalid =
|
| + LoadTestData("invalid", "AttributeTypeAndValue", "badAttributeType");
|
| + // Verification should fail due to Attribute Type not being an OID.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueNotSequence) {
|
| + std::string invalid =
|
| + LoadTestData("invalid", "AttributeTypeAndValue", "setNotSequence");
|
| + // Verification should fail due to AttributeTypeAndValue being a Set instead
|
| + // of a Sequence.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnRdnNotSet) {
|
| + std::string invalid = LoadTestData("invalid", "RDN", "sequenceInsteadOfSet");
|
| + // Verification should fail due to RDN being a Sequence instead of a Set.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnEmptyRdn) {
|
| + std::string invalid = LoadTestData("invalid", "RDN", "empty");
|
| + // Verification should fail due to RDN having zero AttributeTypeAndValues.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnNameNotSequence) {
|
| + std::string invalid = LoadTestData("invalid", "Name", "setInsteadOfSequence");
|
| + // Verification should fail due to Name being a Set instead of a Sequence.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnEmptyName) {
|
| + std::string invalid = LoadTestData("invalid", "Name", "empty");
|
| + // Verification should fail due to Name having zero RDNs.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| +}
|
| +
|
| +TEST(VerifyNameMatchInvalidDataTest, FailOnTrailingData) {
|
| + std::string invalid =
|
| + LoadTestData("ascii", "PRINTABLESTRING", "unmangled") + "X";
|
| + // Verification should fail due to the appended character.
|
| + EXPECT_FALSE(VerifyNameMatch(der::Input(invalid), der::Input(invalid)));
|
| }
|
|
|
| } // namespace net
|
|
|