1 // Copyright 2014 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 "ios/web/public/cert_policy.h"
7 #include "base/memory/ref_counted.h"
8 #include "net/cert/x509_certificate.h"
9 #include "net/test/test_certificate_data.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(CertPolicyTest
, Policy
) {
15 scoped_refptr
<net::X509Certificate
> google_cert(
16 net::X509Certificate::CreateFromBytes(
17 reinterpret_cast<const char*>(google_der
), sizeof(google_der
)));
19 scoped_refptr
<net::X509Certificate
> webkit_cert(
20 net::X509Certificate::CreateFromBytes(
21 reinterpret_cast<const char*>(webkit_der
), sizeof(webkit_der
)));
25 // To begin with, everything should be unknown.
26 EXPECT_EQ(CertPolicy::UNKNOWN
,
27 policy
.Check(google_cert
.get(), net::CERT_STATUS_DATE_INVALID
));
30 policy
.Check(webkit_cert
.get(), net::CERT_STATUS_COMMON_NAME_INVALID
));
32 // Test adding one certificate with one error.
33 policy
.Allow(google_cert
.get(), net::CERT_STATUS_DATE_INVALID
);
34 EXPECT_EQ(CertPolicy::ALLOWED
,
35 policy
.Check(google_cert
.get(), net::CERT_STATUS_DATE_INVALID
));
38 policy
.Check(google_cert
.get(), net::CERT_STATUS_COMMON_NAME_INVALID
));
39 EXPECT_EQ(CertPolicy::UNKNOWN
,
40 policy
.Check(google_cert
.get(),
41 net::CERT_STATUS_DATE_INVALID
|
42 net::CERT_STATUS_COMMON_NAME_INVALID
));
45 policy
.Check(webkit_cert
.get(), net::CERT_STATUS_COMMON_NAME_INVALID
));
47 // Test saving the same certificate with a new error.
48 policy
.Allow(google_cert
.get(), net::CERT_STATUS_AUTHORITY_INVALID
);
49 EXPECT_EQ(CertPolicy::UNKNOWN
,
50 policy
.Check(google_cert
.get(), net::CERT_STATUS_DATE_INVALID
));
53 policy
.Check(google_cert
.get(), net::CERT_STATUS_AUTHORITY_INVALID
));
56 policy
.Check(webkit_cert
.get(), net::CERT_STATUS_COMMON_NAME_INVALID
));
58 // Test adding one certificate with two errors.
61 net::CERT_STATUS_DATE_INVALID
| net::CERT_STATUS_AUTHORITY_INVALID
);
62 EXPECT_EQ(CertPolicy::ALLOWED
,
63 policy
.Check(google_cert
.get(), net::CERT_STATUS_DATE_INVALID
));
66 policy
.Check(google_cert
.get(), net::CERT_STATUS_AUTHORITY_INVALID
));
69 policy
.Check(google_cert
.get(), net::CERT_STATUS_COMMON_NAME_INVALID
));
72 policy
.Check(webkit_cert
.get(), net::CERT_STATUS_COMMON_NAME_INVALID
));