forked from protobuf-net/protobuf-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuidTests.cs
198 lines (174 loc) · 6.71 KB
/
GuidTests.cs
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
using System;
using System.IO;
using NUnit.Framework;
using ProtoBuf;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Diagnostics;
namespace Examples
{
[ProtoContract]
class GuidData
{
[ProtoMember(1)]
public Guid Bar { get; set; }
}
[ProtoContract(UseProtoMembersOnly = true)]
[ProtoPartialMember(25, "GUID")]
public partial class User { }
public partial class User
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_GUID", DbType = "UniqueIdentifier", UpdateCheck = UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order = 26)]
public System.Guid GUID
{
get;
set;
}
}
[ProtoContract]
public class UserWithCrazyDefault
{
public UserWithCrazyDefault()
{
GUID = new Guid("01020304050607080102030405060708");
}
[ProtoMember(25), DefaultValue("01020304050607080102030405060708")]
public System.Guid GUID { get; set; }
}
[TestFixture]
public class GuidTests
{
public static int Measure<T>(T value)
{
using (var ms = new MemoryStream())
{
Serializer.Serialize<T>(ms, value);
return (int)ms.Length;
}
}
[Test]
public void TestPartialWithGuid()
{
var user = new User();
var clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(0, Measure(user));
user = new User { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(21, Measure(user));
Serializer.PrepareSerializer<User>();
user = new User();
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(0, Measure(user));
user = new User { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(21, Measure(user));
}
[Test]
public void TestGuidWithCrazyDefault()
{
var user = new UserWithCrazyDefault();
var clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(0, Measure(user));
user = new UserWithCrazyDefault { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(21, Measure(user));
user = new UserWithCrazyDefault { GUID = Guid.Empty };
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(3, Measure(user));
Serializer.PrepareSerializer<UserWithCrazyDefault>();
user = new UserWithCrazyDefault();
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(0, Measure(user));
user = new UserWithCrazyDefault { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(21, Measure(user));
user = new UserWithCrazyDefault { GUID = Guid.Empty };
clone = Serializer.DeepClone(user);
Assert.AreEqual(user.GUID, clone.GUID);
Assert.AreEqual(3, Measure(user));
}
[Test]
public void TestGuidLayout()
{
var guid = new Guid("00112233445566778899AABBCCDDEEFF");
var msBlob = guid.ToByteArray();
var msHex = BitConverter.ToString(msBlob);
Assert.AreEqual("33-22-11-00-55-44-77-66-88-99-AA-BB-CC-DD-EE-FF", msHex);
var obj = new GuidLayout { Value = guid };
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, obj);
string hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);
// 0A = 1010 = field 1, length delimited (sub-object)
// 12 = length 18
// 09 = field 1, fixed-length 64 bit
// 33-22-11-00-55-44-77-66 = payload
// 11 = field 2, fixed-length 64 bit
// 88-99-AA-BB-CC-DD-EE-FF
Assert.AreEqual(
"0A-12-09-33-22-11-00-55-44-77-66-11-88-99-AA-BB-CC-DD-EE-FF",
hex
);
}
}
[ProtoContract]
public class GuidLayout
{
[ProtoMember(1)]
public Guid Value { get; set; }
}
[Test]
public void TestDeserializeEmptyWide()
{
GuidData data = Program.Build<GuidData>(
0x0A, 0x12, // prop 1, string:18
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //1:fixed64:0
0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //2:fixed64:0
);
Assert.AreEqual(Guid.Empty, data.Bar);
}
[Test]
public void TestDeserializeEmptyShort()
{
GuidData data = Program.Build<GuidData>(
0x0A, 0x00 // prop 1, string:0
);
Assert.AreEqual(Guid.Empty, data.Bar);
}
[Test]
public void TestEmptyGuid() {
GuidData foo = new GuidData { Bar = Guid.Empty };
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, foo);
Assert.AreEqual(0, ms.Length); // 1 tag, 1 length (0)
ms.Position = 0;
GuidData clone = Serializer.Deserialize<GuidData>(ms);
Assert.AreEqual(foo.Bar, clone.Bar);
}
}
[Test]
public void TestNonEmptyGuid()
{
GuidData foo = new GuidData { Bar = Guid.NewGuid() };
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, foo);
Assert.AreEqual(20, ms.Length);
ms.Position = 0;
GuidData clone = Serializer.Deserialize<GuidData>(ms);
Assert.AreEqual(foo.Bar, clone.Bar);
}
}
}
}