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
3 // found in the LICENSE file.
5 #include "chrome/common/net/x509_certificate_model.h"
7 #include "base/files/file_path.h"
8 #include "base/path_service.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/test/cert_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 #include "net/cert/nss_cert_database.h"
17 TEST(X509CertificateModelTest
, GetTypeCA
) {
18 scoped_refptr
<net::X509Certificate
> cert(
19 net::ImportCertFromFile(net::GetTestCertsDirectory(),
21 ASSERT_TRUE(cert
.get());
23 #if defined(USE_OPENSSL)
24 // Remove this when OpenSSL build implements the necessary functions.
25 EXPECT_EQ(net::OTHER_CERT
,
26 x509_certificate_model::GetType(cert
->os_cert_handle()));
28 EXPECT_EQ(net::CA_CERT
,
29 x509_certificate_model::GetType(cert
->os_cert_handle()));
31 // Test that explicitly distrusted CA certs are still returned as CA_CERT
32 // type. See http://crbug.com/96654.
33 EXPECT_TRUE(net::NSSCertDatabase::GetInstance()->SetCertTrust(
34 cert
.get(), net::CA_CERT
, net::NSSCertDatabase::DISTRUSTED_SSL
));
36 EXPECT_EQ(net::CA_CERT
,
37 x509_certificate_model::GetType(cert
->os_cert_handle()));
41 TEST(X509CertificateModelTest
, GetTypeServer
) {
42 scoped_refptr
<net::X509Certificate
> cert(
43 net::ImportCertFromFile(net::GetTestCertsDirectory(),
44 "google.single.der"));
45 ASSERT_TRUE(cert
.get());
47 #if defined(USE_OPENSSL)
48 // Remove this when OpenSSL build implements the necessary functions.
49 EXPECT_EQ(net::OTHER_CERT
,
50 x509_certificate_model::GetType(cert
->os_cert_handle()));
52 // Test mozilla_security_manager::GetCertType with server certs and default
53 // trust. Currently this doesn't work.
54 // TODO(mattm): make mozilla_security_manager::GetCertType smarter so we can
55 // tell server certs even if they have no trust bits set.
56 EXPECT_EQ(net::OTHER_CERT
,
57 x509_certificate_model::GetType(cert
->os_cert_handle()));
59 net::NSSCertDatabase
* cert_db
= net::NSSCertDatabase::GetInstance();
60 // Test GetCertType with server certs and explicit trust.
61 EXPECT_TRUE(cert_db
->SetCertTrust(
62 cert
.get(), net::SERVER_CERT
, net::NSSCertDatabase::TRUSTED_SSL
));
64 EXPECT_EQ(net::SERVER_CERT
,
65 x509_certificate_model::GetType(cert
->os_cert_handle()));
67 // Test GetCertType with server certs and explicit distrust.
68 EXPECT_TRUE(cert_db
->SetCertTrust(
69 cert
.get(), net::SERVER_CERT
, net::NSSCertDatabase::DISTRUSTED_SSL
));
71 EXPECT_EQ(net::SERVER_CERT
,
72 x509_certificate_model::GetType(cert
->os_cert_handle()));
76 // An X.509 v1 certificate with the version field omitted should get
77 // the default value v1.
78 TEST(X509CertificateModelTest
, GetVersionOmitted
) {
79 scoped_refptr
<net::X509Certificate
> cert(
80 net::ImportCertFromFile(net::GetTestCertsDirectory(),
82 ASSERT_TRUE(cert
.get());
84 EXPECT_EQ("1", x509_certificate_model::GetVersion(cert
->os_cert_handle()));