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 23 #include <gtest/gtest.h> 24 25 using namespace openpower::pels; 26 27 TEST(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 37 TEST(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"); 43 EXPECT_THROW(getNumberString("%123", number), std::invalid_argument); 44 } 45 46 TEST(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 } 55 56 TEST(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 } 85