Roll src/third_party/WebKit 28cb7a5:eda0012 (svn 190243:190250)
[chromium-blink-merge.git] / ppapi / tests / test_x509_certificate_private.cc
blobfab2f210d714c0b8aaffb8b04057fdee4bddab5b
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 "ppapi/tests/test_x509_certificate_private.h"
7 #include <cmath>
8 #include <limits>
10 #include "ppapi/cpp/private/x509_certificate_private.h"
11 #include "ppapi/cpp/var_array_buffer.h"
12 #include "ppapi/cpp/var.h"
13 #include "ppapi/tests/testing_instance.h"
15 REGISTER_TEST_CASE(X509CertificatePrivate);
17 namespace {
19 bool FieldMatchesString(
20 const pp::X509CertificatePrivate& certificate,
21 PP_X509Certificate_Private_Field field,
22 const std::string& expected) {
23 pp::Var field_value = certificate.GetField(field);
24 if (!field_value.is_string())
25 return false;
26 return field_value.AsString() == expected;
29 bool FieldMatchesDouble(
30 const pp::X509CertificatePrivate& certificate,
31 PP_X509Certificate_Private_Field field,
32 double expected) {
33 pp::Var field_value = certificate.GetField(field);
34 if (!field_value.is_double())
35 return false;
36 return std::fabs(field_value.AsDouble() - expected) <=
37 std::numeric_limits<double>::epsilon();
40 bool FieldMatchesBuffer(
41 const pp::X509CertificatePrivate& certificate,
42 PP_X509Certificate_Private_Field field,
43 const char* expected,
44 uint32_t expected_length) {
45 pp::Var field_value = certificate.GetField(field);
46 if (!field_value.is_array_buffer())
47 return false;
48 pp::VarArrayBuffer array_buffer(field_value);
49 char* bytes = static_cast<char*>(array_buffer.Map());
50 uint32_t length = array_buffer.ByteLength();
51 if (length != expected_length)
52 return false;
53 return std::equal(expected, expected + expected_length, bytes);
56 bool FieldIsNull(
57 const pp::X509CertificatePrivate& certificate,
58 PP_X509Certificate_Private_Field field) {
59 return certificate.GetField(field).is_null();
62 // Google's cert.
63 const unsigned char kGoogleDer[] = {
64 0x30, 0x82, 0x03, 0x21, 0x30, 0x82, 0x02, 0x8a, 0xa0, 0x03, 0x02, 0x01,
65 0x02, 0x02, 0x10, 0x01, 0x2a, 0x39, 0x76, 0x0d, 0x3f, 0x4f, 0xc9, 0x0b,
66 0xe7, 0xbd, 0x2b, 0xcf, 0x95, 0x2e, 0x7a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
67 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x4c,
68 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x5a,
69 0x41, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x1c,
70 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x75,
71 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x28, 0x50, 0x74, 0x79, 0x29, 0x20,
72 0x4c, 0x74, 0x64, 0x2e, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04,
73 0x03, 0x13, 0x0d, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, 0x53, 0x47,
74 0x43, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x39, 0x30, 0x33,
75 0x32, 0x37, 0x32, 0x32, 0x32, 0x30, 0x30, 0x37, 0x5a, 0x17, 0x0d, 0x31,
76 0x30, 0x30, 0x33, 0x32, 0x37, 0x32, 0x32, 0x32, 0x30, 0x30, 0x37, 0x5a,
77 0x30, 0x68, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
78 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
79 0x13, 0x0a, 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61,
80 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x0d, 0x4d,
81 0x6f, 0x75, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x20, 0x56, 0x69, 0x65, 0x77,
82 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0a, 0x47,
83 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x31, 0x17, 0x30,
84 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0e, 0x77, 0x77, 0x77, 0x2e,
85 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x81,
86 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
87 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02,
88 0x81, 0x81, 0x00, 0xd6, 0xb9, 0xe1, 0xad, 0xb8, 0x61, 0x0b, 0x1f, 0x4e,
89 0xb6, 0x3c, 0x09, 0x3d, 0xab, 0xe8, 0xe3, 0x2b, 0xb6, 0xe8, 0xa4, 0x3a,
90 0x78, 0x2f, 0xd3, 0x51, 0x20, 0x22, 0x45, 0x95, 0xd8, 0x00, 0x91, 0x33,
91 0x9a, 0xa7, 0xa2, 0x48, 0xea, 0x30, 0x57, 0x26, 0x97, 0x66, 0xc7, 0x5a,
92 0xef, 0xf1, 0x9b, 0x0c, 0x3f, 0xe1, 0xb9, 0x7f, 0x7b, 0xc3, 0xc7, 0xcc,
93 0xaf, 0x9c, 0xd0, 0x1f, 0x3c, 0x81, 0x15, 0x10, 0x58, 0xfc, 0x06, 0xb3,
94 0xbf, 0xbc, 0x9c, 0x02, 0xb9, 0x51, 0xdc, 0xfb, 0xa6, 0xb9, 0x17, 0x42,
95 0xe6, 0x46, 0xe7, 0x22, 0xcf, 0x6c, 0x27, 0x10, 0xfe, 0x54, 0xe6, 0x92,
96 0x6c, 0x0c, 0x60, 0x76, 0x9a, 0xce, 0xf8, 0x7f, 0xac, 0xb8, 0x5a, 0x08,
97 0x4a, 0xdc, 0xb1, 0x64, 0xbd, 0xa0, 0x74, 0x41, 0xb2, 0xac, 0x8f, 0x86,
98 0x9d, 0x1a, 0xde, 0x58, 0x09, 0xfd, 0x6c, 0x0a, 0x25, 0xe0, 0x79, 0x02,
99 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81, 0xe7, 0x30, 0x81, 0xe4, 0x30, 0x28,
100 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x21, 0x30, 0x1f, 0x06, 0x08, 0x2b,
101 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b, 0x06, 0x01,
102 0x05, 0x05, 0x07, 0x03, 0x02, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86,
103 0xf8, 0x42, 0x04, 0x01, 0x30, 0x36, 0x06, 0x03, 0x55, 0x1d, 0x1f, 0x04,
104 0x2f, 0x30, 0x2d, 0x30, 0x2b, 0xa0, 0x29, 0xa0, 0x27, 0x86, 0x25, 0x68,
105 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c, 0x2e, 0x74, 0x68,
106 0x61, 0x77, 0x74, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x54, 0x68, 0x61,
107 0x77, 0x74, 0x65, 0x53, 0x47, 0x43, 0x43, 0x41, 0x2e, 0x63, 0x72, 0x6c,
108 0x30, 0x72, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x01,
109 0x04, 0x66, 0x30, 0x64, 0x30, 0x22, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
110 0x05, 0x07, 0x30, 0x01, 0x86, 0x16, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
111 0x2f, 0x6f, 0x63, 0x73, 0x70, 0x2e, 0x74, 0x68, 0x61, 0x77, 0x74, 0x65,
112 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x3e, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
113 0x05, 0x07, 0x30, 0x02, 0x86, 0x32, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
114 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x74, 0x68, 0x61, 0x77, 0x74, 0x65, 0x2e,
115 0x63, 0x6f, 0x6d, 0x2f, 0x72, 0x65, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x6f,
116 0x72, 0x79, 0x2f, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x5f, 0x53, 0x47,
117 0x43, 0x5f, 0x43, 0x41, 0x2e, 0x63, 0x72, 0x74, 0x30, 0x0c, 0x06, 0x03,
118 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0d,
119 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05,
120 0x00, 0x03, 0x81, 0x81, 0x00, 0x39, 0xb6, 0xfb, 0x11, 0xbc, 0x33, 0x2c,
121 0xc3, 0x90, 0x48, 0xe3, 0x6e, 0xc3, 0x9b, 0x38, 0xb1, 0x42, 0xd1, 0x00,
122 0x09, 0x58, 0x63, 0xa0, 0xe1, 0x98, 0x1c, 0x85, 0xf2, 0xef, 0x10, 0x1d,
123 0x60, 0x4e, 0x51, 0x09, 0x62, 0xf5, 0x05, 0xbd, 0x9d, 0x4f, 0x87, 0x6c,
124 0x98, 0x72, 0x07, 0x80, 0xc3, 0x59, 0x48, 0x14, 0xe2, 0xd6, 0xef, 0xd0,
125 0x8f, 0x33, 0x6a, 0x68, 0x31, 0xfa, 0xb7, 0xbb, 0x85, 0xcc, 0xf7, 0xc7,
126 0x47, 0x7b, 0x67, 0x93, 0x3c, 0xc3, 0x16, 0x51, 0x9b, 0x6f, 0x87, 0x20,
127 0xfd, 0x67, 0x4c, 0x2b, 0xea, 0x6a, 0x49, 0xdb, 0x11, 0xd1, 0xbd, 0xd7,
128 0x95, 0x22, 0x43, 0x7a, 0x06, 0x7b, 0x4e, 0xf6, 0x37, 0x8e, 0xa2, 0xb9,
129 0xcf, 0x1f, 0xa5, 0xd2, 0xbd, 0x3b, 0x04, 0x97, 0x39, 0xb3, 0x0f, 0xfa,
130 0x38, 0xb5, 0xaf, 0x55, 0x20, 0x88, 0x60, 0x93, 0xf2, 0xde, 0xdb, 0xff,
131 0xdf
134 } // namespace
136 TestX509CertificatePrivate::TestX509CertificatePrivate(
137 TestingInstance* instance)
138 : TestCase(instance) {
141 bool TestX509CertificatePrivate::Init() {
142 if (!pp::X509CertificatePrivate::IsAvailable())
143 return false;
145 return true;
148 void TestX509CertificatePrivate::RunTests(const std::string& filter) {
149 RUN_TEST_FORCEASYNC_AND_NOT(ValidCertificate, filter);
150 RUN_TEST_FORCEASYNC_AND_NOT(InvalidCertificate, filter);
153 std::string TestX509CertificatePrivate::TestValidCertificate() {
154 pp::X509CertificatePrivate certificate(instance_);
155 bool successful = certificate.Initialize(
156 reinterpret_cast<const char*>(kGoogleDer), sizeof(kGoogleDer));
157 ASSERT_TRUE(successful);
159 ASSERT_TRUE(FieldMatchesString(certificate,
160 PP_X509CERTIFICATE_PRIVATE_SUBJECT_COMMON_NAME, "www.google.com"));
161 ASSERT_TRUE(FieldMatchesString(certificate,
162 PP_X509CERTIFICATE_PRIVATE_SUBJECT_LOCALITY_NAME, "Mountain View"));
163 ASSERT_TRUE(FieldMatchesString(certificate,
164 PP_X509CERTIFICATE_PRIVATE_SUBJECT_STATE_OR_PROVINCE_NAME, "California"));
165 ASSERT_TRUE(FieldMatchesString(certificate,
166 PP_X509CERTIFICATE_PRIVATE_SUBJECT_COUNTRY_NAME, "US"));
167 ASSERT_TRUE(FieldMatchesString(certificate,
168 PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_NAME, "Google Inc"));
169 ASSERT_TRUE(FieldMatchesString(
170 certificate,
171 PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_UNIT_NAME,
172 std::string()));
174 ASSERT_TRUE(FieldMatchesString(certificate,
175 PP_X509CERTIFICATE_PRIVATE_ISSUER_COMMON_NAME, "Thawte SGC CA"));
176 ASSERT_TRUE(
177 FieldMatchesString(certificate,
178 PP_X509CERTIFICATE_PRIVATE_ISSUER_LOCALITY_NAME,
179 std::string()));
180 ASSERT_TRUE(FieldMatchesString(
181 certificate,
182 PP_X509CERTIFICATE_PRIVATE_ISSUER_STATE_OR_PROVINCE_NAME,
183 std::string()));
184 ASSERT_TRUE(FieldMatchesString(
185 certificate, PP_X509CERTIFICATE_PRIVATE_ISSUER_COUNTRY_NAME, "ZA"));
186 ASSERT_TRUE(FieldMatchesString(certificate,
187 PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_NAME,
188 "Thawte Consulting (Pty) Ltd."));
189 ASSERT_TRUE(FieldMatchesString(
190 certificate,
191 PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_UNIT_NAME,
192 std::string()));
194 ASSERT_FALSE(FieldIsNull(certificate,
195 PP_X509CERTIFICATE_PRIVATE_SERIAL_NUMBER));
196 ASSERT_TRUE(FieldMatchesDouble(certificate,
197 PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_BEFORE, 1238192407));
198 ASSERT_TRUE(FieldMatchesDouble(certificate,
199 PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_AFTER, 1269728407));
200 ASSERT_TRUE(FieldMatchesBuffer(certificate,
201 PP_X509CERTIFICATE_PRIVATE_RAW,
202 reinterpret_cast<const char*>(kGoogleDer), sizeof(kGoogleDer)));
204 // Check unimplemented fields return null.
205 ASSERT_TRUE(FieldIsNull(certificate,
206 PP_X509CERTIFICATE_PRIVATE_SUBJECT_UNIQUE_ID));
207 ASSERT_TRUE(FieldIsNull(certificate,
208 PP_X509CERTIFICATE_PRIVATE_ISSUER_UNIQUE_ID));
209 ASSERT_TRUE(FieldIsNull(certificate,
210 PP_X509CERTIFICATE_PRIVATE_SUBJECT_DISTINGUISHED_NAME));
211 ASSERT_TRUE(FieldIsNull(certificate,
212 PP_X509CERTIFICATE_PRIVATE_ISSUER_DISTINGUISHED_NAME));
213 ASSERT_TRUE(FieldIsNull(certificate,
214 PP_X509CERTIFICATE_PRIVATE_VERSION));
215 ASSERT_TRUE(FieldIsNull(certificate,
216 PP_X509CERTIFICATE_PRIVATE_SIGNATURE_ALGORITHM_OID));
217 ASSERT_TRUE(FieldIsNull(certificate,
218 PP_X509CERTIFICATE_PRIVATE_SIGNATURE_ALGORITHM_PARAMATERS_RAW));
219 ASSERT_TRUE(FieldIsNull(certificate,
220 PP_X509CERTIFICATE_PRIVATE_SUBJECT_PUBLIC_KEY));
222 PASS();
225 std::string TestX509CertificatePrivate::TestInvalidCertificate() {
226 bool successful;
227 pp::X509CertificatePrivate null_certificate(instance_);
228 successful = null_certificate.Initialize(NULL, 0);
229 ASSERT_FALSE(successful);
231 pp::X509CertificatePrivate bad_certificate(instance_);
232 successful = bad_certificate.Initialize("acbde", 0);
233 ASSERT_FALSE(successful);
235 PASS();