blob: beef00ec4fd8f9ca41f353198fcfd3cb0d229751 [file] [log] [blame]
Darin Petkovd9050bb2012-09-26 16:02:52 +02001// 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
14using std::string;
15using testing::Test;
16
17namespace shill {
18
19namespace shims {
20
21class CertificatesTest : public Test {
22};
23
24TEST_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
38TEST_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