197f7abcfSMatt Spinler /**
297f7abcfSMatt Spinler * Copyright © 2019 IBM Corporation
397f7abcfSMatt Spinler *
497f7abcfSMatt Spinler * Licensed under the Apache License, Version 2.0 (the "License");
597f7abcfSMatt Spinler * you may not use this file except in compliance with the License.
697f7abcfSMatt Spinler * You may obtain a copy of the License at
797f7abcfSMatt Spinler *
897f7abcfSMatt Spinler * http://www.apache.org/licenses/LICENSE-2.0
997f7abcfSMatt Spinler *
1097f7abcfSMatt Spinler * Unless required by applicable law or agreed to in writing, software
1197f7abcfSMatt Spinler * distributed under the License is distributed on an "AS IS" BASIS,
1297f7abcfSMatt Spinler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1397f7abcfSMatt Spinler * See the License for the specific language governing permissions and
1497f7abcfSMatt Spinler * limitations under the License.
1597f7abcfSMatt Spinler */
16df13bdb6SMatt Spinler #include "extensions/openpower-pels/log_id.hpp"
17df13bdb6SMatt Spinler #include "extensions/openpower-pels/paths.hpp"
18df13bdb6SMatt Spinler
19df13bdb6SMatt Spinler #include <arpa/inet.h>
20df13bdb6SMatt Spinler
21620ef38cSAndrew Geissler #include <chrono>
22df13bdb6SMatt Spinler #include <filesystem>
23f380c516SSumit Kumar #include <fstream>
24620ef38cSAndrew Geissler #include <thread>
25df13bdb6SMatt Spinler
26df13bdb6SMatt Spinler #include <gtest/gtest.h>
27df13bdb6SMatt Spinler
28df13bdb6SMatt Spinler using namespace openpower::pels;
29df13bdb6SMatt Spinler namespace fs = std::filesystem;
30df13bdb6SMatt Spinler
TEST(LogIdTest,TimeBasedIDTest)31df13bdb6SMatt Spinler TEST(LogIdTest, TimeBasedIDTest)
32df13bdb6SMatt Spinler {
33df13bdb6SMatt Spinler uint32_t lastID = 0;
34df13bdb6SMatt Spinler for (int i = 0; i < 10; i++)
35df13bdb6SMatt Spinler {
36df13bdb6SMatt Spinler auto id = detail::getTimeBasedLogID();
37df13bdb6SMatt Spinler
38df13bdb6SMatt Spinler EXPECT_EQ(id & 0xFF000000, 0x50000000);
39df13bdb6SMatt Spinler EXPECT_NE(id, lastID);
40df13bdb6SMatt Spinler lastID = id;
41620ef38cSAndrew Geissler std::this_thread::sleep_for(std::chrono::milliseconds(1));
42df13bdb6SMatt Spinler }
43df13bdb6SMatt Spinler }
44df13bdb6SMatt Spinler
TEST(LogIdTest,IDTest)45df13bdb6SMatt Spinler TEST(LogIdTest, IDTest)
46df13bdb6SMatt Spinler {
47df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000001);
48df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000002);
49df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000003);
50df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000004);
51df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000005);
52df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000006);
53df13bdb6SMatt Spinler
54df13bdb6SMatt Spinler auto backingFile = getPELIDFile();
55df13bdb6SMatt Spinler fs::remove(backingFile);
56df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000001);
57df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000002);
58df13bdb6SMatt Spinler EXPECT_EQ(generatePELID(), 0x50000003);
59df13bdb6SMatt Spinler
60df13bdb6SMatt Spinler fs::remove_all(fs::path{backingFile}.parent_path());
61df13bdb6SMatt Spinler }
62f380c516SSumit Kumar
TEST(LogIdTest,PELIDTest)63f380c516SSumit Kumar TEST(LogIdTest, PELIDTest)
64f380c516SSumit Kumar {
65f380c516SSumit Kumar // Get PEL ID file updated with binary zeros
66f380c516SSumit Kumar auto backingFile = getPELIDFile();
67f380c516SSumit Kumar std::ofstream wf{backingFile, std::ios::binary};
68f380c516SSumit Kumar char id = '\0';
69f380c516SSumit Kumar for (int i = 0; i < 4; i++)
70f380c516SSumit Kumar {
71f380c516SSumit Kumar wf.write(&id, sizeof(id));
72f380c516SSumit Kumar }
73f380c516SSumit Kumar wf.close();
74f380c516SSumit Kumar
75f380c516SSumit Kumar // Expect existing PEL ID file to be deleted and
76f380c516SSumit Kumar // new PEL ID regenerated
77f380c516SSumit Kumar EXPECT_EQ(generatePELID(), 0x50000001);
78f380c516SSumit Kumar EXPECT_EQ(generatePELID(), 0x50000002);
79f380c516SSumit Kumar EXPECT_EQ(generatePELID(), 0x50000003);
80f380c516SSumit Kumar EXPECT_EQ(generatePELID(), 0x50000004);
81f380c516SSumit Kumar EXPECT_EQ(generatePELID(), 0x50000005);
82f380c516SSumit Kumar
83f380c516SSumit Kumar // Get PEL ID file updated with binary zeros again
84f380c516SSumit Kumar std::ofstream fw{backingFile, std::ios::binary};
85f380c516SSumit Kumar for (int i = 0; i < 4; i++)
86f380c516SSumit Kumar {
87f380c516SSumit Kumar fw.write(&id, sizeof(id));
88f380c516SSumit Kumar }
89f380c516SSumit Kumar fw.close();
90f380c516SSumit Kumar
91f380c516SSumit Kumar // This time PEL IDs are random generated
92f380c516SSumit Kumar EXPECT_NE(generatePELID(), 0x50000001);
93f380c516SSumit Kumar EXPECT_NE(generatePELID(), 0x50000002);
94f380c516SSumit Kumar EXPECT_NE(generatePELID(), 0x50000003);
95f380c516SSumit Kumar EXPECT_NE(generatePELID(), 0x50000004);
96f380c516SSumit Kumar EXPECT_NE(generatePELID(), 0x50000005);
97*7410260aSMatt Spinler
98*7410260aSMatt Spinler fs::remove_all(fs::path{backingFile}.parent_path());
99f380c516SSumit Kumar }
100