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 | |
| 9 | #include "shill/mock_glib.h" |
| 10 | |
| 11 | using std::vector; |
| 12 | using testing::_; |
Darin Petkov | e2c3d11 | 2012-04-17 12:20:15 +0200 | [diff] [blame] | 13 | using testing::NotNull; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 14 | using testing::Return; |
| 15 | using testing::SetArgumentPointee; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | class 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 | |
| 33 | namespace { |
| 34 | MATCHER_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 | |
| 48 | TEST_F(NSSTest, GetCertfile) { |
| 49 | EXPECT_CALL(glib_, |
Darin Petkov | e2c3d11 | 2012-04-17 12:20:15 +0200 | [diff] [blame] | 50 | SpawnSync(_, |
| 51 | GetCertfileArgv("pem"), NotNull(), _, _, _, _, _, _, _)) |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 52 | .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 | |
| 60 | TEST_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 | |
| 67 | TEST_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 |