Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 23 | #include "environment.h" |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 24 | #include "get_test_data.h" |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 25 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 26 | using namespace simpleperf; |
| 27 | |
Yabin Cui | f79a008 | 2016-11-14 11:23:14 -0800 | [diff] [blame] | 28 | TEST(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 Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 38 | |
| 39 | TEST(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 Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 63 | |
| 64 | TEST(utils, GetCpusFromString) { |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 65 | 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 | |
| 75 | TEST(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 Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 78 | } |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 79 | |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 80 | TEST(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 Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 94 | TEST(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 | } |