blob: d75b543b83d2bb91a03d1d4f0f886505d4177bee [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bruno Rocha7f9aea22011-09-12 14:31:24 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo8427b4a2014-11-05 14:00:32 -08005#include "update_engine/certificate_checker.h"
6
Bruno Rocha7f9aea22011-09-12 14:31:24 -07007#include <string>
8
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/strings/string_util.h>
10#include <base/strings/stringprintf.h>
Bruno Rocha7f9aea22011-09-12 14:31:24 -070011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13#include <metrics/metrics_library_mock.h>
14
Jay Srinivasand29695d2013-04-08 15:08:05 -070015#include "update_engine/constants.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070016#include "update_engine/fake_system_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/mock_certificate_checker.h"
18#include "update_engine/mock_prefs.h"
Bruno Rocha7f9aea22011-09-12 14:31:24 -070019
Bruno Rocha7f9aea22011-09-12 14:31:24 -070020using ::testing::DoAll;
21using ::testing::Return;
22using ::testing::SetArgumentPointee;
23using ::testing::SetArrayArgument;
Alex Deymof329b932014-10-30 01:37:48 -070024using ::testing::_;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070025using std::string;
26
27namespace chromeos_update_engine {
28
29class CertificateCheckerTest : public testing::Test {
30 public:
31 CertificateCheckerTest() {}
32
33 protected:
34 virtual void SetUp() {
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 Vakulenko75039d72014-03-25 12:36:28 -070045 cert_key_ = base::StringPrintf("%s-%d-%d",
46 cert_key_prefix_.c_str(),
47 server_to_check_,
48 depth_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -070049 kCertChanged = "Updater.ServerCertificateChanged";
50 kCertFailed = "Updater.ServerCertificateFailed";
Gilad Arnold5bb4c902014-04-10 12:32:13 -070051 CertificateChecker::set_system_state(&fake_system_state_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -070052 CertificateChecker::set_openssl_wrapper(&openssl_wrapper_);
Gilad Arnold5bb4c902014-04-10 12:32:13 -070053 prefs_ = fake_system_state_.mock_prefs();
Bruno Rocha7f9aea22011-09-12 14:31:24 -070054 }
55
56 virtual void TearDown() {}
57
Gilad Arnold5bb4c902014-04-10 12:32:13 -070058 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -080059 MockPrefs* prefs_; // shortcut to fake_system_state_.mock_prefs()
60 MockOpenSSLWrapper openssl_wrapper_;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070061 // Parameters of our mock certificate digest.
62 int depth_;
63 unsigned int length_;
64 unsigned char digest_[4];
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
75TEST_F(CertificateCheckerTest, NewCertificate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070076 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070077 .WillOnce(DoAll(
78 SetArgumentPointee<1>(depth_),
79 SetArgumentPointee<2>(length_),
80 SetArrayArgument<3>(digest_, digest_ + 4),
81 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080082 EXPECT_CALL(*prefs_, GetString(cert_key_, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070083 .WillOnce(Return(false));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080084 EXPECT_CALL(*prefs_, SetString(cert_key_, digest_hex_))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070085 .WillOnce(Return(true));
86 ASSERT_TRUE(CertificateChecker::CheckCertificateChange(
Alex Vakulenko88b591f2014-08-28 16:48:57 -070087 server_to_check_, 1, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -070088}
89
90// check certificate change, unchanged
91TEST_F(CertificateCheckerTest, SameCertificate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070092 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070093 .WillOnce(DoAll(
94 SetArgumentPointee<1>(depth_),
95 SetArgumentPointee<2>(length_),
96 SetArrayArgument<3>(digest_, digest_ + 4),
97 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080098 EXPECT_CALL(*prefs_, GetString(cert_key_, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070099 .WillOnce(DoAll(
100 SetArgumentPointee<1>(digest_hex_),
101 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800102 EXPECT_CALL(*prefs_, SetString(_, _)).Times(0);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700103 ASSERT_TRUE(CertificateChecker::CheckCertificateChange(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700104 server_to_check_, 1, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700105}
106
107// check certificate change, changed
108TEST_F(CertificateCheckerTest, ChangedCertificate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700109 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700110 .WillOnce(DoAll(
111 SetArgumentPointee<1>(depth_),
112 SetArgumentPointee<2>(length_),
113 SetArrayArgument<3>(digest_, digest_ + 4),
114 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800115 EXPECT_CALL(*prefs_, GetString(cert_key_, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700116 .WillOnce(DoAll(
117 SetArgumentPointee<1>(diff_digest_hex_),
118 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800119 EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendUpdate,
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700120 kCertChanged))
121 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800122 EXPECT_CALL(*prefs_, SetString(cert_key_, digest_hex_))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700123 .WillOnce(Return(true));
124 ASSERT_TRUE(CertificateChecker::CheckCertificateChange(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700125 server_to_check_, 1, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700126}
127
128// check certificate change, failed
129TEST_F(CertificateCheckerTest, FailedCertificate) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800130 EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendUpdate,
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700131 kCertFailed))
132 .WillOnce(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700133 EXPECT_CALL(*prefs_, GetString(_, _)).Times(0);
134 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(_, _, _, _)).Times(0);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700135 ASSERT_FALSE(CertificateChecker::CheckCertificateChange(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700136 server_to_check_, 0, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700137}
138
139// flush send report
140TEST_F(CertificateCheckerTest, FlushReport) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800141 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendUpdate, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700142 .WillOnce(DoAll(
143 SetArgumentPointee<1>(kCertChanged),
144 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800145 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendDownload, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700146 .WillOnce(Return(false));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700147 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800148 SendUserActionToUMA(kCertChanged))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700149 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800150 EXPECT_CALL(*prefs_, Delete(kPrefsCertificateReportToSendUpdate))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700151 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800152 EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendDownload, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700153 .Times(0);
154 CertificateChecker::FlushReport();
155}
156
157// flush nothing to report
158TEST_F(CertificateCheckerTest, FlushNothingToReport) {
159 string empty = "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800160 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendUpdate, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700161 .WillOnce(DoAll(
162 SetArgumentPointee<1>(empty),
163 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800164 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendDownload, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700165 .WillOnce(Return(false));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700166 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800167 SendUserActionToUMA(_)).Times(0);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700168 EXPECT_CALL(*prefs_, SetString(_, _)).Times(0);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700169 CertificateChecker::FlushReport();
170}
171
172} // namespace chromeos_update_engine