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/fru_identity.hpp"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace openpower::pels;
21 using namespace openpower::pels::src;
22 
23 // Unflatten a FRUIdentity that is a HW FRU callout
24 TEST(FRUIdentityTest, TestHardwareFRU)
25 {
26     // Has PN, SN, CCIN
27     std::vector<uint8_t> data{'I', 'D', 0x1C, 0x1D, // type, size, flags
28                               '1', '2', '3',  '4',  // PN
29                               '5', '6', '7',  0x00, 'A', 'A', 'A', 'A', // CCIN
30                               '1', '2', '3',  '4',  '5', '6', '7', '8', // SN
31                               '9', 'A', 'B',  'C'};
32 
33     Stream stream{data};
34 
35     FRUIdentity fru{stream};
36 
37     EXPECT_EQ(fru.failingComponentType(), FRUIdentity::hardwareFRU);
38     EXPECT_EQ(fru.flattenedSize(), data.size());
39 
40     EXPECT_EQ(fru.getPN().value(), "1234567");
41     EXPECT_EQ(fru.getCCIN().value(), "AAAA");
42     EXPECT_EQ(fru.getSN().value(), "123456789ABC");
43     EXPECT_FALSE(fru.getMaintProc());
44 
45     // Flatten
46     std::vector<uint8_t> newData;
47     Stream newStream{newData};
48     fru.flatten(newStream);
49     EXPECT_EQ(data, newData);
50 }
51 
52 // Unflatten a FRUIdentity that is a Maintenance Procedure callout
53 TEST(FRUIdentityTest, TestMaintProcedure)
54 {
55     // Only contains the maintenance procedure
56     std::vector<uint8_t> data{
57         0x49, 0x44, 0x0C, 0x42,                     // type, size, flags
58         '1',  '2',  '3',  '4',  '5', '6', '7', 0x00 // Procedure
59     };
60 
61     Stream stream{data};
62 
63     FRUIdentity fru{stream};
64 
65     EXPECT_EQ(fru.failingComponentType(), FRUIdentity::maintenanceProc);
66     EXPECT_EQ(fru.flattenedSize(), data.size());
67 
68     EXPECT_EQ(fru.getMaintProc().value(), "1234567");
69     EXPECT_FALSE(fru.getPN());
70     EXPECT_FALSE(fru.getCCIN());
71     EXPECT_FALSE(fru.getSN());
72 
73     // Flatten
74     std::vector<uint8_t> newData;
75     Stream newStream{newData};
76     fru.flatten(newStream);
77     EXPECT_EQ(data, newData);
78 }
79 
80 // Try to unflatten garbage data
81 TEST(FRUIdentityTest, BadDataTest)
82 {
83     std::vector<uint8_t> data{0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
84                               0xFF, 0xFF, 0xFF, 0xFF};
85 
86     Stream stream{data};
87 
88     EXPECT_THROW(FRUIdentity fru{stream}, std::out_of_range);
89 }
90