Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 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 <string> |
| 6 | |
| 7 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 8 | #include <base/stringprintf.h> |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | #include <metrics/metrics_library_mock.h> |
| 12 | |
| 13 | #include "update_engine/certificate_checker.h" |
| 14 | #include "update_engine/certificate_checker_mock.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 15 | #include "update_engine/mock_system_state.h" |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 16 | #include "update_engine/prefs_mock.h" |
| 17 | |
| 18 | using ::testing::_; |
| 19 | using ::testing::DoAll; |
| 20 | using ::testing::Return; |
| 21 | using ::testing::SetArgumentPointee; |
| 22 | using ::testing::SetArrayArgument; |
| 23 | using std::string; |
| 24 | |
| 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | class CertificateCheckerTest : public testing::Test { |
| 28 | public: |
| 29 | CertificateCheckerTest() {} |
| 30 | |
| 31 | protected: |
| 32 | virtual void SetUp() { |
| 33 | depth_ = 0; |
| 34 | length_ = 4; |
| 35 | digest_[0] = 0x17; |
| 36 | digest_[1] = 0x7D; |
| 37 | digest_[2] = 0x07; |
| 38 | digest_[3] = 0x5F; |
| 39 | digest_hex_ = "177D075F"; |
| 40 | diff_digest_hex_ = "1234ABCD"; |
| 41 | cert_key_prefix_ = kPrefsUpdateServerCertificate; |
| 42 | server_to_check_ = CertificateChecker::kUpdate; |
| 43 | cert_key_ = StringPrintf("%s-%d-%d", |
| 44 | cert_key_prefix_.c_str(), |
| 45 | server_to_check_, |
| 46 | depth_); |
| 47 | kCertChanged = "Updater.ServerCertificateChanged"; |
| 48 | kCertFailed = "Updater.ServerCertificateFailed"; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 49 | CertificateChecker::set_system_state(&mock_system_state_); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 50 | CertificateChecker::set_openssl_wrapper(&openssl_wrapper_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 51 | prefs_ = mock_system_state_.mock_prefs(); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | virtual void TearDown() {} |
| 55 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 56 | MockSystemState mock_system_state_; |
| 57 | PrefsMock* prefs_; // shortcut to mock_system_state_.mock_prefs() |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 58 | OpenSSLWrapperMock openssl_wrapper_; |
| 59 | // Parameters of our mock certificate digest. |
| 60 | int depth_; |
| 61 | unsigned int length_; |
| 62 | unsigned char digest_[4]; |
| 63 | string digest_hex_; |
| 64 | string diff_digest_hex_; |
| 65 | string cert_key_prefix_; |
| 66 | CertificateChecker::ServerToCheck server_to_check_; |
| 67 | string cert_key_; |
| 68 | string kCertChanged; |
| 69 | string kCertFailed; |
| 70 | }; |
| 71 | |
| 72 | // check certificate change, new |
| 73 | TEST_F(CertificateCheckerTest, NewCertificate) { |
| 74 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(NULL, _, _, _)) |
| 75 | .WillOnce(DoAll( |
| 76 | SetArgumentPointee<1>(depth_), |
| 77 | SetArgumentPointee<2>(length_), |
| 78 | SetArrayArgument<3>(digest_, digest_ + 4), |
| 79 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 80 | EXPECT_CALL(*prefs_, GetString(cert_key_, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 81 | .WillOnce(Return(false)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 82 | EXPECT_CALL(*prefs_, SetString(cert_key_, digest_hex_)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 83 | .WillOnce(Return(true)); |
| 84 | ASSERT_TRUE(CertificateChecker::CheckCertificateChange( |
| 85 | server_to_check_, 1, NULL)); |
| 86 | } |
| 87 | |
| 88 | // check certificate change, unchanged |
| 89 | TEST_F(CertificateCheckerTest, SameCertificate) { |
| 90 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(NULL, _, _, _)) |
| 91 | .WillOnce(DoAll( |
| 92 | SetArgumentPointee<1>(depth_), |
| 93 | SetArgumentPointee<2>(length_), |
| 94 | SetArrayArgument<3>(digest_, digest_ + 4), |
| 95 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 96 | EXPECT_CALL(*prefs_, GetString(cert_key_, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 97 | .WillOnce(DoAll( |
| 98 | SetArgumentPointee<1>(digest_hex_), |
| 99 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 100 | EXPECT_CALL(*prefs_, SetString(_, _)).Times(0); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(CertificateChecker::CheckCertificateChange( |
| 102 | server_to_check_, 1, NULL)); |
| 103 | } |
| 104 | |
| 105 | // check certificate change, changed |
| 106 | TEST_F(CertificateCheckerTest, ChangedCertificate) { |
| 107 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(NULL, _, _, _)) |
| 108 | .WillOnce(DoAll( |
| 109 | SetArgumentPointee<1>(depth_), |
| 110 | SetArgumentPointee<2>(length_), |
| 111 | SetArrayArgument<3>(digest_, digest_ + 4), |
| 112 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 113 | EXPECT_CALL(*prefs_, GetString(cert_key_, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 114 | .WillOnce(DoAll( |
| 115 | SetArgumentPointee<1>(diff_digest_hex_), |
| 116 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 117 | EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendUpdate, |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 118 | kCertChanged)) |
| 119 | .WillOnce(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 120 | EXPECT_CALL(*prefs_, SetString(cert_key_, digest_hex_)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 121 | .WillOnce(Return(true)); |
| 122 | ASSERT_TRUE(CertificateChecker::CheckCertificateChange( |
| 123 | server_to_check_, 1, NULL)); |
| 124 | } |
| 125 | |
| 126 | // check certificate change, failed |
| 127 | TEST_F(CertificateCheckerTest, FailedCertificate) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 128 | EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendUpdate, |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 129 | kCertFailed)) |
| 130 | .WillOnce(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 131 | EXPECT_CALL(*prefs_, GetString(_,_)).Times(0); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 132 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(_,_,_,_)).Times(0); |
| 133 | ASSERT_FALSE(CertificateChecker::CheckCertificateChange( |
| 134 | server_to_check_, 0, NULL)); |
| 135 | } |
| 136 | |
| 137 | // flush send report |
| 138 | TEST_F(CertificateCheckerTest, FlushReport) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 139 | EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendUpdate, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 140 | .WillOnce(DoAll( |
| 141 | SetArgumentPointee<1>(kCertChanged), |
| 142 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 143 | EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendDownload, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 144 | .WillOnce(Return(false)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 145 | EXPECT_CALL(*mock_system_state_.mock_metrics_lib(), |
| 146 | SendUserActionToUMA(kCertChanged)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 147 | .WillOnce(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 148 | EXPECT_CALL(*prefs_, Delete(kPrefsCertificateReportToSendUpdate)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 149 | .WillOnce(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 150 | EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendDownload, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 151 | .Times(0); |
| 152 | CertificateChecker::FlushReport(); |
| 153 | } |
| 154 | |
| 155 | // flush nothing to report |
| 156 | TEST_F(CertificateCheckerTest, FlushNothingToReport) { |
| 157 | string empty = ""; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 158 | EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendUpdate, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 159 | .WillOnce(DoAll( |
| 160 | SetArgumentPointee<1>(empty), |
| 161 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 162 | EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendDownload, _)) |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 163 | .WillOnce(Return(false)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 164 | EXPECT_CALL(*mock_system_state_.mock_metrics_lib(), |
| 165 | SendUserActionToUMA(_)).Times(0); |
| 166 | EXPECT_CALL(*prefs_, SetString(_,_)).Times(0); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 167 | CertificateChecker::FlushReport(); |
| 168 | } |
| 169 | |
| 170 | } // namespace chromeos_update_engine |