blob: 31903c7e1db1fc879d7f141de4b800cceb5bd325 [file] [log] [blame]
David Valleau46c3f412018-11-14 10:25:47 -08001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "smart_buffer.h"
6
David Valleau46c3f412018-11-14 10:25:47 -08007#include <vector>
8
9#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
Fletcher Woodruff76e9e782020-04-30 16:51:26 -060012#include "device_descriptors.h"
13
David Valleau46c3f412018-11-14 10:25:47 -080014namespace {
15
16TEST(Add, AddPrimitive) {
17 SmartBuffer buf1(1);
18 SmartBuffer buf2(1);
19 std::vector<uint8_t> expected = {'h'};
20 uint8_t byte = 'h';
21
22 buf1.Add(&byte, sizeof(byte));
23 EXPECT_EQ(expected, buf1.contents());
24 buf2.Add(byte);
25 EXPECT_EQ(expected, buf2.contents());
26}
27
28TEST(Add, AddVector) {
29 SmartBuffer buf1(5);
30 SmartBuffer buf2(5);
31 std::vector<uint8_t> expected = {1, 2, 3, 4, 5};
32
33 buf1.Add(expected.data(), expected.size());
34 EXPECT_EQ(expected, buf1.contents());
35 buf2.Add(expected);
36 EXPECT_EQ(expected, buf2.contents());
37}
38
39TEST(Add, AddString) {
40 SmartBuffer buf1(10);
41 SmartBuffer buf2(10);
42 std::string s = "helloworld";
43 std::vector<uint8_t> expected = {'h', 'e', 'l', 'l', 'o',
44 'w', 'o', 'r', 'l', 'd'};
45 buf1.Add(s.c_str(), s.size());
46 EXPECT_EQ(expected, buf1.contents());
47 buf2.Add(s);
48 EXPECT_EQ(expected, buf2.contents());
49}
50
51TEST(Add, AddDeviceDescriptor) {
52 UsbDeviceDescriptor dev_dsc(18, 1, 272, 0, 0, 0, 8, 1193, 10216, 0, 1, 2, 1,
53 1);
54 std::vector<uint8_t> expected = {18, 1, 16, 1, 0, 0, 0, 8,
55 169, 4, 232, 39, 0, 0, 1, 2, 1, 1};
56 SmartBuffer buf1(sizeof(dev_dsc));
57 buf1.Add(&dev_dsc, sizeof(dev_dsc));
58 EXPECT_EQ(expected, buf1.contents());
59
60 SmartBuffer buf2(sizeof(dev_dsc));
61 buf2.Add(dev_dsc);
62 EXPECT_EQ(expected, buf2.contents());
63}
64
65TEST(Add, AddSmartBuffer) {
66 SmartBuffer buf1(5);
67 SmartBuffer buf2(5);
68 std::vector<uint8_t> expected = {1, 2, 3, 4, 5};
69 buf1.Add(expected.data(), expected.size());
70 buf2.Add(buf1);
71 EXPECT_EQ(buf1.contents(), buf2.contents());
72}
73
74// Test that the proper suffix from the provided SmartBuffer is added.
75TEST(Add, AddSmartBufferSuffix) {
76 SmartBuffer buf1(5);
77 SmartBuffer buf2(5);
78 std::vector<uint8_t> contents = {1, 2, 3, 4, 5};
79 buf1.Add(contents.data(), contents.size());
80
81 std::vector<uint8_t> expected = {3, 4, 5};
82 buf2.Add(buf1, 2);
83 EXPECT_EQ(expected, buf2.contents());
84}
85
86// Test that if the provided start point is 0, then the entire contents of the
87// provided SmartBuffer is added.
88TEST(Add, AddSmartBufferFullSuffix) {
89 SmartBuffer buf1(5);
90 SmartBuffer buf2(5);
91 std::vector<uint8_t> contents = {1, 2, 3, 4, 5};
92 buf1.Add(contents.data(), contents.size());
93
94 buf2.Add(buf1, 0);
95 EXPECT_EQ(contents, buf2.contents());
96}
97
98TEST(Add, AddSmartBufferRange) {
99 SmartBuffer to_copy({1, 2, 3, 4, 5});
100 SmartBuffer to_extend(5);
101 to_extend.Add(to_copy, 1, 3);
102 const std::vector<uint8_t> expected = {2, 3, 4};
103 EXPECT_EQ(to_extend.contents(), expected);
104}
105
Fletcher Woodruff86deb602020-05-06 16:43:56 -0600106TEST(Add, AddDynamicCharPointer) {
107 std::string input("TEST");
108
109 SmartBuffer buf;
110 buf.Add(input.c_str());
111 const std::vector<uint8_t> expected = {'T', 'E', 'S', 'T'};
112 EXPECT_EQ(buf.contents(), expected);
113}
114
David Valleau46c3f412018-11-14 10:25:47 -0800115TEST(Erase, EraseSmartBufferRange) {
116 SmartBuffer buf({1, 2, 3, 4, 5});
117 const std::vector<uint8_t> expected = {1, 5};
118 buf.Erase(1, 3);
119 EXPECT_EQ(buf.contents(), expected);
120}
121
122TEST(Resize, Shrink) {
123 SmartBuffer buf(5);
124 std::vector<uint8_t> contents = {1, 2, 3, 4, 5};
125 buf.Add(contents.data(), contents.size());
126 buf.Shrink(3);
127 std::vector<uint8_t> expected = {1, 2, 3};
128 EXPECT_EQ(expected, buf.contents());
129}
130
131} // namespace