OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/net/x509_certificate_model.h" | 5 #include "chrome/common/net/x509_certificate_model.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "net/base/cert_database.h" | 10 #include "net/base/cert_database.h" |
| 11 #include "net/base/cert_test_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 class X509CertificateModelTest : public testing::Test { | 14 TEST(X509CertificateModelTest, GetTypeCA) { |
14 protected: | 15 scoped_refptr<net::X509Certificate> cert( |
15 static std::string ReadTestFile(const std::string& name) { | 16 net::ImportCertFromFile(net::GetTestCertsDirectory(), |
16 std::string result; | 17 "root_ca_cert.crt")); |
17 FilePath cert_path = GetTestCertsDirectory().AppendASCII(name); | 18 ASSERT_TRUE(cert.get()); |
18 EXPECT_TRUE(file_util::ReadFileToString(cert_path, &result)); | |
19 return result; | |
20 } | |
21 | |
22 private: | |
23 // Returns a FilePath object representing the src/net/data/ssl/certificates | |
24 // directory in the source tree. | |
25 static FilePath GetTestCertsDirectory() { | |
26 FilePath certs_dir; | |
27 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); | |
28 certs_dir = certs_dir.AppendASCII("net"); | |
29 certs_dir = certs_dir.AppendASCII("data"); | |
30 certs_dir = certs_dir.AppendASCII("ssl"); | |
31 certs_dir = certs_dir.AppendASCII("certificates"); | |
32 return certs_dir; | |
33 } | |
34 }; | |
35 | |
36 TEST_F(X509CertificateModelTest, GetTypeCA) { | |
37 std::string cert_data = ReadTestFile("root_ca_cert.crt"); | |
38 | |
39 net::CertificateList certs = | |
40 net::X509Certificate::CreateCertificateListFromBytes( | |
41 cert_data.data(), cert_data.size(), | |
42 net::X509Certificate::FORMAT_AUTO); | |
43 ASSERT_EQ(1U, certs.size()); | |
44 | 19 |
45 #if defined(USE_OPENSSL) | 20 #if defined(USE_OPENSSL) |
46 // Remove this when OpenSSL build implements the necessary functions. | 21 // Remove this when OpenSSL build implements the necessary functions. |
47 EXPECT_EQ(net::UNKNOWN_CERT, | 22 EXPECT_EQ(net::UNKNOWN_CERT, |
48 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 23 x509_certificate_model::GetType(cert->os_cert_handle())); |
49 #else | 24 #else |
50 EXPECT_EQ(net::CA_CERT, | 25 EXPECT_EQ(net::CA_CERT, |
51 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 26 x509_certificate_model::GetType(cert->os_cert_handle())); |
52 | 27 |
53 // Test that explicitly distrusted CA certs are still returned as CA_CERT | 28 // Test that explicitly distrusted CA certs are still returned as CA_CERT |
54 // type. See http://crbug.com/96654. | 29 // type. See http://crbug.com/96654. |
55 net::CertDatabase cert_db; | 30 net::CertDatabase cert_db; |
56 // TODO(mattm): This depends on the implementation details of SetCertTrust | 31 // TODO(mattm): This depends on the implementation details of SetCertTrust |
57 // where calling with SERVER_CERT and UNTRUSTED causes a cert to be explicitly | 32 // where calling with SERVER_CERT and UNTRUSTED causes a cert to be explicitly |
58 // distrusted (trust set to CERTDB_TERMINAL_RECORD). See | 33 // distrusted (trust set to CERTDB_TERMINAL_RECORD). See |
59 // http://crbug.com/116411. When I fix that bug I'll also add a way to set | 34 // http://crbug.com/116411. When I fix that bug I'll also add a way to set |
60 // this directly. | 35 // this directly. |
61 EXPECT_TRUE(cert_db.SetCertTrust(certs[0], net::SERVER_CERT, | 36 EXPECT_TRUE(cert_db.SetCertTrust(cert, net::SERVER_CERT, |
62 net::CertDatabase::UNTRUSTED)); | 37 net::CertDatabase::UNTRUSTED)); |
63 | 38 |
64 EXPECT_EQ(net::CA_CERT, | 39 EXPECT_EQ(net::CA_CERT, |
65 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 40 x509_certificate_model::GetType(cert->os_cert_handle())); |
66 #endif | 41 #endif |
67 } | 42 } |
68 | 43 |
69 TEST_F(X509CertificateModelTest, GetTypeServer) { | 44 TEST(X509CertificateModelTest, GetTypeServer) { |
70 std::string cert_data = ReadTestFile("google.single.der"); | 45 scoped_refptr<net::X509Certificate> cert( |
71 | 46 net::ImportCertFromFile(net::GetTestCertsDirectory(), |
72 net::CertificateList certs = | 47 "google.single.der")); |
73 net::X509Certificate::CreateCertificateListFromBytes( | 48 ASSERT_TRUE(cert.get()); |
74 cert_data.data(), cert_data.size(), | |
75 net::X509Certificate::FORMAT_AUTO); | |
76 ASSERT_EQ(1U, certs.size()); | |
77 | 49 |
78 #if defined(USE_OPENSSL) | 50 #if defined(USE_OPENSSL) |
79 // Remove this when OpenSSL build implements the necessary functions. | 51 // Remove this when OpenSSL build implements the necessary functions. |
80 EXPECT_EQ(net::UNKNOWN_CERT, | 52 EXPECT_EQ(net::UNKNOWN_CERT, |
81 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 53 x509_certificate_model::GetType(cert->os_cert_handle())); |
82 #else | 54 #else |
83 // TODO(mattm): make GetCertType smarter so we can tell server certs even if | 55 // TODO(mattm): make GetCertType smarter so we can tell server certs even if |
84 // they have no trust bits set. | 56 // they have no trust bits set. |
85 EXPECT_EQ(net::UNKNOWN_CERT, | 57 EXPECT_EQ(net::UNKNOWN_CERT, |
86 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 58 x509_certificate_model::GetType(cert->os_cert_handle())); |
87 | 59 |
88 net::CertDatabase cert_db; | 60 net::CertDatabase cert_db; |
89 EXPECT_TRUE(cert_db.SetCertTrust(certs[0], net::SERVER_CERT, | 61 EXPECT_TRUE(cert_db.SetCertTrust(cert, net::SERVER_CERT, |
90 net::CertDatabase::TRUSTED_SSL)); | 62 net::CertDatabase::TRUSTED_SSL)); |
91 | 63 |
92 EXPECT_EQ(net::SERVER_CERT, | 64 EXPECT_EQ(net::SERVER_CERT, |
93 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 65 x509_certificate_model::GetType(cert->os_cert_handle())); |
94 | 66 |
95 EXPECT_TRUE(cert_db.SetCertTrust(certs[0], net::SERVER_CERT, | 67 EXPECT_TRUE(cert_db.SetCertTrust(cert, net::SERVER_CERT, |
96 net::CertDatabase::UNTRUSTED)); | 68 net::CertDatabase::UNTRUSTED)); |
97 | 69 |
98 EXPECT_EQ(net::SERVER_CERT, | 70 EXPECT_EQ(net::SERVER_CERT, |
99 x509_certificate_model::GetType(certs[0]->os_cert_handle())); | 71 x509_certificate_model::GetType(cert->os_cert_handle())); |
100 #endif | 72 #endif |
101 } | 73 } |
OLD | NEW |