Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 1 | // Copyright 2021 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 15 | #include <array> |
| 16 | |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_kvs/fake_flash_memory.h" |
| 19 | #include "pw_kvs/test_key_value_store.h" |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 20 | #include "pw_software_update/bundled_update_backend.h" |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 21 | #include "pw_software_update/update_bundle_accessor.h" |
| 22 | #include "pw_stream/memory_stream.h" |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 23 | #include "test_bundles.h" |
| 24 | |
| 25 | #define ASSERT_OK(status) ASSERT_EQ(OkStatus(), status) |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 26 | #define ASSERT_NOT_OK(status) ASSERT_NE(OkStatus(), status) |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 27 | |
| 28 | namespace pw::software_update { |
| 29 | namespace { |
| 30 | |
| 31 | constexpr size_t kBufferSize = 256; |
| 32 | static constexpr size_t kFlashAlignment = 16; |
| 33 | constexpr size_t kSectorSize = 2048; |
| 34 | constexpr size_t kSectorCount = 2; |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 35 | constexpr size_t kMetadataBufferSize = |
| 36 | blob_store::BlobStore::BlobWriter::RequiredMetadataBufferSize(0); |
| 37 | |
| 38 | class TestBundledUpdateBackend final : public BundledUpdateBackend { |
| 39 | public: |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 40 | TestBundledUpdateBackend() |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 41 | : current_manifest_reader_({}), trusted_root_memory_reader_({}) {} |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 42 | |
David Rogers | c6d70ab | 2021-11-17 16:44:20 -0800 | [diff] [blame] | 43 | Status ApplyReboot() override { return Status::Unimplemented(); } |
David Rogers | b19d586 | 2021-11-18 19:59:53 -0800 | [diff] [blame] | 44 | Status PostRebootFinalize() override { return OkStatus(); } |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 45 | |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 46 | Status VerifyTargetFile( |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 47 | [[maybe_unused]] ManifestAccessor manifest, |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 48 | [[maybe_unused]] std::string_view target_file_name) override { |
| 49 | backend_verified_files_++; |
| 50 | return verify_target_file_result_; |
| 51 | }; |
| 52 | |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 53 | Status ApplyTargetFile(std::string_view, stream::Reader&, size_t) override { |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 54 | return OkStatus(); |
| 55 | } |
Yecheng Zhao | a53cf13 | 2021-09-22 10:07:09 -0700 | [diff] [blame] | 56 | |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 57 | Result<uint32_t> EnableBundleTransferHandler(std::string_view) override { |
| 58 | return 0; |
| 59 | } |
Yecheng Zhao | a53cf13 | 2021-09-22 10:07:09 -0700 | [diff] [blame] | 60 | |
| 61 | void DisableBundleTransferHandler() override {} |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 62 | |
| 63 | void SetTrustedRoot(ConstByteSpan trusted_root) { |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 64 | trusted_root_memory_reader_ = stream::MemoryReader(trusted_root); |
| 65 | trusted_root_reader_ = stream::IntervalReader( |
| 66 | trusted_root_memory_reader_, |
| 67 | 0, |
| 68 | trusted_root_memory_reader_.ConservativeReadLimit()); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 71 | void SetCurrentManifest(ConstByteSpan current_manifest) { |
| 72 | current_manifest_reader_ = stream::MemoryReader(current_manifest); |
| 73 | } |
| 74 | |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 75 | virtual Result<stream::SeekableReader*> GetRootMetadataReader() override { |
| 76 | return &trusted_root_reader_; |
| 77 | }; |
| 78 | |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 79 | virtual Result<stream::SeekableReader*> GetCurrentManifestReader() { |
| 80 | return ¤t_manifest_reader_; |
| 81 | } |
| 82 | |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 83 | virtual Status SafelyPersistRootMetadata( |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 84 | [[maybe_unused]] stream::IntervalReader root_metadata) override { |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 85 | new_root_persisted_ = true; |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 86 | trusted_root_reader_ = root_metadata; |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 87 | return OkStatus(); |
| 88 | }; |
| 89 | |
| 90 | bool IsNewRootPersisted() const { return new_root_persisted_; } |
| 91 | |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 92 | size_t NumFilesVerified() const { return backend_verified_files_; } |
| 93 | |
| 94 | void SetVerifyTargetFileResult(Status status) { |
| 95 | verify_target_file_result_ = status; |
| 96 | } |
| 97 | |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 98 | private: |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 99 | stream::IntervalReader trusted_root_reader_; |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 100 | stream::MemoryReader current_manifest_reader_; |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 101 | bool new_root_persisted_ = false; |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 102 | size_t backend_verified_files_ = 0; |
| 103 | Status verify_target_file_result_ = OkStatus(); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 104 | |
| 105 | // A memory reader for buffer passed by SetTrustedRoot(). This will be used |
| 106 | // to back `trusted_root_reader_` |
| 107 | stream::MemoryReader trusted_root_memory_reader_; |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 108 | }; |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 109 | |
| 110 | class UpdateBundleTest : public testing::Test { |
| 111 | public: |
| 112 | UpdateBundleTest() |
| 113 | : blob_flash_(kFlashAlignment), |
| 114 | blob_partition_(&blob_flash_), |
Zihan Chen | f463acd | 2021-09-01 14:32:28 -0700 | [diff] [blame] | 115 | bundle_blob_("TestBundle", |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 116 | blob_partition_, |
| 117 | nullptr, |
| 118 | kvs::TestKvs(), |
| 119 | kBufferSize) {} |
| 120 | |
Zihan Chen | f463acd | 2021-09-01 14:32:28 -0700 | [diff] [blame] | 121 | blob_store::BlobStoreBuffer<kBufferSize>& bundle_blob() { |
| 122 | return bundle_blob_; |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 125 | TestBundledUpdateBackend& backend() { return backend_; } |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 126 | |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 127 | void StageTestBundle(ConstByteSpan bundle_data) { |
| 128 | ASSERT_OK(bundle_blob_.Init()); |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 129 | blob_store::BlobStore::BlobWriter blob_writer(bundle_blob(), |
| 130 | metadata_buffer_); |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 131 | ASSERT_OK(blob_writer.Open()); |
| 132 | ASSERT_OK(blob_writer.Write(bundle_data)); |
| 133 | ASSERT_OK(blob_writer.Close()); |
| 134 | } |
| 135 | |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 136 | // A helper to verify that all bundle operations are disallowed because |
| 137 | // the bundle is not open or verified. |
| 138 | void VerifyAllBundleOperationsDisallowed( |
| 139 | UpdateBundleAccessor& update_bundle) { |
| 140 | // We need to check specificially that failure is due to rejecting |
| 141 | // unverified/unopen bundle, not anything else. |
| 142 | ASSERT_EQ(update_bundle.GetDecoder().status(), |
| 143 | Status::FailedPrecondition()); |
| 144 | ASSERT_EQ(update_bundle.GetTargetPayload("any").status(), |
| 145 | Status::FailedPrecondition()); |
| 146 | ASSERT_EQ(update_bundle.IsTargetPayloadIncluded("any").status(), |
| 147 | Status::FailedPrecondition()); |
| 148 | |
| 149 | std::byte manifest_buffer[sizeof(kTestBundleManifest)]; |
| 150 | stream::MemoryWriter manifest_writer(manifest_buffer); |
| 151 | ASSERT_EQ(update_bundle.PersistManifest(manifest_writer), |
| 152 | Status::FailedPrecondition()); |
| 153 | } |
| 154 | |
| 155 | // A helper to verify that UpdateBundleAccessor::OpenAndVerify() fails and |
| 156 | // that all bundle operations are disallowed as a result. Also check whether |
| 157 | // root metadata should be expected to be persisted. |
| 158 | void CheckOpenAndVerifyFail(UpdateBundleAccessor& update_bundle, |
| 159 | bool expect_new_root_persisted) { |
| 160 | ASSERT_FALSE(backend().IsNewRootPersisted()); |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 161 | ASSERT_NOT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 162 | ASSERT_EQ(backend().IsNewRootPersisted(), expect_new_root_persisted); |
| 163 | VerifyAllBundleOperationsDisallowed(update_bundle); |
| 164 | |
| 165 | ASSERT_OK(update_bundle.Close()); |
| 166 | VerifyAllBundleOperationsDisallowed(update_bundle); |
| 167 | } |
| 168 | |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 169 | private: |
| 170 | kvs::FakeFlashMemoryBuffer<kSectorSize, kSectorCount> blob_flash_; |
| 171 | kvs::FlashPartition blob_partition_; |
Zihan Chen | f463acd | 2021-09-01 14:32:28 -0700 | [diff] [blame] | 172 | blob_store::BlobStoreBuffer<kBufferSize> bundle_blob_; |
Yecheng Zhao | c2b9379 | 2021-09-21 18:36:59 -0700 | [diff] [blame] | 173 | std::array<std::byte, kMetadataBufferSize> metadata_buffer_; |
| 174 | TestBundledUpdateBackend backend_; |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | } // namespace |
| 178 | |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 179 | TEST_F(UpdateBundleTest, GetTargetPayload) { |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 180 | backend().SetTrustedRoot(kDevSignedRoot); |
| 181 | StageTestBundle(kTestDevBundle); |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 182 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 183 | |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 184 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 185 | |
| 186 | { |
| 187 | stream::IntervalReader res = update_bundle.GetTargetPayload("file1"); |
| 188 | ASSERT_OK(res.status()); |
| 189 | |
| 190 | const char kExpectedContent[] = "file 1 content"; |
| 191 | char read_buffer[sizeof(kExpectedContent) + 1] = {0}; |
| 192 | ASSERT_TRUE(res.Read(read_buffer, sizeof(kExpectedContent)).ok()); |
| 193 | ASSERT_STREQ(read_buffer, kExpectedContent); |
| 194 | } |
| 195 | |
| 196 | { |
| 197 | stream::IntervalReader res = update_bundle.GetTargetPayload("file2"); |
| 198 | ASSERT_OK(res.status()); |
| 199 | |
| 200 | const char kExpectedContent[] = "file 2 content"; |
| 201 | char read_buffer[sizeof(kExpectedContent) + 1] = {0}; |
| 202 | ASSERT_TRUE(res.Read(read_buffer, sizeof(kExpectedContent)).ok()); |
| 203 | ASSERT_STREQ(read_buffer, kExpectedContent); |
| 204 | } |
| 205 | |
| 206 | { |
| 207 | stream::IntervalReader res = update_bundle.GetTargetPayload("non-exist"); |
| 208 | ASSERT_EQ(res.status(), Status::NotFound()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | TEST_F(UpdateBundleTest, IsTargetPayloadIncluded) { |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 213 | backend().SetTrustedRoot(kDevSignedRoot); |
| 214 | StageTestBundle(kTestDevBundle); |
Yecheng Zhao | ef6917c | 2021-10-27 14:11:24 -0700 | [diff] [blame] | 215 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 216 | |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 217 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 604695e | 2021-09-17 09:55:49 -0700 | [diff] [blame] | 218 | |
| 219 | Result<bool> res = update_bundle.IsTargetPayloadIncluded("file1"); |
| 220 | ASSERT_OK(res.status()); |
| 221 | ASSERT_TRUE(res.value()); |
| 222 | |
| 223 | res = update_bundle.IsTargetPayloadIncluded("file2"); |
| 224 | ASSERT_OK(res.status()); |
| 225 | ASSERT_TRUE(res.value()); |
| 226 | |
| 227 | res = update_bundle.IsTargetPayloadIncluded("non-exist"); |
| 228 | ASSERT_OK(res.status()); |
| 229 | ASSERT_FALSE(res.value()); |
| 230 | } |
| 231 | |
Yecheng Zhao | 3f758de | 2021-11-05 11:13:22 -0700 | [diff] [blame] | 232 | TEST_F(UpdateBundleTest, PersistManifest) { |
| 233 | backend().SetTrustedRoot(kDevSignedRoot); |
| 234 | StageTestBundle(kTestDevBundle); |
| 235 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 236 | |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 237 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 3f758de | 2021-11-05 11:13:22 -0700 | [diff] [blame] | 238 | |
| 239 | std::byte manifest_buffer[sizeof(kTestBundleManifest)]; |
| 240 | stream::MemoryWriter manifest_writer(manifest_buffer); |
| 241 | ASSERT_OK(update_bundle.PersistManifest(manifest_writer)); |
| 242 | |
| 243 | ASSERT_EQ( |
| 244 | memcmp(manifest_buffer, kTestBundleManifest, sizeof(kTestBundleManifest)), |
| 245 | 0); |
| 246 | } |
| 247 | |
| 248 | TEST_F(UpdateBundleTest, PersistManifestFailIfNotVerified) { |
| 249 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 250 | StageTestBundle(kTestBadProdSignature); |
Yecheng Zhao | 3f758de | 2021-11-05 11:13:22 -0700 | [diff] [blame] | 251 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 252 | |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 253 | ASSERT_NOT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 3f758de | 2021-11-05 11:13:22 -0700 | [diff] [blame] | 254 | |
| 255 | std::byte manifest_buffer[sizeof(kTestBundleManifest)]; |
| 256 | stream::MemoryWriter manifest_writer(manifest_buffer); |
| 257 | ASSERT_NOT_OK(update_bundle.PersistManifest(manifest_writer)); |
| 258 | } |
| 259 | |
| 260 | TEST_F(UpdateBundleTest, BundleVerificationDisabled) { |
| 261 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 262 | StageTestBundle(kTestBadProdSignature); |
Yecheng Zhao | 3f758de | 2021-11-05 11:13:22 -0700 | [diff] [blame] | 263 | UpdateBundleAccessor update_bundle(bundle_blob(), backend(), true); |
| 264 | |
| 265 | // Since bundle verification is disabled. The bad bundle should not report |
| 266 | // error. |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 267 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 3f758de | 2021-11-05 11:13:22 -0700 | [diff] [blame] | 268 | |
| 269 | // Manifest persisting should be allowed as well. |
| 270 | std::byte manifest_buffer[sizeof(kTestBundleManifest)]; |
| 271 | stream::MemoryWriter manifest_writer(manifest_buffer); |
| 272 | ASSERT_OK(update_bundle.PersistManifest(manifest_writer)); |
| 273 | |
| 274 | ASSERT_EQ( |
| 275 | memcmp(manifest_buffer, kTestBundleManifest, sizeof(kTestBundleManifest)), |
| 276 | 0); |
| 277 | } |
| 278 | |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 279 | TEST_F(UpdateBundleTest, OpenAndVerifySucceedsWithAllVerification) { |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 280 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 281 | backend().SetCurrentManifest(kTestBundleManifest); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 282 | StageTestBundle(kTestProdBundle); |
| 283 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 284 | |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 285 | ASSERT_FALSE(backend().IsNewRootPersisted()); |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 286 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 287 | ASSERT_TRUE(backend().IsNewRootPersisted()); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 288 | |
| 289 | // No file is personalized out in kTestProdBundle. Backend verification |
| 290 | // should not be invoked. |
| 291 | ASSERT_EQ(backend().NumFilesVerified(), static_cast<size_t>(0)); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 292 | |
| 293 | ASSERT_OK(update_bundle.Close()); |
| 294 | VerifyAllBundleOperationsDisallowed(update_bundle); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 297 | TEST_F(UpdateBundleTest, |
| 298 | OpenAndVerifyWithoutIncomingRootSucceedsWithAllVerification) { |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 299 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 300 | backend().SetCurrentManifest(kTestBundleManifest); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 301 | // kTestDevBundle does not contain an incoming root. See |
| 302 | // pw_software_update/py/pw_software_update/generate_test_bundle.py for |
| 303 | // detail of generation. |
| 304 | StageTestBundle(kTestDevBundle); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 305 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 306 | |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 307 | ASSERT_FALSE(backend().IsNewRootPersisted()); |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 308 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 309 | ASSERT_FALSE(backend().IsNewRootPersisted()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 310 | |
| 311 | // No file is personalized out in kTestDevBundle. Backend verification |
| 312 | // should not be invoked. |
| 313 | ASSERT_EQ(backend().NumFilesVerified(), static_cast<size_t>(0)); |
| 314 | |
| 315 | ASSERT_OK(update_bundle.Close()); |
| 316 | VerifyAllBundleOperationsDisallowed(update_bundle); |
| 317 | } |
| 318 | |
| 319 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMismatchedRootKeyAndSignature) { |
| 320 | backend().SetTrustedRoot(kDevSignedRoot); |
| 321 | backend().SetCurrentManifest(kTestBundleManifest); |
| 322 | // kTestMismatchedRootKeyAndSignature has a dev root metadata that is |
| 323 | // prod signed. The root metadata will not be able to verify itself. |
| 324 | // See pw_software_update/py/pw_software_update/generate_test_bundle.py for |
| 325 | // detail of generation. |
| 326 | StageTestBundle(kTestMismatchedRootKeyAndSignature); |
| 327 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 328 | CheckOpenAndVerifyFail(update_bundle, false); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnBadProdSignature) { |
| 332 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 333 | backend().SetCurrentManifest(kTestBundleManifest); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 334 | StageTestBundle(kTestBadProdSignature); |
| 335 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 336 | CheckOpenAndVerifyFail(update_bundle, false); |
Yecheng Zhao | 28072eb | 2021-10-29 15:37:06 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Yecheng Zhao | 38fee79 | 2021-11-04 15:49:16 -0700 | [diff] [blame] | 339 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnBadTargetsSignature) { |
| 340 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 341 | backend().SetCurrentManifest(kTestBundleManifest); |
Yecheng Zhao | 38fee79 | 2021-11-04 15:49:16 -0700 | [diff] [blame] | 342 | StageTestBundle(kTestBadTargetsSignature); |
| 343 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 344 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | 38fee79 | 2021-11-04 15:49:16 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 347 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnBadTargetsRollBack) { |
| 348 | backend().SetTrustedRoot(kDevSignedRoot); |
| 349 | backend().SetCurrentManifest(kTestBundleManifest); |
| 350 | StageTestBundle(kTestTargetsRollback); |
| 351 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 352 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 355 | TEST_F(UpdateBundleTest, OpenAndVerifySucceedsWithoutExistingManifest) { |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 356 | backend().SetTrustedRoot(kDevSignedRoot); |
| 357 | StageTestBundle(kTestProdBundle); |
| 358 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 359 | |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 360 | ASSERT_FALSE(backend().IsNewRootPersisted()); |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 361 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 362 | ASSERT_TRUE(backend().IsNewRootPersisted()); |
Yecheng Zhao | ed735e1 | 2021-11-09 15:04:24 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Yecheng Zhao | 16f00aa | 2021-11-09 15:55:21 -0800 | [diff] [blame] | 365 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnRootRollback) { |
| 366 | backend().SetTrustedRoot(kDevSignedRoot); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 367 | backend().SetCurrentManifest(kTestBundleManifest); |
Yecheng Zhao | 16f00aa | 2021-11-09 15:55:21 -0800 | [diff] [blame] | 368 | StageTestBundle(kTestRootRollback); |
| 369 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 370 | CheckOpenAndVerifyFail(update_bundle, false); |
Yecheng Zhao | 16f00aa | 2021-11-09 15:55:21 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 373 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMismatchedTargetHashFile0) { |
| 374 | backend().SetTrustedRoot(kDevSignedRoot); |
| 375 | backend().SetCurrentManifest(kTestBundleManifest); |
| 376 | // `kTestBundleMismatchedTargetHashFile0` is auto generated by |
| 377 | // pw_software_update/py/pw_software_update/generate_test_bundle.py. |
| 378 | // The hash value for file 0 in the targets metadata is made incorrect. |
| 379 | StageTestBundle(kTestBundleMismatchedTargetHashFile0); |
| 380 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 381 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMismatchedTargetHashFile1) { |
| 385 | backend().SetTrustedRoot(kDevSignedRoot); |
| 386 | backend().SetCurrentManifest(kTestBundleManifest); |
| 387 | // `kTestBundleMismatchedTargetHashFile1` is auto generated by |
| 388 | // pw_software_update/py/pw_software_update/generate_test_bundle.py |
| 389 | // The hash value for file 1 in the targets metadata is made incorrect. |
| 390 | StageTestBundle(kTestBundleMismatchedTargetHashFile1); |
| 391 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 392 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMissingTargetHashFile0) { |
| 396 | backend().SetTrustedRoot(kDevSignedRoot); |
| 397 | backend().SetCurrentManifest(kTestBundleManifest); |
| 398 | // `kTestBundleMismatchedTargetHashFile0` is auto generated by |
| 399 | // pw_software_update/py/pw_software_update/generate_test_bundle.py. |
| 400 | // The hash value for file 0 is removed. |
| 401 | StageTestBundle(kTestBundleMissingTargetHashFile0); |
| 402 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 403 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMissingTargetHashFile1) { |
| 407 | backend().SetTrustedRoot(kDevSignedRoot); |
| 408 | backend().SetCurrentManifest(kTestBundleManifest); |
| 409 | // `kTestBundleMismatchedTargetHashFile1` is auto generated by |
| 410 | // pw_software_update/py/pw_software_update/generate_test_bundle.py |
| 411 | // The hash value for file 1 is removed. |
| 412 | StageTestBundle(kTestBundleMissingTargetHashFile1); |
| 413 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 414 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMismatchedTargetLengthFile0) { |
| 418 | backend().SetTrustedRoot(kDevSignedRoot); |
| 419 | backend().SetCurrentManifest(kTestBundleManifest); |
| 420 | // `kTestBundleMismatchedTargetLengthFile0` is auto generated by |
| 421 | // pw_software_update/py/pw_software_update/generate_test_bundle.py. |
| 422 | // The length value for file 0 in the targets metadata is made incorrect (1). |
| 423 | StageTestBundle(kTestBundleMismatchedTargetLengthFile0); |
| 424 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 425 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnMismatchedTargetLengthFile1) { |
| 429 | backend().SetTrustedRoot(kDevSignedRoot); |
| 430 | backend().SetCurrentManifest(kTestBundleManifest); |
| 431 | // `kTestBundleMismatchedTargetLengthFile1` is auto generated by |
| 432 | // pw_software_update/py/pw_software_update/generate_test_bundle.py. |
| 433 | // The length value for file 0 in the targets metadata is made incorrect (1). |
| 434 | StageTestBundle(kTestBundleMismatchedTargetLengthFile1); |
| 435 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 436 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | TEST_F(UpdateBundleTest, OpenAndVerifySucceedsWithPersonalizedOutFile0) { |
| 440 | backend().SetTrustedRoot(kDevSignedRoot); |
| 441 | backend().SetCurrentManifest(kTestBundleManifest); |
| 442 | // `kTestBundlePersonalizedOutFile0` is auto generated by |
| 443 | // pw_software_update/py/pw_software_update/generate_test_bundle.py |
| 444 | // The payload for file 0 is removed from the bundle to emulate being |
| 445 | // personalized out. |
| 446 | StageTestBundle(kTestBundlePersonalizedOutFile0); |
| 447 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 448 | |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 449 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 450 | // Backend specific file check shall be performed only on files personalized |
| 451 | // out. |
| 452 | ASSERT_EQ(backend().NumFilesVerified(), static_cast<size_t>(1)); |
| 453 | } |
| 454 | |
| 455 | TEST_F(UpdateBundleTest, OpenAndVerifySucceedsWithPersonalizedOutFile1) { |
| 456 | backend().SetTrustedRoot(kDevSignedRoot); |
| 457 | backend().SetCurrentManifest(kTestBundleManifest); |
| 458 | // `kTestBundlePersonalizedOutFile1` is auto generated by |
| 459 | // pw_software_update/py/pw_software_update/generate_test_bundle.py |
| 460 | // The payload for file 1 is removed from the bundle to emulate being |
| 461 | // personalized out. |
| 462 | StageTestBundle(kTestBundlePersonalizedOutFile1); |
| 463 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 464 | |
Zihan Chen | 9455224 | 2021-12-13 17:54:27 -0800 | [diff] [blame] | 465 | ASSERT_OK(update_bundle.OpenAndVerify()); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 466 | // Backend specific file check shall be performed only on files personalized |
| 467 | // out. |
| 468 | ASSERT_EQ(backend().NumFilesVerified(), static_cast<size_t>(1)); |
| 469 | } |
| 470 | |
| 471 | TEST_F(UpdateBundleTest, OpenAndVerifyFailsOnBackendVerification) { |
| 472 | backend().SetTrustedRoot(kDevSignedRoot); |
| 473 | backend().SetCurrentManifest(kTestBundleManifest); |
| 474 | StageTestBundle(kTestBundlePersonalizedOutFile1); |
| 475 | UpdateBundleAccessor update_bundle(bundle_blob(), backend()); |
| 476 | backend().SetVerifyTargetFileResult(Status::Internal()); |
Yecheng Zhao | 50262fe | 2021-12-02 19:10:52 -0800 | [diff] [blame] | 477 | CheckOpenAndVerifyFail(update_bundle, true); |
Yecheng Zhao | d099757 | 2021-11-12 12:11:23 -0800 | [diff] [blame] | 478 | } |
Yecheng Zhao | afa5137 | 2021-12-15 11:58:48 -0800 | [diff] [blame] | 479 | |
Yecheng Zhao | 637b773 | 2021-08-23 22:45:40 -0700 | [diff] [blame] | 480 | } // namespace pw::software_update |