xref: /openbmc/phosphor-logging/test/openpower-pels/json_utils_test.cpp (revision 8a09b982ddeb0c1e13190d9cd196e06a778d6140)
1 /**
2  * Copyright © 2019 IBM Corporation
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 #include "extensions/openpower-pels/json_utils.hpp"
17 #include "extensions/openpower-pels/paths.hpp"
18 
19 #include <nlohmann/json.hpp>
20 
21 #include <filesystem>
22 #include <fstream>
23 
24 #include <gtest/gtest.h>
25 
26 using namespace openpower::pels;
27 
TEST(JsonUtilsTest,TrimEndTest)28 TEST(JsonUtilsTest, TrimEndTest)
29 {
30     std::string testStr("Test string 1");
31     EXPECT_EQ(trimEnd(testStr), "Test string 1");
32     testStr = "Test string 2 ";
33     EXPECT_EQ(trimEnd(testStr), "Test string 2");
34     testStr = " Test string 3  ";
35     EXPECT_EQ(trimEnd(testStr), " Test string 3");
36 }
37 
TEST(JsonUtilsTest,NumberToStringTest)38 TEST(JsonUtilsTest, NumberToStringTest)
39 {
40     size_t number = 123;
41     EXPECT_EQ(getNumberString("%d", number), "123");
42     EXPECT_EQ(getNumberString("%03X", number), "07B");
43     EXPECT_EQ(getNumberString("0x%X", number), "0x7B");
44     EXPECT_THROW(getNumberString("%123", number), std::invalid_argument);
45 }
46 
TEST(JsonUtilsTest,JsonInsertTest)47 TEST(JsonUtilsTest, JsonInsertTest)
48 {
49     std::string json;
50     jsonInsert(json, "Key", "Value1", 1);
51     EXPECT_EQ(json, "    \"Key\":                      \"Value1\",\n");
52     jsonInsert(json, "Keyxxxxxxxxxxxxxxxxxxxxxxxxxx", "Value2", 2);
53     EXPECT_EQ(json, "    \"Key\":                      \"Value1\",\n"
54                     "        \"Keyxxxxxxxxxxxxxxxxxxxxxxxxxx\": \"Value2\",\n");
55 }
56 
TEST(JsonUtilsTest,GetComponentNameTest)57 TEST(JsonUtilsTest, GetComponentNameTest)
58 {
59     const auto compIDs = R"(
60     {
61         "1000": "some comp",
62         "2222": "another comp"
63     })"_json;
64 
65     auto dataPath = getPELReadOnlyDataPath();
66     std::ofstream file{dataPath / "O_component_ids.json"};
67     file << compIDs;
68     file.close();
69 
70     // The component ID file exists
71     EXPECT_EQ(getComponentName(0x1000, 'O'), "some comp");
72     EXPECT_EQ(getComponentName(0x2222, 'O'), "another comp");
73     EXPECT_EQ(getComponentName(0x0001, 'O'), "0x0001");
74 
75     // No component ID file
76     EXPECT_EQ(getComponentName(0x3456, 'B'), "0x3456");
77 
78     // PHYP, uses characters if both bytes nonzero
79     EXPECT_EQ(getComponentName(0x4552, 'H'), "ER");
80     EXPECT_EQ(getComponentName(0x584D, 'H'), "XM");
81     EXPECT_EQ(getComponentName(0x5800, 'H'), "0x5800");
82     EXPECT_EQ(getComponentName(0x0058, 'H'), "0x0058");
83 
84     std::filesystem::remove_all(dataPath);
85 }
86