1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util.hpp"
16
17 #include <filesystem>
18 #include <fstream>
19
20 #include "gtest/gtest.h"
21
TEST(IsNumericPath,invalidNumericPath)22 TEST(IsNumericPath, invalidNumericPath)
23 {
24 std::string badPath{"badNumericPath"};
25 int id = -1;
26 EXPECT_FALSE(metric_blob::isNumericPath(badPath, id));
27 EXPECT_EQ(id, -1);
28 }
29
TEST(IsNumericPath,validNumericPath)30 TEST(IsNumericPath, validNumericPath)
31 {
32 std::string goodPath{"proc/10000"};
33 int id = -1;
34 EXPECT_TRUE(metric_blob::isNumericPath(goodPath, id));
35 EXPECT_EQ(id, 10000);
36 }
37
TEST(ReadFileThenGrepIntoString,goodFile)38 TEST(ReadFileThenGrepIntoString, goodFile)
39 {
40 const std::string& fileName = "./test_file";
41 std::ofstream ofs(fileName, std::ios::trunc);
42 std::string_view content = "This is\ntest\tcontentt\n\n\n\n.\n\n##$#$";
43 ofs << content;
44 ofs.close();
45 std::string readContent = metric_blob::readFileThenGrepIntoString(fileName);
46 std::filesystem::remove(fileName);
47 EXPECT_EQ(readContent, content);
48 }
49
TEST(ReadFileThenGrepIntoString,inexistentFile)50 TEST(ReadFileThenGrepIntoString, inexistentFile)
51 {
52 const std::string& fileName = "./inexistent_file";
53 std::string readContent = metric_blob::readFileThenGrepIntoString(fileName);
54 EXPECT_EQ(readContent, "");
55 }
56
TEST(GetTcommUtimeStime,validInput)57 TEST(GetTcommUtimeStime, validInput)
58 {
59 // ticks_per_sec is usually 100 on the BMC
60 const long ticksPerSec = 100;
61
62 const std::string_view content =
63 "2596 (dbus-broker) R 2577 2577 2577 0 -1 "
64 "4194560 299 0 1 0 333037 246110 0 0 20 0 "
65 "1 0 1545 3411968 530 4294967295 65536 "
66 "246512 2930531712 0 0 0 81923 4";
67
68 metric_blob::TcommUtimeStime t =
69 metric_blob::parseTcommUtimeStimeString(content, ticksPerSec);
70 const float EPS = 0.01; // The difference was 0.000117188
71 EXPECT_LT(std::abs(t.utime - 3330.37), EPS);
72 EXPECT_LT(std::abs(t.stime - 2461.10), EPS);
73 EXPECT_EQ(t.tcomm, "(dbus-broker)");
74 }
75
TEST(GetTcommUtimeStime,invalidInput)76 TEST(GetTcommUtimeStime, invalidInput)
77 {
78 // ticks_per_sec is usually 100 on the BMC
79 const long ticksPerSec = 100;
80
81 const std::string_view content =
82 "x invalid x x x x x x x x x x x x x x x x x x x x x x x x x x x";
83
84 metric_blob::TcommUtimeStime t =
85 metric_blob::parseTcommUtimeStimeString(content, ticksPerSec);
86
87 EXPECT_EQ(t.utime, 0);
88 EXPECT_EQ(t.stime, 0);
89 EXPECT_EQ(t.tcomm, "invalid");
90 }
91
TEST(ParseMeminfoValue,validInput)92 TEST(ParseMeminfoValue, validInput)
93 {
94 const std::string_view content =
95 "MemTotal: 1027040 kB\n"
96 "MemFree: 868144 kB\n"
97 "MemAvailable: 919308 kB\n"
98 "Buffers: 13008 kB\n"
99 "Cached: 82840 kB\n"
100 "SwapCached: 0 kB\n"
101 "Active: 62076 kB\n";
102 int value;
103 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "MemTotal:", value));
104 EXPECT_EQ(value, 1027040);
105 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "MemFree:", value));
106 EXPECT_EQ(value, 868144);
107 EXPECT_TRUE(
108 metric_blob::parseMeminfoValue(content, "MemAvailable:", value));
109 EXPECT_EQ(value, 919308);
110 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Buffers:", value));
111 EXPECT_EQ(value, 13008);
112 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Cached:", value));
113 EXPECT_EQ(value, 82840);
114 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "SwapCached:", value));
115 EXPECT_EQ(value, 0);
116 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Active:", value));
117 EXPECT_EQ(value, 62076);
118 }
119
TEST(ParseMeminfoValue,invalidInput)120 TEST(ParseMeminfoValue, invalidInput)
121 {
122 const std::string_view invalid = "MemTotal: 1";
123 int value = -999;
124 EXPECT_FALSE(metric_blob::parseMeminfoValue(invalid, "MemTotal:", value));
125 EXPECT_EQ(value, -999);
126 EXPECT_FALSE(metric_blob::parseMeminfoValue(invalid, "x", value));
127 EXPECT_EQ(value, -999);
128 }
129
TEST(ParseProcUptime,validInput)130 TEST(ParseProcUptime, validInput)
131 {
132 const std::string_view content = "266923.67 512184.95";
133 const double eps =
134 1e-4; // Empirical threshold for floating point number compare
135 double uptime, idleProcessTime;
136 EXPECT_EQ(metric_blob::parseProcUptime(content, uptime, idleProcessTime),
137 true);
138 EXPECT_LT(abs(uptime - 266923.67), eps);
139 EXPECT_LT(abs(idleProcessTime - 512184.95), eps);
140 }
141
TEST(TrimStringRight,nonEmptyResult)142 TEST(TrimStringRight, nonEmptyResult)
143 {
144 EXPECT_EQ(
145 metric_blob::trimStringRight("\n\nabc\n\t\r\x00\x01\x02\x03").size(),
146 5); // "\n\nabc" is left
147 }
148
TEST(TrimStringRight,trimToEmpty)149 TEST(TrimStringRight, trimToEmpty)
150 {
151 EXPECT_TRUE(metric_blob::trimStringRight(" ").empty());
152 EXPECT_TRUE(metric_blob::trimStringRight("").empty());
153 }
154
main(int argc,char ** argv)155 int main(int argc, char** argv)
156 {
157 ::testing::InitGoogleTest(&argc, argv);
158 return RUN_ALL_TESTS();
159 }
160