Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / cert / pem_tokenizer_unittest.cc
blobd5334db8999c155b536b3db7b3ee4598134bc72d
1 // Copyright (c) 2010 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 "net/cert/pem_tokenizer.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace net {
11 TEST(PEMTokenizerTest, BasicParsing) {
12 const char data[] =
13 "-----BEGIN EXPECTED-BLOCK-----\n"
14 "TWF0Y2hlc0FjY2VwdGVkQmxvY2tUeXBl\n"
15 "-----END EXPECTED-BLOCK-----\n";
16 base::StringPiece string_piece(data);
17 std::vector<std::string> accepted_types;
18 accepted_types.push_back("EXPECTED-BLOCK");
20 PEMTokenizer tokenizer(string_piece, accepted_types);
21 EXPECT_TRUE(tokenizer.GetNext());
23 EXPECT_EQ("EXPECTED-BLOCK", tokenizer.block_type());
24 EXPECT_EQ("MatchesAcceptedBlockType", tokenizer.data());
26 EXPECT_FALSE(tokenizer.GetNext());
29 TEST(PEMTokenizerTest, CarriageReturnLineFeeds) {
30 const char data[] =
31 "-----BEGIN EXPECTED-BLOCK-----\r\n"
32 "TWF0Y2hlc0FjY2VwdGVkQmxvY2tUeXBl\r\n"
33 "-----END EXPECTED-BLOCK-----\r\n";
34 base::StringPiece string_piece(data);
35 std::vector<std::string> accepted_types;
36 accepted_types.push_back("EXPECTED-BLOCK");
38 PEMTokenizer tokenizer(string_piece, accepted_types);
39 EXPECT_TRUE(tokenizer.GetNext());
41 EXPECT_EQ("EXPECTED-BLOCK", tokenizer.block_type());
42 EXPECT_EQ("MatchesAcceptedBlockType", tokenizer.data());
44 EXPECT_FALSE(tokenizer.GetNext());
47 TEST(PEMTokenizerTest, NoAcceptedBlockTypes) {
48 const char data[] =
49 "-----BEGIN UNEXPECTED-BLOCK-----\n"
50 "SWdub3Jlc1JlamVjdGVkQmxvY2tUeXBl\n"
51 "-----END UNEXPECTED-BLOCK-----\n";
52 base::StringPiece string_piece(data);
53 std::vector<std::string> accepted_types;
54 accepted_types.push_back("EXPECTED-BLOCK");
56 PEMTokenizer tokenizer(string_piece, accepted_types);
57 EXPECT_FALSE(tokenizer.GetNext());
60 TEST(PEMTokenizerTest, MultipleAcceptedBlockTypes) {
61 const char data[] =
62 "-----BEGIN BLOCK-ONE-----\n"
63 "RW5jb2RlZERhdGFPbmU=\n"
64 "-----END BLOCK-ONE-----\n"
65 "-----BEGIN BLOCK-TWO-----\n"
66 "RW5jb2RlZERhdGFUd28=\n"
67 "-----END BLOCK-TWO-----\n";
68 base::StringPiece string_piece(data);
69 std::vector<std::string> accepted_types;
70 accepted_types.push_back("BLOCK-ONE");
71 accepted_types.push_back("BLOCK-TWO");
73 PEMTokenizer tokenizer(string_piece, accepted_types);
74 EXPECT_TRUE(tokenizer.GetNext());
76 EXPECT_EQ("BLOCK-ONE", tokenizer.block_type());
77 EXPECT_EQ("EncodedDataOne", tokenizer.data());
79 EXPECT_TRUE(tokenizer.GetNext());
81 EXPECT_EQ("BLOCK-TWO", tokenizer.block_type());
82 EXPECT_EQ("EncodedDataTwo", tokenizer.data());
84 EXPECT_FALSE(tokenizer.GetNext());
87 TEST(PEMTokenizerTest, MissingFooter) {
88 const char data[] =
89 "-----BEGIN MISSING-FOOTER-----\n"
90 "RW5jb2RlZERhdGFPbmU=\n"
91 "-----END MISSING-FOOTER-----\n"
92 "-----BEGIN MISSING-FOOTER-----\n"
93 "RW5jb2RlZERhdGFUd28=\n";
94 base::StringPiece string_piece(data);
95 std::vector<std::string> accepted_types;
96 accepted_types.push_back("MISSING-FOOTER");
98 PEMTokenizer tokenizer(string_piece, accepted_types);
99 EXPECT_TRUE(tokenizer.GetNext());
101 EXPECT_EQ("MISSING-FOOTER", tokenizer.block_type());
102 EXPECT_EQ("EncodedDataOne", tokenizer.data());
104 EXPECT_FALSE(tokenizer.GetNext());
107 TEST(PEMTokenizerTest, NestedEncoding) {
108 const char data[] =
109 "-----BEGIN BLOCK-ONE-----\n"
110 "RW5jb2RlZERhdGFPbmU=\n"
111 "-----BEGIN BLOCK-TWO-----\n"
112 "RW5jb2RlZERhdGFUd28=\n"
113 "-----END BLOCK-TWO-----\n"
114 "-----END BLOCK-ONE-----\n"
115 "-----BEGIN BLOCK-ONE-----\n"
116 "RW5jb2RlZERhdGFUaHJlZQ==\n"
117 "-----END BLOCK-ONE-----\n";
118 base::StringPiece string_piece(data);
119 std::vector<std::string> accepted_types;
120 accepted_types.push_back("BLOCK-ONE");
122 PEMTokenizer tokenizer(string_piece, accepted_types);
123 EXPECT_TRUE(tokenizer.GetNext());
125 EXPECT_EQ("BLOCK-ONE", tokenizer.block_type());
126 EXPECT_EQ("EncodedDataThree", tokenizer.data());
128 EXPECT_FALSE(tokenizer.GetNext());
131 TEST(PEMTokenizerTest, EmptyAcceptedTypes) {
132 const char data[] =
133 "-----BEGIN BLOCK-ONE-----\n"
134 "RW5jb2RlZERhdGFPbmU=\n"
135 "-----END BLOCK-ONE-----\n";
136 base::StringPiece string_piece(data);
137 std::vector<std::string> accepted_types;
139 PEMTokenizer tokenizer(string_piece, accepted_types);
140 EXPECT_FALSE(tokenizer.GetNext());
143 TEST(PEMTokenizerTest, BlockWithHeader) {
144 const char data[] =
145 "-----BEGIN BLOCK-ONE-----\n"
146 "Header-One: Data data data\n"
147 "Header-Two: \n"
148 " continuation\n"
149 "Header-Three: Mix-And,Match\n"
150 "\n"
151 "RW5jb2RlZERhdGFPbmU=\n"
152 "-----END BLOCK-ONE-----\n"
153 "-----BEGIN BLOCK-ONE-----\n"
154 "RW5jb2RlZERhdGFUd28=\n"
155 "-----END BLOCK-ONE-----\n";
156 base::StringPiece string_piece(data);
157 std::vector<std::string> accepted_types;
158 accepted_types.push_back("BLOCK-ONE");
160 PEMTokenizer tokenizer(string_piece, accepted_types);
161 EXPECT_TRUE(tokenizer.GetNext());
163 EXPECT_EQ("BLOCK-ONE", tokenizer.block_type());
164 EXPECT_EQ("EncodedDataTwo", tokenizer.data());
166 EXPECT_FALSE(tokenizer.GetNext());
169 } // namespace net