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 <gtest/gtest.h>
17 #include <ctime>
18 #include <securec.h>
19 #include <string>
20 #include <sys/utsname.h>
21 #include <vector>
22 
23 #include "arch_util.h"
24 #include "unwind_define.h"
25 
26 using namespace OHOS::HiviewDFX;
27 using namespace testing::ext;
28 using namespace std;
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 class ArchUtilTest : public testing::Test {
33 public:
SetUpTestCase(void)34     static void SetUpTestCase(void) {}
TearDownTestCase(void)35     static void TearDownTestCase(void) {}
SetUp()36     void SetUp() {}
TearDown()37     void TearDown() {}
38 };
39 
40 /**
41  * @tc.name: ArchUtilTest001
42  * @tc.desc: test ArchUtil functions
43  * @tc.type: FUNC
44  */
45 HWTEST_F(ArchUtilTest, ArchUtilTest001, TestSize.Level2)
46 {
47     GTEST_LOG_(INFO) << "ArchUtilTest001: start.";
48     utsname systemName;
49 #if defined(__arm__)
50     if ((uname(&systemName)) != 0) {
51         ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_ARM);
52     }
53     ASSERT_EQ(GetCurrentArch(), ArchType::ARCH_ARM);
54 #elif defined(__aarch64__)
55     if ((uname(&systemName)) != 0) {
56         ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_ARM64);
57     }
58     ASSERT_EQ(GetCurrentArch(), ArchType::ARCH_ARM64);
59 #elif defined(__i386__)
60     if ((uname(&systemName)) != 0) {
61         ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_X86);
62     }
63     ASSERT_EQ(GetCurrentArch(), ArchType::ARCH_X86);
64 #elif defined(__x86_64__)
65     if ((uname(&systemName)) != 0) {
66         ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_X86_64);
67     }
68     ASSER_EQ(GetCurrentArch(), ArchType::ARCH_X86_64);
69 #else
70     if ((uname(&systemName)) != 0) {
71         ASSERT_EQ(GetArchFromUname(systemName.machine), ArchType::ARCH_UNKNOWN);
72     }
73     ASSER_EQ(GetCurrentArch(), ArchType::ARCH_UNKNOWN);
74 #endif
75     ASSERT_EQ(GetArchName(ArchType::ARCH_X86), "X86_32");
76     ASSERT_EQ(GetArchName(ArchType::ARCH_X86_64), "X86_64");
77     ASSERT_EQ(GetArchName(ArchType::ARCH_ARM), "ARM");
78     ASSERT_EQ(GetArchName(ArchType::ARCH_ARM64), "ARM64");
79     ASSERT_EQ(GetArchName(ArchType::ARCH_RISCV64), "RISCV64");
80     ASSERT_EQ(GetArchName(ArchType::ARCH_UNKNOWN), "Unsupport");
81     GTEST_LOG_(INFO) << "ArchUtilTest001: end.";
82 }
83 
84 /**
85  * @tc.name: ArchUtilTest002
86  * @tc.desc: test ArchUtil GetArchFromUname
87  * @tc.type: FUNC
88  */
89 HWTEST_F(ArchUtilTest, ArchUtilTest002, TestSize.Level2)
90 {
91     GTEST_LOG_(INFO) << "ArchUtilTest002: start.";
92     std::unordered_map<std::string, ArchType> machineMap = {
93         {"arm", ArchType::ARCH_ARM},
94         {"armv8l", ArchType::ARCH_ARM64},
95         {"aarch64", ArchType::ARCH_ARM64},
96         {"riscv64", ArchType::ARCH_RISCV64},
97         {"x86_64", ArchType::ARCH_X86_64},
98         {"x86", ArchType::ARCH_X86},
99     };
100 
101     std::unordered_map<std::string, ArchType>::iterator it;
102     for (it = machineMap.begin(); it != machineMap.end(); it++) {
103         ArchType archType = GetArchFromUname(it->first);
104         ASSERT_EQ(archType, it->second);
105     }
106     std::string machine = "test";
107     ArchType archType = GetArchFromUname(machine);
108     ASSERT_EQ(archType, ArchType::ARCH_UNKNOWN);
109     GTEST_LOG_(INFO) << "ArchUtilTest002: end.";
110 }
111 } // namespace HiviewDFX
112 } // namespace OHOS