blob: 6e5a26c1e223d79e2702b0fbb158e5d589d165cd [file] [log] [blame]
Shuhei Takahashib9e86a92020-03-23 02:09:151// 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 Kastingb53b81912021-04-28 19:23:3010#include "base/strings/string_piece.h"
Shuhei Takahashib9e86a92020-03-23 02:09:1511#include "base/strings/string_util.h"
Ken Rockot30f6a4a2020-09-17 08:34:0112#include "content/common/set_process_title_linux.h"
Shuhei Takahashib9e86a92020-03-23 02:09:1513#include "testing/gtest/include/gtest/gtest.h"
14
15namespace {
16
17const std::string kNullChr(1, '\0');
18
19std::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 Kastingb53b81912021-04-28 19:23:3029 return std::string(base::TrimString(cmdline, kNullChr, base::TRIM_TRAILING));
Shuhei Takahashib9e86a92020-03-23 02:09:1530}
31
32TEST(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
42TEST(SetProcTitleLinuxTest, Empty) {
43 setproctitle("-");
44 EXPECT_EQ(ReadCmdline(), "");
45}
46
47TEST(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