1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include "utils.h"
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker #include "environment.h"
24*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
27*288bf522SAndroid Build Coastguard Worker
28*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,ConvertBytesToValue)29*288bf522SAndroid Build Coastguard Worker TEST(utils, ConvertBytesToValue) {
30*288bf522SAndroid Build Coastguard Worker char buf[8];
31*288bf522SAndroid Build Coastguard Worker for (int i = 0; i < 8; ++i) {
32*288bf522SAndroid Build Coastguard Worker buf[i] = i;
33*288bf522SAndroid Build Coastguard Worker }
34*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0x1ULL, ConvertBytesToValue(buf + 1, 1));
35*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0x201ULL, ConvertBytesToValue(buf + 1, 2));
36*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0x05040302ULL, ConvertBytesToValue(buf + 2, 4));
37*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0x0706050403020100ULL, ConvertBytesToValue(buf, 8));
38*288bf522SAndroid Build Coastguard Worker }
39*288bf522SAndroid Build Coastguard Worker
40*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,ArchiveHelper)41*288bf522SAndroid Build Coastguard Worker TEST(utils, ArchiveHelper) {
42*288bf522SAndroid Build Coastguard Worker std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(GetTestData(APK_FILE));
43*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(ahelper);
44*288bf522SAndroid Build Coastguard Worker bool found = false;
45*288bf522SAndroid Build Coastguard Worker ZipEntry lib_entry;
46*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
47*288bf522SAndroid Build Coastguard Worker if (name == NATIVELIB_IN_APK) {
48*288bf522SAndroid Build Coastguard Worker found = true;
49*288bf522SAndroid Build Coastguard Worker lib_entry = entry;
50*288bf522SAndroid Build Coastguard Worker return false;
51*288bf522SAndroid Build Coastguard Worker }
52*288bf522SAndroid Build Coastguard Worker return true;
53*288bf522SAndroid Build Coastguard Worker }));
54*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(found);
55*288bf522SAndroid Build Coastguard Worker ZipEntry entry;
56*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(ahelper->FindEntry(NATIVELIB_IN_APK, &entry));
57*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(entry.offset, lib_entry.offset);
58*288bf522SAndroid Build Coastguard Worker std::vector<uint8_t> data;
59*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(ahelper->GetEntryData(entry, &data));
60*288bf522SAndroid Build Coastguard Worker
61*288bf522SAndroid Build Coastguard Worker // Check reading wrong file formats.
62*288bf522SAndroid Build Coastguard Worker ASSERT_FALSE(ArchiveHelper::CreateInstance(GetTestData(ELF_FILE)));
63*288bf522SAndroid Build Coastguard Worker ASSERT_FALSE(ArchiveHelper::CreateInstance("/dev/zero"));
64*288bf522SAndroid Build Coastguard Worker }
65*288bf522SAndroid Build Coastguard Worker
66*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,GetCpusFromString)67*288bf522SAndroid Build Coastguard Worker TEST(utils, GetCpusFromString) {
68*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString("0-2"), std::make_optional<std::set<int>>({0, 1, 2}));
69*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString("0,2-3"), std::make_optional<std::set<int>>({0, 2, 3}));
70*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::make_optional<std::set<int>>({0, 1, 2, 3, 4}));
71*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString("0,1-3, 5, 7-8"),
72*288bf522SAndroid Build Coastguard Worker std::make_optional<std::set<int>>({0, 1, 2, 3, 5, 7, 8}));
73*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString(""), std::nullopt);
74*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString("-3"), std::nullopt);
75*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetCpusFromString("3,2-1"), std::nullopt);
76*288bf522SAndroid Build Coastguard Worker }
77*288bf522SAndroid Build Coastguard Worker
78*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,GetTidsFromString)79*288bf522SAndroid Build Coastguard Worker TEST(utils, GetTidsFromString) {
80*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetTidsFromString("0,12,9", false), std::make_optional(std::set<pid_t>({0, 9, 12})));
81*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetTidsFromString("-2", false), std::nullopt);
82*288bf522SAndroid Build Coastguard Worker }
83*288bf522SAndroid Build Coastguard Worker
84*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,GetPidsFromStrings)85*288bf522SAndroid Build Coastguard Worker TEST(utils, GetPidsFromStrings) {
86*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetPidsFromStrings({"0,12", "9"}, false, false),
87*288bf522SAndroid Build Coastguard Worker std::make_optional(std::set<pid_t>({0, 9, 12})));
88*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetPidsFromStrings({"-2"}, false, false), std::nullopt);
89*288bf522SAndroid Build Coastguard Worker #if defined(__linux__)
90*288bf522SAndroid Build Coastguard Worker pid_t pid = getpid();
91*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetPidsFromStrings({std::to_string(pid)}, true, false),
92*288bf522SAndroid Build Coastguard Worker std::make_optional(std::set<pid_t>({pid})));
93*288bf522SAndroid Build Coastguard Worker std::string process_name = GetCompleteProcessName(pid);
94*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetPidsFromStrings({process_name}, true, true),
95*288bf522SAndroid Build Coastguard Worker std::make_optional(std::set<pid_t>({pid})));
96*288bf522SAndroid Build Coastguard Worker #endif // defined(__linux__)
97*288bf522SAndroid Build Coastguard Worker }
98*288bf522SAndroid Build Coastguard Worker
99*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,LineReader)100*288bf522SAndroid Build Coastguard Worker TEST(utils, LineReader) {
101*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
102*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
103*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2", tmpfile.path));
104*288bf522SAndroid Build Coastguard Worker LineReader reader(tmpfile.path);
105*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(reader.Ok());
106*288bf522SAndroid Build Coastguard Worker std::string* line = reader.ReadLine();
107*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(line != nullptr);
108*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(*line, "line1");
109*288bf522SAndroid Build Coastguard Worker line = reader.ReadLine();
110*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(line != nullptr);
111*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(*line, "line2");
112*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(reader.ReadLine() == nullptr);
113*288bf522SAndroid Build Coastguard Worker }
114*288bf522SAndroid Build Coastguard Worker
115*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(utils,ReadableCount)116*288bf522SAndroid Build Coastguard Worker TEST(utils, ReadableCount) {
117*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(ReadableCount(0), "0");
118*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(ReadableCount(204), "204");
119*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(ReadableCount(1000), "1,000");
120*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(ReadableCount(123456789), "123,456,789");
121*288bf522SAndroid Build Coastguard Worker }
122