OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium 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 CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_OWNER_KEY_UTILS_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_OWNER_KEY_UTILS_H_ |
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_OWNER_KEY_UTILS_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_OWNER_KEY_UTILS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/crypto/rsa_private_key.h" | 12 #include "base/crypto/rsa_private_key.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "chrome/browser/browser_thread.h" | 14 #include "chrome/browser/browser_thread.h" |
15 #include "chrome/browser/chromeos/cros/login_library.h" | 15 #include "chrome/browser/chromeos/cros/login_library.h" |
16 #include "chrome/browser/chromeos/login/owner_key_utils.h" | 16 #include "chrome/browser/chromeos/login/owner_key_utils.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 using ::base::RSAPrivateKey; | 20 using ::base::RSAPrivateKey; |
21 | 21 |
22 namespace chromeos { | 22 namespace chromeos { |
23 | 23 |
24 class MockKeyUtils : public OwnerKeyUtils { | 24 class MockKeyUtils : public OwnerKeyUtils { |
25 public: | 25 public: |
26 MockKeyUtils() {} | 26 MockKeyUtils() {} |
27 MOCK_METHOD0(GenerateKeyPair, RSAPrivateKey*()); | |
28 MOCK_METHOD2(ExportPublicKeyViaDbus, bool(RSAPrivateKey* pair, | |
29 LoginLibrary::Delegate*)); | |
30 MOCK_METHOD2(ExportPublicKeyToFile, bool(RSAPrivateKey* pair, | |
31 const FilePath& key_file)); | |
32 MOCK_METHOD2(ImportPublicKey, bool(const FilePath& key_file, | 27 MOCK_METHOD2(ImportPublicKey, bool(const FilePath& key_file, |
33 std::vector<uint8>* output)); | 28 std::vector<uint8>* output)); |
34 MOCK_METHOD3(Verify, bool(const std::string& data, | 29 MOCK_METHOD3(Verify, bool(const std::string& data, |
35 const std::vector<uint8> signature, | 30 const std::vector<uint8> signature, |
36 const std::vector<uint8> public_key)); | 31 const std::vector<uint8> public_key)); |
37 MOCK_METHOD3(Sign, bool(const std::string& data, | 32 MOCK_METHOD3(Sign, bool(const std::string& data, |
38 std::vector<uint8>* OUT_signature, | 33 std::vector<uint8>* OUT_signature, |
39 base::RSAPrivateKey* key)); | 34 base::RSAPrivateKey* key)); |
40 MOCK_METHOD1(FindPrivateKey, RSAPrivateKey*(const std::vector<uint8>& key)); | 35 MOCK_METHOD1(FindPrivateKey, RSAPrivateKey*(const std::vector<uint8>& key)); |
41 MOCK_METHOD0(GetOwnerKeyFilePath, FilePath()); | 36 MOCK_METHOD0(GetOwnerKeyFilePath, FilePath()); |
42 | 37 MOCK_METHOD2(ExportPublicKeyToFile, bool(base::RSAPrivateKey* pair, |
43 // To simulate doing a LoginLibrary::SetOwnerKey call | 38 const FilePath& key_file)); |
44 static void SetOwnerKeyCallback(LoginLibrary::Delegate* callback, | |
45 bool value) { | |
46 callback->OnComplete(value); | |
47 } | |
48 | |
49 static bool ExportPublicKeyViaDbusWin(RSAPrivateKey* key, | |
50 LoginLibrary::Delegate* d) { | |
51 BrowserThread::PostTask( | |
52 BrowserThread::UI, FROM_HERE, | |
53 NewRunnableFunction(&SetOwnerKeyCallback, d, true)); | |
54 return true; | |
55 } | |
56 | |
57 static bool ExportPublicKeyViaDbusFail(RSAPrivateKey* key, | |
58 LoginLibrary::Delegate* d) { | |
59 BrowserThread::PostTask( | |
60 BrowserThread::UI, FROM_HERE, | |
61 NewRunnableFunction(&SetOwnerKeyCallback, d, false)); | |
62 return false; | |
63 } | |
64 | |
65 protected: | 39 protected: |
66 virtual ~MockKeyUtils() {} | 40 virtual ~MockKeyUtils() {} |
67 | 41 |
68 private: | 42 private: |
69 DISALLOW_COPY_AND_ASSIGN(MockKeyUtils); | 43 DISALLOW_COPY_AND_ASSIGN(MockKeyUtils); |
70 }; | 44 }; |
71 | 45 |
72 class MockInjector : public OwnerKeyUtils::Factory { | 46 class MockInjector : public OwnerKeyUtils::Factory { |
73 public: | 47 public: |
74 // Takes ownership of |mock|. | 48 // Takes ownership of |mock|. |
75 explicit MockInjector(MockKeyUtils* mock) : transient_(mock) { | 49 explicit MockInjector(MockKeyUtils* mock) : transient_(mock) { |
76 } | 50 } |
77 | 51 |
78 virtual ~MockInjector() {} | 52 virtual ~MockInjector() {} |
79 | 53 |
80 // If this is called, its caller takes ownership of |transient_|. | 54 // If this is called, its caller takes ownership of |transient_|. |
81 // If it's never called, |transient_| remains our problem. | 55 // If it's never called, |transient_| remains our problem. |
82 OwnerKeyUtils* CreateOwnerKeyUtils() { | 56 OwnerKeyUtils* CreateOwnerKeyUtils() { |
83 return transient_.get(); | 57 return transient_.get(); |
84 } | 58 } |
85 | 59 |
86 private: | 60 private: |
87 scoped_refptr<MockKeyUtils> transient_; | 61 scoped_refptr<MockKeyUtils> transient_; |
88 DISALLOW_COPY_AND_ASSIGN(MockInjector); | 62 DISALLOW_COPY_AND_ASSIGN(MockInjector); |
89 }; | 63 }; |
90 | 64 |
91 } // namespace chromeos | 65 } // namespace chromeos |
92 | 66 |
93 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_OWNER_KEY_UTILS_H_ | 67 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_MOCK_OWNER_KEY_UTILS_H_ |
OLD | NEW |