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