Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "net/base/pem_tokenizer.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace net {
     10 
     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");
     19 
     20   PEMTokenizer tokenizer(string_piece, accepted_types);
     21   EXPECT_TRUE(tokenizer.GetNext());
     22 
     23   EXPECT_EQ("EXPECTED-BLOCK", tokenizer.block_type());
     24   EXPECT_EQ("MatchesAcceptedBlockType", tokenizer.data());
     25 
     26   EXPECT_FALSE(tokenizer.GetNext());
     27 }
     28 
     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");
     37 
     38   PEMTokenizer tokenizer(string_piece, accepted_types);
     39   EXPECT_TRUE(tokenizer.GetNext());
     40 
     41   EXPECT_EQ("EXPECTED-BLOCK", tokenizer.block_type());
     42   EXPECT_EQ("MatchesAcceptedBlockType", tokenizer.data());
     43 
     44   EXPECT_FALSE(tokenizer.GetNext());
     45 }
     46 
     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");
     55 
     56   PEMTokenizer tokenizer(string_piece, accepted_types);
     57   EXPECT_FALSE(tokenizer.GetNext());
     58 }
     59 
     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");
     72 
     73   PEMTokenizer tokenizer(string_piece, accepted_types);
     74   EXPECT_TRUE(tokenizer.GetNext());
     75 
     76   EXPECT_EQ("BLOCK-ONE", tokenizer.block_type());
     77   EXPECT_EQ("EncodedDataOne", tokenizer.data());
     78 
     79   EXPECT_TRUE(tokenizer.GetNext());
     80 
     81   EXPECT_EQ("BLOCK-TWO", tokenizer.block_type());
     82   EXPECT_EQ("EncodedDataTwo", tokenizer.data());
     83 
     84   EXPECT_FALSE(tokenizer.GetNext());
     85 }
     86 
     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");
     97 
     98   PEMTokenizer tokenizer(string_piece, accepted_types);
     99   EXPECT_TRUE(tokenizer.GetNext());
    100 
    101   EXPECT_EQ("MISSING-FOOTER", tokenizer.block_type());
    102   EXPECT_EQ("EncodedDataOne", tokenizer.data());
    103 
    104   EXPECT_FALSE(tokenizer.GetNext());
    105 }
    106 
    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");
    121 
    122   PEMTokenizer tokenizer(string_piece, accepted_types);
    123   EXPECT_TRUE(tokenizer.GetNext());
    124 
    125   EXPECT_EQ("BLOCK-ONE", tokenizer.block_type());
    126   EXPECT_EQ("EncodedDataThree", tokenizer.data());
    127 
    128   EXPECT_FALSE(tokenizer.GetNext());
    129 }
    130 
    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;
    138 
    139   PEMTokenizer tokenizer(string_piece, accepted_types);
    140   EXPECT_FALSE(tokenizer.GetNext());
    141 }
    142 
    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");
    159 
    160   PEMTokenizer tokenizer(string_piece, accepted_types);
    161   EXPECT_TRUE(tokenizer.GetNext());
    162 
    163   EXPECT_EQ("BLOCK-ONE", tokenizer.block_type());
    164   EXPECT_EQ("EncodedDataTwo", tokenizer.data());
    165 
    166   EXPECT_FALSE(tokenizer.GetNext());
    167 }
    168 
    169 }  // namespace net
    170