certificate_checker_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. //
  16. #include "update_engine/certificate_checker.h"
  17. #include <string>
  18. #include <base/strings/string_util.h>
  19. #include <base/strings/stringprintf.h>
  20. #include <gmock/gmock.h>
  21. #include <gtest/gtest.h>
  22. #include "update_engine/common/constants.h"
  23. #include "update_engine/common/mock_prefs.h"
  24. #include "update_engine/mock_certificate_checker.h"
  25. using std::string;
  26. using ::testing::_;
  27. using ::testing::DoAll;
  28. using ::testing::Return;
  29. using ::testing::SetArgPointee;
  30. using ::testing::SetArrayArgument;
  31. namespace chromeos_update_engine {
  32. class MockCertificateCheckObserver : public CertificateChecker::Observer {
  33. public:
  34. MOCK_METHOD2(CertificateChecked,
  35. void(ServerToCheck server_to_check,
  36. CertificateCheckResult result));
  37. };
  38. class CertificateCheckerTest : public testing::Test {
  39. protected:
  40. void SetUp() override {
  41. cert_key_ = base::StringPrintf("%s-%d-%d",
  42. cert_key_prefix_.c_str(),
  43. static_cast<int>(server_to_check_),
  44. depth_);
  45. cert_checker.Init();
  46. cert_checker.SetObserver(&observer_);
  47. }
  48. void TearDown() override { cert_checker.SetObserver(nullptr); }
  49. MockPrefs prefs_;
  50. MockOpenSSLWrapper openssl_wrapper_;
  51. // Parameters of our mock certificate digest.
  52. int depth_{0};
  53. unsigned int length_{4};
  54. uint8_t digest_[4]{0x17, 0x7D, 0x07, 0x5F};
  55. string digest_hex_{"177D075F"};
  56. string diff_digest_hex_{"1234ABCD"};
  57. string cert_key_prefix_{kPrefsUpdateServerCertificate};
  58. ServerToCheck server_to_check_{ServerToCheck::kUpdate};
  59. string cert_key_;
  60. testing::StrictMock<MockCertificateCheckObserver> observer_;
  61. CertificateChecker cert_checker{&prefs_, &openssl_wrapper_};
  62. };
  63. // check certificate change, new
  64. TEST_F(CertificateCheckerTest, NewCertificate) {
  65. EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
  66. .WillOnce(DoAll(SetArgPointee<1>(depth_),
  67. SetArgPointee<2>(length_),
  68. SetArrayArgument<3>(digest_, digest_ + 4),
  69. Return(true)));
  70. EXPECT_CALL(prefs_, GetString(cert_key_, _)).WillOnce(Return(false));
  71. EXPECT_CALL(prefs_, SetString(cert_key_, digest_hex_)).WillOnce(Return(true));
  72. EXPECT_CALL(
  73. observer_,
  74. CertificateChecked(server_to_check_, CertificateCheckResult::kValid));
  75. ASSERT_TRUE(
  76. cert_checker.CheckCertificateChange(1, nullptr, server_to_check_));
  77. }
  78. // check certificate change, unchanged
  79. TEST_F(CertificateCheckerTest, SameCertificate) {
  80. EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
  81. .WillOnce(DoAll(SetArgPointee<1>(depth_),
  82. SetArgPointee<2>(length_),
  83. SetArrayArgument<3>(digest_, digest_ + 4),
  84. Return(true)));
  85. EXPECT_CALL(prefs_, GetString(cert_key_, _))
  86. .WillOnce(DoAll(SetArgPointee<1>(digest_hex_), Return(true)));
  87. EXPECT_CALL(prefs_, SetString(_, _)).Times(0);
  88. EXPECT_CALL(
  89. observer_,
  90. CertificateChecked(server_to_check_, CertificateCheckResult::kValid));
  91. ASSERT_TRUE(
  92. cert_checker.CheckCertificateChange(1, nullptr, server_to_check_));
  93. }
  94. // check certificate change, changed
  95. TEST_F(CertificateCheckerTest, ChangedCertificate) {
  96. EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(nullptr, _, _, _))
  97. .WillOnce(DoAll(SetArgPointee<1>(depth_),
  98. SetArgPointee<2>(length_),
  99. SetArrayArgument<3>(digest_, digest_ + 4),
  100. Return(true)));
  101. EXPECT_CALL(prefs_, GetString(cert_key_, _))
  102. .WillOnce(DoAll(SetArgPointee<1>(diff_digest_hex_), Return(true)));
  103. EXPECT_CALL(observer_,
  104. CertificateChecked(server_to_check_,
  105. CertificateCheckResult::kValidChanged));
  106. EXPECT_CALL(prefs_, SetString(cert_key_, digest_hex_)).WillOnce(Return(true));
  107. ASSERT_TRUE(
  108. cert_checker.CheckCertificateChange(1, nullptr, server_to_check_));
  109. }
  110. // check certificate change, failed
  111. TEST_F(CertificateCheckerTest, FailedCertificate) {
  112. EXPECT_CALL(
  113. observer_,
  114. CertificateChecked(server_to_check_, CertificateCheckResult::kFailed));
  115. EXPECT_CALL(prefs_, GetString(_, _)).Times(0);
  116. EXPECT_CALL(openssl_wrapper_, GetCertificateDigest(_, _, _, _)).Times(0);
  117. ASSERT_FALSE(
  118. cert_checker.CheckCertificateChange(0, nullptr, server_to_check_));
  119. }
  120. } // namespace chromeos_update_engine