Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 "shill/certificate_file.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/file_path.h> |
| 10 | #include <base/file_util.h> |
| 11 | #include <base/files/scoped_temp_dir.h> |
| 12 | #include <base/stringprintf.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | #include "shill/mock_glib.h" |
| 16 | |
| 17 | using base::FilePath; |
| 18 | using base::StringPrintf; |
| 19 | using std::string; |
| 20 | using testing::_; |
| 21 | using testing::Return; |
| 22 | using testing::SetArgumentPointee; |
| 23 | using testing::StrEq; |
| 24 | |
| 25 | namespace shill { |
| 26 | |
| 27 | class CertificateFileTest : public testing::Test { |
| 28 | public: |
| 29 | CertificateFileTest() : certificate_file_(&glib_) {} |
| 30 | |
| 31 | virtual void SetUp() { |
| 32 | CHECK(temp_dir_.CreateUniqueTempDir()); |
| 33 | certificate_directory_ = temp_dir_.path().Append("certificates"); |
| 34 | certificate_file_.set_root_directory(certificate_directory_); |
| 35 | } |
| 36 | |
| 37 | protected: |
| 38 | static const char kDERData[]; |
| 39 | static const char kPEMData[]; |
| 40 | |
| 41 | string ExtractHexData(const std::string &pem_data) { |
| 42 | return CertificateFile::ExtractHexData(pem_data); |
| 43 | } |
| 44 | const FilePath &GetOutputFile() { return certificate_file_.output_file_; } |
| 45 | const FilePath &GetRootDirectory() { |
| 46 | return certificate_file_.root_directory_; |
| 47 | } |
| 48 | const char *GetPEMHeader() { return CertificateFile::kPEMHeader; } |
| 49 | const char *GetPEMFooter() { return CertificateFile::kPEMFooter; } |
| 50 | |
| 51 | CertificateFile certificate_file_; |
| 52 | MockGLib glib_; |
| 53 | base::ScopedTempDir temp_dir_; |
| 54 | base::FilePath certificate_directory_; |
| 55 | }; |
| 56 | |
| 57 | const char CertificateFileTest::kDERData[] = |
| 58 | "This does not have to be a real certificate " |
| 59 | "since we are not testing its validity."; |
| 60 | const char CertificateFileTest::kPEMData[] = |
| 61 | "VGhpcyBkb2VzIG5vdCBoYXZlIHRvIGJlIGEgcmVhbCBjZXJ0aWZpY2F0ZSBzaW5j\n" |
| 62 | "ZSB3ZSBhcmUgbm90IHRlc3RpbmcgaXRzIHZhbGlkaXR5Lgo=\n"; |
| 63 | |
| 64 | |
| 65 | TEST_F(CertificateFileTest, Construction) { |
| 66 | EXPECT_TRUE(GetRootDirectory() == certificate_directory_); |
| 67 | EXPECT_FALSE(file_util::PathExists(GetRootDirectory())); |
| 68 | EXPECT_TRUE(GetOutputFile().empty()); |
| 69 | } |
| 70 | |
| 71 | TEST_F(CertificateFileTest, CreatePEMFromString) { |
| 72 | // Create a formatted PEM file from the inner HEX data. |
| 73 | FilePath outfile0 = certificate_file_.CreatePEMFromString(kPEMData); |
| 74 | EXPECT_FALSE(outfile0.empty()); |
| 75 | EXPECT_TRUE(file_util::PathExists(outfile0)); |
| 76 | EXPECT_TRUE(file_util::ContainsPath(certificate_directory_, outfile0)); |
| 77 | string file_string0; |
| 78 | string expected_output = StringPrintf( |
| 79 | "%s\n%s%s\n", GetPEMHeader(), kPEMData, GetPEMFooter()); |
| 80 | EXPECT_TRUE(file_util::ReadFileToString(outfile0, &file_string0)); |
| 81 | EXPECT_EQ(expected_output, file_string0); |
| 82 | // Create a formatted PEM file from formatted PEM. |
| 83 | FilePath outfile1 = certificate_file_.CreatePEMFromString(expected_output); |
| 84 | EXPECT_FALSE(outfile1.empty()); |
| 85 | EXPECT_TRUE(file_util::PathExists(outfile1)); |
| 86 | EXPECT_FALSE(file_util::PathExists(outfile0)); // Old file is deleted. |
| 87 | string file_string1; |
| 88 | EXPECT_TRUE(file_util::ReadFileToString(outfile1, &file_string1)); |
| 89 | EXPECT_EQ(expected_output, file_string1); |
| 90 | |
| 91 | // Fail to create a PEM file. Old file should not have been deleted. |
| 92 | FilePath outfile2 = certificate_file_.CreatePEMFromString(""); |
| 93 | EXPECT_TRUE(outfile2.empty()); |
| 94 | EXPECT_TRUE(file_util::PathExists(outfile1)); |
| 95 | } |
| 96 | |
| 97 | TEST_F(CertificateFileTest, CreateDERFromString) { |
| 98 | // Create a DER file from the inner HEX data. |
| 99 | const string kPEMString = kPEMData; |
Christopher Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 100 | const string fake_data("this is a fake"); |
| 101 | |
| 102 | EXPECT_CALL(glib_, B64Decode(StrEq(kPEMString), _)) |
| 103 | .WillOnce(Return(false)) |
| 104 | .WillOnce(DoAll(SetArgumentPointee<1>(fake_data), |
| 105 | Return(true))); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(certificate_file_.CreateDERFromString(kPEMData).empty()); |
| 107 | |
| 108 | FilePath outfile = certificate_file_.CreateDERFromString(kPEMData); |
| 109 | EXPECT_FALSE(outfile.empty()); |
| 110 | EXPECT_TRUE(file_util::PathExists(outfile)); |
| 111 | string file_string; |
| 112 | EXPECT_TRUE(file_util::ReadFileToString(outfile, &file_string)); |
Christopher Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 113 | EXPECT_EQ(fake_data, file_string); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(CertificateFileTest, ExtractHexData) { |
| 117 | EXPECT_EQ("", ExtractHexData("")); |
| 118 | EXPECT_EQ("foo\n", ExtractHexData("foo")); |
| 119 | EXPECT_EQ("foo\nbar\n", ExtractHexData("foo\r\n\t\n bar\n")); |
| 120 | EXPECT_EQ("", ExtractHexData( |
| 121 | StringPrintf("%s\nfoo\nbar\n%s\n", GetPEMFooter(), GetPEMHeader()))); |
| 122 | EXPECT_EQ("", ExtractHexData( |
| 123 | StringPrintf("%s\nfoo\nbar\n%s\n", GetPEMHeader(), GetPEMHeader()))); |
| 124 | EXPECT_EQ("", ExtractHexData( |
| 125 | StringPrintf("%s\nfoo\nbar\n", GetPEMHeader()))); |
| 126 | EXPECT_EQ("", ExtractHexData( |
| 127 | StringPrintf("foo\nbar\n%s\n", GetPEMFooter()))); |
| 128 | EXPECT_EQ("foo\nbar\n", ExtractHexData( |
| 129 | StringPrintf("%s\nfoo\nbar\n%s\n", GetPEMHeader(), GetPEMFooter()))); |
| 130 | EXPECT_EQ("bar\n", ExtractHexData( |
| 131 | StringPrintf("foo\n%s\nbar\n%s\nbaz\n", GetPEMHeader(), GetPEMFooter()))); |
| 132 | } |
| 133 | |
| 134 | TEST_F(CertificateFileTest, Destruction) { |
| 135 | FilePath outfile; |
| 136 | { |
| 137 | CertificateFile certificate_file(&glib_); |
| 138 | certificate_file.set_root_directory(temp_dir_.path()); |
| 139 | outfile = certificate_file.CreatePEMFromString(kPEMData); |
| 140 | EXPECT_TRUE(file_util::PathExists(outfile)); |
| 141 | } |
| 142 | // The output file should be deleted when certificate_file goes out-of-scope. |
| 143 | EXPECT_FALSE(file_util::PathExists(outfile)); |
| 144 | } |
| 145 | |
| 146 | } // namespace shill |