1 /*
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 "utils.h"
18
19 #include <gtest/gtest.h>
20
21 #include <android-base/file.h>
22
23 #include "environment.h"
24 #include "get_test_data.h"
25
26 using namespace simpleperf;
27
28 // @CddTest = 6.1/C-0-2
TEST(utils,ConvertBytesToValue)29 TEST(utils, ConvertBytesToValue) {
30 char buf[8];
31 for (int i = 0; i < 8; ++i) {
32 buf[i] = i;
33 }
34 ASSERT_EQ(0x1ULL, ConvertBytesToValue(buf + 1, 1));
35 ASSERT_EQ(0x201ULL, ConvertBytesToValue(buf + 1, 2));
36 ASSERT_EQ(0x05040302ULL, ConvertBytesToValue(buf + 2, 4));
37 ASSERT_EQ(0x0706050403020100ULL, ConvertBytesToValue(buf, 8));
38 }
39
40 // @CddTest = 6.1/C-0-2
TEST(utils,ArchiveHelper)41 TEST(utils, ArchiveHelper) {
42 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(GetTestData(APK_FILE));
43 ASSERT_TRUE(ahelper);
44 bool found = false;
45 ZipEntry lib_entry;
46 ASSERT_TRUE(ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
47 if (name == NATIVELIB_IN_APK) {
48 found = true;
49 lib_entry = entry;
50 return false;
51 }
52 return true;
53 }));
54 ASSERT_TRUE(found);
55 ZipEntry entry;
56 ASSERT_TRUE(ahelper->FindEntry(NATIVELIB_IN_APK, &entry));
57 ASSERT_EQ(entry.offset, lib_entry.offset);
58 std::vector<uint8_t> data;
59 ASSERT_TRUE(ahelper->GetEntryData(entry, &data));
60
61 // Check reading wrong file formats.
62 ASSERT_FALSE(ArchiveHelper::CreateInstance(GetTestData(ELF_FILE)));
63 ASSERT_FALSE(ArchiveHelper::CreateInstance("/dev/zero"));
64 }
65
66 // @CddTest = 6.1/C-0-2
TEST(utils,GetCpusFromString)67 TEST(utils, GetCpusFromString) {
68 ASSERT_EQ(GetCpusFromString("0-2"), std::make_optional<std::set<int>>({0, 1, 2}));
69 ASSERT_EQ(GetCpusFromString("0,2-3"), std::make_optional<std::set<int>>({0, 2, 3}));
70 ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::make_optional<std::set<int>>({0, 1, 2, 3, 4}));
71 ASSERT_EQ(GetCpusFromString("0,1-3, 5, 7-8"),
72 std::make_optional<std::set<int>>({0, 1, 2, 3, 5, 7, 8}));
73 ASSERT_EQ(GetCpusFromString(""), std::nullopt);
74 ASSERT_EQ(GetCpusFromString("-3"), std::nullopt);
75 ASSERT_EQ(GetCpusFromString("3,2-1"), std::nullopt);
76 }
77
78 // @CddTest = 6.1/C-0-2
TEST(utils,GetTidsFromString)79 TEST(utils, GetTidsFromString) {
80 ASSERT_EQ(GetTidsFromString("0,12,9", false), std::make_optional(std::set<pid_t>({0, 9, 12})));
81 ASSERT_EQ(GetTidsFromString("-2", false), std::nullopt);
82 }
83
84 // @CddTest = 6.1/C-0-2
TEST(utils,GetPidsFromStrings)85 TEST(utils, GetPidsFromStrings) {
86 ASSERT_EQ(GetPidsFromStrings({"0,12", "9"}, false, false),
87 std::make_optional(std::set<pid_t>({0, 9, 12})));
88 ASSERT_EQ(GetPidsFromStrings({"-2"}, false, false), std::nullopt);
89 #if defined(__linux__)
90 pid_t pid = getpid();
91 ASSERT_EQ(GetPidsFromStrings({std::to_string(pid)}, true, false),
92 std::make_optional(std::set<pid_t>({pid})));
93 std::string process_name = GetCompleteProcessName(pid);
94 ASSERT_EQ(GetPidsFromStrings({process_name}, true, true),
95 std::make_optional(std::set<pid_t>({pid})));
96 #endif // defined(__linux__)
97 }
98
99 // @CddTest = 6.1/C-0-2
TEST(utils,LineReader)100 TEST(utils, LineReader) {
101 TemporaryFile tmpfile;
102 close(tmpfile.release());
103 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2", tmpfile.path));
104 LineReader reader(tmpfile.path);
105 ASSERT_TRUE(reader.Ok());
106 std::string* line = reader.ReadLine();
107 ASSERT_TRUE(line != nullptr);
108 ASSERT_EQ(*line, "line1");
109 line = reader.ReadLine();
110 ASSERT_TRUE(line != nullptr);
111 ASSERT_EQ(*line, "line2");
112 ASSERT_TRUE(reader.ReadLine() == nullptr);
113 }
114
115 // @CddTest = 6.1/C-0-2
TEST(utils,ReadableCount)116 TEST(utils, ReadableCount) {
117 ASSERT_EQ(ReadableCount(0), "0");
118 ASSERT_EQ(ReadableCount(204), "204");
119 ASSERT_EQ(ReadableCount(1000), "1,000");
120 ASSERT_EQ(ReadableCount(123456789), "123,456,789");
121 }
122