summaryrefslogtreecommitdiffstats
path: root/test/openpower-pels/user_data_test.cpp
blob: a957a4d42ffd56b87f520962b4ad29e2238fcc1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * Copyright © 2019 IBM Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "extensions/openpower-pels/user_data.hpp"
#include "pel_utils.hpp"

#include <gtest/gtest.h>

using namespace openpower::pels;

std::vector<uint8_t> udSectionData{0x55, 0x44, // ID 'UD'
                                   0x00, 0x10, // Size
                                   0x01, 0x02, // version, subtype
                                   0x03, 0x04, // comp ID

                                   // Data
                                   0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                                   0x18};

TEST(UserDataTest, UnflattenFlattenTest)
{
    Stream stream(udSectionData);
    UserData ud(stream);

    EXPECT_TRUE(ud.valid());
    EXPECT_EQ(ud.header().id, 0x5544);
    EXPECT_EQ(ud.header().size, udSectionData.size());
    EXPECT_EQ(ud.header().version, 0x01);
    EXPECT_EQ(ud.header().subType, 0x02);
    EXPECT_EQ(ud.header().componentID, 0x0304);

    const auto& data = ud.data();

    // The data itself starts after the header
    EXPECT_EQ(data.size(), udSectionData.size() - 8);

    for (size_t i = 0; i < data.size(); i++)
    {
        EXPECT_EQ(data[i], udSectionData[i + 8]);
    }

    // Now flatten
    std::vector<uint8_t> newData;
    Stream newStream(newData);
    ud.flatten(newStream);

    EXPECT_EQ(udSectionData, newData);
}

TEST(UserDataTest, BadDataTest)
{
    auto data = udSectionData;
    data.resize(4);

    Stream stream(data);
    UserData ud(stream);
    EXPECT_FALSE(ud.valid());
}

TEST(UserDataTest, BadSizeFieldTest)
{
    auto data = udSectionData;

    {
        data[3] = 0xFF; // Set the size field too large
        Stream stream(data);
        UserData ud(stream);
        EXPECT_FALSE(ud.valid());
    }
    {
        data[3] = 0x7; // Set the size field too small
        Stream stream(data);
        UserData ud(stream);
        EXPECT_FALSE(ud.valid());
    }
}

TEST(UserDataTest, ConstructorTest)
{
    std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};

    UserData ud(0x1112, 0x42, 0x01, data);
    EXPECT_TRUE(ud.valid());

    EXPECT_EQ(ud.header().id, 0x5544);
    EXPECT_EQ(ud.header().size, 16);
    EXPECT_EQ(ud.header().version, 0x01);
    EXPECT_EQ(ud.header().subType, 0x42);
    EXPECT_EQ(ud.header().componentID, 0x1112);

    const auto& d = ud.data();

    EXPECT_EQ(d, data);
}
OpenPOWER on IntegriCloud