Shuhei Takahashi | b9e86a9 | 2020-03-23 02:09:15 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | #include "base/files/file_path.h" |
| 9 | #include "base/files/file_util.h" |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 10 | #include "base/strings/string_piece.h" |
Shuhei Takahashi | b9e86a9 | 2020-03-23 02:09:15 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
Ken Rockot | 30f6a4a | 2020-09-17 08:34:01 | [diff] [blame] | 12 | #include "content/common/set_process_title_linux.h" |
Shuhei Takahashi | b9e86a9 | 2020-03-23 02:09:15 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | const std::string kNullChr(1, '\0'); |
| 18 | |
| 19 | std::string ReadCmdline() { |
| 20 | std::string cmdline; |
| 21 | CHECK(base::ReadFileToString(base::FilePath("/proc/self/cmdline"), &cmdline)); |
| 22 | // The process title appears in different formats depending on Linux kernel |
| 23 | // version: |
| 24 | // "title" (on Linux --4.17) |
| 25 | // "title\0\0\0...\0" (on Linux 4.18--5.2) |
| 26 | // "title\0" (on Linux 5.3--) |
| 27 | // |
| 28 | // For unit tests, just trim trailing null characters to support all cases. |
Peter Kasting | b53b8191 | 2021-04-28 19:23:30 | [diff] [blame] | 29 | return std::string(base::TrimString(cmdline, kNullChr, base::TRIM_TRAILING)); |
Shuhei Takahashi | b9e86a9 | 2020-03-23 02:09:15 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | TEST(SetProcTitleLinuxTest, Simple) { |
| 33 | setproctitle("a %s cat", "cute"); |
| 34 | EXPECT_TRUE(base::EndsWith(ReadCmdline(), " a cute cat", |
| 35 | base::CompareCase::SENSITIVE)) |
| 36 | << ReadCmdline(); |
| 37 | |
| 38 | setproctitle("-a %s cat", "cute"); |
| 39 | EXPECT_EQ(ReadCmdline(), "a cute cat"); |
| 40 | } |
| 41 | |
| 42 | TEST(SetProcTitleLinuxTest, Empty) { |
| 43 | setproctitle("-"); |
| 44 | EXPECT_EQ(ReadCmdline(), ""); |
| 45 | } |
| 46 | |
| 47 | TEST(SetProcTitleLinuxTest, Long) { |
| 48 | setproctitle("-long cat is l%0100000dng", 0); |
| 49 | EXPECT_TRUE(base::StartsWith(ReadCmdline(), "long cat is l00000000", |
| 50 | base::CompareCase::SENSITIVE)) |
| 51 | << ReadCmdline(); |
| 52 | } |
| 53 | |
| 54 | } // namespace |