blob: 15811c0fa8cc510e58023fde5a373d83eef4eecf [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Bruno Rocha7f9aea22011-09-12 14:31:24 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/certificate_checker.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080018
Bruno Rocha7f9aea22011-09-12 14:31:24 -070019#include <string>
20
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/string_util.h>
22#include <base/strings/stringprintf.h>
Bruno Rocha7f9aea22011-09-12 14:31:24 -070023#include <gmock/gmock.h>
24#include <gtest/gtest.h>
Bruno Rocha7f9aea22011-09-12 14:31:24 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/constants.h"
27#include "update_engine/common/mock_certificate_checker.h"
28#include "update_engine/common/mock_prefs.h"
Bruno Rocha7f9aea22011-09-12 14:31:24 -070029
Bruno Rocha7f9aea22011-09-12 14:31:24 -070030using ::testing::DoAll;
31using ::testing::Return;
32using ::testing::SetArgumentPointee;
33using ::testing::SetArrayArgument;
Alex Deymof329b932014-10-30 01:37:48 -070034using ::testing::_;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070035using std::string;
36
37namespace chromeos_update_engine {
38
Alex Deymoc1c17b42015-11-23 03:53:15 -030039class MockCertificateCheckObserver : public CertificateChecker::Observer {
Bruno Rocha7f9aea22011-09-12 14:31:24 -070040 public:
Alex Deymoc1c17b42015-11-23 03:53:15 -030041 MOCK_METHOD2(CertificateChecked,
42 void(ServerToCheck server_to_check,
43 CertificateCheckResult result));
44};
Bruno Rocha7f9aea22011-09-12 14:31:24 -070045
Alex Deymoc1c17b42015-11-23 03:53:15 -030046class CertificateCheckerTest : public testing::Test {
Bruno Rocha7f9aea22011-09-12 14:31:24 -070047 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080048 void SetUp() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070049 cert_key_ = base::StringPrintf("%s-%d-%d",
50 cert_key_prefix_.c_str(),
Alex Deymoc1c17b42015-11-23 03:53:15 -030051 static_cast<int>(server_to_check_),
Alex Vakulenko75039d72014-03-25 12:36:28 -070052 depth_);
Alex Deymoc1c17b42015-11-23 03:53:15 -030053 cert_checker.SetObserver(&observer_);
Bruno Rocha7f9aea22011-09-12 14:31:24 -070054 }
55
Alex Deymoc1c17b42015-11-23 03:53:15 -030056 void TearDown() override {
57 cert_checker.SetObserver(nullptr);
58 }
Bruno Rocha7f9aea22011-09-12 14:31:24 -070059
Alex Deymoc1c17b42015-11-23 03:53:15 -030060 MockPrefs prefs_;
Alex Deymo8427b4a2014-11-05 14:00:32 -080061 MockOpenSSLWrapper openssl_wrapper_;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070062 // Parameters of our mock certificate digest.
Alex Deymoc1c17b42015-11-23 03:53:15 -030063 int depth_{0};
64 unsigned int length_{4};
65 uint8_t digest_[4]{0x17, 0x7D, 0x07, 0x5F};
66 string digest_hex_{"177D075F"};
67 string diff_digest_hex_{"1234ABCD"};
68 string cert_key_prefix_{kPrefsUpdateServerCertificate};
69 ServerToCheck server_to_check_{ServerToCheck::kUpdate};
Bruno Rocha7f9aea22011-09-12 14:31:24 -070070 string cert_key_;
Alex Deymoc1c17b42015-11-23 03:53:15 -030071
72 testing::StrictMock<MockCertificateCheckObserver> observer_;
73 CertificateChecker cert_checker{&prefs_, &openssl_wrapper_, server_to_check_};
Bruno Rocha7f9aea22011-09-12 14:31:24 -070074};
75
76// check certificate change, new
77TEST_F(CertificateCheckerTest, NewCertificate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070078 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070079 .WillOnce(DoAll(
80 SetArgumentPointee<1>(depth_),
81 SetArgumentPointee<2>(length_),
82 SetArrayArgument<3>(digest_, digest_ + 4),
83 Return(true)));
Alex Deymoc1c17b42015-11-23 03:53:15 -030084 EXPECT_CALL(prefs_, GetString(cert_key_, _)).WillOnce(Return(false));
85 EXPECT_CALL(prefs_, SetString(cert_key_, digest_hex_)).WillOnce(Return(true));
86 EXPECT_CALL(observer_,
87 CertificateChecked(server_to_check_,
88 CertificateCheckResult::kValid));
89 ASSERT_TRUE(cert_checker.CheckCertificateChange(1, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -070090}
91
92// check certificate change, unchanged
93TEST_F(CertificateCheckerTest, SameCertificate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070094 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -070095 .WillOnce(DoAll(
96 SetArgumentPointee<1>(depth_),
97 SetArgumentPointee<2>(length_),
98 SetArrayArgument<3>(digest_, digest_ + 4),
99 Return(true)));
Alex Deymoc1c17b42015-11-23 03:53:15 -0300100 EXPECT_CALL(prefs_, GetString(cert_key_, _))
101 .WillOnce(DoAll(SetArgumentPointee<1>(digest_hex_), Return(true)));
102 EXPECT_CALL(prefs_, SetString(_, _)).Times(0);
103 EXPECT_CALL(observer_,
104 CertificateChecked(server_to_check_,
105 CertificateCheckResult::kValid));
106 ASSERT_TRUE(cert_checker.CheckCertificateChange(1, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700107}
108
109// check certificate change, changed
110TEST_F(CertificateCheckerTest, ChangedCertificate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700111 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700112 .WillOnce(DoAll(
113 SetArgumentPointee<1>(depth_),
114 SetArgumentPointee<2>(length_),
115 SetArrayArgument<3>(digest_, digest_ + 4),
116 Return(true)));
Alex Deymoc1c17b42015-11-23 03:53:15 -0300117 EXPECT_CALL(prefs_, GetString(cert_key_, _))
118 .WillOnce(DoAll(SetArgumentPointee<1>(diff_digest_hex_), Return(true)));
119 EXPECT_CALL(observer_,
120 CertificateChecked(server_to_check_,
121 CertificateCheckResult::kValidChanged));
122 EXPECT_CALL(prefs_, SetString(cert_key_, digest_hex_)).WillOnce(Return(true));
123 ASSERT_TRUE(cert_checker.CheckCertificateChange(1, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700124}
125
126// check certificate change, failed
127TEST_F(CertificateCheckerTest, FailedCertificate) {
Alex Deymoc1c17b42015-11-23 03:53:15 -0300128 EXPECT_CALL(observer_, CertificateChecked(server_to_check_,
129 CertificateCheckResult::kFailed));
130 EXPECT_CALL(prefs_, GetString(_, _)).Times(0);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700131 EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(_, _, _, _)).Times(0);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300132 ASSERT_FALSE(cert_checker.CheckCertificateChange(0, nullptr));
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700133}
134
135} // namespace chromeos_update_engine