Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // 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 | #include <vector> |
| 7 | #include <gtest/gtest.h> |
| 8 | #include "base/logging.h" |
| 9 | #include "update_engine/payload_signer.h" |
| 10 | #include "update_engine/update_metadata.pb.h" |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | using std::vector; |
| 15 | |
| 16 | // Note: the test key was generated with the following command: |
| 17 | // openssl genrsa -out unittest_key.pem 1024 |
| 18 | |
| 19 | const char kUnittestPrivateKeyPath[] = "unittest_key.pem"; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | //class PayloadSignerTest : public ::testing::Test {}; |
| 24 | |
| 25 | TEST(PayloadSignerTest, SimpleTest) { |
| 26 | // Some data and its corresponding signature: |
| 27 | const string kDataToSign = "This is some data to sign."; |
| 28 | const char kExpectedSignature[] = { |
| 29 | 0x74, 0xd9, 0xea, 0x45, 0xf4, 0xd8, 0x64, 0x16, |
| 30 | 0x88, 0x1b, 0x7f, 0x8b, 0x5d, 0xcb, 0x22, 0x2c, |
| 31 | 0xb1, 0xce, 0x6d, 0x6d, 0x7c, 0x8f, 0x76, 0xf0, |
| 32 | 0xb7, 0xa9, 0x80, 0xb3, 0x5e, 0x0b, 0xdd, 0x99, |
| 33 | 0xfd, 0x88, 0x1f, 0x64, 0xd6, 0xac, 0x0c, 0x1b, |
| 34 | 0xb1, 0x3c, 0x28, 0x11, 0x97, 0x15, 0x97, 0xec, |
| 35 | 0x90, 0x25, 0xa0, 0x64, 0x90, 0x36, 0x5a, 0x96, |
| 36 | 0x21, 0xdf, 0x16, 0x42, 0x6d, 0x7c, 0xb1, 0xf2, |
| 37 | 0xf6, 0xe3, 0xb2, 0xa9, 0xea, 0xc8, 0xec, 0x6b, |
| 38 | 0xa1, 0x99, 0x8a, 0xf0, 0x25, 0x0d, 0xcd, 0x41, |
| 39 | 0x85, 0x76, 0x7c, 0xe1, 0xd6, 0x70, 0x71, 0xda, |
| 40 | 0x02, 0x9f, 0xa2, 0x40, 0xb2, 0xfe, 0xfd, 0x84, |
| 41 | 0x5c, 0xcf, 0x08, 0xa8, 0x50, 0x16, 0x46, 0xc1, |
| 42 | 0x37, 0xe1, 0x16, 0xd2, 0xf5, 0x49, 0xe3, 0xcb, |
| 43 | 0x58, 0x57, 0x11, 0x97, 0x49, 0x8f, 0x14, 0x1d, |
| 44 | 0x4d, 0xa6, 0xfc, 0x75, 0x63, 0x64, 0xa3, 0xd5 |
| 45 | }; |
| 46 | |
| 47 | string data_path; |
| 48 | ASSERT_TRUE( |
| 49 | utils::MakeTempFile("/tmp/data.XXXXXX", &data_path, NULL)); |
| 50 | ScopedPathUnlinker data_path_unlinker(data_path); |
| 51 | ASSERT_TRUE(utils::WriteFile(data_path.c_str(), |
| 52 | kDataToSign.data(), |
| 53 | kDataToSign.size())); |
| 54 | uint64_t length = 0; |
| 55 | EXPECT_TRUE(PayloadSigner::SignatureBlobLength(kUnittestPrivateKeyPath, |
| 56 | &length)); |
| 57 | EXPECT_GT(length, 0); |
| 58 | vector<char> signature_blob; |
| 59 | EXPECT_TRUE(PayloadSigner::SignPayload(data_path, |
| 60 | kUnittestPrivateKeyPath, |
| 61 | &signature_blob)); |
| 62 | EXPECT_EQ(length, signature_blob.size()); |
| 63 | |
| 64 | // Check the signature itself |
| 65 | |
| 66 | Signatures signatures; |
| 67 | EXPECT_TRUE(signatures.ParseFromArray(&signature_blob[0], |
| 68 | signature_blob.size())); |
| 69 | EXPECT_EQ(1, signatures.signatures_size()); |
| 70 | const Signatures_Signature& signature = signatures.signatures(0); |
| 71 | EXPECT_EQ(kSignatureMessageVersion, signature.version()); |
| 72 | const string sig_data = signature.data(); |
| 73 | ASSERT_EQ(sizeof(kExpectedSignature), sig_data.size()); |
| 74 | for (size_t i = 0; i < sizeof(kExpectedSignature); i++) { |
| 75 | EXPECT_EQ(kExpectedSignature[i], sig_data[i]); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } // namespace chromeos_update_engine |