Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 17 | #include "update_engine/certificate_checker.h" |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 18 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 21 | #include <base/strings/string_util.h> |
| 22 | #include <base/strings/stringprintf.h> |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 23 | #include <gmock/gmock.h> |
| 24 | #include <gtest/gtest.h> |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 25 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/mock_prefs.h" |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 28 | #include "update_engine/mock_certificate_checker.h" |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 29 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 30 | using std::string; |
| 31 | using ::testing::_; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 32 | using ::testing::DoAll; |
| 33 | using ::testing::Return; |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 34 | using ::testing::SetArgPointee; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 35 | using ::testing::SetArrayArgument; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 39 | class MockCertificateCheckObserver : public CertificateChecker::Observer { |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 40 | public: |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 41 | MOCK_METHOD2(CertificateChecked, |
| 42 | void(ServerToCheck server_to_check, |
| 43 | CertificateCheckResult result)); |
| 44 | }; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 45 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 46 | class CertificateCheckerTest : public testing::Test { |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 47 | protected: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 48 | void SetUp() override { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 49 | cert_key_ = base::StringPrintf("%s-%d-%d", |
| 50 | cert_key_prefix_.c_str(), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 51 | static_cast<int>(server_to_check_), |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 52 | depth_); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 53 | cert_checker.Init(); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 54 | cert_checker.SetObserver(&observer_); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 57 | void TearDown() override { cert_checker.SetObserver(nullptr); } |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 58 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 59 | MockPrefs prefs_; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 60 | MockOpenSSLWrapper openssl_wrapper_; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 61 | // Parameters of our mock certificate digest. |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 62 | int depth_{0}; |
| 63 | unsigned int length_{4}; |
| 64 | uint8_t digest_[4]{0x17, 0x7D, 0x07, 0x5F}; |
| 65 | string digest_hex_{"177D075F"}; |
| 66 | string diff_digest_hex_{"1234ABCD"}; |
| 67 | string cert_key_prefix_{kPrefsUpdateServerCertificate}; |
| 68 | ServerToCheck server_to_check_{ServerToCheck::kUpdate}; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 69 | string cert_key_; |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 70 | |
| 71 | testing::StrictMock<MockCertificateCheckObserver> observer_; |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 72 | CertificateChecker cert_checker{&prefs_, &openssl_wrapper_}; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // check certificate change, new |
| 76 | TEST_F(CertificateCheckerTest, NewCertificate) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 77 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _)) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 78 | .WillOnce(DoAll(SetArgPointee<1>(depth_), |
| 79 | SetArgPointee<2>(length_), |
| 80 | SetArrayArgument<3>(digest_, digest_ + 4), |
| 81 | Return(true))); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 82 | EXPECT_CALL(prefs_, GetString(cert_key_, _)).WillOnce(Return(false)); |
| 83 | EXPECT_CALL(prefs_, SetString(cert_key_, digest_hex_)).WillOnce(Return(true)); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 84 | EXPECT_CALL( |
| 85 | observer_, |
| 86 | CertificateChecked(server_to_check_, CertificateCheckResult::kValid)); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 87 | ASSERT_TRUE( |
| 88 | cert_checker.CheckCertificateChange(1, nullptr, server_to_check_)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // check certificate change, unchanged |
| 92 | TEST_F(CertificateCheckerTest, SameCertificate) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 93 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _)) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 94 | .WillOnce(DoAll(SetArgPointee<1>(depth_), |
| 95 | SetArgPointee<2>(length_), |
| 96 | SetArrayArgument<3>(digest_, digest_ + 4), |
| 97 | Return(true))); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 98 | EXPECT_CALL(prefs_, GetString(cert_key_, _)) |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 99 | .WillOnce(DoAll(SetArgPointee<1>(digest_hex_), Return(true))); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 100 | EXPECT_CALL(prefs_, SetString(_, _)).Times(0); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 101 | EXPECT_CALL( |
| 102 | observer_, |
| 103 | CertificateChecked(server_to_check_, CertificateCheckResult::kValid)); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 104 | ASSERT_TRUE( |
| 105 | cert_checker.CheckCertificateChange(1, nullptr, server_to_check_)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // check certificate change, changed |
| 109 | TEST_F(CertificateCheckerTest, ChangedCertificate) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 110 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _)) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 111 | .WillOnce(DoAll(SetArgPointee<1>(depth_), |
| 112 | SetArgPointee<2>(length_), |
| 113 | SetArrayArgument<3>(digest_, digest_ + 4), |
| 114 | Return(true))); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 115 | EXPECT_CALL(prefs_, GetString(cert_key_, _)) |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 116 | .WillOnce(DoAll(SetArgPointee<1>(diff_digest_hex_), Return(true))); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 117 | EXPECT_CALL(observer_, |
| 118 | CertificateChecked(server_to_check_, |
| 119 | CertificateCheckResult::kValidChanged)); |
| 120 | EXPECT_CALL(prefs_, SetString(cert_key_, digest_hex_)).WillOnce(Return(true)); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 121 | ASSERT_TRUE( |
| 122 | cert_checker.CheckCertificateChange(1, nullptr, server_to_check_)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // check certificate change, failed |
| 126 | TEST_F(CertificateCheckerTest, FailedCertificate) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 127 | EXPECT_CALL( |
| 128 | observer_, |
| 129 | CertificateChecked(server_to_check_, CertificateCheckResult::kFailed)); |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 130 | EXPECT_CALL(prefs_, GetString(_, _)).Times(0); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 131 | EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(_, _, _, _)).Times(0); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 132 | ASSERT_FALSE( |
| 133 | cert_checker.CheckCertificateChange(0, nullptr, server_to_check_)); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace chromeos_update_engine |