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