| // 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/device/fake.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; |
| |
| class ReadTest : public OpTest<device::Fake> |
| {}; |
| |
| TEST_F(ReadTest, InvalidOffset) |
| { |
| EXPECT_THROW(read(d, /*dev_offset=*/13, f, /*file_offset=*/0, m, |
| /*max_size=*/0, /*stride_size=*/std::nullopt), |
| std::invalid_argument); |
| } |
| |
| TEST_F(ReadTest, InvalidStride) |
| { |
| EXPECT_THROW(read(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, |
| /*max_size=*/0, /*stride_size=*/0), |
| std::invalid_argument); |
| } |
| |
| TEST_F(ReadTest, ValidWhole) |
| { |
| EXPECT_NE(f.data.size(), df.data.size()); |
| read(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, /*max_size=*/20, |
| /*stride_size=*/std::nullopt); |
| EXPECT_EQ(f.data, df.data); |
| memset(f.data.data(), 0, f.data.size()); |
| read(d, /*dev_offset=*/0, f, /*file_offset=*/0, m, /*max_size=*/12, |
| /*stride_size=*/5); |
| EXPECT_EQ(f.data, df.data); |
| } |
| |
| TEST_F(ReadTest, MutateApplied) |
| { |
| m.mutations.push_back(std::make_unique<mutate::Asymmetric>()); |
| memset(df.data.data(), 0xff, df.data.size()); |
| read(d, /*dev_offset=*/2, f, /*file_offset=*/1, m, /*max_size=*/4, |
| /*stride_size=*/3); |
| EXPECT_THAT(f.data, ElementsAre(0_b, 0xfd_b, 0xfc_b, 0xfb_b, 0xfa_b)); |
| } |
| |
| } // namespace ops |
| } // namespace flasher |