summaryrefslogtreecommitdiff
path: root/payload_consumer/certificate_parser_android_unittest.cc
blob: e300414b7dea42903c9a0070dc9b3d94c0ed582f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "update_engine/payload_consumer/certificate_parser_interface.h"

#include <string>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "update_engine/common/hash_calculator.h"
#include "update_engine/common/test_utils.h"
#include "update_engine/common/utils.h"
#include "update_engine/payload_consumer/payload_verifier.h"
#include "update_engine/payload_generator/payload_signer.h"

namespace chromeos_update_engine {

extern const char* kUnittestPrivateKeyPath;
const char* kUnittestOtacertsPath = "otacerts.zip";

TEST(CertificateParserAndroidTest, ParseZipArchive) {
  std::string ota_cert =
      test_utils::GetBuildArtifactsPath(kUnittestOtacertsPath);
  ASSERT_TRUE(utils::FileExists(ota_cert.c_str()));

  std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> keys;
  auto parser = CreateCertificateParser();
  ASSERT_TRUE(parser->ReadPublicKeysFromCertificates(ota_cert, &keys));
  ASSERT_EQ(1u, keys.size());
}

TEST(CertificateParserAndroidTest, VerifySignature) {
  brillo::Blob hash_blob;
  ASSERT_TRUE(HashCalculator::RawHashOfData({'x'}, &hash_blob));
  brillo::Blob sig_blob;
  ASSERT_TRUE(PayloadSigner::SignHash(
      hash_blob,
      test_utils::GetBuildArtifactsPath(kUnittestPrivateKeyPath),
      &sig_blob));

  auto verifier = PayloadVerifier::CreateInstanceFromZipPath(
      test_utils::GetBuildArtifactsPath(kUnittestOtacertsPath));
  ASSERT_TRUE(verifier != nullptr);
  ASSERT_TRUE(verifier->VerifyRawSignature(sig_blob, hash_blob, nullptr));
}

}  // namespace chromeos_update_engine