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