| // Copyright 2021 Google LLC |
| // |
| // 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 "util.hpp" |
| |
| #include <flasher/mutate/asymmetric.hpp> |
| #include <flasher/ops.hpp> |
| |
| #include <cstring> |
| #include <memory> |
| #include <optional> |
| #include <stdexcept> |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| namespace flasher |
| { |
| namespace ops |
| { |
| |
| using testing::ElementsAre; |
| using testing::SizeIs; |
| |
| class WriteTest : public OpTest<testing::NiceMock<MockOrFakeDevice>> |
| { |
| protected: |
| WriteTest() |
| { |
| fillFileInc(f, 0, 12); |
| EXPECT_CALL(d, eraseBlocks).Times(0); |
| } |
| }; |
| |
| TEST_F(WriteTest, InvalidOffset) |
| { |
| EXPECT_THROW(write(d, /*dev_offset=*/13, f, /*file_offset=*/0, m, |
| /*max_size=*/0, /*stride_size=*/std::nullopt, |
| /*noread=*/false), |
| std::invalid_argument); |
| } |
| |
| TEST_F(WriteTest, InvalidStride) |
| { |
| EXPECT_THROW(write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, |
| /*max_size=*/0, /*stride_size=*/0, /*noread=*/false), |
| std::invalid_argument); |
| } |
| |
| TEST_F(WriteTest, SimpleOverwrite) |
| { |
| memset(f.data.data(), 0, f.data.size()); |
| EXPECT_NE(f.data, df.data); |
| write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, /*max_size=*/20, |
| /*stride_size=*/4, /*noread=*/false); |
| EXPECT_EQ(f.data, df.data); |
| } |
| |
| TEST_F(WriteTest, SimpleOverwriteNoRead) |
| { |
| memset(f.data.data(), 0, f.data.size()); |
| EXPECT_NE(f.data, df.data); |
| write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, /*max_size=*/20, |
| /*stride_size=*/4, /*noread=*/true); |
| EXPECT_EQ(f.data, df.data); |
| } |
| |
| TEST_F(WriteTest, SmallFile) |
| { |
| f.data.resize(4); |
| memset(f.data.data(), 0, f.data.size()); |
| write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, /*max_size=*/20, |
| /*stride_size=*/std::nullopt, /*noread=*/false); |
| df.data.resize(4); |
| EXPECT_EQ(f.data, df.data); |
| } |
| |
| TEST_F(WriteTest, FileTooBig) |
| { |
| fillFileInc(f, 0, 13); |
| EXPECT_THROW(write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, |
| /*max_size=*/20, /*stride_size=*/std::nullopt, |
| /*noread=*/false), |
| std::runtime_error); |
| EXPECT_THROW(write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, |
| /*max_size=*/20, /*stride_size=*/std::nullopt, |
| /*noread=*/true), |
| std::runtime_error); |
| } |
| |
| TEST_F(WriteTest, ValidSubset) |
| { |
| fillFileInc(f, 2, 10); |
| memset(df.data.data(), 0xff, df.data.size()); |
| write(d, /*dev_offset=*/3, f, /*file_offset=*/1, m, /*max_size=*/5, |
| /*stride_size=*/std::nullopt, /*noread=*/false); |
| EXPECT_THAT(df.data, ElementsAre(0xff_b, 0xff_b, 0xff_b, 3_b, 4_b, 5_b, 6_b, |
| 7_b, 0xff_b, 0xff_b, 0xff_b, 0xff_b)); |
| } |
| |
| TEST_F(WriteTest, ValidSubsetNoRead) |
| { |
| fillFileInc(f, 2, 10); |
| memset(df.data.data(), 0xff, df.data.size()); |
| write(d, /*dev_offset=*/3, f, /*file_offset=*/1, m, /*max_size=*/5, |
| /*stride_size=*/3, /*noread=*/true); |
| EXPECT_THAT(df.data, ElementsAre(0xff_b, 0xff_b, 0xff_b, 3_b, 4_b, 5_b, 6_b, |
| 7_b, 0xff_b, 0xff_b, 0xff_b, 0xff_b)); |
| } |
| |
| TEST_F(WriteTest, TooBigSubset) |
| { |
| fillFileInc(f, 2, 11); |
| EXPECT_THROW(write(d, /*dev_offset=*/3, f, /*file_offset=*/1, m, |
| /*max_size=*/20, /*stride_size=*/std::nullopt, |
| /*noread=*/false), |
| std::runtime_error); |
| EXPECT_THROW(write(d, /*dev_offset=*/3, f, /*file_offset=*/1, m, |
| /*max_size=*/20, /*stride_size=*/std::nullopt, |
| /*noread=*/true), |
| std::runtime_error); |
| } |
| |
| TEST_F(WriteTest, MutateApplied) |
| { |
| m.mutations.push_back(std::make_unique<mutate::Asymmetric>()); |
| memset(f.data.data(), 0, f.data.size()); |
| memset(df.data.data(), 0xff, df.data.size()); |
| write(d, /*dev_offset=*/1, f, /*file_offset=*/1, m, /*max_size=*/4, |
| /*stride_size=*/std::nullopt, /*noread=*/false); |
| EXPECT_THAT(df.data, ElementsAre(0xff_b, 1_b, 2_b, 3_b, 4_b, 0xff_b, 0xff_b, |
| 0xff_b, 0xff_b, 0xff_b, 0xff_b, 0xff_b)); |
| } |
| |
| TEST_F(WriteTest, OnlyNeededWrites) |
| { |
| f.data = {3_b, 3_b, 3_b, 3_b, 3_b, 3_b, 3_b, 3_b}; |
| df.data = {3_b, 1_b, 2_b, 3_b, 3_b, 3_b, 4_b, 3_b}; |
| EXPECT_CALL(d, writeAt(SizeIs(1), 6)); |
| write(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, /*max_size=*/8, |
| /*stride_size=*/4, /*noread=*/false); |
| } |
| |
| } // namespace ops |
| } // namespace flasher |