blob: 5a3d1a1468b403def44776ffdc72229ab628bd23 [file] [log] [blame]
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +08001/**
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"
Matt Spinlerb832aa52023-03-21 15:32:34 -050017#include "extensions/openpower-pels/paths.hpp"
18
19#include <nlohmann/json.hpp>
20
21#include <filesystem>
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080022
23#include <gtest/gtest.h>
24
25using namespace openpower::pels;
26
27TEST(JsonUtilsTest, TrimEndTest)
28{
29 std::string testStr("Test string 1");
30 EXPECT_EQ(trimEnd(testStr), "Test string 1");
31 testStr = "Test string 2 ";
32 EXPECT_EQ(trimEnd(testStr), "Test string 2");
33 testStr = " Test string 3 ";
34 EXPECT_EQ(trimEnd(testStr), " Test string 3");
35}
36
37TEST(JsonUtilsTest, NumberToStringTest)
38{
39 size_t number = 123;
40 EXPECT_EQ(getNumberString("%d", number), "123");
41 EXPECT_EQ(getNumberString("%03X", number), "07B");
42 EXPECT_EQ(getNumberString("0x%X", number), "0x7B");
Patrick Williams23867bc2021-04-22 09:26:47 -050043 EXPECT_THROW(getNumberString("%123", number), std::invalid_argument);
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080044}
45
46TEST(JsonUtilsTest, JsonInsertTest)
47{
48 std::string json;
49 jsonInsert(json, "Key", "Value1", 1);
50 EXPECT_EQ(json, " \"Key\": \"Value1\",\n");
51 jsonInsert(json, "Keyxxxxxxxxxxxxxxxxxxxxxxxxxx", "Value2", 2);
52 EXPECT_EQ(json, " \"Key\": \"Value1\",\n"
53 " \"Keyxxxxxxxxxxxxxxxxxxxxxxxxxx\": \"Value2\",\n");
54}
Matt Spinlerb832aa52023-03-21 15:32:34 -050055
56TEST(JsonUtilsTest, GetComponentNameTest)
57{
58 const auto compIDs = R"(
59 {
60 "1000": "some comp",
61 "2222": "another comp"
62 })"_json;
63
64 auto dataPath = getPELReadOnlyDataPath();
65 std::ofstream file{dataPath / "O_component_ids.json"};
66 file << compIDs;
67 file.close();
68
69 // The component ID file exists
70 EXPECT_EQ(getComponentName(0x1000, 'O'), "some comp");
71 EXPECT_EQ(getComponentName(0x2222, 'O'), "another comp");
72 EXPECT_EQ(getComponentName(0x0001, 'O'), "0x0001");
73
74 // No component ID file
75 EXPECT_EQ(getComponentName(0x3456, 'B'), "0x3456");
76
77 // PHYP, uses characters if both bytes nonzero
78 EXPECT_EQ(getComponentName(0x4552, 'H'), "ER");
79 EXPECT_EQ(getComponentName(0x584D, 'H'), "XM");
80 EXPECT_EQ(getComponentName(0x5800, 'H'), "0x5800");
81 EXPECT_EQ(getComponentName(0x0058, 'H'), "0x0058");
82
83 std::filesystem::remove_all(dataPath);
84}