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