blob: 1bfb4ba9b9e65d2bc07c08d2f9ba98405decbdae [file] [log] [blame]
Darin Petkov3c5e4dc2012-04-02 14:44:27 +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/nss.h"
6
7#include <gtest/gtest.h>
8
9#include "shill/mock_glib.h"
10
11using std::vector;
12using testing::_;
Darin Petkove2c3d112012-04-17 12:20:15 +020013using testing::NotNull;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020014using testing::Return;
15using testing::SetArgumentPointee;
16
17namespace shill {
18
19class NSSTest : public testing::Test {
20 public:
21 NSSTest() : nss_(NSS::GetInstance()) {
22 nss_->glib_ = &glib_;
23 test_id_.push_back(0x1a);
24 test_id_.push_back(0x2b);
25 }
26
27 protected:
28 vector<char> test_id_;
29 MockGLib glib_;
30 NSS *nss_;
31};
32
33namespace {
34MATCHER_P(GetCertfileArgv, type, "") {
35 if (!arg || !arg[0] || !arg[1] || !arg[2] || !arg[3] || arg[4]) {
36 return false;
37 }
38 if (strcmp(type, arg[2])) {
39 return false;
40 }
41 if (strcmp(arg[3], "/tmp/nss-cert.1a2b")) {
42 return false;
43 }
44 return true;
45}
46} // namespace
47
48TEST_F(NSSTest, GetCertfile) {
49 EXPECT_CALL(glib_,
Darin Petkove2c3d112012-04-17 12:20:15 +020050 SpawnSync(_,
51 GetCertfileArgv("pem"), NotNull(), _, _, _, _, _, _, _))
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020052 .WillOnce(Return(false))
53 .WillOnce(DoAll(SetArgumentPointee<8>(1), Return(true)))
54 .WillOnce(DoAll(SetArgumentPointee<8>(0), Return(true)));
55 EXPECT_TRUE(nss_->GetCertfile("foo", test_id_, "pem").empty());
56 EXPECT_TRUE(nss_->GetCertfile("foo", test_id_, "pem").empty());
57 EXPECT_FALSE(nss_->GetCertfile("foo", test_id_, "pem").empty());
58}
59
60TEST_F(NSSTest, GetPEMCertfile) {
61 EXPECT_CALL(glib_,
62 SpawnSync(_, GetCertfileArgv("pem"), _, _, _, _, _, _, _, _))
63 .WillOnce(DoAll(SetArgumentPointee<8>(0), Return(true)));
64 EXPECT_FALSE(nss_->GetPEMCertfile("foo", test_id_).empty());
65}
66
67TEST_F(NSSTest, GetDERCertfile) {
68 EXPECT_CALL(glib_,
69 SpawnSync(_, GetCertfileArgv("der"), _, _, _, _, _, _, _, _))
70 .WillOnce(DoAll(SetArgumentPointee<8>(0), Return(true)));
71 EXPECT_FALSE(nss_->GetDERCertfile("foo", test_id_).empty());
72}
73
74} // namespace shill