summaryrefslogtreecommitdiffstats
path: root/test/message/payload.cpp
blob: 9d20ff0417ce541109031a1e15c68803afe46e04 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * Copyright © 2018 Intel 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 <ipmid/api.hpp>
#include <ipmid/message.hpp>

#include <gtest/gtest.h>

TEST(Payload, InputSize)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    size_t input_size = i.size();
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    ASSERT_EQ(input_size, p.size());
}

TEST(Payload, OutputSize)
{
    ipmi::message::Payload p;
    ASSERT_EQ(0, p.size());
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    p.pack(i);
    ASSERT_EQ(i.size(), p.size());
    p.pack(i);
    p.pack(i);
    ASSERT_EQ(3 * i.size(), p.size());
}

TEST(Payload, Resize)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p;
    p.pack(i);
    p.resize(16);
    ASSERT_EQ(p.size(), 16);
}

TEST(Payload, Data)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p;
    p.pack(i);
    ASSERT_NE(nullptr, p.data());
}

TEST(PayloadResponse, Append)
{
    std::string s("0123456789abcdef");
    ipmi::message::Payload p;
    p.append(s.data(), s.data() + s.size());
    ASSERT_EQ(s.size(), p.size());
}

TEST(PayloadResponse, AppendDrain)
{
    std::string s("0123456789abcdef");
    ipmi::message::Payload p;
    bool b = true;
    // first pack a lone bit
    p.pack(b);
    p.append(s.data(), s.data() + s.size());
    // append will 'drain' first, padding the lone bit into a full byte
    ASSERT_EQ(s.size() + 1, p.size());
}

TEST(PayloadResponse, AppendBits)
{
    ipmi::message::Payload p;
    p.appendBits(3, 0b101);
    ASSERT_EQ(p.bitStream, 0b101);
    p.appendBits(4, 0b1100);
    ASSERT_EQ(p.bitStream, 0b1100101);
    p.appendBits(1, 0b1);
    ASSERT_EQ(p.bitStream, 0);
    ASSERT_EQ(p.bitCount, 0);
    // appended 8 bits, should be one byte
    ASSERT_EQ(p.size(), 1);
    std::vector<uint8_t> k1 = {0b11100101};
    ASSERT_EQ(p.raw, k1);
    p.appendBits(7, 0b1110111);
    // appended 7 more bits, should still be one byte
    ASSERT_EQ(p.size(), 1);
    p.drain();
    // drain forces padding; should be two bytes now
    ASSERT_EQ(p.size(), 2);
    std::vector<uint8_t> k2 = {0b11100101, 0b01110111};
    ASSERT_EQ(p.raw, k2);
}

TEST(PayloadResponse, Drain16Bits)
{
    ipmi::message::Payload p;
    p.bitStream = 0b1011010011001111;
    p.bitCount = 16;
    p.drain();
    ASSERT_EQ(p.size(), 2);
    ASSERT_EQ(p.bitCount, 0);
    ASSERT_EQ(p.bitStream, 0);
    std::vector<uint8_t> k1 = {0b11001111, 0b10110100};
    ASSERT_EQ(p.raw, k1);
}

TEST(PayloadResponse, Drain15Bits)
{
    ipmi::message::Payload p;
    p.bitStream = 0b101101001100111;
    p.bitCount = 15;
    p.drain();
    ASSERT_EQ(p.size(), 2);
    ASSERT_EQ(p.bitCount, 0);
    ASSERT_EQ(p.bitStream, 0);
    std::vector<uint8_t> k1 = {0b1100111, 0b1011010};
    ASSERT_EQ(p.raw, k1);
}

TEST(PayloadResponse, Drain15BitsWholeBytesOnly)
{
    ipmi::message::Payload p;
    p.bitStream = 0b101101001100111;
    p.bitCount = 15;
    p.drain(true);
    // only the first whole byte should have been 'drained' into p.raw
    ASSERT_EQ(p.size(), 1);
    ASSERT_EQ(p.bitCount, 7);
    ASSERT_EQ(p.bitStream, 0b1011010);
    std::vector<uint8_t> k1 = {0b1100111};
    ASSERT_EQ(p.raw, k1);
}

TEST(PayloadRequest, Pop)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    const auto& [vb, ve] = p.pop<uint8_t>(4);
    std::vector<uint8_t> v(vb, ve);
    std::vector<uint8_t> k = {0xbf, 0x04, 0x86, 0x00};
    ASSERT_EQ(v, k);
}

