/* ======================================================================== * Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Text; namespace Opc.Ua.Fuzzing { /// /// Fuzzing code for OPC UA string parsers. /// public static partial class FuzzableCode { /// /// The NodeId parser fuzz target for afl-fuzz. /// public static void AflfuzzNodeIdParse(string input) { FuzzParser(() => _ = NodeId.Parse(input)); } /// /// The NodeId parser fuzz target for libfuzzer. /// public static void LibfuzzNodeIdParse(ReadOnlySpan input) { AflfuzzNodeIdParse(ToUtf8String(input)); } /// /// The ExpandedNodeId parser fuzz target for afl-fuzz. /// public static void AflfuzzExpandedNodeIdParse(string input) { FuzzParser(() => _ = ExpandedNodeId.Parse(input)); } /// /// The ExpandedNodeId parser fuzz target for libfuzzer. /// public static void LibfuzzExpandedNodeIdParse(ReadOnlySpan input) { AflfuzzExpandedNodeIdParse(ToUtf8String(input)); } /// /// The RelativePathFormatter parser fuzz target for afl-fuzz. /// public static void AflfuzzRelativePathFormatterParse(string input) { FuzzParser(() => _ = RelativePathFormatter.Parse(input)); } /// /// The RelativePathFormatter parser fuzz target for libfuzzer. /// public static void LibfuzzRelativePathFormatterParse(ReadOnlySpan input) { AflfuzzRelativePathFormatterParse(ToUtf8String(input)); } /// /// The QualifiedName parser fuzz target for afl-fuzz. /// public static void AflfuzzQualifiedNameParse(string input) { FuzzParser(() => _ = QualifiedName.Parse(input)); } /// /// The QualifiedName parser fuzz target for libfuzzer. /// public static void LibfuzzQualifiedNameParse(ReadOnlySpan input) { AflfuzzQualifiedNameParse(ToUtf8String(input)); } /// /// The NumericRange parser fuzz target for afl-fuzz. /// public static void AflfuzzNumericRangeParse(string input) { FuzzParser(() => _ = NumericRange.Parse(input)); } /// /// The NumericRange parser fuzz target for libfuzzer. /// public static void LibfuzzNumericRangeParse(ReadOnlySpan input) { AflfuzzNumericRangeParse(ToUtf8String(input)); } /// /// The Uuid round-trip fuzz target for afl-fuzz. /// public static void AflfuzzUuidRoundTrip(string input) { FuzzParser(() => { Uuid uuid = Uuid.Parse(input); Uuid roundTripped = Uuid.Parse(uuid.ToString()); if (!uuid.Equals(roundTripped)) { throw new InvalidOperationException("Uuid round-trip failed."); } }); } /// /// The Uuid round-trip fuzz target for libfuzzer. /// public static void LibfuzzUuidRoundTrip(ReadOnlySpan input) { AflfuzzUuidRoundTrip(ToUtf8String(input)); } private static void FuzzParser(Action parse) { try { parse(); } catch (ServiceResultException) { } catch (FormatException) { } catch (ArgumentException) { } } private static string ToUtf8String(ReadOnlySpan input) { #if NETFRAMEWORK return Encoding.UTF8.GetString(input.ToArray()); #else return Encoding.UTF8.GetString(input); #endif } } }