blob: 9588b078bdd0f53d52912952d05359995f827982 [file] [log] [blame]
Matt Spinler97f7abc2019-11-06 09:40:23 -06001/**
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 */
Matt Spinlerdf13bdb2019-07-10 16:54:13 -050016#include "extensions/openpower-pels/log_id.hpp"
17#include "extensions/openpower-pels/paths.hpp"
18
19#include <arpa/inet.h>
20
Andrew Geissler620ef382020-06-16 14:52:14 -050021#include <chrono>
Matt Spinlerdf13bdb2019-07-10 16:54:13 -050022#include <filesystem>
Sumit Kumarf380c512021-11-19 06:07:31 -060023#include <fstream>
Andrew Geissler620ef382020-06-16 14:52:14 -050024#include <thread>
Matt Spinlerdf13bdb2019-07-10 16:54:13 -050025
26#include <gtest/gtest.h>
27
28using namespace openpower::pels;
29namespace fs = std::filesystem;
30
31TEST(LogIdTest, TimeBasedIDTest)
32{
33 uint32_t lastID = 0;
34 for (int i = 0; i < 10; i++)
35 {
36 auto id = detail::getTimeBasedLogID();
37
38 EXPECT_EQ(id & 0xFF000000, 0x50000000);
39 EXPECT_NE(id, lastID);
40 lastID = id;
Andrew Geissler620ef382020-06-16 14:52:14 -050041 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Matt Spinlerdf13bdb2019-07-10 16:54:13 -050042 }
43}
44
45TEST(LogIdTest, IDTest)
46{
47 EXPECT_EQ(generatePELID(), 0x50000001);
48 EXPECT_EQ(generatePELID(), 0x50000002);
49 EXPECT_EQ(generatePELID(), 0x50000003);
50 EXPECT_EQ(generatePELID(), 0x50000004);
51 EXPECT_EQ(generatePELID(), 0x50000005);
52 EXPECT_EQ(generatePELID(), 0x50000006);
53
54 auto backingFile = getPELIDFile();
55 fs::remove(backingFile);
56 EXPECT_EQ(generatePELID(), 0x50000001);
57 EXPECT_EQ(generatePELID(), 0x50000002);
58 EXPECT_EQ(generatePELID(), 0x50000003);
59
60 fs::remove_all(fs::path{backingFile}.parent_path());
61}
Sumit Kumarf380c512021-11-19 06:07:31 -060062
63TEST(LogIdTest, PELIDTest)
64{
65 // Get PEL ID file updated with binary zeros
66 auto backingFile = getPELIDFile();
67 std::ofstream wf{backingFile, std::ios::binary};
68 char id = '\0';
69 for (int i = 0; i < 4; i++)
70 {
71 wf.write(&id, sizeof(id));
72 }
73 wf.close();
74
75 // Expect existing PEL ID file to be deleted and
76 // new PEL ID regenerated
77 EXPECT_EQ(generatePELID(), 0x50000001);
78 EXPECT_EQ(generatePELID(), 0x50000002);
79 EXPECT_EQ(generatePELID(), 0x50000003);
80 EXPECT_EQ(generatePELID(), 0x50000004);
81 EXPECT_EQ(generatePELID(), 0x50000005);
82
83 // Get PEL ID file updated with binary zeros again
84 std::ofstream fw{backingFile, std::ios::binary};
85 for (int i = 0; i < 4; i++)
86 {
87 fw.write(&id, sizeof(id));
88 }
89 fw.close();
90
91 // This time PEL IDs are random generated
92 EXPECT_NE(generatePELID(), 0x50000001);
93 EXPECT_NE(generatePELID(), 0x50000002);
94 EXPECT_NE(generatePELID(), 0x50000003);
95 EXPECT_NE(generatePELID(), 0x50000004);
96 EXPECT_NE(generatePELID(), 0x50000005);
97}