1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci#include <ctime>
18800b99b8Sopenharmony_ci#include <securec.h>
19800b99b8Sopenharmony_ci#include <string>
20800b99b8Sopenharmony_ci#include <sys/utsname.h>
21800b99b8Sopenharmony_ci#include <vector>
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_ci#include "arch_util.h"
24800b99b8Sopenharmony_ci#include "unwind_define.h"
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX;
27800b99b8Sopenharmony_ciusing namespace testing::ext;
28800b99b8Sopenharmony_ciusing namespace std;
29800b99b8Sopenharmony_ci
30800b99b8Sopenharmony_cinamespace OHOS {
31800b99b8Sopenharmony_cinamespace HiviewDFX {
32800b99b8Sopenharmony_ciclass ArchUtilTest : public testing::Test {
33800b99b8Sopenharmony_cipublic:
34800b99b8Sopenharmony_ci    static void SetUpTestCase(void) {}
35800b99b8Sopenharmony_ci    static void TearDownTestCase(void) {}
36800b99b8Sopenharmony_ci    void SetUp() {}
37800b99b8Sopenharmony_ci    void TearDown() {}
38800b99b8Sopenharmony_ci};
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ci/**
41800b99b8Sopenharmony_ci * @tc.name: ArchUtilTest001
42800b99b8Sopenharmony_ci * @tc.desc: test ArchUtil functions
43800b99b8Sopenharmony_ci * @tc.type: FUNC
44800b99b8Sopenharmony_ci */
45800b99b8Sopenharmony_ciHWTEST_F(ArchUtilTest, ArchUtilTest001, TestSize.Level2)
46800b99b8Sopenharmony_ci{
47800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "ArchUtilTest001: start.";
48800b99b8Sopenharmony_ci    utsname systemName;
49800b99b8Sopenharmony_ci#if defined(__arm__)
50800b99b8Sopenharmony_ci    if ((uname(&systemName)) != 0) {
51800b99b8Sopenharmony_ci        ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_ARM);
52800b99b8Sopenharmony_ci    }
53800b99b8Sopenharmony_ci    ASSERT_EQ(GetCurrentArch(), ArchType::ARCH_ARM);
54800b99b8Sopenharmony_ci#elif defined(__aarch64__)
55800b99b8Sopenharmony_ci    if ((uname(&systemName)) != 0) {
56800b99b8Sopenharmony_ci        ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_ARM64);
57800b99b8Sopenharmony_ci    }
58800b99b8Sopenharmony_ci    ASSERT_EQ(GetCurrentArch(), ArchType::ARCH_ARM64);
59800b99b8Sopenharmony_ci#elif defined(__i386__)
60800b99b8Sopenharmony_ci    if ((uname(&systemName)) != 0) {
61800b99b8Sopenharmony_ci        ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_X86);
62800b99b8Sopenharmony_ci    }
63800b99b8Sopenharmony_ci    ASSERT_EQ(GetCurrentArch(), ArchType::ARCH_X86);
64800b99b8Sopenharmony_ci#elif defined(__x86_64__)
65800b99b8Sopenharmony_ci    if ((uname(&systemName)) != 0) {
66800b99b8Sopenharmony_ci        ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_X86_64);
67800b99b8Sopenharmony_ci    }
68800b99b8Sopenharmony_ci    ASSER_EQ(GetCurrentArch(), ArchType::ARCH_X86_64);
69800b99b8Sopenharmony_ci#else
70800b99b8Sopenharmony_ci    if ((uname(&systemName)) != 0) {
71800b99b8Sopenharmony_ci        ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_UNKNOWN);
72800b99b8Sopenharmony_ci    }
73800b99b8Sopenharmony_ci    ASSER_EQ(GetCurrentArch(), ArchType::ARCH_UNKNOWN);
74800b99b8Sopenharmony_ci#endif
75800b99b8Sopenharmony_ci    ASSERT_EQ(GetArchName(ArchType::ARCH_X86), "X86_32");
76800b99b8Sopenharmony_ci    ASSERT_EQ(GetArchName(ArchType::ARCH_X86_64), "X86_64");
77800b99b8Sopenharmony_ci    ASSERT_EQ(GetArchName(ArchType::ARCH_ARM), "ARM");
78800b99b8Sopenharmony_ci    ASSERT_EQ(GetArchName(ArchType::ARCH_ARM64), "ARM64");
79800b99b8Sopenharmony_ci    ASSERT_EQ(GetArchName(ArchType::ARCH_RISCV64), "RISCV64");
80800b99b8Sopenharmony_ci    ASSERT_EQ(GetArchName(ArchType::ARCH_UNKNOWN), "Unsupport");
81800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "ArchUtilTest001: end.";
82800b99b8Sopenharmony_ci}
83800b99b8Sopenharmony_ci
84800b99b8Sopenharmony_ci/**
85800b99b8Sopenharmony_ci * @tc.name: ArchUtilTest002
86800b99b8Sopenharmony_ci * @tc.desc: test ArchUtil GetArchFromUname
87800b99b8Sopenharmony_ci * @tc.type: FUNC
88800b99b8Sopenharmony_ci */
89800b99b8Sopenharmony_ciHWTEST_F(ArchUtilTest, ArchUtilTest002, TestSize.Level2)
90800b99b8Sopenharmony_ci{
91800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "ArchUtilTest002: start.";
92800b99b8Sopenharmony_ci    std::unordered_map<std::string, ArchType> machineMap = {
93800b99b8Sopenharmony_ci        {"arm", ArchType::ARCH_ARM},
94800b99b8Sopenharmony_ci        {"armv8l", ArchType::ARCH_ARM64},
95800b99b8Sopenharmony_ci        {"aarch64", ArchType::ARCH_ARM64},
96800b99b8Sopenharmony_ci        {"riscv64", ArchType::ARCH_RISCV64},
97800b99b8Sopenharmony_ci        {"x86_64", ArchType::ARCH_X86_64},
98800b99b8Sopenharmony_ci        {"x86", ArchType::ARCH_X86},
99800b99b8Sopenharmony_ci    };
100800b99b8Sopenharmony_ci
101800b99b8Sopenharmony_ci    std::unordered_map<std::string, ArchType>::iterator it;
102800b99b8Sopenharmony_ci    for (it = machineMap.begin(); it != machineMap.end(); it++) {
103800b99b8Sopenharmony_ci        ArchType archType = GetArchFromUname(it->first);
104800b99b8Sopenharmony_ci        ASSERT_EQ(archType, it->second);
105800b99b8Sopenharmony_ci    }
106800b99b8Sopenharmony_ci    std::string machine = "test";
107800b99b8Sopenharmony_ci    ArchType archType = GetArchFromUname(machine);
108800b99b8Sopenharmony_ci    ASSERT_EQ(archType, ArchType::ARCH_UNKNOWN);
109800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "ArchUtilTest002: end.";
110800b99b8Sopenharmony_ci}
111800b99b8Sopenharmony_ci} // namespace HiviewDFX
112800b99b8Sopenharmony_ci} // namespace OHOS