예제 #1
0
        public void LetsFuzz()
        {
            // if we don't throw any exceptions and generate a valid object the the test is successful

            var libraryHandle = LibraryLoader.LoadAndGetHandle();
            var wafNative     = new WafNative(libraryHandle);
            var encoder       = new AppSec.Waf.Encoder(wafNative);

            var jsonGenerator = new JsonGenerator();

            var errorOccured = false;

            for (int i = 0; i < 100; i++)
            {
                var buffer = jsonGenerator.GenerateJsonBuffer();
                try
                {
                    using var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count, false);
                    using var streamReader = new StreamReader(memoryStream);
                    using var jsonReader   = new JsonTextReader(streamReader);
                    var root = JToken.ReadFrom(jsonReader);

                    var l = new List <Obj>();
                    using var result = encoder.Encode(root, l);

                    // check the object is valid
                    Assert.NotEqual(ObjType.Invalid, result.ArgsType);

                    l.ForEach(x => x.Dispose());
                }
                catch (Exception ex)
                {
                    errorOccured = true;

                    _outputHelper.WriteLine($"Error occured on run '{i}' parsing json: {ex}");
                    _outputHelper.WriteLine("Json causing the error was:");
                    ViewJson(buffer);
                }
            }

            Assert.False(errorOccured);
        }