OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef LOGIN_MANAGER_MOCK_NSS_UTIL_H_ | 5 #ifndef LOGIN_MANAGER_MOCK_NSS_UTIL_H_ |
6 #define LOGIN_MANAGER_MOCK_NSS_UTIL_H_ | 6 #define LOGIN_MANAGER_MOCK_NSS_UTIL_H_ |
7 | 7 |
8 #include "login_manager/nss_util.h" | 8 #include "login_manager/nss_util.h" |
9 | 9 |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #include <base/file_path.h> | 11 #include <base/file_path.h> |
12 #include <base/nss_util.h> | 12 #include <base/nss_util.h> |
13 #include <gmock/gmock.h> | 13 #include <gmock/gmock.h> |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 class RSAPrivateKey; | 16 class RSAPrivateKey; |
17 } | 17 } |
18 | 18 |
19 namespace login_manager { | 19 namespace login_manager { |
20 using ::testing::Invoke; | 20 using ::testing::Invoke; |
21 using ::testing::Return; | 21 using ::testing::Return; |
22 using ::testing::_; | 22 using ::testing::_; |
23 | 23 |
24 class MockNssUtil : public NssUtil { | 24 class MockNssUtil : public NssUtil { |
25 public: | 25 public: |
26 MockNssUtil() {} | 26 MockNssUtil() {} |
27 virtual ~MockNssUtil() {} | 27 virtual ~MockNssUtil() {} |
28 | 28 |
| 29 MOCK_METHOD0(MightHaveKeys, bool()); |
29 MOCK_METHOD0(OpenUserDB, bool()); | 30 MOCK_METHOD0(OpenUserDB, bool()); |
30 MOCK_METHOD1(GetPrivateKey, base::RSAPrivateKey*(const std::vector<uint8>&)); | 31 MOCK_METHOD1(GetPrivateKey, base::RSAPrivateKey*(const std::vector<uint8>&)); |
31 MOCK_METHOD0(GenerateKeyPair, base::RSAPrivateKey*()); | 32 MOCK_METHOD0(GenerateKeyPair, base::RSAPrivateKey*()); |
32 MOCK_METHOD0(GetOwnerKeyFilePath, FilePath()); | 33 MOCK_METHOD0(GetOwnerKeyFilePath, FilePath()); |
33 MOCK_METHOD8(Verify, bool(const uint8* algorithm, int algorithm_len, | 34 MOCK_METHOD8(Verify, bool(const uint8* algorithm, int algorithm_len, |
34 const uint8* signature, int signature_len, | 35 const uint8* signature, int signature_len, |
35 const uint8* data, int data_len, | 36 const uint8* data, int data_len, |
36 const uint8* public_key, int public_key_len)); | 37 const uint8* public_key, int public_key_len)); |
37 MOCK_METHOD4(Sign, bool(const uint8* data, int data_len, | 38 MOCK_METHOD4(Sign, bool(const uint8* data, int data_len, |
38 std::vector<uint8>* OUT_signature, | 39 std::vector<uint8>* OUT_signature, |
39 base::RSAPrivateKey* key)); | 40 base::RSAPrivateKey* key)); |
40 protected: | 41 protected: |
41 void ExpectGetOwnerKeyFilePath() { | 42 void ExpectGetOwnerKeyFilePath() { |
42 EXPECT_CALL(*this, GetOwnerKeyFilePath()) | 43 EXPECT_CALL(*this, GetOwnerKeyFilePath()).WillOnce(Return(FilePath(""))); |
43 .WillOnce(Return(FilePath(""))); | |
44 } | 44 } |
45 }; | 45 }; |
46 | 46 |
47 template<typename T> | 47 template<typename T> |
48 class MockFactory : public NssUtil::Factory { | 48 class MockFactory : public NssUtil::Factory { |
49 public: | 49 public: |
50 MockFactory() {} | 50 MockFactory() {} |
51 ~MockFactory() {} | 51 ~MockFactory() {} |
52 NssUtil* CreateNssUtil() { | 52 NssUtil* CreateNssUtil() { |
53 return new T; | 53 return new T; |
54 } | 54 } |
55 private: | 55 private: |
56 DISALLOW_COPY_AND_ASSIGN(MockFactory); | 56 DISALLOW_COPY_AND_ASSIGN(MockFactory); |
57 }; | 57 }; |
58 | 58 |
59 class KeyCheckUtil : public MockNssUtil { | 59 class KeyCheckUtil : public MockNssUtil { |
60 public: | 60 public: |
61 KeyCheckUtil() { | 61 KeyCheckUtil() { |
62 ExpectGetOwnerKeyFilePath(); | 62 ExpectGetOwnerKeyFilePath(); |
63 EXPECT_CALL(*this, OpenUserDB()) | 63 EXPECT_CALL(*this, MightHaveKeys()).WillOnce(Return(true)); |
64 .WillOnce(Return(true)); | 64 EXPECT_CALL(*this, OpenUserDB()).WillOnce(Return(true)); |
65 EXPECT_CALL(*this, GetPrivateKey(_)) | 65 EXPECT_CALL(*this, GetPrivateKey(_)) |
66 .WillOnce(Return(reinterpret_cast<base::RSAPrivateKey*>(7))); | 66 .WillOnce(Return(reinterpret_cast<base::RSAPrivateKey*>(7))); |
67 } | 67 } |
68 virtual ~KeyCheckUtil() {} | 68 virtual ~KeyCheckUtil() {} |
69 }; | 69 }; |
70 | 70 |
71 class KeyFailUtil : public MockNssUtil { | 71 class KeyFailUtil : public MockNssUtil { |
72 public: | 72 public: |
73 KeyFailUtil() { | 73 KeyFailUtil() { |
74 ExpectGetOwnerKeyFilePath(); | 74 ExpectGetOwnerKeyFilePath(); |
75 EXPECT_CALL(*this, OpenUserDB()) | 75 EXPECT_CALL(*this, MightHaveKeys()).WillOnce(Return(true)); |
76 .WillOnce(Return(true)); | 76 EXPECT_CALL(*this, OpenUserDB()).WillOnce(Return(true)); |
77 EXPECT_CALL(*this, GetPrivateKey(_)) | 77 EXPECT_CALL(*this, GetPrivateKey(_)) |
78 .WillOnce(Return(reinterpret_cast<base::RSAPrivateKey*>(NULL))); | 78 .WillOnce(Return(reinterpret_cast<base::RSAPrivateKey*>(NULL))); |
79 } | 79 } |
80 virtual ~KeyFailUtil() {} | 80 virtual ~KeyFailUtil() {} |
81 }; | 81 }; |
82 | 82 |
83 class SadNssUtil : public MockNssUtil { | 83 class SadNssUtil : public MockNssUtil { |
84 public: | 84 public: |
85 SadNssUtil() { | 85 SadNssUtil() { |
86 ExpectGetOwnerKeyFilePath(); | 86 ExpectGetOwnerKeyFilePath(); |
87 EXPECT_CALL(*this, OpenUserDB()) | 87 EXPECT_CALL(*this, MightHaveKeys()).WillOnce(Return(true)); |
88 .WillOnce(Return(false)); | 88 EXPECT_CALL(*this, OpenUserDB()).WillOnce(Return(false)); |
89 } | 89 } |
90 virtual ~SadNssUtil() {} | 90 virtual ~SadNssUtil() {} |
91 }; | 91 }; |
92 | 92 |
| 93 class EmptyNssUtil : public MockNssUtil { |
| 94 public: |
| 95 EmptyNssUtil() { |
| 96 ExpectGetOwnerKeyFilePath(); |
| 97 EXPECT_CALL(*this, MightHaveKeys()).WillOnce(Return(false)); |
| 98 } |
| 99 virtual ~EmptyNssUtil() {} |
| 100 }; |
| 101 |
93 class ShortKeyGenerator : public MockNssUtil { | 102 class ShortKeyGenerator : public MockNssUtil { |
94 public: | 103 public: |
95 ShortKeyGenerator() { | 104 ShortKeyGenerator() { |
96 base::EnsureNSSInit(); | 105 base::EnsureNSSInit(); |
97 base::OpenPersistentNSSDB(); | 106 base::OpenPersistentNSSDB(); |
98 ON_CALL(*this, GenerateKeyPair()) | 107 ON_CALL(*this, GenerateKeyPair()).WillByDefault(Invoke(CreateFake)); |
99 .WillByDefault(Invoke(CreateFake)); | |
100 } | 108 } |
101 virtual ~ShortKeyGenerator() {} | 109 virtual ~ShortKeyGenerator() {} |
102 | 110 |
103 static base::RSAPrivateKey* CreateFake() { | 111 static base::RSAPrivateKey* CreateFake() { |
104 base::RSAPrivateKey* ret = base::RSAPrivateKey::CreateSensitive(512); | 112 base::RSAPrivateKey* ret = base::RSAPrivateKey::CreateSensitive(512); |
105 LOG_IF(INFO, ret == NULL) << "returning NULL!!!"; | 113 LOG_IF(INFO, ret == NULL) << "returning NULL!!!"; |
106 return ret; | 114 return ret; |
107 } | 115 } |
108 }; | 116 }; |
109 | 117 |
110 class ShortKeyUtil : public ShortKeyGenerator { | 118 class ShortKeyUtil : public ShortKeyGenerator { |
111 public: | 119 public: |
112 ShortKeyUtil() { | 120 ShortKeyUtil() { |
113 ExpectGetOwnerKeyFilePath(); | 121 ExpectGetOwnerKeyFilePath(); |
114 EXPECT_CALL(*this, OpenUserDB()) | 122 EXPECT_CALL(*this, MightHaveKeys()).WillOnce(Return(true)); |
115 .WillOnce(Return(true)); | 123 EXPECT_CALL(*this, OpenUserDB()).WillOnce(Return(true)); |
116 EXPECT_CALL(*this, GenerateKeyPair()) | 124 EXPECT_CALL(*this, GenerateKeyPair()).Times(1); |
117 .Times(1); | |
118 } | 125 } |
119 virtual ~ShortKeyUtil() {} | 126 virtual ~ShortKeyUtil() {} |
120 }; | 127 }; |
121 | 128 |
122 } // namespace login_manager | 129 } // namespace login_manager |
123 | 130 |
124 #endif // LOGIN_MANAGER_MOCK_NSS_UTIL_H_ | 131 #endif // LOGIN_MANAGER_MOCK_NSS_UTIL_H_ |
OLD | NEW |