forked from microsoft/winget-cli-restsource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuzzer.cs
More file actions
51 lines (48 loc) · 1.75 KB
/
Copy pathFuzzer.cs
File metadata and controls
51 lines (48 loc) · 1.75 KB
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
// -----------------------------------------------------------------------
// <copyright file="Fuzzer.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------
namespace Microsoft.WinGet.RestSource.Fuzzing
{
using Microsoft.WinGet.RestSource.Utils.Common;
using Microsoft.WinGet.RestSource.Utils.Models.Schemas;
using Newtonsoft.Json;
/// <summary>
/// Fuzzer class.
/// </summary>
public static class Fuzzer
{
/// <summary>
/// Target fuzzing method.
/// </summary>
/// <param name="input">Fuzz test inputs.</param>
public static void FuzzTest(ReadOnlySpan<byte> input)
{
using var ms = new MemoryStream(input.ToArray());
FuzzTestOne<PackageManifest>(ms);
FuzzTestOne<Installer>(ms);
FuzzTestOne<Locale>(ms);
FuzzTestOne<ManifestSearchRequest>(ms);
FuzzTestOne<Package>(ms);
FuzzTestOne<Version>(ms);
}
private static void FuzzTestOne<T>(MemoryStream ms)
where T : class
{
try
{
var result = Task.Run(() => Parser.StreamParser<T>(ms)).Result;
}
catch (AggregateException ae)
{
// Getting the result of a Task can throw an Aggregate Exception.
// Handle json parsing exceptions as these are expected.
ae.Handle(ex =>
{
return ex is JsonReaderException || ex is JsonWriterException || ex is JsonSerializationException;
});
}
}
}
}