|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.Json; |
| 5 | +using FluentAssertions; |
| 6 | +using FsCheck; |
| 7 | +using FsCheck.Xunit; |
| 8 | +using Microsoft.OneFuzz.Service; |
| 9 | +using Microsoft.OneFuzz.Service.OneFuzzLib.Orm; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | + |
| 13 | +namespace Tests; |
| 14 | + |
| 15 | +public class EventExportConverterTests { |
| 16 | + enum Color { |
| 17 | + Red, |
| 18 | + Blue |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void BaseTypesAreBounded() { |
| 23 | + var a = new { |
| 24 | + guid = Guid.NewGuid(), |
| 25 | + date = new DateTime(), |
| 26 | + en = Color.Red, |
| 27 | + b = 1, |
| 28 | + boo = false, |
| 29 | + flo = float.Pi, |
| 30 | + doub = double.Tau, |
| 31 | + lon = long.MinValue, |
| 32 | + cha = 'a' |
| 33 | + }; |
| 34 | + |
| 35 | + a.GetType().GetProperties().All(p => BoundedSerializer.HasBoundedSerialization(p)).Should().BeTrue(); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void StringIsNotBounded() { |
| 40 | + var a = new { |
| 41 | + bad = "this is not bounded" |
| 42 | + }; |
| 43 | + |
| 44 | + BoundedSerializer.HasBoundedSerialization(a.GetType().GetProperty("bad")!).Should().BeFalse(); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void ValidatedStringIsBounded() { |
| 49 | + var a = new { |
| 50 | + scalesetid = ScalesetId.Parse("abc-123") |
| 51 | + }; |
| 52 | + |
| 53 | + BoundedSerializer.HasBoundedSerialization(a.GetType().GetProperty("scalesetid")!).Should().BeTrue(); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void ComplexObjectsAreSerialized() { |
| 58 | + var randomGuid = Guid.NewGuid(); |
| 59 | + var a = new DownloadableEventMessage( |
| 60 | + randomGuid, |
| 61 | + EventType.CrashReported, |
| 62 | + new EventCrashReported( |
| 63 | + new Report( |
| 64 | + "https://example.com", |
| 65 | + null, |
| 66 | + "target.exe", |
| 67 | + "crash", |
| 68 | + string.Empty, |
| 69 | + new List<string> { "this", "is", "a", "stacktrace" }, |
| 70 | + string.Empty, |
| 71 | + string.Empty, |
| 72 | + null, |
| 73 | + Guid.NewGuid(), |
| 74 | + Guid.NewGuid(), |
| 75 | + null, |
| 76 | + null, |
| 77 | + null, |
| 78 | + null, |
| 79 | + null, |
| 80 | + null, |
| 81 | + null, |
| 82 | + null, |
| 83 | + null, |
| 84 | + null, |
| 85 | + null, |
| 86 | + null |
| 87 | + ), |
| 88 | + Container.Parse("this-is-a-container"), |
| 89 | + "crash-abc123", |
| 90 | + null |
| 91 | + ), |
| 92 | + Guid.NewGuid(), |
| 93 | + "onefuzz", |
| 94 | + DateTime.Now, |
| 95 | + new Uri("https://example.com"), |
| 96 | + null |
| 97 | + ); |
| 98 | + var serializerOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()); |
| 99 | + serializerOptions.Converters.Add(new EventExportConverter<DownloadableEventMessage>()); |
| 100 | + |
| 101 | + var serialized = JsonSerializer.Serialize(a, serializerOptions); |
| 102 | + |
| 103 | + serialized.Should().NotBeNullOrEmpty(); |
| 104 | + serialized.Should().NotContain("stacktrace"); // List<string> is not serialized |
| 105 | + serialized.Should().NotContain("crash-abc123"); // string is not serialized |
| 106 | + serialized.Should().Contain("this-is-a-container"); // ValidatedString is serialized |
| 107 | + serialized.Should().Contain("crash_reported"); // Enum is serialized |
| 108 | + serialized.Should().Contain(DateTime.Now.Year.ToString()); // DateTime is serialized |
| 109 | + serialized.Should().Contain(randomGuid.ToString()); // Guid id serialized |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void TestWebhookMessage() { |
| 114 | + var a = new WebhookMessageEventGrid( |
| 115 | + "2.0.0", |
| 116 | + "eventsubject", |
| 117 | + EventType.JobCreated, |
| 118 | + DateTime.Now, |
| 119 | + Guid.NewGuid(), |
| 120 | + new WebhookMessage( |
| 121 | + Guid.NewGuid(), |
| 122 | + EventType.JobCreated, |
| 123 | + new EventJobCreated( |
| 124 | + Guid.NewGuid(), |
| 125 | + new JobConfig("some project", "some name", "some build", 1, "some logs"), |
| 126 | + null, |
| 127 | + "8.0"), |
| 128 | + Guid.NewGuid(), |
| 129 | + "onefuzz", |
| 130 | + Guid.NewGuid(), |
| 131 | + DateTime.Now, |
| 132 | + new Uri("https://example.com") |
| 133 | + ) |
| 134 | + ); |
| 135 | + |
| 136 | + var serializerOptions = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()); |
| 137 | + serializerOptions.Converters.Add(new EventExportConverter<WebhookMessage>()); |
| 138 | + |
| 139 | + var serialized = JsonSerializer.Serialize(a, serializerOptions); |
| 140 | + |
| 141 | + serialized.Should().Contain("eventsubject"); |
| 142 | + serialized.Should().NotContain("some project"); |
| 143 | + } |
| 144 | + |
| 145 | + public class EventExportConverterSerializationTests { |
| 146 | + private readonly JsonSerializerOptions _opts = new JsonSerializerOptions(EntityConverter.GetJsonSerializerOptions()); |
| 147 | + public EventExportConverterSerializationTests() { |
| 148 | + _ = Arb.Register<Arbitraries>(); |
| 149 | + _opts.Converters.Add(new EventExportConverter<DownloadableEventMessage>()); |
| 150 | + } |
| 151 | + |
| 152 | + void Test<T>(T v) { |
| 153 | + // TODO: Try cloning/creating a new serializer options from the existing one? |
| 154 | + var serialized = JsonSerializer.Serialize(v, _opts); |
| 155 | + var _ = JsonSerializer.Deserialize<dynamic>(serialized); |
| 156 | + } |
| 157 | + |
| 158 | + [Property] |
| 159 | + public void EventNodeHeartbeat(EventNodeHeartbeat e) => Test(e); |
| 160 | + |
| 161 | + |
| 162 | + [Property] |
| 163 | + public void EventTaskHeartbeat(EventTaskHeartbeat e) => Test(e); |
| 164 | + |
| 165 | + [Property] |
| 166 | + public void EventTaskStopped(EventTaskStopped e) => Test(e); |
| 167 | + |
| 168 | + [Property] |
| 169 | + public void EventInstanceConfigUpdated(EventInstanceConfigUpdated e) => Test(e); |
| 170 | + |
| 171 | + [Property] |
| 172 | + public void EventProxyCreated(EventProxyCreated e) => Test(e); |
| 173 | + |
| 174 | + [Property] |
| 175 | + public void EventProxyDeleted(EventProxyDeleted e) => Test(e); |
| 176 | + |
| 177 | + [Property] |
| 178 | + public void EventProxyFailed(EventProxyFailed e) => Test(e); |
| 179 | + |
| 180 | + [Property] |
| 181 | + public void EventProxyStateUpdated(EventProxyStateUpdated e) => Test(e); |
| 182 | + |
| 183 | + |
| 184 | + [Property] |
| 185 | + public void EventCrashReported(EventCrashReported e) => Test(e); |
| 186 | + |
| 187 | + |
| 188 | + [Property] |
| 189 | + public void EventRegressionReported(EventRegressionReported e) => Test(e); |
| 190 | + |
| 191 | + |
| 192 | + [Property] |
| 193 | + public void EventFileAdded(EventFileAdded e) => Test(e); |
| 194 | + |
| 195 | + [Property] |
| 196 | + public void EventTaskFailed(EventTaskFailed e) => Test(e); |
| 197 | + |
| 198 | + [Property] |
| 199 | + public void EventTaskStateUpdated(EventTaskStateUpdated e) => Test(e); |
| 200 | + |
| 201 | + [Property] |
| 202 | + public void EventScalesetFailed(EventScalesetFailed e) => Test(e); |
| 203 | + |
| 204 | + [Property] |
| 205 | + public void EventScalesetResizeScheduled(EventScalesetResizeScheduled e) => Test(e); |
| 206 | + |
| 207 | + [Property] |
| 208 | + public void EventScalesetStateUpdated(EventScalesetStateUpdated e) => Test(e); |
| 209 | + |
| 210 | + [Property] |
| 211 | + public void EventNodeDeleted(EventNodeDeleted e) => Test(e); |
| 212 | + |
| 213 | + [Property] |
| 214 | + public void EventNodeCreated(EventNodeCreated e) => Test(e); |
| 215 | + |
| 216 | + [Property] |
| 217 | + public void EventMessage(DownloadableEventMessage e) => Test(e); |
| 218 | + } |
| 219 | +} |
0 commit comments