| // 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 "utility.hpp" |
| |
| #include <flashupdate/flash/mock.hpp> |
| #include <flashupdate/info.hpp> |
| #include <flashupdate/ops.hpp> |
| #include <flashupdate/validator/mock.hpp> |
| #include <nlohmann/json.hpp> |
| |
| #include <format> |
| |
| #include <gtest/gtest.h> |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| namespace flashupdate |
| { |
| |
| TEST_F(OperationTest, InvalidValidator) |
| { |
| Args args; |
| args.validatorHelper = nullptr; |
| |
| EXPECT_THROW( |
| try { ops::fetchVersion(args); } catch (const std::runtime_error& e) { |
| EXPECT_STREQ(e.what(), "invalid Validator Helper"); |
| throw; |
| }, |
| std::runtime_error); |
| } |
| |
| TEST_F(OperationTest, FetchVersionInvalidSecondaryPartition) |
| { |
| Args args; |
| args.primary = false; |
| args.stagingIndex = 10; |
| args.config.flash.secondary = std::vector<Config::Partition>(1); |
| |
| EXPECT_THROW( |
| try { ops::fetchVersion(args); } catch (const std::runtime_error& e) { |
| EXPECT_STREQ(e.what(), |
| std::format("invalid staged_index for secondary " |
| "partition: got {}, want < " |
| "{}", |
| args.stagingIndex, |
| args.config.flash.secondary.size()) |
| .c_str()); |
| throw; |
| }, |
| std::runtime_error); |
| } |
| |
| TEST_F(OperationTest, FetchVersionInvalidPartition) |
| { |
| Args args; |
| args.primary = false; |
| args.stagingIndex = 0; |
| args.config.flash.secondary = std::vector<Config::Partition>(1); |
| |
| EXPECT_THROW( |
| try { ops::fetchVersion(args); } catch (const std::runtime_error& e) { |
| EXPECT_STREQ(e.what(), "failed to find flash partition"); |
| throw; |
| }, |
| std::runtime_error); |
| } |
| |
| TEST_F(OperationTest, FetchVersionPass) |
| { |
| resetInfo(); |
| |
| Args args; |
| args.primary = false; |
| args.stagingIndex = 0; |
| args.config.flash.secondary = std::vector<Config::Partition>(1); |
| const std::string version = "1.2.3.4"; |
| std::string filename = CaseTmpDir() + "/fetch_version_metadata"; |
| createFakeEeprom(args, filename); |
| |
| validator::Mock validatorMockHelper; |
| args.setValidatorHelper(&validatorMockHelper); |
| EXPECT_CALL(validatorMockHelper, fetchVersion(_, _, _)) |
| .WillOnce(Return(version)); |
| |
| flash::Mock flashMockHelper; |
| args.setFlashHelper(&flashMockHelper); |
| EXPECT_CALL(flashMockHelper, getFlash(_, _)) |
| .WillOnce(Return(std::make_pair(createTestDev(), inputData.size()))); |
| |
| EXPECT_EQ(ops::fetchVersion(args), version); |
| } |
| |
| } // namespace flashupdate |