blob: eb7067fb811c45b1198a67db39975c999f600f13 [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
5#include <string>
6
7#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -04008#include <base/stringprintf.h>
Bruno Rocha7f9aea22011-09-12 14:31:24 -07009#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 Srinivasand29695d2013-04-08 15:08:05 -070015#include "update_engine/constants.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016#include "update_engine/mock_system_state.h"
Bruno Rocha7f9aea22011-09-12 14:31:24 -070017#include "update_engine/prefs_mock.h"
18
19using ::testing::_;
20using ::testing::DoAll;
21using ::testing::Return;
22using ::testing::SetArgumentPointee;
23using ::testing::SetArrayArgument;
24using std::string;
25
26namespace chromeos_update_engine {
27
28class 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 Srinivasan6f6ea002012-12-14 11:26:28 -080050 CertificateChecker::set_system_state(&mock_system_state_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -070051 CertificateChecker::set_openssl_wrapper(&openssl_wrapper_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 prefs_ = mock_system_state_.mock_prefs();
Bruno Rocha7f9aea22011-09-12 14:31:24 -070053 }
54
55 virtual void TearDown() {}
56
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080057 MockSystemState mock_system_state_;
58 PrefsMock* prefs_; // shortcut to mock_system_state_.mock_prefs()
Bruno Rocha7f9aea22011-09-12 14:31:24 -070059 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
74TEST_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 Srinivasan6f6ea002012-12-14 11:26:28 -080081 EXPECT_CALL(*prefs_, GetString(cert_key_, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070082 .WillOnce(Return(false));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080083 EXPECT_CALL(*prefs_, SetString(cert_key_, digest_hex_))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070084 .WillOnce(Return(true));
85 ASSERT_TRUE(CertificateChecker::CheckCertificateChange(
86 server_to_check_, 1, NULL));
87}
88
89// check certificate change, unchanged
90TEST_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 Srinivasan6f6ea002012-12-14 11:26:28 -080097 EXPECT_CALL(*prefs_, GetString(cert_key_, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070098 .WillOnce(DoAll(
99 SetArgumentPointee<1>(digest_hex_),
100 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800101 EXPECT_CALL(*prefs_, SetString(_, _)).Times(0);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700102 ASSERT_TRUE(CertificateChecker::CheckCertificateChange(
103 server_to_check_, 1, NULL));
104}
105
106// check certificate change, changed
107TEST_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 Srinivasan6f6ea002012-12-14 11:26:28 -0800114 EXPECT_CALL(*prefs_, GetString(cert_key_, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700115 .WillOnce(DoAll(
116 SetArgumentPointee<1>(diff_digest_hex_),
117 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800118 EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendUpdate,
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700119 kCertChanged))
120 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800121 EXPECT_CALL(*prefs_, SetString(cert_key_, digest_hex_))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700122 .WillOnce(Return(true));
123 ASSERT_TRUE(CertificateChecker::CheckCertificateChange(
124 server_to_check_, 1, NULL));
125}
126
127// check certificate change, failed
128TEST_F(CertificateCheckerTest, FailedCertificate) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800129 EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendUpdate,
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700130 kCertFailed))
131 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800132 EXPECT_CALL(*prefs_, GetString(_,_)).Times(0);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700133 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(_,_,_,_)).Times(0);
134 ASSERT_FALSE(CertificateChecker::CheckCertificateChange(
135 server_to_check_, 0, NULL));
136}
137
138// flush send report
139TEST_F(CertificateCheckerTest, FlushReport) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800140 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendUpdate, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700141 .WillOnce(DoAll(
142 SetArgumentPointee<1>(kCertChanged),
143 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800144 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendDownload, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700145 .WillOnce(Return(false));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800146 EXPECT_CALL(*mock_system_state_.mock_metrics_lib(),
147 SendUserActionToUMA(kCertChanged))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700148 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800149 EXPECT_CALL(*prefs_, Delete(kPrefsCertificateReportToSendUpdate))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700150 .WillOnce(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800151 EXPECT_CALL(*prefs_, SetString(kPrefsCertificateReportToSendDownload, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700152 .Times(0);
153 CertificateChecker::FlushReport();
154}
155
156// flush nothing to report
157TEST_F(CertificateCheckerTest, FlushNothingToReport) {
158 string empty = "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800159 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendUpdate, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700160 .WillOnce(DoAll(
161 SetArgumentPointee<1>(empty),
162 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800163 EXPECT_CALL(*prefs_, GetString(kPrefsCertificateReportToSendDownload, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700164 .WillOnce(Return(false));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800165 EXPECT_CALL(*mock_system_state_.mock_metrics_lib(),
166 SendUserActionToUMA(_)).Times(0);
167 EXPECT_CALL(*prefs_, SetString(_,_)).Times(0);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700168 CertificateChecker::FlushReport();
169}
170
171} // namespace chromeos_update_engine