Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: net/cert/internal/parse_certificate_unittest.cc

Issue 1288193003: Add a function for parsing RFC 5280's "Certificate". (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@test_helpers
Patch Set: rebase onto master Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "net/cert/internal/parse_certificate.h"
6
7 #include "net/cert/internal/test_helpers.h"
8 #include "net/der/input.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12
13 namespace {
14
15 std::string GetFilePath(const std::string file_name) {
16 return std::string("net/data/parse_certificate_unittest/") + file_name;
17 }
18
19 // Loads certificate data and expectations from the PEM file |file_name|.
20 // Verifies that parsing the Certificate succeeds, and each parsed field matches
21 // the expectations.
22 void EnsureParsingCertificateSucceds(const std::string& file_name) {
23 std::string data;
24 std::string expected_tbs_certificate;
25 std::string expected_signature_algorithm;
26 std::string expected_signature;
27
28 // Read the certificate data and test expectations from a single PEM file.
29 const PemBlockMapping mappings[] = {
30 {"CERTIFICATE", &data},
31 {"SIGNATURE", &expected_signature},
32 {"SIGNATURE ALGORITHM", &expected_signature_algorithm},
33 {"TBS CERTIFICATE", &expected_tbs_certificate},
34 };
35 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));
36
37 // Parsing the certificate should succeed.
38 ParsedCertificate parsed;
39 ASSERT_TRUE(ParseCertificate(InputFromString(&data), &parsed));
40
41 // Ensure that the ParsedCertificate matches expectations.
42 EXPECT_EQ(0, parsed.signature_value.unused_bits());
43 EXPECT_EQ(InputFromString(&expected_signature),
44 parsed.signature_value.bytes());
45 EXPECT_EQ(InputFromString(&expected_signature_algorithm),
46 parsed.signature_algorithm_tlv);
47 EXPECT_EQ(InputFromString(&expected_tbs_certificate),
48 parsed.tbs_certificate_tlv);
49 }
50
51 // Loads certificate data from the PEM file |file_name| and verifies that the
52 // Certificate parsing fails.
53 void EnsureParsingCertificateFails(const std::string& file_name) {
54 std::string data;
55
56 const PemBlockMapping mappings[] = {
57 {"CERTIFICATE", &data},
58 };
59
60 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));
61
62 // Parsing the Certificate should fail.
63 ParsedCertificate parsed;
64 ASSERT_FALSE(ParseCertificate(InputFromString(&data), &parsed));
65 }
66
67 // Tests parsing a Certificate.
68 TEST(ParseCertificateTest, Version3) {
69 EnsureParsingCertificateSucceds("cert_version3.pem");
70 }
71
72 // Tests parsing a simplified Certificate-like structure (the sub-fields for
73 // algorithm and tbsCertificate are not actually valid, but ParseCertificate()
74 // doesn't check them)
75 TEST(ParseCertificateTest, Skeleton) {
76 EnsureParsingCertificateSucceds("cert_skeleton.pem");
77 }
78
79 // Tests parsing a Certificate that is not a sequence fails.
80 TEST(ParseCertificateTest, NotSequence) {
81 EnsureParsingCertificateFails("cert_not_sequence.pem");
82 }
83
84 // Tests that uncomsumed data is not allowed after the main SEQUENCE.
85 TEST(ParseCertificateTest, DataAfterSignature) {
86 EnsureParsingCertificateFails("cert_data_after_signature.pem");
87 }
88
89 // Tests that parsing fails if the signature BIT STRING is missing.
90 TEST(ParseCertificateTest, MissingSignature) {
91 EnsureParsingCertificateFails("cert_missing_signature.pem");
92 }
93
94 // Tests that parsing fails if the signature is present but not a BIT STRING.
95 TEST(ParseCertificateTest, SignatureNotBitString) {
96 EnsureParsingCertificateFails("cert_signature_not_bit_string.pem");
97 }
98
99 // Tests that parsing fails if the main SEQUENCE is empty (missing all the
100 // fields).
101 TEST(ParseCertificateTest, EmptySequence) {
102 EnsureParsingCertificateFails("cert_empty_sequence.pem");
103 }
104
105 // Tests what happens when the signature algorithm is present, but has the wrong
106 // tag.
107 TEST(ParseCertificateTest, AlgorithmNotSequence) {
108 EnsureParsingCertificateFails("cert_algorithm_not_sequence.pem");
109 }
110
111 } // namespace
112
113 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698