summaryrefslogtreecommitdiffstats
path: root/test/message/payload.cpp
blob: 56d8d41426107de8ac72b5c5d9e4ac813665718c (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
 * 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.
 */
#define SD_JOURNAL_SUPPRESS_LOCATION

#include <systemd/sd-journal.h>

#include <ipmid/api.hpp>
#include <ipmid/message.hpp>
#include <stdexcept>

#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);
}

std::vector<std::string> logs;

extern "C" {
int sd_journal_send(const char* format, ...)
{
    logs.push_back(format);
    return 0;
}

int sd_journal_send_with_location(const char* file, const char* line,
                                  const char* func, const char* format, ...)
{
    logs.push_back(format);
    return 0;
}
}

class PayloadLogging : public testing::Test
{
  public:
    void SetUp()
    {
        logs.clear();
    }
};

TEST_F(PayloadLogging, TrailingOk)
{
    {
        ipmi::message::Payload p({1, 2});
    }
    EXPECT_EQ(logs.size(), 0);
}

TEST_F(PayloadLogging, EnforcingUnchecked)
{
    {
        ipmi::message::Payload p({1, 2});
        p.trailingOk = false;
    }
    EXPECT_EQ(logs.size(), 1);
}

TEST_F(PayloadLogging, EnforcingUncheckedUnpacked)
{
    {
        ipmi::message::Payload p({1, 2});
        p.trailingOk = false;
        uint8_t out;
        p.unpack(out, out);
    }
    EXPECT_EQ(logs.size(), 1);
}

TEST_F(PayloadLogging, EnforcingUncheckedError)
{
    {
        ipmi::message::Payload p({1, 2});
        p.trailingOk = false;
        uint32_t out;
        p.unpack(out);
    }
    EXPECT_EQ(logs.size(), 0);
}

TEST_F(PayloadLogging, EnforcingChecked)
{
    {
        ipmi::message::Payload p({1, 2});
        p.trailingOk = false;
        EXPECT_FALSE(p.fullyUnpacked());
    }
    EXPECT_EQ(logs.size(), 0);
}

TEST_F(PayloadLogging, EnforcingCheckedUnpacked)
{
    {
        ipmi::message::Payload p({1, 2});
        p.trailingOk = false;
        uint8_t out;
        p.unpack(out, out);
        EXPECT_TRUE(p.fullyUnpacked());
    }
    EXPECT_EQ(logs.size(), 0);
}

TEST_F(PayloadLogging, EnforcingUnpackPayload)
{
    {
        ipmi::message::Payload p;
        {
            ipmi::message::Payload q({1, 2});
            q.trailingOk = false;
            q.unpack(p);
        }
        EXPECT_EQ(logs.size(), 0);
    }
    EXPECT_EQ(logs.size(), 1);
}

TEST_F(PayloadLogging, EnforcingMove)
{
    {
        ipmi::message::Payload p;
        {
            ipmi::message::Payload q({1, 2});
            q.trailingOk = false;
            p = std::move(q);
        }
        EXPECT_EQ(logs.size(), 0);
    }
    EXPECT_EQ(logs.size(), 1);
}

TEST_F(PayloadLogging, EnforcingException)
{
    try
    {
        ipmi::message::Payload p({1, 2});
        p.trailingOk = false;
        throw std::runtime_error("test");
    }
    catch (...)
    {
    }
    EXPECT_EQ(logs.size(), 0);
}
OpenPOWER on IntegriCloud