-
Notifications
You must be signed in to change notification settings - Fork 65
/
Serializes.cs
256 lines (222 loc) · 7.84 KB
/
Serializes.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
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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.CommonSchema.Serialization;
using FluentAssertions;
using Xunit;
namespace Elastic.CommonSchema.Tests
{
public class Serializes
{
private readonly Log _log = new Log { Level = "debug" };
[Fact]
public void SerializesSomethingToString()
{
var b = new EcsDocument { Agent = new Agent { Name = "some-agent" }, Log = _log };
var serialized = b.Serialize();
serialized.Should().NotBeNullOrWhiteSpace();
serialized.Should().Contain("log.level\":\"debug\"");
var deserialized = EcsDocument.Deserialize(serialized);
deserialized.Agent.Should().NotBeNull();
deserialized.Agent.Name.Should().Be("some-agent");
deserialized.Log.Should().NotBeNull();
deserialized.Log.Level.Should().Be("debug");
}
[Fact]
public void SerializesNestedProperties()
{
var b = new EcsDocument { Http = new Http { RequestMethod = "GET" } };
var serialized = b.Serialize();
serialized.Should().NotBeNullOrWhiteSpace();
serialized.Should().Contain("\"request.method\":\"GET\"");
var deserialized = EcsDocument.Deserialize(serialized);
deserialized.Http.Should().NotBeNull();
deserialized.Http.RequestMethod.Should().Be("GET");
}
[Fact]
public void MetaDataKeysAreSerializedVerbatim()
{
var b = new EcsDocument
{
Metadata = new MetadataDictionary
{
["MessageTemplate"] = "some-template",
["WriteIO"] = "some-io",
["User_Id"] = 1,
["eventId"] = "some-id",
["rule"] = "some-rule",
}
};
var serialized = b.Serialize();
var deserialized = EcsSerializerFactory<EcsDocument>.Deserialize(serialized);
deserialized.Metadata.Should().ContainKey("MessageTemplate");
deserialized.Metadata.Should().ContainKey("WriteIO");
deserialized.Metadata.Should().ContainKey("User_Id");
deserialized.Metadata.Should().ContainKey("eventId");
deserialized.Metadata.Should().ContainKey("rule");
}
[JsonConverter(typeof(EcsDocumentJsonConverterFactory))]
public class SubclassedDocument : EcsDocument
{
[JsonPropertyName("agent2")]
public Agent Agent2 { get; set; }
protected override bool TryRead(string propertyName, out Type type)
{
type = propertyName switch
{
"agent2" => typeof(Agent),
_ => null
};
return type != null;
}
protected override bool ReceiveProperty(string propertyName, object value) =>
propertyName switch
{
"agent2" => null != (Agent2 = value as Agent),
_ => false
};
protected override void WriteAdditionalProperties(Action<string, object> write) => write("agent2", Agent2);
}
public class SubClassedAgent : Agent
{
[JsonPropertyName("another_name")]
public string Name2 { get; set; }
}
[Fact]
public void SerializesSubClass()
{
var b = new SubclassedDocument { Timestamp = DateTimeOffset.Now, Agent2 = new Agent { Name = "some-agent" }, Log = _log };
var serialized = b.Serialize();
serialized.Should().NotBeNullOrWhiteSpace();
serialized.Should().Contain("agent2");
serialized.Should().Contain("log.level\":\"debug\"");
var deserialized = EcsSerializerFactory<SubclassedDocument>.Deserialize(serialized);
deserialized.Log.Should().NotBeNull();
deserialized.Agent2.Should().NotBeNull();
}
[Fact]
public void SerializesSubClassProperties()
{
var b = new EcsDocument { Agent = new SubClassedAgent { Name2 = "some-agent" }, Log = _log };
var serialized = b.Serialize();
serialized.Should().NotBeNullOrWhiteSpace();
serialized.Should().Contain("some-agent");
serialized.Should().Contain("log.level\":\"debug\"");
serialized.Should().Contain("another_name\":\"some-agent\"");
var deserialized = EcsSerializerFactory<SubclassedDocument>.Deserialize(serialized);
deserialized.Log.Should().NotBeNull();
deserialized.Log.Level.Should().Be("debug");
deserialized.Agent.Should().NotBeNull();
}
[Fact]
public void SerializesSubClassPropertiesOnSubclass()
{
var b = new SubclassedDocument
{
Agent = new SubClassedAgent { Name2 = "some-agent", Id = "X" }, Agent2 = new Agent { Name = "some-agent" }, Log = _log
};
var serialized = b.Serialize();
serialized.Should().NotBeNullOrWhiteSpace();
serialized.Should().Contain("some-agent");
serialized.Should().Contain("log.level\":\"debug\"");
var deserialized = EcsSerializerFactory<SubclassedDocument>.Deserialize(serialized);
deserialized.Log.Should().NotBeNull();
deserialized.Log.Level.Should().Be("debug");
deserialized.Agent.Should().NotBeNull();
deserialized.Agent.Id.Should().Be("X");
deserialized.Agent2.Should().NotBeNull();
deserialized.Agent2.Name.Should().Be("some-agent");
}
[Fact]
public void SerializesDocumentInTheReadMe()
{
var ecsDocument = new EcsDocument
{
Timestamp = DateTimeOffset.Parse("2019-10-23T19:44:38.485Z"),
Dns = new Dns
{
Id = "23666",
OpCode = "QUERY",
Type = "answer",
QuestionName = "www.example.com",
QuestionType = "A",
QuestionClass = "IN",
QuestionRegisteredDomain = "example.com",
HeaderFlags = new[] { "RD", "RA" },
ResponseCode = "NOERROR",
ResolvedIp = new[] { "10.0.190.47", "10.0.190.117" },
Answers = new[]
{
new DnsAnswers
{
Data = "10.0.190.47",
Name = "www.example.com",
Type = "A",
Class = "IN",
Ttl = 59
},
new DnsAnswers
{
Data = "10.0.190.117",
Name = "www.example.com",
Type = "A",
Class = "IN",
Ttl = 59
}
}
},
Network = new Network
{
Type = "ipv4",
Transport = "udp",
Protocol = "dns",
Direction = "outbound",
CommunityId = "1:19beef+RWVW9+BEEF/Q45VFU+2Y=",
Bytes = 126
},
Source = new Source { Ip = "192.168.86.26", Port = 5785, Bytes = 31 },
Destination = new Destination { Ip = "8.8.4.4", Port = 53, Bytes = 95 },
Client = new Client { Ip = "192.168.86.26", Port = 5785, Bytes = 31 },
Server = new Server { Ip = "8.8.4.4", Port = 53, Bytes = 95 },
Event = new Event
{
Duration = 122433000,
Start = DateTimeOffset.Parse("2019-10-23T19:44:38.485Z"),
End = DateTimeOffset.Parse("2019-10-23T19:44:38.607Z"),
Kind = "event",
Category = new[] { "network_traffic" }
},
Ecs = new Ecs { Version = "1.2.0" },
Metadata = new MetadataDictionary { { "client", "ecs-dotnet" } }
};
var serialized = ecsDocument.Serialize();
serialized.Should().NotBeNullOrWhiteSpace();
serialized.Should().Contain("1:19beef+RWVW9+BEEF");
serialized.Should().Contain("192.168.86.26");
var deserialized = EcsSerializerFactory<SubclassedDocument>.Deserialize(serialized);
deserialized.Dns.Should().NotBeNull();
deserialized.Dns.Answers.Should().NotBeNull().And.HaveCount(2);
deserialized.Dns.Answers[0].Name.Should().NotBeNull().And.Be("www.example.com");
deserialized.Dns.Answers[1].Data.Should().NotBeNull().And.EndWith(".117");
deserialized.Metadata.Should().NotBeNull().And.HaveCount(1);
}
private enum MyEnum { One, Two, Three }
[Fact]
public void UseCustomSerializer()
{
var ecsDocument = new EcsDocument
{
Timestamp = DateTimeOffset.Parse("2024-05-27T23:56:15.785Z"),
Message = "Hello World!",
Metadata = new MetadataDictionary { { "MyEnum", MyEnum.Two } },
Ecs = new Ecs { Version = "8.11.0" }
};
var serializerOptions = new JsonSerializerOptions { Converters = { new JsonStringEnumConverter() } };
var json = JsonSerializer.Serialize(ecsDocument, serializerOptions);
json.Should().Be("{\"@timestamp\":\"2024-05-27T23:56:15.785+00:00\",\"message\":\"Hello World!\",\"ecs.version\":\"8.11.0\",\"metadata\":{\"MyEnum\":\"Two\"}}");
}
}
}