|
| 1 | +/* ======================================================================== |
| 2 | + * Copyright (c) 2005-2024 The OPC Foundation, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * OPC Foundation MIT License 1.00 |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person |
| 7 | + * obtaining a copy of this software and associated documentation |
| 8 | + * files (the "Software"), to deal in the Software without |
| 9 | + * restriction, including without limitation the rights to use, |
| 10 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the |
| 12 | + * Software is furnished to do so, subject to the following |
| 13 | + * conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be |
| 16 | + * included in all copies or substantial portions of the Software. |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + * |
| 26 | + * The complete license agreement can be found here: |
| 27 | + * http://opcfoundation.org/License/MIT/1.00/ |
| 28 | + * ======================================================================*/ |
| 29 | + |
| 30 | +using System.IO; |
| 31 | +using System.Text; |
| 32 | +using NUnit.Framework; |
| 33 | +using System.Linq; |
| 34 | +using System; |
| 35 | +using Opc.Ua.Tests; |
| 36 | +using System.Reflection; |
| 37 | + |
| 38 | +namespace Opc.Ua.Fuzzing |
| 39 | +{ |
| 40 | + |
| 41 | + [TestFixture] |
| 42 | + public class EncoderTests |
| 43 | + { |
| 44 | + #region DataPointSources |
| 45 | + public static readonly TestcaseAsset[] GoodTestcases = new AssetCollection<TestcaseAsset>(TestUtils.EnumerateTestAssets("Testcases", "*.*")).ToArray(); |
| 46 | + public static readonly TestcaseAsset[] CrashAssets = new AssetCollection<TestcaseAsset>(TestUtils.EnumerateTestAssets("Assets", "crash*.*")).ToArray(); |
| 47 | + public static readonly TestcaseAsset[] TimeoutAssets = new AssetCollection<TestcaseAsset>(TestUtils.EnumerateTestAssets("Assets", "timeout*.*")).ToArray(); |
| 48 | + public static readonly TestcaseAsset[] SlowAssets = new AssetCollection<TestcaseAsset>(TestUtils.EnumerateTestAssets("Assets", "slow*.*")).ToArray(); |
| 49 | + |
| 50 | + [DatapointSource] |
| 51 | + public static readonly string[] TestcaseEncoderSuffixes = new string[] { ".Binary", ".Json", ".Xml" }; |
| 52 | + |
| 53 | + [DatapointSource] |
| 54 | + public static readonly FuzzTargetFunction[] FuzzableFunctions = typeof(FuzzableCode).GetMethods(BindingFlags.Static | BindingFlags.Public) |
| 55 | + .Where(f => f.GetParameters().Length == 1) |
| 56 | + .Select(f => new FuzzTargetFunction(f)).ToArray(); |
| 57 | + #endregion |
| 58 | + |
| 59 | + [Theory] |
| 60 | + public void FuzzGoodTestcases( |
| 61 | + FuzzTargetFunction fuzzableCode, |
| 62 | + [ValueSource(nameof(GoodTestcases))] TestcaseAsset messageEncoder) |
| 63 | + { |
| 64 | + FuzzTarget(fuzzableCode, messageEncoder.Testcase); |
| 65 | + } |
| 66 | + |
| 67 | + [Theory] |
| 68 | + public void FuzzCrashAssets(FuzzTargetFunction fuzzableCode) |
| 69 | + { |
| 70 | + // note: too many crash files can take forever to create |
| 71 | + // all permutations with nunit, so just run all in one batch |
| 72 | + foreach (var messageEncoder in CrashAssets) |
| 73 | + { |
| 74 | + FuzzTarget(fuzzableCode, messageEncoder.Testcase); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [CancelAfter(1000)] |
| 80 | + public void FuzzTimeoutAssets( |
| 81 | + FuzzTargetFunction fuzzableCode, |
| 82 | + [ValueSource(nameof(TimeoutAssets))] TestcaseAsset messageEncoder) |
| 83 | + { |
| 84 | + FuzzTarget(fuzzableCode, messageEncoder.Testcase); |
| 85 | + } |
| 86 | + |
| 87 | + [Theory] |
| 88 | + [CancelAfter(1000)] |
| 89 | + public void FuzzSlowAssets( |
| 90 | + FuzzTargetFunction fuzzableCode, |
| 91 | + [ValueSource(nameof(SlowAssets))] TestcaseAsset messageEncoder) |
| 92 | + { |
| 93 | + FuzzTarget(fuzzableCode, messageEncoder.Testcase); |
| 94 | + } |
| 95 | + |
| 96 | + delegate void LibFuzzTemplate(ReadOnlySpan<byte> span); |
| 97 | + |
| 98 | + private void FuzzTarget(FuzzTargetFunction fuzzableCode, byte[] blob) |
| 99 | + { |
| 100 | + var parameters = fuzzableCode.MethodInfo.GetParameters(); |
| 101 | + if (parameters.Length != 1) |
| 102 | + { |
| 103 | + throw new InvalidOperationException("Fuzzable function must have exactly one parameter."); |
| 104 | + } |
| 105 | + if (parameters[0].ParameterType == typeof(string)) |
| 106 | + { |
| 107 | + string text = Encoding.UTF8.GetString(blob); |
| 108 | + _ = fuzzableCode.MethodInfo.Invoke(null, new object[] { text }); |
| 109 | + } |
| 110 | + else if (typeof(Stream).IsAssignableFrom(parameters[0].ParameterType)) |
| 111 | + { |
| 112 | + using (var stream = new MemoryStream(blob)) |
| 113 | + { |
| 114 | + _ = fuzzableCode.MethodInfo.Invoke(null, new object[] { stream }); |
| 115 | + } |
| 116 | + } |
| 117 | + else if (parameters[0].ParameterType == typeof(ReadOnlySpan<byte>)) |
| 118 | + { |
| 119 | + var span = new ReadOnlySpan<byte>(blob); |
| 120 | + LibFuzzTemplate fuzzFunction = (LibFuzzTemplate)fuzzableCode.MethodInfo.CreateDelegate(typeof(LibFuzzTemplate)); |
| 121 | + fuzzFunction(span); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// A Testcase as test asset. |
| 128 | + /// </summary> |
| 129 | + public class TestcaseAsset : IAsset, IFormattable |
| 130 | + { |
| 131 | + public TestcaseAsset() { } |
| 132 | + |
| 133 | + public string Path { get; private set; } |
| 134 | + public byte[] Testcase { get; private set; } |
| 135 | + |
| 136 | + public void Initialize(byte[] blob, string path) |
| 137 | + { |
| 138 | + Path = path; |
| 139 | + Testcase = blob; |
| 140 | + } |
| 141 | + |
| 142 | + public string ToString(string format, IFormatProvider formatProvider) |
| 143 | + { |
| 144 | + var file = System.IO.Path.GetFileName(Path); |
| 145 | + return $"{file}"; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// A Testcase as test asset. |
| 151 | + /// </summary> |
| 152 | + public class FuzzTargetFunction : IFormattable |
| 153 | + { |
| 154 | + public FuzzTargetFunction(MethodInfo methodInfo) |
| 155 | + { |
| 156 | + MethodInfo = methodInfo; |
| 157 | + } |
| 158 | + |
| 159 | + public MethodInfo MethodInfo { get; private set; } |
| 160 | + |
| 161 | + public string ToString(string format, IFormatProvider formatProvider) |
| 162 | + { |
| 163 | + var name = MethodInfo.Name; |
| 164 | + return $"{name}"; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments