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
cc / paint / paint_op_writer_reader_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "cc/paint/paint_op_reader.h"
#include "cc/paint/paint_op_writer.h"
#include "cc/test/test_options_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
TEST(PaintOpWriterReaderTest, SizeT) {
static_assert(PaintOpWriter::SerializedSize<int>() == 4u);
static_assert(PaintOpWriter::SerializedSize<size_t>() == 8u);
static_assert(PaintOpWriter::SerializedSize(static_cast<size_t>(0u)) == 8u);
char buffer[128];
TestOptionsProvider options_provider;
memset(buffer, 0xa5, std::size(buffer));
PaintOpWriter writer(buffer, std::size(buffer),
options_provider.serialize_options(),
/*enable_security_constraints*/ true);
int i = 0x5555aaaa;
size_t s1 = static_cast<size_t>(0x123456789abcdef0L);
size_t s2 = static_cast<size_t>(0xfedcba9876543210L);
writer.WriteSize(s1);
writer.Write(i);
writer.WriteSize(s2);
EXPECT_EQ(20u, writer.size());
PaintOpReader reader(buffer, writer.size(),
options_provider.deserialize_options(),
/*enable_security_constraints*/ true);
int read_i;
size_t read_s1, read_s2;
reader.ReadSize(&read_s1);
reader.Read(&read_i);
reader.ReadSize(&read_s2);
EXPECT_EQ(i, read_i);
EXPECT_EQ(s1, read_s1);
EXPECT_EQ(s2, read_s2);
EXPECT_TRUE(reader.valid());
EXPECT_EQ(0u, reader.remaining_bytes());
reader.ReadSize(&read_s2);
EXPECT_FALSE(reader.valid());
}
TEST(PaintOpWriterReaderTest, Vector) {
char buffer[128];
TestOptionsProvider options_provider;
memset(buffer, 0xa5, std::size(buffer));
PaintOpWriter writer(buffer, std::size(buffer),
options_provider.serialize_options(),
/*enable_security_constraints*/ true);
writer.Write(std::vector<float>{});
EXPECT_EQ(writer.size(), 8u);
writer.Write(std::vector<float>{1, 2});
EXPECT_EQ(writer.size(), 24u);
writer.Write(std::vector<uint32_t>{1, 2, 3});
EXPECT_EQ(writer.size(), 44u);
PaintOpReader reader(buffer, writer.size(),
options_provider.deserialize_options(),
/*enable_security_constraints*/ true);
std::vector<float> float_vec;
reader.Read(float_vec);
EXPECT_EQ(float_vec, std::vector<float>{});
reader.Read(float_vec);
EXPECT_EQ(float_vec, std::vector<float>({1, 2}));
std::vector<uint32_t> uint_vec;
reader.Read(uint_vec);
EXPECT_EQ(uint_vec, std::vector<uint32_t>({1, 2, 3}));
}
TEST(PaintOpWriterReaderTest, SkString) {
char buffer[128];
TestOptionsProvider options_provider;
memset(buffer, 0xa5, std::size(buffer));
PaintOpWriter writer(buffer, std::size(buffer),
options_provider.serialize_options(),
/*enable_security_constraints=*/true);
const SkString original("test string");
writer.Write(original);
// 8 bytes for the `original.size()` (size_t), 11 bytes for the string, but
// aligned to 12 bytes.
EXPECT_EQ(writer.size(), 20u);
PaintOpReader reader(buffer, writer.size(),
options_provider.deserialize_options(),
/*enable_security_constraints=*/true);
SkString deseralized;
reader.Read(&deseralized);
EXPECT_EQ(deseralized, original);
}
TEST(PaintOpWriterReaderTest, EmptySkString) {
char buffer[128];
TestOptionsProvider options_provider;
memset(buffer, 0xa5, std::size(buffer));
PaintOpWriter writer(buffer, std::size(buffer),
options_provider.serialize_options(),
/*enable_security_constraints=*/true);
const SkString original;
writer.Write(original);
// 8 bytes for size_t 0.
EXPECT_EQ(writer.size(), 8u);
PaintOpReader reader(buffer, writer.size(),
options_provider.deserialize_options(),
/*enable_security_constraints=*/true);
SkString deseralized;
reader.Read(&deseralized);
EXPECT_EQ(deseralized, original);
}
} // namespace cc