Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +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/nss.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 9 | #include "shill/mock_minijail.h" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 10 | |
| 11 | using std::vector; |
| 12 | using testing::_; |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 13 | using testing::ElementsAre; |
| 14 | using testing::IsNull; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 15 | using testing::Return; |
| 16 | using testing::SetArgumentPointee; |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 17 | using testing::StrEq; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | class NSSTest : public testing::Test { |
| 22 | public: |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 23 | NSSTest() : |
| 24 | nss_(NSS::GetInstance()) { |
| 25 | nss_->minijail_ = &minijail_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 26 | test_id_.push_back(0x1a); |
| 27 | test_id_.push_back(0x2b); |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | vector<char> test_id_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 32 | NSS *nss_; |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 33 | MockMinijail minijail_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 36 | TEST_F(NSSTest, GetCertfile) { |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 37 | 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 Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 45 | .WillOnce(Return(false)) |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 46 | .WillOnce(DoAll(SetArgumentPointee<2>(1), Return(true))) |
| 47 | .WillOnce(DoAll(SetArgumentPointee<2>(0), Return(true))); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 48 | 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 | |
| 53 | TEST_F(NSSTest, GetPEMCertfile) { |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 54 | 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 Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 63 | EXPECT_FALSE(nss_->GetPEMCertfile("foo", test_id_).empty()); |
| 64 | } |
| 65 | |
| 66 | TEST_F(NSSTest, GetDERCertfile) { |
Jorge Lucangeli Obes | ccd5c85 | 2012-12-19 18:08:40 -0800 | [diff] [blame] | 67 | 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 Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 76 | EXPECT_FALSE(nss_->GetDERCertfile("foo", test_id_).empty()); |
| 77 | } |
| 78 | |
| 79 | } // namespace shill |