| // 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 <flashupdate/info.hpp> |
| #include <flashupdate/ops.hpp> |
| #include <stdplus/gtest/tmp.hpp> |
| |
| #include <chrono> |
| #include <format> |
| #include <fstream> |
| #include <sstream> |
| #include <string> |
| #include <thread> |
| |
| #include <gtest/gtest.h> |
| |
| namespace flashupdate |
| { |
| |
| class OperationTest : public stdplus::gtest::TestWithTmp |
| { |
| protected: |
| OperationTest() |
| { |
| resetInfo(); |
| } |
| |
| void resetInfo() |
| { |
| updateInfo.active = version::Version(activeVersion); |
| updateInfo.stage = version::Version(stageVersion); |
| updateInfo.stagingIndex = stagingIndex; |
| updateInfo.state = |
| static_cast<uint8_t>(info::UpdateInfo::State::ACTIVATED); |
| updateInfo.stageDescriptor.offset = 0; |
| updateInfo.stageDescriptor.size = 0; |
| std::memcpy(updateInfo.stageDescriptor.hash, defaultHash.data(), |
| defaultHash.size()); |
| } |
| |
| void createFakeEeprom(Args& args, std::string& filename) |
| { |
| std::ofstream fakeEeprom(filename.data(), |
| std::ios::out | std::ios::binary); |
| fakeEeprom.write(blank.data(), blank.size()); |
| fakeEeprom.close(); |
| |
| args.config.metadata.path = |
| std::format("fake,type=simple,erase=0,{}", filename); |
| args.config.metadata.offset = 0; |
| ops::writeInfo(args, updateInfo); |
| } |
| |
| std::string createTestImage() |
| { |
| std::string imageName = CaseTmpDir() + "/test-image"; |
| std::ofstream testImage(imageName.data(), |
| std::ios::out | std::ios::binary); |
| testImage.write(std::string(inputData.size(), '\0').data(), |
| inputData.size()); |
| testImage.close(); |
| return std::format("fake,type=nor,erase={},{}", inputData.size(), |
| imageName); |
| } |
| |
| std::string createTestDev() |
| { |
| std::string devName = CaseTmpDir() + "/test-dev"; |
| std::ofstream testImage(devName.data(), |
| std::ios::out | std::ios::binary); |
| testImage.write(inputData.data(), inputData.size()); |
| testImage.close(); |
| return std::format("fake,type=nor,erase={},{}", inputData.size(), |
| devName); |
| } |
| |
| std::string createTestBin() |
| { |
| std::string testBin = CaseTmpDir() + "/test.bin"; |
| std::ofstream testImage(testBin.data(), |
| std::ios::out | std::ios::binary); |
| testImage.write(inputData.data(), inputData.size()); |
| testImage.close(); |
| return testBin; |
| } |
| |
| info::UpdateInfo updateInfo; |
| std::string activeVersion = "10.11.12.13"; |
| std::string stageVersion = "4.3.2.1"; |
| std::string inputData = "hello world"; |
| uint8_t stagingIndex = 3; |
| std::vector<uint8_t> defaultHash = |
| std::vector<uint8_t>(SHA256_DIGEST_LENGTH, 16); |
| |
| std::vector<char> blank = std::vector<char>(sizeof(info::UpdateInfo), 0); |
| }; |
| |
| } // namespace flashupdate |