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/log_id.hpp"
17 #include "extensions/openpower-pels/paths.hpp"
18
19 #include <arpa/inet.h>
20
21 #include <chrono>
22 #include <filesystem>
23 #include <fstream>
24 #include <thread>
25
26 #include <gtest/gtest.h>
27
28 using namespace openpower::pels;
29 namespace fs = std::filesystem;
30
TEST(LogIdTest,TimeBasedIDTest)31 TEST(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;
41 std::this_thread::sleep_for(std::chrono::milliseconds(1));
42 }
43 }
44
TEST(LogIdTest,IDTest)45 TEST(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 }
62
TEST(LogIdTest,PELIDTest)63 TEST(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
98 fs::remove_all(fs::path{backingFile}.parent_path());
99 }
100