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