Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AllowPrivateTest.cs « ShareTests « Tests « Scripts « Assets « MessagePack.UnityClient « src - github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c418e7ea4f54df6d4864fcc937aed7c7e00e733 (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
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !ENABLE_IL2CPP

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MessagePack.Resolvers;
using MsgPack.Serialization;
using Xunit;

#pragma warning disable SA1300 // Element should begin with uppercase letter

namespace MessagePack.Tests
{
    public class AllowPrivateTest
    {
        [MessagePackObject]
        public class HasPrivate
        {
            [Key(0)]
            private int privateKey;

            [Key(1)]
            public int PublicKey { get; set; }

            [Key(2)]
            private string privateKeyS { get; set; }

            [Key(3)]
            public string PublicKeyS { get; set; }

            public void SetPrivate(int p1, string p2)
            {
                this.privateKey = p1;
                this.privateKeyS = p2;
            }

            public int GetPrivateInt()
            {
                return this.privateKey;
            }

            public string GetPrivateStr()
            {
                return this.privateKeyS;
            }
        }

        [MessagePackObject]
        public struct HasPrivateStruct
        {
            [Key(0)]
            private int privateKey;

            [Key(1)]
            public int PublicKey { get; set; }

            [Key(2)]
            private string privateKeyS { get; set; }

            [Key(3)]
            public string PublicKeyS { get; set; }

            public void SetPrivate(int p1, string p2)
            {
                this.privateKey = p1;
                this.privateKeyS = p2;
            }

            public int GetPrivateInt()
            {
                return this.privateKey;
            }

            public string GetPrivateStr()
            {
                return this.privateKeyS;
            }
        }

        [MessagePackObject(true)]
        public class HasPrivateStringKey
        {
            private int privateKey;

            public int PublicKey { get; set; }

            private string privateKeyS { get; set; }

            public string PublicKeyS { get; set; }

            public void SetPrivate(int p1, string p2)
            {
                this.privateKey = p1;
                this.privateKeyS = p2;
            }

            public int GetPrivateInt()
            {
                return this.privateKey;
            }

            public string GetPrivateStr()
            {
                return this.privateKeyS;
            }
        }

        public class HasPrivateContractless
        {
            private int privateKey;

            public int PublicKey { get; set; }

            private string privateKeyS { get; set; }

            public string PublicKeyS { get; set; }

            public void SetPrivate(int p1, string p2)
            {
                this.privateKey = p1;
                this.privateKeyS = p2;
            }

            public int GetPrivateInt()
            {
                return this.privateKey;
            }

            public string GetPrivateStr()
            {
                return this.privateKeyS;
            }
        }

        [MessagePackObject]
        public struct EmptyConstructorStruct
        {
            [Key(0)]
            public int X;
        }

        internal enum InternalEnum
        {
            One,
            Two,
        }

        [MessagePackObject]
        internal class InternalClass
        {
            [Key(0)]
            internal InternalEnum EnumProperty { get; set; }
        }

#if !ENABLE_IL2CPP

        [MessagePackObject]
        public class ImmutablePrivateClass
        {
            [Key(0)]
            private int x;

            [Key(1)]
            private int y;

            [SerializationConstructor]
            private ImmutablePrivateClass(int x, int y)
            {
                this.x = x;
                this.y = y;
                this.CreatedUsingPrivateCtor = true;
            }

            public ImmutablePrivateClass(int x, int y, bool dummy)
            {
                this.x = x;
                this.y = y;
                this.CreatedUsingPrivateCtor = false;
            }

            [IgnoreMember]
            public int X => this.x;

            [IgnoreMember]
            public int Y => this.y;

            [IgnoreMember]
            public bool CreatedUsingPrivateCtor { get; }
        }

        [MessagePackObject]
        public class CompletelyPrivateConstructor
        {
            [Key(0)]
            private int x;

            [Key(1)]
            private int y;

            private CompletelyPrivateConstructor(int x, int y)
            {
                this.x = x;
                this.y = y;
            }

            public static CompletelyPrivateConstructor Create(int x, int y)
            {
                return new CompletelyPrivateConstructor(x, y);
            }

            [IgnoreMember]
            public int X => this.x;

            [IgnoreMember]
            public int Y => this.y;
        }

#endif

        [Fact]
        public void AllowPrivate()
        {
            {
                var p = new HasPrivate { PublicKey = 100, PublicKeyS = "foo" };
                p.SetPrivate(99, "bar");

                var bin = MessagePackSerializer.Serialize(p, StandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("[99,100,\"bar\",\"foo\"]");

                HasPrivate r2 = MessagePackSerializer.Deserialize<HasPrivate>(bin, StandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }

            {
                var p = new HasPrivateStruct { PublicKey = 100, PublicKeyS = "foo" };
                p.SetPrivate(99, "bar");

                var bin = MessagePackSerializer.Serialize(p, StandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("[99,100,\"bar\",\"foo\"]");

                HasPrivate r2 = MessagePackSerializer.Deserialize<HasPrivate>(bin, StandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }

            {
                var p = new HasPrivateStringKey { PublicKey = 100, PublicKeyS = "foo" };
                p.SetPrivate(99, "bar");

                var bin = MessagePackSerializer.Serialize(p, StandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("{\"PublicKey\":100,\"privateKeyS\":\"bar\",\"PublicKeyS\":\"foo\",\"privateKey\":99}");

                HasPrivateStringKey r2 = MessagePackSerializer.Deserialize<HasPrivateStringKey>(bin, StandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }

            {
                var p = new HasPrivateContractless { PublicKey = 100, PublicKeyS = "foo" };
                p.SetPrivate(99, "bar");

                var bin = MessagePackSerializer.Serialize(p, ContractlessStandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("{\"PublicKey\":100,\"privateKeyS\":\"bar\",\"PublicKeyS\":\"foo\",\"privateKey\":99}");

                HasPrivateContractless r2 = MessagePackSerializer.Deserialize<HasPrivateContractless>(bin, ContractlessStandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }
        }

        [Fact]
        public void Empty()
        {
            var x = MessagePackSerializer.Serialize(new EmptyConstructorStruct { X = 99 }, StandardResolverAllowPrivate.Options);
            MessagePackSerializer.Deserialize<EmptyConstructorStruct>(x, StandardResolverAllowPrivate.Options).X.Is(99);
        }

        [Fact]
        public void InternalClassWithInternalEnum()
        {
            InternalClass expected = new InternalClass
            {
                EnumProperty = InternalEnum.Two,
            };

            byte[] bytes = MessagePackSerializer.Serialize(expected, StandardResolverAllowPrivate.Options);
            InternalClass actual = MessagePackSerializer.Deserialize<InternalClass>(bytes, StandardResolverAllowPrivate.Options);
            Assert.Equal(expected.EnumProperty, actual.EnumProperty);
        }

#if !ENABLE_IL2CPP

        [Fact]
        public void PrivateConstructor()
        {
            var p1 = new ImmutablePrivateClass(10, 20, dummy: false);
            var bin = MessagePackSerializer.Serialize(p1, StandardResolverAllowPrivate.Options);
            var p2 = MessagePackSerializer.Deserialize<ImmutablePrivateClass>(bin, StandardResolverAllowPrivate.Options);

            Assert.Equal(p1.X, p2.X);
            Assert.Equal(p1.Y, p2.Y);
            Assert.False(p1.CreatedUsingPrivateCtor);
            Assert.True(p2.CreatedUsingPrivateCtor);
        }

        [Fact]
        public void PrivateConstructor2()
        {
            var p1 = CompletelyPrivateConstructor.Create(10, 20);
            var bin = MessagePackSerializer.Serialize(p1, StandardResolverAllowPrivate.Options);
            var p2 = MessagePackSerializer.Deserialize<CompletelyPrivateConstructor>(bin, StandardResolverAllowPrivate.Options);

            Assert.Equal(p1.X, p2.X);
            Assert.Equal(p1.Y, p2.Y);
        }
#endif
    }
}

#endif