1 /*
2 * Copyright (C) 2024 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 <processgroup/util.h>
18
19 #include "gtest/gtest.h"
20
TEST(EmptyInputs,bothEmpty)21 TEST(EmptyInputs, bothEmpty) {
22 EXPECT_EQ(GetCgroupDepth({}, {}), 0);
23 }
24
TEST(EmptyInputs,rootEmpty)25 TEST(EmptyInputs, rootEmpty) {
26 EXPECT_EQ(GetCgroupDepth({}, "foo"), 0);
27 }
28
TEST(EmptyInputs,pathEmpty)29 TEST(EmptyInputs, pathEmpty) {
30 EXPECT_EQ(GetCgroupDepth("foo", {}), 0);
31 }
32
TEST(InvalidInputs,pathNotInRoot)33 TEST(InvalidInputs, pathNotInRoot) {
34 EXPECT_EQ(GetCgroupDepth("foo", "bar"), 0);
35 }
36
TEST(InvalidInputs,rootLargerThanPath)37 TEST(InvalidInputs, rootLargerThanPath) {
38 EXPECT_EQ(GetCgroupDepth("/a/long/path", "/short"), 0);
39 }
40
TEST(InvalidInputs,pathLargerThanRoot)41 TEST(InvalidInputs, pathLargerThanRoot) {
42 EXPECT_EQ(GetCgroupDepth("/short", "/a/long/path"), 0);
43 }
44
TEST(InvalidInputs,missingSeparator)45 TEST(InvalidInputs, missingSeparator) {
46 EXPECT_EQ(GetCgroupDepth("/controller/root", "/controller/rootcgroup"), 0);
47 }
48
TEST(ExtraSeparators,root)49 TEST(ExtraSeparators, root) {
50 EXPECT_EQ(GetCgroupDepth("///sys/fs/cgroup", "/sys/fs/cgroup/a/b/c"), 3);
51 EXPECT_EQ(GetCgroupDepth("/sys///fs/cgroup", "/sys/fs/cgroup/a/b/c"), 3);
52 EXPECT_EQ(GetCgroupDepth("/sys/fs///cgroup", "/sys/fs/cgroup/a/b/c"), 3);
53
54 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "///sys/fs/cgroup/a/b/c"), 3);
55 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys///fs/cgroup/a/b/c"), 3);
56 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs///cgroup/a/b/c"), 3);
57 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup///a/b/c"), 3);
58 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/a///b/c"), 3);
59 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/a/b///c"), 3);
60 }
61
TEST(SeparatorEndings,rootEndsInSeparator)62 TEST(SeparatorEndings, rootEndsInSeparator) {
63 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup/", "/sys/fs/cgroup/a/b"), 2);
64 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup///", "/sys/fs/cgroup/a/b"), 2);
65 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup/", "/sys/fs/cgroup/a/b/"), 2);
66 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup///", "/sys/fs/cgroup/a/b/"), 2);
67 }
68
TEST(SeparatorEndings,pathEndsInSeparator)69 TEST(SeparatorEndings, pathEndsInSeparator) {
70 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/a/b/"), 2);
71 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/a/b///"), 2);
72 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup/", "/sys/fs/cgroup/a/b/"), 2);
73 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup/", "/sys/fs/cgroup/a/b///"), 2);
74 }
75
TEST(ValidInputs,rootHasZeroDepth)76 TEST(ValidInputs, rootHasZeroDepth) {
77 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup"), 0);
78 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup/", "/sys/fs/cgroup"), 0);
79 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/"), 0);
80 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup/", "/sys/fs/cgroup/"), 0);
81 }
82
TEST(ValidInputs,atLeastDepth10)83 TEST(ValidInputs, atLeastDepth10) {
84 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/a/b/c/d/e/f/g/h/i/j"), 10);
85 }
86
TEST(ValidInputs,androidCgroupNames)87 TEST(ValidInputs, androidCgroupNames) {
88 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/system/uid_0/pid_1000"), 3);
89 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/uid_0/pid_1000"), 2);
90
91 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/apps/uid_100000/pid_1000"), 3);
92 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/uid_100000/pid_1000"), 2);
93
94 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/apps"), 1);
95 EXPECT_EQ(GetCgroupDepth("/sys/fs/cgroup", "/sys/fs/cgroup/system"), 1);
96 }
97
TEST(ValidInputs,androidCgroupNames_nonDefaultRoot)98 TEST(ValidInputs, androidCgroupNames_nonDefaultRoot) {
99 EXPECT_EQ(GetCgroupDepth("/custom/root", "/custom/root/system/uid_0/pid_1000"), 3);
100 EXPECT_EQ(GetCgroupDepth("/custom/root", "/custom/root/uid_0/pid_1000"), 2);
101
102 EXPECT_EQ(GetCgroupDepth("/custom/root", "/custom/root/apps/uid_100000/pid_1000"), 3);
103 EXPECT_EQ(GetCgroupDepth("/custom/root", "/custom/root/uid_100000/pid_1000"), 2);
104
105 EXPECT_EQ(GetCgroupDepth("/custom/root", "/custom/root/apps"), 1);
106 EXPECT_EQ(GetCgroupDepth("/custom/root", "/custom/root/system"), 1);
107 }
108