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::_; |
| 13 | using testing::Return; |
| 14 | using testing::SetArgumentPointee; |
| 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | class 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 | |
| 32 | namespace { |
| 33 | MATCHER_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 | |
| 47 | TEST_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 | |
| 58 | TEST_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 | |
| 65 | TEST_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 |