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/mtms.hpp"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace openpower::pels;
21 
TEST(MTMSTest,SizeTest)22 TEST(MTMSTest, SizeTest)
23 {
24     EXPECT_EQ(MTMS::flattenedSize(), 20);
25 }
26 
TEST(MTMSTest,ConstructorTest)27 TEST(MTMSTest, ConstructorTest)
28 {
29     {
30         std::string tm{"TTTT-MMM"};
31         std::string sn{"123456789ABC"};
32 
33         MTMS mtms{tm, sn};
34 
35         const std::array<uint8_t, 8> t{'T', 'T', 'T', 'T', '-', 'M', 'M', 'M'};
36         EXPECT_EQ(t, mtms.machineTypeAndModelRaw());
37         EXPECT_EQ("TTTT-MMM", mtms.machineTypeAndModel());
38 
39         const std::array<uint8_t, 12> s{'1', '2', '3', '4', '5', '6',
40                                         '7', '8', '9', 'A', 'B', 'C'};
41         EXPECT_EQ(s, mtms.machineSerialNumberRaw());
42         EXPECT_EQ("123456789ABC", mtms.machineSerialNumber());
43     }
44 
45     {
46         // too long- truncate it
47         std::string tm{"TTTT-MMME"};
48         std::string sn{"123456789ABCE"};
49 
50         MTMS mtms{tm, sn};
51 
52         const std::array<uint8_t, 8> t{'T', 'T', 'T', 'T', '-', 'M', 'M', 'M'};
53         EXPECT_EQ(t, mtms.machineTypeAndModelRaw());
54 
55         const std::array<uint8_t, 12> s{'1', '2', '3', '4', '5', '6',
56                                         '7', '8', '9', 'A', 'B', 'C'};
57         EXPECT_EQ(s, mtms.machineSerialNumberRaw());
58     }
59 
60     {
61         // short
62         std::string tm{"TTTT"};
63         std::string sn{"1234"};
64 
65         MTMS mtms{tm, sn};
66 
67         const std::array<uint8_t, 8> t{'T', 'T', 'T', 'T', 0, 0, 0, 0};
68         EXPECT_EQ(t, mtms.machineTypeAndModelRaw());
69         EXPECT_EQ("TTTT", mtms.machineTypeAndModel());
70 
71         const std::array<uint8_t, 12> s{'1', '2', '3', '4', 0, 0,
72                                         0,   0,   0,   0,   0, 0};
73         EXPECT_EQ(s, mtms.machineSerialNumberRaw());
74         EXPECT_EQ("1234", mtms.machineSerialNumber());
75     }
76 
77     {
78         // Stream constructor
79         std::vector<uint8_t> data{'T', 'T', 'T', 'T', '-', 'M', 'M',
80                                   'M', '1', '2', '3', '4', '5', '6',
81                                   '7', '8', '9', 'A', 'B', 'C'};
82         Stream stream{data};
83 
84         MTMS mtms{stream};
85 
86         EXPECT_EQ("TTTT-MMM", mtms.machineTypeAndModel());
87 
88         EXPECT_EQ("123456789ABC", mtms.machineSerialNumber());
89     }
90 }
91 
TEST(MTMSTest,OperatorExtractTest)92 TEST(MTMSTest, OperatorExtractTest)
93 {
94     std::string tm{"TTTT-MMM"};
95     std::string sn{"123456789ABC"};
96 
97     MTMS mtms{tm, sn};
98 
99     // Check that we can extract the same data
100     std::vector<uint8_t> data;
101     Stream stream{data};
102     stream << mtms;
103 
104     std::vector<uint8_t> expected{'T', 'T', 'T', 'T', '-', 'M', 'M',
105                                   'M', '1', '2', '3', '4', '5', '6',
106                                   '7', '8', '9', 'A', 'B', 'C'};
107     EXPECT_EQ(expected, data);
108 }
109 
TEST(MTMSTest,OperatorInsertTest)110 TEST(MTMSTest, OperatorInsertTest)
111 {
112     std::vector<uint8_t> data{'T', 'T', 'T', 'T', '-', 'M', 'M', 'M', '1', '2',
113                               '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'};
114     Stream stream{data};
115 
116     // Check that when we insert data it is what's expected
117     MTMS mtms;
118     stream >> mtms;
119 
120     EXPECT_EQ("TTTT-MMM", mtms.machineTypeAndModel());
121 
122     EXPECT_EQ("123456789ABC", mtms.machineSerialNumber());
123 }
124