Chromium Code Reviews| 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..7fdca240bb26a548a2e0996056804966ca1224b2 100644 |
| --- a/net/cert/internal/verify_name_match_unittest.cc |
| +++ b/net/cert/internal/verify_name_match_unittest.cc |
| @@ -4,17 +4,147 @@ |
| #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 "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< |
| + std::tr1::tuple<const char*, const char*>> { |
|
Ryan Sleevi
2015/05/13 01:27:53
use ::testing::tuple, not std::str1::tuple
mattm
2015/05/13 03:24:06
Done.
|
| + public: |
| + std::string value_type() const { return std::tr1::get<0>(GetParam()); } |
|
Ryan Sleevi
2015/05/13 01:27:53
::testing::get<0>
mattm
2015/05/13 03:24:06
Done.
|
| + std::string suffix() const { return std::tr1::get<1>(GetParam()); } |
| +}; |
| + |
| +TEST_P(VerifyNameMatchSimpleTest, ExactEquality) { |
|
Ryan Sleevi
2015/05/13 01:27:53
Document each of these tests
mattm
2015/05/13 03:24:06
Done.
|
| + std::string der = LoadTestData("ascii", value_type(), suffix()); |
| + EXPECT_TRUE(VerifyNameMatch(der::Input(der), der::Input(der))); |
| + |
| + std::string der_extra_attr = |
|
Ryan Sleevi
2015/05/13 01:27:53
document what you're testing
mattm
2015/05/13 03:24:06
Acknowledged. (I think the test comment addresses
|
| + 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 = |
|
Ryan Sleevi
2015/05/13 01:27:53
Document
mattm
2015/05/13 03:24:06
Acknowledged.
|
| + LoadTestData("ascii", value_type(), suffix() + "-extra_rdn"); |
| + EXPECT_TRUE( |
| + VerifyNameMatch(der::Input(der_extra_rdn), der::Input(der_extra_rdn))); |
| +} |
| + |
| +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))); |
| +} |
| + |
| +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<std::tr1::tuple<bool, const char*>> { |
| + public: |
| + bool expected_result() const { return std::tr1::get<0>(GetParam()); } |
| + std::string value_type() const { return std::tr1::get<1>(GetParam()); } |
| +}; |
| + |
| +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))); |
| +} |
| + |
| +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(std::tr1::make_tuple(true, "PRINTABLESTRING"), |
| + std::tr1::make_tuple(false, "T61STRING"), |
| + std::tr1::make_tuple(false, "UTF8"), |
| + std::tr1::make_tuple(false, "BMPSTRING"), |
| + std::tr1::make_tuple(false, "UNIVERSALSTRING"))); |
| + |
| +class VerifyNameMatchDifferingTypesTest |
| + : public ::testing::TestWithParam< |
| + std::tr1::tuple<const char*, const char*>> { |
| + public: |
| + std::string value_type_1() const { return std::tr1::get<0>(GetParam()); } |
| + std::string value_type_2() const { return std::tr1::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 |
|
Ryan Sleevi
2015/05/13 01:27:53
s/unicode/Unicode/
mattm
2015/05/13 03:24:06
Done.
|
| +// 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))); |
| + |
| } // namespace net |