blob: f6e38029274f8b54b157a80ccbaa8e1b945b6d09 [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
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -08009#include "shill/mock_minijail.h"
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020010
11using std::vector;
12using testing::_;
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080013using testing::ElementsAre;
14using testing::IsNull;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020015using testing::Return;
16using testing::SetArgumentPointee;
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080017using testing::StrEq;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020018
19namespace shill {
20
21class NSSTest : public testing::Test {
22 public:
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080023 NSSTest() :
24 nss_(NSS::GetInstance()) {
25 nss_->minijail_ = &minijail_;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020026 test_id_.push_back(0x1a);
27 test_id_.push_back(0x2b);
28 }
29
30 protected:
31 vector<char> test_id_;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020032 NSS *nss_;
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080033 MockMinijail minijail_;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020034};
35
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020036TEST_F(NSSTest, GetCertfile) {
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080037 EXPECT_CALL(minijail_, DropRoot(_, StrEq("chronos"))).Times(3);
38 EXPECT_CALL(minijail_,
39 RunSyncAndDestroy(_,
40 ElementsAre(_, _,
41 StrEq("pem"),
42 StrEq("/tmp/nss-cert.1a2b"),
43 IsNull()),
44 _))
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020045 .WillOnce(Return(false))
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080046 .WillOnce(DoAll(SetArgumentPointee<2>(1), Return(true)))
47 .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true)));
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020048 EXPECT_TRUE(nss_->GetCertfile("foo", test_id_, "pem").empty());
49 EXPECT_TRUE(nss_->GetCertfile("foo", test_id_, "pem").empty());
50 EXPECT_FALSE(nss_->GetCertfile("foo", test_id_, "pem").empty());
51}
52
53TEST_F(NSSTest, GetPEMCertfile) {
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080054 EXPECT_CALL(minijail_, DropRoot(_, StrEq("chronos")));
55 EXPECT_CALL(minijail_,
56 RunSyncAndDestroy(_,
57 ElementsAre(_, _,
58 StrEq("pem"),
59 StrEq("/tmp/nss-cert.1a2b"),
60 IsNull()),
61 _))
62 .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true)));
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020063 EXPECT_FALSE(nss_->GetPEMCertfile("foo", test_id_).empty());
64}
65
66TEST_F(NSSTest, GetDERCertfile) {
Jorge Lucangeli Obesccd5c852012-12-19 18:08:40 -080067 EXPECT_CALL(minijail_, DropRoot(_, StrEq("chronos")));
68 EXPECT_CALL(minijail_,
69 RunSyncAndDestroy(_,
70 ElementsAre(_, _,
71 StrEq("der"),
72 StrEq("/tmp/nss-cert.1a2b"),
73 IsNull()),
74 _))
75 .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true)));
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020076 EXPECT_FALSE(nss_->GetDERCertfile("foo", test_id_).empty());
77}
78
79} // namespace shill