This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'user/noharper/unique-report-metric' of https://github.c…
…om/nharper285/onefuzz into user/noharper/unique-report-metric
- Loading branch information
Showing
12 changed files
with
384 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.OneFuzz.Service { | ||
public class RemoveUserInfo : JsonConverter<UserInfo> { | ||
public override UserInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { | ||
throw new NotSupportedException("reading UserInfo is not supported"); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, UserInfo value, JsonSerializerOptions options) { | ||
writer.WriteStartObject(); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <b>THIS IS A WRITE ONLY JSON CONVERTER</b> | ||
/// <br/> | ||
/// It should only be used when serializing events to be sent outside of the service | ||
/// </summary> | ||
public class EventExportConverter<T> : JsonConverter<T> | ||
where T : DownloadableEventMessage { | ||
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { | ||
throw new NotSupportedException("This converter should only be used when serializing event messages to sent outside of the service"); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) { | ||
BoundedSerializer.WriteInternal(writer, value, options); | ||
} | ||
} | ||
|
||
public class BoundedSerializer { | ||
private static HashSet<Type> boundedTypes = new HashSet<Type>{ | ||
typeof(Guid), | ||
typeof(DateTime), | ||
typeof(int), | ||
typeof(bool), | ||
typeof(float), | ||
typeof(double), | ||
typeof(long), | ||
typeof(char), | ||
typeof(Uri) | ||
}; | ||
|
||
public static void WriteInternal(Utf8JsonWriter writer, object type, JsonSerializerOptions options) { | ||
writer.WriteStartObject(); | ||
var properties = type.GetType().GetProperties(); | ||
foreach (var property in properties) { | ||
if (property.GetValue(type, null) == null | ||
|| typeof(IEnumerable).IsAssignableFrom(property.PropertyType) | ||
|| type.GetType() == property.PropertyType) { | ||
continue; | ||
} | ||
if (HasBoundedSerialization(property)) { | ||
var serialized = JsonSerializer.Serialize(property.GetValue(type, null), property.PropertyType, options); | ||
if (!string.IsNullOrEmpty(serialized)) { | ||
writer.WritePropertyName(property.Name); | ||
writer.WriteRawValue(serialized); | ||
} | ||
} else if (property.PropertyType.IsClass) { | ||
writer.WritePropertyName(property.Name); | ||
WriteInternal(writer, property.GetValue(type, null)!, options); | ||
} | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
public static bool HasBoundedSerialization(PropertyInfo propertyInfo) { | ||
return propertyInfo.PropertyType.IsEnum || | ||
boundedTypes.Contains(propertyInfo.PropertyType) || | ||
typeof(IValidatedString).IsAssignableFrom(propertyInfo.PropertyType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using FsCheck; | ||
using FsCheck.Xunit; | ||
using Microsoft.OneFuzz.Service; | ||
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm; | ||
using Xunit; | ||
|
||
|
||
namespace Tests; | ||
|
||
public class EventExportConverterTests { | ||
enum Color { | ||
Red, | ||
Blue | ||
} | ||
|
||
[Fact] | ||
public void BaseTypesAreBounded() { | ||
var a = new { | ||
guid = Guid.NewGuid(), | ||
date = new DateTime(), | ||
en = Color.Red, | ||
b = 1, | ||
boo = false, | ||
flo = float.Pi, | ||
doub = double.Tau, | ||
lon = long.MinValue, | ||
cha = 'a' | ||
}; | ||
|
||
a.GetType().GetProperties().All(p => BoundedSerializer.HasBoundedSerialization(p)).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void StringIsNotBounded() { | ||
var a = new { | ||
bad = "this is not bounded" | ||
}; | ||
|
||
BoundedSerializer.HasBoundedSerialization(a.GetType().GetProperty("bad")!).Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ValidatedStringIsBounded() { | ||
var a = new { | ||
scalesetid = ScalesetId.Parse("abc-123") | ||
}; | ||
|
||
BoundedSerializer.HasBoundedSerialization(a.GetType().GetProperty("scalesetid")!).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ComplexObjectsAreSerialized() { | ||
var randomGuid = Guid.NewGuid(); | ||
var a = new DownloadableEventMessage( | ||
randomGuid, | ||
EventType.CrashReported, | ||
new EventCrashReported( | ||
new Report( | ||
"https://example.com", | ||
null, | ||
"target.exe", | ||
"crash", | ||
string.Empty, | ||
new List<string> { "this", "is", "a", "stacktrace" }, | ||
string.Empty, | ||
string.Empty, | ||
null, | ||
Guid.NewGuid(), | ||
Guid.NewGuid(), | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null | ||
), | ||
Container.Parse("this-is-a-container"), | ||
"crash-abc123", | ||
null | ||
), | ||
Guid.NewGuid(), | ||
"onefuzz", | ||
DateTime.Now, | ||
new Uri("https://example.com"), | ||
null | ||
); | ||
var serializerOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()); | ||
serializerOptions.Converters.Add(new EventExportConverter<DownloadableEventMessage>()); | ||
|
||
var serialized = JsonSerializer.Serialize(a, serializerOptions); | ||
|
||
serialized.Should().NotBeNullOrEmpty(); | ||
serialized.Should().NotContain("stacktrace"); // List<string> is not serialized | ||
serialized.Should().NotContain("crash-abc123"); // string is not serialized | ||
serialized.Should().Contain("this-is-a-container"); // ValidatedString is serialized | ||
serialized.Should().Contain("crash_reported"); // Enum is serialized | ||
serialized.Should().Contain(DateTime.Now.Year.ToString()); // DateTime is serialized | ||
serialized.Should().Contain(randomGuid.ToString()); // Guid id serialized | ||
} | ||
|
||
[Fact] | ||
public void TestWebhookMessage() { | ||
var a = new WebhookMessageEventGrid( | ||
"2.0.0", | ||
"eventsubject", | ||
EventType.JobCreated, | ||
DateTime.Now, | ||
Guid.NewGuid(), | ||
new WebhookMessage( | ||
Guid.NewGuid(), | ||
EventType.JobCreated, | ||
new EventJobCreated( | ||
Guid.NewGuid(), | ||
new JobConfig("some project", "some name", "some build", 1, "some logs"), | ||
null, | ||
"8.0"), | ||
Guid.NewGuid(), | ||
"onefuzz", | ||
Guid.NewGuid(), | ||
DateTime.Now, | ||
new Uri("https://example.com") | ||
) | ||
); | ||
|
||
var serializerOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()); | ||
serializerOptions.Converters.Add(new EventExportConverter<WebhookMessage>()); | ||
|
||
var serialized = JsonSerializer.Serialize(a, serializerOptions); | ||
|
||
serialized.Should().Contain("eventsubject"); | ||
serialized.Should().NotContain("some project"); | ||
} | ||
|
||
public class EventExportConverterSerializationTests { | ||
private readonly JsonSerializerOptions _opts = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()); | ||
public EventExportConverterSerializationTests() { | ||
_ = Arb.Register<Arbitraries>(); | ||
_opts.Converters.Add(new EventExportConverter<DownloadableEventMessage>()); | ||
} | ||
|
||
void Test<T>(T v) { | ||
// TODO: Try cloning/creating a new serializer options from the existing one? | ||
var serialized = JsonSerializer.Serialize(v, _opts); | ||
var _ = JsonSerializer.Deserialize<dynamic>(serialized); | ||
} | ||
|
||
[Property] | ||
public void EventNodeHeartbeat(EventNodeHeartbeat e) => Test(e); | ||
|
||
|
||
[Property] | ||
public void EventTaskHeartbeat(EventTaskHeartbeat e) => Test(e); | ||
|
||
[Property] | ||
public void EventTaskStopped(EventTaskStopped e) => Test(e); | ||
|
||
[Property] | ||
public void EventInstanceConfigUpdated(EventInstanceConfigUpdated e) => Test(e); | ||
|
||
[Property] | ||
public void EventProxyCreated(EventProxyCreated e) => Test(e); | ||
|
||
[Property] | ||
public void EventProxyDeleted(EventProxyDeleted e) => Test(e); | ||
|
||
[Property] | ||
public void EventProxyFailed(EventProxyFailed e) => Test(e); | ||
|
||
[Property] | ||
public void EventProxyStateUpdated(EventProxyStateUpdated e) => Test(e); | ||
|
||
|
||
[Property] | ||
public void EventCrashReported(EventCrashReported e) => Test(e); | ||
|
||
|
||
[Property] | ||
public void EventRegressionReported(EventRegressionReported e) => Test(e); | ||
|
||
|
||
[Property] | ||
public void EventFileAdded(EventFileAdded e) => Test(e); | ||
|
||
[Property] | ||
public void EventTaskFailed(EventTaskFailed e) => Test(e); | ||
|
||
[Property] | ||
public void EventTaskStateUpdated(EventTaskStateUpdated e) => Test(e); | ||
|
||
[Property] | ||
public void EventScalesetFailed(EventScalesetFailed e) => Test(e); | ||
|
||
[Property] | ||
public void EventScalesetResizeScheduled(EventScalesetResizeScheduled e) => Test(e); | ||
|
||
[Property] | ||
public void EventScalesetStateUpdated(EventScalesetStateUpdated e) => Test(e); | ||
|
||
[Property] | ||
public void EventNodeDeleted(EventNodeDeleted e) => Test(e); | ||
|
||
[Property] | ||
public void EventNodeCreated(EventNodeCreated e) => Test(e); | ||
|
||
[Property] | ||
public void EventMessage(DownloadableEventMessage e) => Test(e); | ||
} | ||
} |
Oops, something went wrong.