forked from microsoft/SEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.cpp
263 lines (237 loc) · 10.2 KB
/
serialization.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/serialization.h"
#include "seal/util/defines.h"
#include <fstream>
#include <functional>
#include <sstream>
#include <string>
#include "gtest/gtest.h"
using namespace seal;
using namespace std;
namespace sealtest
{
namespace
{
struct test_struct
{
int a;
int b;
double c;
void save_members(ostream &stream)
{
stream.write(reinterpret_cast<const char *>(&a), sizeof(int));
stream.write(reinterpret_cast<const char *>(&b), sizeof(int));
stream.write(reinterpret_cast<const char *>(&c), sizeof(double));
}
void load_members(istream &stream)
{
stream.read(reinterpret_cast<char *>(&a), sizeof(int));
stream.read(reinterpret_cast<char *>(&b), sizeof(int));
stream.read(reinterpret_cast<char *>(&c), sizeof(double));
}
streamoff save_size(compr_mode_type compr_mode) const
{
size_t members_size = Serialization::ComprSizeEstimate(sizeof(test_struct), compr_mode);
return static_cast<streamoff>(sizeof(Serialization::SEALHeader) + members_size);
}
};
} // namespace
TEST(SerializationTest, IsValidHeader)
{
ASSERT_EQ(sizeof(Serialization::SEALHeader), Serialization::seal_header_size);
Serialization::SEALHeader header;
ASSERT_TRUE(Serialization::IsValidHeader(header));
#ifdef SEAL_USE_ZLIB
header.compr_mode = compr_mode_type::zlib;
ASSERT_TRUE(Serialization::IsValidHeader(header));
#endif
#ifdef SEAL_USE_ZSTD
header.compr_mode = compr_mode_type::zstd;
ASSERT_TRUE(Serialization::IsValidHeader(header));
#endif
Serialization::SEALHeader invalid_header;
invalid_header.magic = 0x1212;
ASSERT_FALSE(Serialization::IsValidHeader(invalid_header));
invalid_header.magic = Serialization::seal_magic;
ASSERT_EQ(invalid_header.header_size, Serialization::seal_header_size);
invalid_header.version_major = 0x02;
ASSERT_FALSE(Serialization::IsValidHeader(invalid_header));
invalid_header.version_major = SEAL_VERSION_MAJOR;
invalid_header.compr_mode = (compr_mode_type)0x03;
ASSERT_FALSE(Serialization::IsValidHeader(invalid_header));
}
TEST(SerializationTest, SEALHeaderSaveLoad)
{
{
// Serialize to stream
Serialization::SEALHeader header, loaded_header;
header.compr_mode = Serialization::compr_mode_default;
header.size = 256;
stringstream stream;
Serialization::SaveHeader(header, stream);
ASSERT_TRUE(Serialization::IsValidHeader(header));
Serialization::LoadHeader(stream, loaded_header);
ASSERT_EQ(Serialization::seal_magic, loaded_header.magic);
ASSERT_EQ(Serialization::seal_header_size, loaded_header.header_size);
ASSERT_EQ(SEAL_VERSION_MAJOR, loaded_header.version_major);
ASSERT_EQ(SEAL_VERSION_MINOR, loaded_header.version_minor);
ASSERT_EQ(Serialization::compr_mode_default, loaded_header.compr_mode);
ASSERT_EQ(0x00, loaded_header.reserved);
ASSERT_EQ(256, loaded_header.size);
}
{
// Serialize to buffer
Serialization::SEALHeader header, loaded_header;
header.compr_mode = Serialization::compr_mode_default;
header.size = 256;
vector<seal_byte> buffer(16);
Serialization::SaveHeader(header, buffer.data(), buffer.size());
ASSERT_TRUE(Serialization::IsValidHeader(header));
Serialization::LoadHeader(buffer.data(), buffer.size(), loaded_header);
ASSERT_EQ(Serialization::seal_magic, loaded_header.magic);
ASSERT_EQ(Serialization::seal_header_size, loaded_header.header_size);
ASSERT_EQ(SEAL_VERSION_MAJOR, loaded_header.version_major);
ASSERT_EQ(SEAL_VERSION_MINOR, loaded_header.version_minor);
ASSERT_EQ(Serialization::compr_mode_default, loaded_header.compr_mode);
ASSERT_EQ(0x00, loaded_header.reserved);
ASSERT_EQ(256, loaded_header.size);
}
}
/*
TEST(SerializationTest, SEALHeaderUpgrade)
{
legacy_headers::SEALHeader_3_4 header_3_4;
header_3_4.compr_mode = Serialization::compr_mode_default;
header_3_4.size = 0xF3F3;
{
Serialization::SEALHeader header;
Serialization::LoadHeader(
reinterpret_cast<const seal_byte *>(&header_3_4), sizeof(legacy_headers::SEALHeader_3_4), header);
ASSERT_TRUE(Serialization::IsValidHeader(header));
ASSERT_EQ(header_3_4.compr_mode, header.compr_mode);
ASSERT_EQ(header_3_4.size, header.size);
}
{
Serialization::SEALHeader header;
Serialization::LoadHeader(
reinterpret_cast<const seal_byte *>(&header_3_4), sizeof(legacy_headers::SEALHeader_3_4), header,
false);
// No upgrade requested
ASSERT_FALSE(Serialization::IsValidHeader(header));
}
}
*/
TEST(SerializationTest, SaveLoadToStream)
{
test_struct st{ 3, ~0, 3.14159 }, st2;
using namespace placeholders;
stringstream stream;
auto out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::none), stream,
compr_mode_type::none, false);
auto in_size = Serialization::Load(bind(&test_struct::load_members, &st2, _1), stream, false);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st2.a);
ASSERT_EQ(st.b, st2.b);
ASSERT_EQ(st.c, st2.c);
#ifdef SEAL_USE_ZSTD
{
test_struct st3;
out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::zstd), stream,
compr_mode_type::zstd, false);
in_size = Serialization::Load(bind(&test_struct::load_members, &st3, _1), stream, false);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st3.a);
ASSERT_EQ(st.b, st3.b);
ASSERT_EQ(st.c, st3.c);
}
#endif
#ifdef SEAL_USE_ZLIB
{
test_struct st3;
out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::zlib), stream,
compr_mode_type::zlib, false);
in_size = Serialization::Load(bind(&test_struct::load_members, &st3, _1), stream, false);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st3.a);
ASSERT_EQ(st.b, st3.b);
ASSERT_EQ(st.c, st3.c);
}
#endif
}
TEST(SerializationTest, SaveLoadToBuffer)
{
test_struct st{ 3, ~0, 3.14159 }, st2;
using namespace placeholders;
constexpr size_t arr_size = 1024;
seal_byte buffer[arr_size]{};
stringstream ss;
auto test_out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(Serialization::compr_mode_default), ss,
Serialization::compr_mode_default, false);
auto out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(Serialization::compr_mode_default), buffer,
arr_size, Serialization::compr_mode_default, false);
ASSERT_EQ(test_out_size, out_size);
for (size_t i = static_cast<size_t>(out_size); i < arr_size; i++)
{
ASSERT_TRUE(seal_byte{} == buffer[i]);
}
auto in_size = Serialization::Load(bind(&test_struct::load_members, &st2, _1), buffer, arr_size, false);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st2.a);
ASSERT_EQ(st.b, st2.b);
ASSERT_EQ(st.c, st2.c);
#ifdef SEAL_USE_ZSTD
{
// Reset buffer back to zero
memset(buffer, 0, arr_size);
test_struct st3;
ss.seekp(0);
test_out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::zstd), ss,
compr_mode_type::zstd, false);
out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::zstd), buffer, arr_size,
compr_mode_type::zstd, false);
ASSERT_EQ(test_out_size, out_size);
for (size_t i = static_cast<size_t>(out_size); i < arr_size; i++)
{
ASSERT_EQ(seal_byte{}, buffer[i]);
}
in_size = Serialization::Load(bind(&test_struct::load_members, &st3, _1), buffer, arr_size, false);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st3.a);
ASSERT_EQ(st.b, st3.b);
ASSERT_EQ(st.c, st3.c);
}
#endif
#ifdef SEAL_USE_ZLIB
{
// Reset buffer back to zero
memset(buffer, 0, arr_size);
test_struct st3;
ss.seekp(0);
test_out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::zlib), ss,
compr_mode_type::zlib, false);
out_size = Serialization::Save(
bind(&test_struct::save_members, &st, _1), st.save_size(compr_mode_type::zlib), buffer, arr_size,
compr_mode_type::zlib, false);
ASSERT_EQ(test_out_size, out_size);
for (size_t i = static_cast<size_t>(out_size); i < arr_size; i++)
{
ASSERT_EQ(seal_byte{}, buffer[i]);
}
in_size = Serialization::Load(bind(&test_struct::load_members, &st3, _1), buffer, arr_size, false);
ASSERT_EQ(out_size, in_size);
ASSERT_EQ(st.a, st3.a);
ASSERT_EQ(st.b, st3.b);
ASSERT_EQ(st.c, st3.c);
}
#endif
}
} // namespace sealtest