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 <gtest/gtest.h>
18 
19 #include <android-base/file.h>
20 
21 #include "get_test_data.h"
22 #include "utils.h"
23 
24 using namespace simpleperf;
25 
TEST(utils,ConvertBytesToValue)26 TEST(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 }
36 
TEST(utils,ArchiveHelper)37 TEST(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 }
61 
TEST(utils,GetCpusFromString)62 TEST(utils, GetCpusFromString) {
63   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 
TEST(utils,GetTidsFromString)73 TEST(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);
76 }
77 
TEST(utils,LineReader)78 TEST(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 }
92