106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include <cstring>
1606f6ba60Sopenharmony_ci
1706f6ba60Sopenharmony_ci#include <gtest/gtest.h>
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include "kernel_symbol_info.h"
2006f6ba60Sopenharmony_ci
2106f6ba60Sopenharmony_ciusing namespace testing::ext;
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_cinamespace OHOS {
2406f6ba60Sopenharmony_cinamespace Developtools {
2506f6ba60Sopenharmony_cinamespace Hiebpf {
2606f6ba60Sopenharmony_ciclass KernelSymbolInfoTest : public ::testing::Test {
2706f6ba60Sopenharmony_cipublic:
2806f6ba60Sopenharmony_ci    static void SetUpTestCase() {};
2906f6ba60Sopenharmony_ci    static void TearDownTestCase() {};
3006f6ba60Sopenharmony_ci
3106f6ba60Sopenharmony_ci    void SetUp() {}
3206f6ba60Sopenharmony_ci    void TearDown() {}
3306f6ba60Sopenharmony_ci};
3406f6ba60Sopenharmony_ci
3506f6ba60Sopenharmony_ci/**
3606f6ba60Sopenharmony_ci * @tc.name: GetSymbolData
3706f6ba60Sopenharmony_ci * @tc.desc: Test framework
3806f6ba60Sopenharmony_ci * @tc.type: FUNC
3906f6ba60Sopenharmony_ci */
4006f6ba60Sopenharmony_ciHWTEST_F(KernelSymbolInfoTest, GetSymbolData, TestSize.Level1)
4106f6ba60Sopenharmony_ci{
4206f6ba60Sopenharmony_ci    std::vector<uint8_t> buf;
4306f6ba60Sopenharmony_ci    uint32_t headSize = sizeof(uint64_t) + sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint32_t);
4406f6ba60Sopenharmony_ci    uint32_t size = KernelSymbolInfo::GetSymbolData(buf);
4506f6ba60Sopenharmony_ci    ASSERT_GE(size, headSize);
4606f6ba60Sopenharmony_ci    uint8_t *p = buf.data();
4706f6ba60Sopenharmony_ci    uint64_t vaddrStart =  *(reinterpret_cast<uint64_t *>(p));
4806f6ba60Sopenharmony_ci    p += sizeof(uint64_t);
4906f6ba60Sopenharmony_ci    uint64_t vaddrEnd =  *(reinterpret_cast<uint64_t *>(p));
5006f6ba60Sopenharmony_ci    p += sizeof(uint64_t);
5106f6ba60Sopenharmony_ci    uint32_t symTabLen =  *(reinterpret_cast<uint32_t *>(p));
5206f6ba60Sopenharmony_ci    p += sizeof(uint32_t);
5306f6ba60Sopenharmony_ci    uint32_t strTabLen =  *(reinterpret_cast<uint32_t *>(p));
5406f6ba60Sopenharmony_ci    p += sizeof(uint32_t);
5506f6ba60Sopenharmony_ci    ASSERT_GT(vaddrStart, 0);
5606f6ba60Sopenharmony_ci    ASSERT_GE(vaddrEnd, vaddrStart);
5706f6ba60Sopenharmony_ci    vaddrStart--;
5806f6ba60Sopenharmony_ci    ASSERT_EQ(size, headSize + symTabLen + strTabLen);
5906f6ba60Sopenharmony_ci
6006f6ba60Sopenharmony_ci    char *pStrTab = reinterpret_cast<char *>(p) + symTabLen;
6106f6ba60Sopenharmony_ci    const uint32_t symItemSize = sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint32_t);
6206f6ba60Sopenharmony_ci    while (symTabLen > symItemSize) {
6306f6ba60Sopenharmony_ci        uint64_t value =  *(reinterpret_cast<uint64_t *>(p));
6406f6ba60Sopenharmony_ci        p += sizeof(uint64_t);
6506f6ba60Sopenharmony_ci        ASSERT_GT(value, vaddrStart);
6606f6ba60Sopenharmony_ci        vaddrStart = value;
6706f6ba60Sopenharmony_ci
6806f6ba60Sopenharmony_ci        uint32_t symbolSize =  *(reinterpret_cast<uint32_t *>(p));
6906f6ba60Sopenharmony_ci        p += sizeof(uint32_t);
7006f6ba60Sopenharmony_ci        ASSERT_GE(vaddrEnd, value + symbolSize);
7106f6ba60Sopenharmony_ci
7206f6ba60Sopenharmony_ci        uint32_t symbolNameOffset =  *(reinterpret_cast<uint32_t *>(p));
7306f6ba60Sopenharmony_ci        p += sizeof(uint32_t);
7406f6ba60Sopenharmony_ci        std::string name(pStrTab + symbolNameOffset);
7506f6ba60Sopenharmony_ci        ASSERT_GE(strTabLen, symbolNameOffset + name.size() + 1);
7606f6ba60Sopenharmony_ci
7706f6ba60Sopenharmony_ci        symTabLen -= symItemSize;
7806f6ba60Sopenharmony_ci    }
7906f6ba60Sopenharmony_ci}
8006f6ba60Sopenharmony_ci} // namespace Hiebpf
8106f6ba60Sopenharmony_ci} // namespace Developtools
8206f6ba60Sopenharmony_ci} // namespace OHOS
83