blob: 2dfbd90162b6407e4644668c47cfc55f21656945 [file] [log] [blame]
Yabin Cuib4212972016-05-25 14:08:05 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cui1c6be752023-02-28 11:46:37 -080017#include "utils.h"
18
Yabin Cuib4212972016-05-25 14:08:05 -070019#include <gtest/gtest.h>
20
Yabin Cui40eef9e2021-04-13 13:08:31 -070021#include <android-base/file.h>
22
Yabin Cui1c6be752023-02-28 11:46:37 -080023#include "environment.h"
Yabin Cui2a53ff32018-05-21 17:37:00 -070024#include "get_test_data.h"
Yabin Cuib4212972016-05-25 14:08:05 -070025
Yabin Cuifaa7b922021-01-11 17:35:57 -080026using namespace simpleperf;
27
Yabin Cuif79a0082016-11-14 11:23:14 -080028TEST(utils, ConvertBytesToValue) {
29 char buf[8];
30 for (int i = 0; i < 8; ++i) {
31 buf[i] = i;
32 }
33 ASSERT_EQ(0x1ULL, ConvertBytesToValue(buf + 1, 1));
34 ASSERT_EQ(0x201ULL, ConvertBytesToValue(buf + 1, 2));
35 ASSERT_EQ(0x05040302ULL, ConvertBytesToValue(buf + 2, 4));
36 ASSERT_EQ(0x0706050403020100ULL, ConvertBytesToValue(buf, 8));
37}
Yabin Cui2a53ff32018-05-21 17:37:00 -070038
39TEST(utils, ArchiveHelper) {
40 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(GetTestData(APK_FILE));
41 ASSERT_TRUE(ahelper);
42 bool found = false;
43 ZipEntry lib_entry;
44 ASSERT_TRUE(ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
45 if (name == NATIVELIB_IN_APK) {
46 found = true;
47 lib_entry = entry;
48 return false;
49 }
50 return true;
51 }));
52 ASSERT_TRUE(found);
53 ZipEntry entry;
54 ASSERT_TRUE(ahelper->FindEntry(NATIVELIB_IN_APK, &entry));
55 ASSERT_EQ(entry.offset, lib_entry.offset);
56 std::vector<uint8_t> data;
57 ASSERT_TRUE(ahelper->GetEntryData(entry, &data));
58
59 // Check reading wrong file formats.
60 ASSERT_FALSE(ArchiveHelper::CreateInstance(GetTestData(ELF_FILE)));
61 ASSERT_FALSE(ArchiveHelper::CreateInstance("/dev/zero"));
62}
Namhyung Kima5f1d422019-12-17 17:15:02 +090063
64TEST(utils, GetCpusFromString) {
Yabin Cuie3ca9982020-10-16 13:16:26 -070065 ASSERT_EQ(GetCpusFromString("0-2"), std::make_optional<std::set<int>>({0, 1, 2}));
66 ASSERT_EQ(GetCpusFromString("0,2-3"), std::make_optional<std::set<int>>({0, 2, 3}));
67 ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::make_optional<std::set<int>>({0, 1, 2, 3, 4}));
68 ASSERT_EQ(GetCpusFromString("0,1-3, 5, 7-8"),
69 std::make_optional<std::set<int>>({0, 1, 2, 3, 5, 7, 8}));
70 ASSERT_EQ(GetCpusFromString(""), std::nullopt);
71 ASSERT_EQ(GetCpusFromString("-3"), std::nullopt);
72 ASSERT_EQ(GetCpusFromString("3,2-1"), std::nullopt);
73}
74
75TEST(utils, GetTidsFromString) {
76 ASSERT_EQ(GetTidsFromString("0,12,9", false), std::make_optional(std::set<pid_t>({0, 9, 12})));
77 ASSERT_EQ(GetTidsFromString("-2", false), std::nullopt);
Namhyung Kima5f1d422019-12-17 17:15:02 +090078}
Yabin Cui40eef9e2021-04-13 13:08:31 -070079
Yabin Cui1c6be752023-02-28 11:46:37 -080080TEST(utils, GetPidsFromStrings) {
81 ASSERT_EQ(GetPidsFromStrings({"0,12", "9"}, false, false),
82 std::make_optional(std::set<pid_t>({0, 9, 12})));
83 ASSERT_EQ(GetPidsFromStrings({"-2"}, false, false), std::nullopt);
84#if defined(__linux__)
85 pid_t pid = getpid();
86 ASSERT_EQ(GetPidsFromStrings({std::to_string(pid)}, true, false),
87 std::make_optional(std::set<pid_t>({pid})));
88 std::string process_name = GetCompleteProcessName(pid);
89 ASSERT_EQ(GetPidsFromStrings({process_name}, true, true),
90 std::make_optional(std::set<pid_t>({pid})));
91#endif // defined(__linux__)
92}
93
Yabin Cui40eef9e2021-04-13 13:08:31 -070094TEST(utils, LineReader) {
95 TemporaryFile tmpfile;
96 close(tmpfile.release());
97 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2", tmpfile.path));
98 LineReader reader(tmpfile.path);
99 ASSERT_TRUE(reader.Ok());
100 std::string* line = reader.ReadLine();
101 ASSERT_TRUE(line != nullptr);
102 ASSERT_EQ(*line, "line1");
103 line = reader.ReadLine();
104 ASSERT_TRUE(line != nullptr);
105 ASSERT_EQ(*line, "line2");
106 ASSERT_TRUE(reader.ReadLine() == nullptr);
107}