1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "verifier.h" 17 18 #include <gtest/gtest.h> 19 #include <string> 20 #include <cstdlib> 21 22 #include "file.h" 23 #include "utils.h" 24 25 using namespace testing::ext; 26 27 namespace panda::verifier { 28 class VerifierTest : public testing::Test { 29 public: SetUpTestCase(void)30 static void SetUpTestCase(void) {}; TearDownTestCase(void)31 static void TearDownTestCase(void) {}; SetUp()32 void SetUp() {}; TearDown()33 void TearDown() {}; 34 }; 35 36 /** 37 * @tc.name: verifier_checksum_001 38 * @tc.desc: Verify the abc file checksum value function. 39 * @tc.type: FUNC 40 * @tc.require: file path and name 41 */ 42 HWTEST_F(VerifierTest, verifier_checksum_001, TestSize.Level1) 43 { 44 const std::string file_name = GRAPH_TEST_ABC_DIR "test_checksum.abc"; 45 panda::verifier::Verifier ver {file_name}; 46 EXPECT_TRUE(ver.VerifyChecksum()); 47 } 48 49 /** 50 * @tc.name: verifier_checksum_002 51 * @tc.desc: Verify the modified abc file checksum value function. 52 * @tc.type: FUNC 53 * @tc.require: file path and name 54 */ 55 HWTEST_F(VerifierTest, verifier_checksum_002, TestSize.Level1) 56 { 57 const std::string base_file_name = GRAPH_TEST_ABC_DIR "test_checksum.abc"; 58 { 59 panda::verifier::Verifier ver {base_file_name}; 60 EXPECT_TRUE(ver.VerifyChecksum()); 61 } 62 std::ifstream base_file(base_file_name, std::ios::binary); 63 EXPECT_TRUE(base_file.is_open()); 64 65 std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(base_file), {}); 66 67 std::vector<uint8_t> new_checksum = {0x01, 0x01, 0x01, 0x01}; 68 69 // The 8~11 elements in the buffer of the abc file hold the checksum 70 buffer[8] = new_checksum[0]; 71 buffer[9] = new_checksum[1]; 72 buffer[10] = new_checksum[2]; 73 buffer[11] = new_checksum[3]; 74 75 const std::string target_file_name = GRAPH_TEST_ABC_DIR "verifier_checksum_002.abc"; 76 GenerateModifiedAbc(buffer, target_file_name); 77 base_file.close(); 78 79 { 80 panda::verifier::Verifier ver {target_file_name}; 81 EXPECT_FALSE(ver.VerifyChecksum()); 82 } 83 } 84 85 /** 86 * @tc.name: verifier_checksum_003 87 * @tc.desc: Verify the modified abc file content function. 88 * @tc.type: FUNC 89 * @tc.require: file path and name 90 */ 91 HWTEST_F(VerifierTest, verifier_checksum_003, TestSize.Level1) 92 { 93 const std::string base_file_name = GRAPH_TEST_ABC_DIR "test_checksum.abc"; 94 { 95 panda::verifier::Verifier ver {base_file_name}; 96 EXPECT_TRUE(ver.VerifyChecksum()); 97 } 98 std::ifstream base_file(base_file_name, std::ios::binary); 99 EXPECT_TRUE(base_file.is_open()); 100 101 std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(base_file), {}); 102 103 std::vector<uint8_t> new_content = {0x01, 0x01}; 104 105 // The checksum calculation starts with the 12th element 106 buffer[12] = new_content[0]; 107 buffer[13] = new_content[1]; 108 109 const std::string target_file_name = GRAPH_TEST_ABC_DIR "verifier_checksum_003.abc"; 110 GenerateModifiedAbc(buffer, target_file_name); 111 base_file.close(); 112 113 { 114 panda::verifier::Verifier ver {target_file_name}; 115 EXPECT_FALSE(ver.VerifyChecksum()); 116 } 117 } 118 119 }; // namespace panda::verifier 120