blob: 92b9990745b699fab2803acd32aefcb7fe14726e [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>
Matt Spinler8a09b982025-05-09 14:09:10 -050022#include <fstream>
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080023
24#include <gtest/gtest.h>
25
26using namespace openpower::pels;
27
28TEST(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
38TEST(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");
Patrick Williams23867bc2021-04-22 09:26:47 -050044 EXPECT_THROW(getNumberString("%123", number), std::invalid_argument);
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080045}
46
47TEST(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}
Matt Spinlerb832aa52023-03-21 15:32:34 -050056
57TEST(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}