verity_writer_android_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (C) 2018 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/payload_consumer/verity_writer_android.h"
  17. #include <brillo/secure_blob.h>
  18. #include <gtest/gtest.h>
  19. #include "update_engine/common/test_utils.h"
  20. #include "update_engine/common/utils.h"
  21. namespace chromeos_update_engine {
  22. class VerityWriterAndroidTest : public ::testing::Test {
  23. protected:
  24. void SetUp() override {
  25. partition_.target_path = temp_file_.path();
  26. partition_.block_size = 4096;
  27. partition_.hash_tree_data_offset = 0;
  28. partition_.hash_tree_data_size = 4096;
  29. partition_.hash_tree_offset = 4096;
  30. partition_.hash_tree_size = 4096;
  31. partition_.hash_tree_algorithm = "sha1";
  32. partition_.fec_roots = 2;
  33. }
  34. VerityWriterAndroid verity_writer_;
  35. InstallPlan::Partition partition_;
  36. test_utils::ScopedTempFile temp_file_;
  37. };
  38. TEST_F(VerityWriterAndroidTest, SimpleTest) {
  39. brillo::Blob part_data(8192);
  40. test_utils::WriteFileVector(partition_.target_path, part_data);
  41. ASSERT_TRUE(verity_writer_.Init(partition_));
  42. EXPECT_TRUE(verity_writer_.Update(0, part_data.data(), 4096));
  43. EXPECT_TRUE(verity_writer_.Update(4096, part_data.data() + 4096, 4096));
  44. brillo::Blob actual_part;
  45. utils::ReadFile(partition_.target_path, &actual_part);
  46. // dd if=/dev/zero bs=4096 count=1 2>/dev/null | sha1sum | xxd -r -p |
  47. // hexdump -v -e '/1 "0x%02x, "'
  48. brillo::Blob hash = {0x1c, 0xea, 0xf7, 0x3d, 0xf4, 0x0e, 0x53,
  49. 0x1d, 0xf3, 0xbf, 0xb2, 0x6b, 0x4f, 0xb7,
  50. 0xcd, 0x95, 0xfb, 0x7b, 0xff, 0x1d};
  51. memcpy(part_data.data() + 4096, hash.data(), hash.size());
  52. EXPECT_EQ(part_data, actual_part);
  53. }
  54. TEST_F(VerityWriterAndroidTest, NoOpTest) {
  55. partition_.hash_tree_data_size = 0;
  56. partition_.hash_tree_size = 0;
  57. brillo::Blob part_data(4096);
  58. ASSERT_TRUE(verity_writer_.Init(partition_));
  59. EXPECT_TRUE(verity_writer_.Update(0, part_data.data(), part_data.size()));
  60. EXPECT_TRUE(verity_writer_.Update(4096, part_data.data(), part_data.size()));
  61. EXPECT_TRUE(verity_writer_.Update(8192, part_data.data(), part_data.size()));
  62. }
  63. TEST_F(VerityWriterAndroidTest, InvalidHashAlgorithmTest) {
  64. partition_.hash_tree_algorithm = "sha123";
  65. EXPECT_FALSE(verity_writer_.Init(partition_));
  66. }
  67. TEST_F(VerityWriterAndroidTest, WrongHashTreeSizeTest) {
  68. partition_.hash_tree_size = 8192;
  69. EXPECT_FALSE(verity_writer_.Init(partition_));
  70. }
  71. TEST_F(VerityWriterAndroidTest, SHA256Test) {
  72. partition_.hash_tree_algorithm = "sha256";
  73. brillo::Blob part_data(8192);
  74. test_utils::WriteFileVector(partition_.target_path, part_data);
  75. ASSERT_TRUE(verity_writer_.Init(partition_));
  76. EXPECT_TRUE(verity_writer_.Update(0, part_data.data(), 4096));
  77. EXPECT_TRUE(verity_writer_.Update(4096, part_data.data() + 4096, 4096));
  78. brillo::Blob actual_part;
  79. utils::ReadFile(partition_.target_path, &actual_part);
  80. // dd if=/dev/zero bs=4096 count=1 2>/dev/null | sha256sum | xxd -r -p |
  81. // hexdump -v -e '/1 "0x%02x, "'
  82. brillo::Blob hash = {0xad, 0x7f, 0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9,
  83. 0x66, 0xc0, 0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02,
  84. 0x4f, 0x58, 0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a,
  85. 0x85, 0xda, 0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7};
  86. memcpy(part_data.data() + 4096, hash.data(), hash.size());
  87. EXPECT_EQ(part_data, actual_part);
  88. }
  89. TEST_F(VerityWriterAndroidTest, FECTest) {
  90. partition_.fec_data_offset = 0;
  91. partition_.fec_data_size = 4096;
  92. partition_.fec_offset = 4096;
  93. partition_.fec_size = 2 * 4096;
  94. brillo::Blob part_data(3 * 4096, 0x1);
  95. test_utils::WriteFileVector(partition_.target_path, part_data);
  96. ASSERT_TRUE(verity_writer_.Init(partition_));
  97. EXPECT_TRUE(verity_writer_.Update(0, part_data.data(), part_data.size()));
  98. brillo::Blob actual_part;
  99. utils::ReadFile(partition_.target_path, &actual_part);
  100. // Write FEC data.
  101. for (size_t i = 4096; i < part_data.size(); i += 2) {
  102. part_data[i] = 0x8e;
  103. part_data[i + 1] = 0x8f;
  104. }
  105. EXPECT_EQ(part_data, actual_part);
  106. }
  107. } // namespace chromeos_update_engine