xref: /openbmc/phosphor-logging/test/openpower-pels/failing_mtms_test.cpp (revision 40fb54935ce7367636a7156039396ee91cc4d5e2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3 
4 #include "extensions/openpower-pels/failing_mtms.hpp"
5 #include "mocks.hpp"
6 
7 #include <gtest/gtest.h>
8 
9 using namespace openpower::pels;
10 using ::testing::Return;
11 
TEST(FailingMTMSTest,SizeTest)12 TEST(FailingMTMSTest, SizeTest)
13 {
14     EXPECT_EQ(FailingMTMS::flattenedSize(), 28);
15 }
16 
TEST(FailingMTMSTest,ConstructorTest)17 TEST(FailingMTMSTest, ConstructorTest)
18 {
19     // Note: the TypeModel field is 8B, and the SN field is 12B
20     {
21         MockDataInterface dataIface;
22 
23         EXPECT_CALL(dataIface, getMachineTypeModel())
24             .WillOnce(Return("AAAA-BBB"));
25 
26         EXPECT_CALL(dataIface, getMachineSerialNumber())
27             .WillOnce(Return("123456789ABC"));
28 
29         FailingMTMS fm{dataIface};
30 
31         // Check the section header
32         EXPECT_EQ(fm.header().id, 0x4D54);
33         EXPECT_EQ(fm.header().size, FailingMTMS::flattenedSize());
34         EXPECT_EQ(fm.header().version, 0x01);
35         EXPECT_EQ(fm.header().subType, 0x00);
36         EXPECT_EQ(fm.header().componentID, 0x2000);
37 
38         EXPECT_EQ(fm.getMachineTypeModel(), "AAAA-BBB");
39         EXPECT_EQ(fm.getMachineSerialNumber(), "123456789ABC");
40     }
41 
42     // longer than the max - will truncate
43     {
44         MockDataInterface dataIface;
45 
46         EXPECT_CALL(dataIface, getMachineTypeModel())
47             .WillOnce(Return("AAAA-BBBTOOLONG"));
48 
49         EXPECT_CALL(dataIface, getMachineSerialNumber())
50             .WillOnce(Return("123456789ABCTOOLONG"));
51 
52         FailingMTMS fm{dataIface};
53 
54         EXPECT_EQ(fm.getMachineTypeModel(), "AAAA-BBB");
55         EXPECT_EQ(fm.getMachineSerialNumber(), "123456789ABC");
56     }
57 
58     // shorter than the max
59     {
60         MockDataInterface dataIface;
61 
62         EXPECT_CALL(dataIface, getMachineTypeModel()).WillOnce(Return("A"));
63 
64         EXPECT_CALL(dataIface, getMachineSerialNumber()).WillOnce(Return("1"));
65 
66         FailingMTMS fm{dataIface};
67 
68         EXPECT_EQ(fm.getMachineTypeModel(), "A");
69         EXPECT_EQ(fm.getMachineSerialNumber(), "1");
70     }
71 }
72 
TEST(FailingMTMSTest,StreamConstructorTest)73 TEST(FailingMTMSTest, StreamConstructorTest)
74 {
75     std::vector<uint8_t> data{
76         0x4D, 0x54, 0x00, 0x1C, 0x01, 0x00, 0x20, 0x00, 'T', 'T',
77         'T',  'T',  '-',  'M',  'M',  'M',  '1',  '2',  '3', '4',
78         '5',  '6',  '7',  '8',  '9',  'A',  'B',  'C'};
79     Stream stream{data};
80     FailingMTMS fm{stream};
81 
82     EXPECT_EQ(fm.valid(), true);
83 
84     EXPECT_EQ(fm.header().id, 0x4D54);
85     EXPECT_EQ(fm.header().size, FailingMTMS::flattenedSize());
86     EXPECT_EQ(fm.header().version, 0x01);
87     EXPECT_EQ(fm.header().subType, 0x00);
88     EXPECT_EQ(fm.header().componentID, 0x2000);
89 
90     EXPECT_EQ(fm.getMachineTypeModel(), "TTTT-MMM");
91     EXPECT_EQ(fm.getMachineSerialNumber(), "123456789ABC");
92 }
93 
TEST(FailingMTMSTest,BadStreamConstructorTest)94 TEST(FailingMTMSTest, BadStreamConstructorTest)
95 {
96     // too short
97     std::vector<uint8_t> data{
98         0x4D, 0x54, 0x00, 0x1C, 0x01, 0x00, 0x20, 0x00, 'T', 'T',
99     };
100     Stream stream{data};
101     FailingMTMS fm{stream};
102 
103     EXPECT_EQ(fm.valid(), false);
104 }
105 
TEST(FailingMTMSTest,FlattenTest)106 TEST(FailingMTMSTest, FlattenTest)
107 {
108     std::vector<uint8_t> data{
109         0x4D, 0x54, 0x00, 0x1C, 0x01, 0x00, 0x20, 0x00, 'T', 'T',
110         'T',  'T',  '-',  'M',  'M',  'M',  '1',  '2',  '3', '4',
111         '5',  '6',  '7',  '8',  '9',  'A',  'B',  'C'};
112     Stream stream{data};
113     FailingMTMS fm{stream};
114 
115     // flatten and check results
116     std::vector<uint8_t> newData;
117     Stream newStream{newData};
118 
119     fm.flatten(newStream);
120     EXPECT_EQ(data, newData);
121 }
122