blob: 725ae4db6b5b0959eb2247a14cd12d2b48a13f56 [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
17#include <gtest/gtest.h>
18
Yabin Cui40eef9e2021-04-13 13:08:31 -070019#include <android-base/file.h>
20
Yabin Cui2a53ff32018-05-21 17:37:00 -070021#include "get_test_data.h"
Yabin Cuib4212972016-05-25 14:08:05 -070022#include "utils.h"
23
Yabin Cuifaa7b922021-01-11 17:35:57 -080024using namespace simpleperf;
25
Yabin Cuif79a0082016-11-14 11:23:14 -080026TEST(utils, ConvertBytesToValue) {
27 char buf[8];
28 for (int i = 0; i < 8; ++i) {
29 buf[i] = i;
30 }
31 ASSERT_EQ(0x1ULL, ConvertBytesToValue(buf + 1, 1));
32 ASSERT_EQ(0x201ULL, ConvertBytesToValue(buf + 1, 2));
33 ASSERT_EQ(0x05040302ULL, ConvertBytesToValue(buf + 2, 4));
34 ASSERT_EQ(0x0706050403020100ULL, ConvertBytesToValue(buf, 8));
35}
Yabin Cui2a53ff32018-05-21 17:37:00 -070036
37TEST(utils, ArchiveHelper) {
38 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(GetTestData(APK_FILE));
39 ASSERT_TRUE(ahelper);
40 bool found = false;
41 ZipEntry lib_entry;
42 ASSERT_TRUE(ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
43 if (name == NATIVELIB_IN_APK) {
44 found = true;
45 lib_entry = entry;
46 return false;
47 }
48 return true;
49 }));
50 ASSERT_TRUE(found);
51 ZipEntry entry;
52 ASSERT_TRUE(ahelper->FindEntry(NATIVELIB_IN_APK, &entry));
53 ASSERT_EQ(entry.offset, lib_entry.offset);
54 std::vector<uint8_t> data;
55 ASSERT_TRUE(ahelper->GetEntryData(entry, &data));
56
57 // Check reading wrong file formats.
58 ASSERT_FALSE(ArchiveHelper::CreateInstance(GetTestData(ELF_FILE)));
59 ASSERT_FALSE(ArchiveHelper::CreateInstance("/dev/zero"));
60}
Namhyung Kima5f1d422019-12-17 17:15:02 +090061
62TEST(utils, GetCpusFromString) {
Yabin Cuie3ca9982020-10-16 13:16:26 -070063 ASSERT_EQ(GetCpusFromString("0-2"), std::make_optional<std::set<int>>({0, 1, 2}));
64 ASSERT_EQ(GetCpusFromString("0,2-3"), std::make_optional<std::set<int>>({0, 2, 3}));
65 ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::make_optional<std::set<int>>({0, 1, 2, 3, 4}));
66 ASSERT_EQ(GetCpusFromString("0,1-3, 5, 7-8"),
67 std::make_optional<std::set<int>>({0, 1, 2, 3, 5, 7, 8}));
68 ASSERT_EQ(GetCpusFromString(""), std::nullopt);
69 ASSERT_EQ(GetCpusFromString("-3"), std::nullopt);
70 ASSERT_EQ(GetCpusFromString("3,2-1"), std::nullopt);
71}
72
73TEST(utils, GetTidsFromString) {
74 ASSERT_EQ(GetTidsFromString("0,12,9", false), std::make_optional(std::set<pid_t>({0, 9, 12})));
75 ASSERT_EQ(GetTidsFromString("-2", false), std::nullopt);
Namhyung Kima5f1d422019-12-17 17:15:02 +090076}
Yabin Cui40eef9e2021-04-13 13:08:31 -070077
78TEST(utils, LineReader) {
79 TemporaryFile tmpfile;
80 close(tmpfile.release());
81 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2", tmpfile.path));
82 LineReader reader(tmpfile.path);
83 ASSERT_TRUE(reader.Ok());
84 std::string* line = reader.ReadLine();
85 ASSERT_TRUE(line != nullptr);
86 ASSERT_EQ(*line, "line1");
87 line = reader.ReadLine();
88 ASSERT_TRUE(line != nullptr);
89 ASSERT_EQ(*line, "line2");
90 ASSERT_TRUE(reader.ReadLine() == nullptr);
91}