| // 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/info.hpp> |
| #include <flashupdate/ops.hpp> |
| #include <flashupdate/validator.hpp> |
| #include <flashupdate/validator/mock.hpp> |
| |
| #include <format> |
| #include <fstream> |
| #include <sstream> |
| #include <string> |
| |
| #include <gtest/gtest.h> |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| namespace flashupdate |
| { |
| |
| TEST_F(OperationTest, InvalidValidator) |
| { |
| Args args; |
| args.validatorHelper = nullptr; |
| |
| EXPECT_THROW( |
| try { ops::hashDescriptor(args); } catch (const std::runtime_error& e) { |
| EXPECT_STREQ(e.what(), "invalid Validator Helper"); |
| throw; |
| }, |
| std::runtime_error); |
| } |
| |
| TEST_F(OperationTest, HashDescriptorInvalidImage) |
| { |
| Args args; |
| std::string testBin = createTestBin(); |
| args.file = flasher::ModArgs(testBin); |
| |
| validator::Mock validatorMockHelper; |
| args.setValidatorHelper(&validatorMockHelper); |
| |
| EXPECT_CALL(validatorMockHelper, validateImage(_, _)) |
| .WillOnce(Return(false)); |
| |
| EXPECT_THROW( |
| try { ops::hashDescriptor(args); } catch (const std::runtime_error& e) { |
| EXPECT_STREQ( |
| e.what(), |
| std::format("failed to validate the CR51 descriptor for {}", |
| testBin) |
| .c_str()); |
| throw; |
| }, |
| std::runtime_error); |
| } |
| |
| TEST_F(OperationTest, HashDescriptorPass) |
| { |
| Args args; |
| args.file = flasher::ModArgs(createTestBin()); |
| |
| validator::Mock validatorMockHelper; |
| args.setValidatorHelper(&validatorMockHelper); |
| |
| std::string expectedHashStr = |
| "000100000000000000000c000000000000000000000000000000000000003000"; |
| std::vector<uint8_t> expectedHash(32); |
| expectedHash[1] = 1; |
| expectedHash[10] = 12; |
| expectedHash[30] = 48; |
| |
| EXPECT_CALL(validatorMockHelper, validateImage(_, _)) |
| .WillOnce(Return(true)); |
| EXPECT_CALL(validatorMockHelper, descriptorHash()) |
| .WillOnce( |
| Return(validator::Validator::DescriptorHash{expectedHash, 0, 0})); |
| |
| EXPECT_EQ(ops::hashDescriptor(args), expectedHashStr); |
| } |
| |
| } // namespace flashupdate |