Darin Petkov | d9050bb | 2012-09-26 16:02:52 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 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/shims/certificates.h" |
| 6 | |
| 7 | #include <sys/stat.h> |
| 8 | |
| 9 | #include <base/file_path.h> |
| 10 | #include <base/file_util.h> |
| 11 | #include <base/scoped_temp_dir.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | using std::string; |
| 15 | using testing::Test; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | namespace shims { |
| 20 | |
| 21 | class CertificatesTest : public Test { |
| 22 | }; |
| 23 | |
| 24 | TEST_F(CertificatesTest, ConvertDERToPEM) { |
| 25 | ByteString der_cert( |
| 26 | string("01234567890123456789012345678901234567890123456789"), false); |
| 27 | ByteString expected_pem_cert( |
| 28 | string( |
| 29 | "-----BEGIN CERTIFICATE-----\n" |
| 30 | "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3\r\n" |
| 31 | "ODk=\n" |
| 32 | "-----END CERTIFICATE-----\n"), |
| 33 | false); |
| 34 | ByteString pem_cert = Certificates::ConvertDERToPEM(der_cert); |
| 35 | EXPECT_TRUE(expected_pem_cert.Equals(pem_cert)); |
| 36 | } |
| 37 | |
| 38 | TEST_F(CertificatesTest, Write) { |
| 39 | string cert_str = "foo"; |
| 40 | ByteString cert(cert_str, false); |
| 41 | ScopedTempDir temp_dir; |
| 42 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 43 | FilePath certfile = temp_dir.path().Append("certfile"); |
| 44 | EXPECT_TRUE(Certificates::Write(cert, certfile)); |
| 45 | string contents; |
| 46 | EXPECT_TRUE(file_util::ReadFileToString(certfile, &contents)); |
| 47 | EXPECT_EQ(cert_str, contents); |
| 48 | struct stat buf; |
| 49 | EXPECT_EQ(0, stat(certfile.value().c_str(), &buf)); |
| 50 | EXPECT_EQ(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, buf.st_mode); |
| 51 | |
| 52 | EXPECT_FALSE(Certificates::Write(cert, temp_dir.path().Append("foo/bar"))); |
| 53 | } |
| 54 | |
| 55 | } // namespace shims |
| 56 | |
| 57 | } // namespace shill |