xref: /openbmc/phosphor-logging/test/openpower-pels/mru_test.cpp (revision 5ab39978f626f6e4d167a4a952ae31efd5208a49)
197f7abcfSMatt Spinler /**
297f7abcfSMatt Spinler  * Copyright © 2019 IBM Corporation
397f7abcfSMatt Spinler  *
497f7abcfSMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
597f7abcfSMatt Spinler  * you may not use this file except in compliance with the License.
697f7abcfSMatt Spinler  * You may obtain a copy of the License at
797f7abcfSMatt Spinler  *
897f7abcfSMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
997f7abcfSMatt Spinler  *
1097f7abcfSMatt Spinler  * Unless required by applicable law or agreed to in writing, software
1197f7abcfSMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
1297f7abcfSMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1397f7abcfSMatt Spinler  * See the License for the specific language governing permissions and
1497f7abcfSMatt Spinler  * limitations under the License.
1597f7abcfSMatt Spinler  */
1690b4a0a0SMatt Spinler #include "extensions/openpower-pels/mru.hpp"
1790b4a0a0SMatt Spinler 
1890b4a0a0SMatt Spinler #include <gtest/gtest.h>
1990b4a0a0SMatt Spinler 
2090b4a0a0SMatt Spinler using namespace openpower::pels;
2190b4a0a0SMatt Spinler using namespace openpower::pels::src;
2290b4a0a0SMatt Spinler 
TEST(MRUTest,TestConstructor)2390b4a0a0SMatt Spinler TEST(MRUTest, TestConstructor)
2490b4a0a0SMatt Spinler {
2590b4a0a0SMatt Spinler     std::vector<uint8_t> data{
2690b4a0a0SMatt Spinler         'M',  'R',  0x28, 0x04, // ID, size, flags
2790b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 0x00, // Reserved
2890b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 'H',  // priority for MRU ID 0
2990b4a0a0SMatt Spinler         0x01, 0x01, 0x01, 0x01, // MRU ID 0
3090b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 'M',  // priority for MRU ID 1
3190b4a0a0SMatt Spinler         0x02, 0x02, 0x02, 0x02, // MRU ID 1
3290b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 'L',  // priority for MRU ID 2
3390b4a0a0SMatt Spinler         0x03, 0x03, 0x03, 0x03, // MRU ID 2
3490b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 'H',  // priority for MRU ID 3
3590b4a0a0SMatt Spinler         0x04, 0x04, 0x04, 0x04, // MRU ID 3
3690b4a0a0SMatt Spinler     };
3790b4a0a0SMatt Spinler 
3890b4a0a0SMatt Spinler     Stream stream{data};
3990b4a0a0SMatt Spinler 
4090b4a0a0SMatt Spinler     MRU mru{stream};
4190b4a0a0SMatt Spinler 
4290b4a0a0SMatt Spinler     EXPECT_EQ(mru.flattenedSize(), data.size());
4390b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().size(), 4);
44*5ab39978SMatt Spinler     EXPECT_EQ(mru.flags(), 4);
4590b4a0a0SMatt Spinler 
4690b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(0).priority, 'H');
4790b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(0).id, 0x01010101);
4890b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(1).priority, 'M');
4990b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(1).id, 0x02020202);
5090b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(2).priority, 'L');
5190b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(2).id, 0x03030303);
5290b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(3).priority, 'H');
5390b4a0a0SMatt Spinler     EXPECT_EQ(mru.mrus().at(3).id, 0x04040404);
5490b4a0a0SMatt Spinler 
5590b4a0a0SMatt Spinler     // Now flatten
5690b4a0a0SMatt Spinler     std::vector<uint8_t> newData;
5790b4a0a0SMatt Spinler     Stream newStream{newData};
5890b4a0a0SMatt Spinler 
5990b4a0a0SMatt Spinler     mru.flatten(newStream);
6090b4a0a0SMatt Spinler     EXPECT_EQ(data, newData);
6190b4a0a0SMatt Spinler }
6290b4a0a0SMatt Spinler 
TEST(MRUTest,TestBadData)6390b4a0a0SMatt Spinler TEST(MRUTest, TestBadData)
6490b4a0a0SMatt Spinler {
6590b4a0a0SMatt Spinler     // 4 MRUs expected, but only 1
6690b4a0a0SMatt Spinler     std::vector<uint8_t> data{
6790b4a0a0SMatt Spinler         'M',  'R',  0x28, 0x04, // ID, size, flags
6890b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 0x00, // Reserved
6990b4a0a0SMatt Spinler         0x00, 0x00, 0x00, 'H',  // priority 0
7090b4a0a0SMatt Spinler         0x01, 0x01, 0x01, 0x01, // MRU ID 0
7190b4a0a0SMatt Spinler     };
7290b4a0a0SMatt Spinler 
7390b4a0a0SMatt Spinler     Stream stream{data};
7490b4a0a0SMatt Spinler     EXPECT_THROW(MRU mru{stream}, std::out_of_range);
7590b4a0a0SMatt Spinler }
76*5ab39978SMatt Spinler 
TEST(MRUTest,TestVectorConstructor)77*5ab39978SMatt Spinler TEST(MRUTest, TestVectorConstructor)
78*5ab39978SMatt Spinler {
79*5ab39978SMatt Spinler     {
80*5ab39978SMatt Spinler         std::vector<MRU::MRUCallout> mrus{{'H', 1}, {'M', 2}, {'L', 3}};
81*5ab39978SMatt Spinler 
82*5ab39978SMatt Spinler         MRU mru{mrus};
83*5ab39978SMatt Spinler 
84*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().size(), 3);
85*5ab39978SMatt Spinler         EXPECT_EQ(mru.flags(), 3);
86*5ab39978SMatt Spinler 
87*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().at(0).priority, 'H');
88*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().at(0).id, 1);
89*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().at(1).priority, 'M');
90*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().at(1).id, 2);
91*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().at(2).priority, 'L');
92*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().at(2).id, 3);
93*5ab39978SMatt Spinler 
94*5ab39978SMatt Spinler         // Flatten and unflatten
95*5ab39978SMatt Spinler         std::vector<uint8_t> data;
96*5ab39978SMatt Spinler         Stream stream{data};
97*5ab39978SMatt Spinler 
98*5ab39978SMatt Spinler         mru.flatten(stream);
99*5ab39978SMatt Spinler         EXPECT_EQ(mru.size(), data.size());
100*5ab39978SMatt Spinler 
101*5ab39978SMatt Spinler         stream.offset(0);
102*5ab39978SMatt Spinler         MRU newMRU{stream};
103*5ab39978SMatt Spinler 
104*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.flattenedSize(), data.size());
105*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.size(), data.size());
106*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().size(), 3);
107*5ab39978SMatt Spinler 
108*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().at(0).priority, 'H');
109*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().at(0).id, 1);
110*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().at(1).priority, 'M');
111*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().at(1).id, 2);
112*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().at(2).priority, 'L');
113*5ab39978SMatt Spinler         EXPECT_EQ(newMRU.mrus().at(2).id, 3);
114*5ab39978SMatt Spinler     }
115*5ab39978SMatt Spinler 
116*5ab39978SMatt Spinler     {
117*5ab39978SMatt Spinler         // Too many MRUs
118*5ab39978SMatt Spinler         std::vector<MRU::MRUCallout> mrus;
119*5ab39978SMatt Spinler         for (uint32_t i = 0; i < 20; i++)
120*5ab39978SMatt Spinler         {
121*5ab39978SMatt Spinler             MRU::MRUCallout mru = {'H', i};
122*5ab39978SMatt Spinler             mrus.push_back(mru);
123*5ab39978SMatt Spinler         }
124*5ab39978SMatt Spinler 
125*5ab39978SMatt Spinler         MRU mru{mrus};
126*5ab39978SMatt Spinler 
127*5ab39978SMatt Spinler         EXPECT_EQ(mru.mrus().size(), 15);
128*5ab39978SMatt Spinler         EXPECT_EQ(mru.flags(), 15);
129*5ab39978SMatt Spinler     }
130*5ab39978SMatt Spinler 
131*5ab39978SMatt Spinler     {
132*5ab39978SMatt Spinler         // Too few MRUs
133*5ab39978SMatt Spinler         std::vector<MRU::MRUCallout> mrus;
134*5ab39978SMatt Spinler         EXPECT_THROW(MRU mru{mrus}, std::runtime_error);
135*5ab39978SMatt Spinler     }
136*5ab39978SMatt Spinler }
137