TEST(PayloadRequest, FillBits)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.fillBits(5);
    ASSERT_FALSE(p.unpackError);
    ASSERT_EQ(p.bitStream, 0xbf);
    ASSERT_EQ(p.bitCount, 8);
    // should still have 5 bits available, no change
    p.fillBits(5);
    ASSERT_FALSE(p.unpackError);
    ASSERT_EQ(p.bitStream, 0xbf);
    ASSERT_EQ(p.bitCount, 8);
    // discard 5 bits (low order)
    p.popBits(5);
    // should add another byte into the stream (high order)
    p.fillBits(5);
    ASSERT_FALSE(p.unpackError);
    ASSERT_EQ(p.bitStream, 0x25);
    ASSERT_EQ(p.bitCount, 11);
}

TEST(PayloadRequest, FillBitsTooManyBits)
{
    std::vector<uint8_t> i = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.fillBits(72);
    ASSERT_TRUE(p.unpackError);
}

TEST(PayloadRequest, FillBitsNotEnoughBytes)
{
    std::vector<uint8_t> i = {1, 2, 3, 4};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.fillBits(48);
    ASSERT_TRUE(p.unpackError);
}

TEST(PayloadRequest, PopBits)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.fillBits(4);
    uint8_t v = p.popBits(4);
    ASSERT_FALSE(p.unpackError);
    ASSERT_EQ(p.bitStream, 0x0b);
    ASSERT_EQ(p.bitCount, 4);
    ASSERT_EQ(v, 0x0f);
}

TEST(PayloadRequest, PopBitsNoFillBits)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.popBits(4);
    ASSERT_TRUE(p.unpackError);
}

TEST(PayloadRequest, DiscardBits)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.fillBits(5);
    ASSERT_FALSE(p.unpackError);
    ASSERT_EQ(p.bitStream, 0xbf);
    ASSERT_EQ(p.bitCount, 8);
    p.discardBits();
    ASSERT_FALSE(p.unpackError);
    ASSERT_EQ(p.bitStream, 0);
    ASSERT_EQ(p.bitCount, 0);
}

TEST(PayloadRequest, FullyUnpacked)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint32_t v1;
    p.unpack(v1);
    // still one remaining byte
    ASSERT_FALSE(p.fullyUnpacked());
    p.fillBits(3);
    p.popBits(3);
    // still five remaining bits
    ASSERT_FALSE(p.fullyUnpacked());
    p.fillBits(5);
    p.popBits(5);
    // fully unpacked, should be no errors
    ASSERT_TRUE(p.fullyUnpacked());
    p.fillBits(4);
    // fullyUnpacked fails because an attempt to unpack too many bytes
    ASSERT_FALSE(p.fullyUnpacked());
}

TEST(PayloadRequest, ResetInternal)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    p.fillBits(4);
    p.unpackError = true;
    p.reset();
    ASSERT_EQ(p.rawIndex, 0);
    ASSERT_EQ(p.bitStream, 0);
    ASSERT_EQ(p.bitCount, 0);
    ASSERT_FALSE(p.unpackError);
}

TEST(PayloadRequest, ResetUsage)
{
    // Payload.reset is used to rewind the unpacking to the initial
    // state. This is needed so that OEM commands can unpack the group
    // number or the IANA to determine which handler needs to be called
    std::vector<uint8_t> i = {0x04, 0x86};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v1;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint8_t k1 = 0x04;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    // do a reset on the payload
    p.reset();
    // unpack a uint16
    uint16_t v2;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v2), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint16_t k2 = 0x8604;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v2, k2);
}

TEST(PayloadRequest, PartialPayload)
{
    std::vector<uint8_t> i = {0xbf, 0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v1;
    ipmi::message::Payload localPayload;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, localPayload), 0);
    // check that the payload was partially unpacked and not in error
    ASSERT_FALSE(p.fullyUnpacked());
    ASSERT_FALSE(p.unpackError);
    // check that the 'extracted' payload is not fully unpacked
    ASSERT_FALSE(localPayload.fullyUnpacked());
    uint8_t k1 = 0xbf;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    uint32_t v2;
    // unpack using the 'extracted' payload
    ASSERT_EQ(localPayload.unpack(v2), 0);
    ASSERT_TRUE(localPayload.fullyUnpacked());
    uint32_t k2 = 0x02008604;
    ASSERT_EQ(v2, k2);
}
OpenPOWER on IntegriCloud