Init WFuzz state

This commit is contained in:
Jan Stárek
2019-10-09 13:24:01 +02:00
parent 7c3ed5ef0b
commit a5eb2a97e1
114 changed files with 6221 additions and 0 deletions

View File

@@ -0,0 +1,118 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using NUnit.Framework;
namespace Parser.Tests
{
public class AttributeParserTests
{
[TestCase(ParameterLocation.Cookie)]
[TestCase(ParameterLocation.Header)]
public void ParsingAttributeElsewhereThanInPathOrQueryShouldReturnNull(ParameterLocation parameterLocation)
{
OpenApiParameter parameter = new OpenApiParameter {In = parameterLocation};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.IsNull(parsedAttribute);
}
[Test]
public void ParsingAttributeWithNoTypeOrFormatShouldReturnNull()
{
OpenApiParameter parameter = new OpenApiParameter {Schema = new OpenApiSchema {Type = null, Format = null}};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.IsNull(parsedAttribute);
}
[Test]
public void ParsingPathAttributeWithValidContentExample()
{
string attributeContent = "test";
OpenApiParameter parameter = new OpenApiParameter
{
In = ParameterLocation.Path, Schema = new OpenApiSchema {Type = "string", Format = null},
Example = new OpenApiString(attributeContent)
};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.AreEqual(attributeContent, parsedAttribute.ExampleValue);
}
[Test]
public void ParsingQueryAttributeWithValidContentExample()
{
string attributeContent = "test";
OpenApiParameter parameter = new OpenApiParameter
{
In = ParameterLocation.Query,
Schema = new OpenApiSchema { Type = "string", Format = null },
Example = new OpenApiString(attributeContent)
};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.AreEqual(attributeContent, parsedAttribute.ExampleValue);
}
[Test]
public void ParsingAttributeWithValidContentExamples()
{
string attributeContent = "test";
OpenApiParameter parameter = new OpenApiParameter
{
In = ParameterLocation.Path, Schema = new OpenApiSchema {Type = "string", Format = null},
Examples = new Dictionary<string, OpenApiExample>
{
{ "testKey 1", new OpenApiExample {Value = new OpenApiString(attributeContent)} },
{ "testKey 2", new OpenApiExample {Value = new OpenApiString(attributeContent)} }
}
};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.AreEqual(attributeContent, parsedAttribute.ExampleValue);
}
[Test]
public void ParsingAttributeWithInheritingExampleJustFromDataType()
{
OpenApiParameter parameter = new OpenApiParameter
{
In = ParameterLocation.Path,
Schema = new OpenApiSchema { Type = "string", Format = null },
Example = null,
Examples = new Dictionary<string, OpenApiExample>()
};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.IsNotNull(parsedAttribute);
Assert.IsTrue(!string.IsNullOrEmpty(parsedAttribute.ExampleValue));
}
[Test]
public void CheckThatParsedAttributeHasCorrectlySetDataTypeAndFormat()
{
OpenApiParameter parameter = new OpenApiParameter
{
In = ParameterLocation.Path,
Schema = new OpenApiSchema { Type = "string", Format = null },
Example = null,
Examples = new Dictionary<string, OpenApiExample>()
};
var parsedAttribute = AttributeParser.ParseAttribute(parameter);
Assert.IsNotNull(parsedAttribute);
Assert.IsTrue(!string.IsNullOrEmpty(parsedAttribute.ExampleValue));
Assert.AreEqual(parameter.Schema.Type, parsedAttribute.Type);
Assert.AreEqual(parameter.Schema.Format, parsedAttribute.Format);
}
}
}

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using NUnit.Framework;
namespace Parser.Tests
{
public class ContentParserTests
{
[Test]
public void NoExampleFoundShouldReturnNull()
{
string parsedExample = ContentParser.GetStringExampleFromContent(null, new Dictionary<string, OpenApiExample>());
Assert.IsNull(parsedExample);
}
[Test]
public void ParsingValidContentExample()
{
string attributeContent = "test";
OpenApiString example = new OpenApiString(attributeContent);
Dictionary<string, OpenApiExample> examples = new Dictionary<string, OpenApiExample>();
string parsedExample = ContentParser.GetStringExampleFromContent(example, examples);
Assert.AreEqual(attributeContent, parsedExample);
}
[Test]
public void ParsingContentExamples()
{
string attributeContent = "test";
Dictionary<string, OpenApiExample> examples = new Dictionary<string, OpenApiExample>
{
{"testKey 1", new OpenApiExample {Value = new OpenApiString(attributeContent)}},
{"testKey 2", new OpenApiExample {Value = new OpenApiString(attributeContent)}}
};
string parsedExample = ContentParser.GetStringExampleFromContent(null, examples);
Assert.AreEqual(attributeContent, parsedExample);
}
}
}

View File

@@ -0,0 +1,62 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Models;
using NUnit.Framework;
using Models;
namespace Parser.Tests
{
public class EndpointParserTests
{
readonly OpenApiDocument _document = new OpenApiDocument {Paths = new OpenApiPaths()};
private void AddTwoTestingPaths()
{
_document.Paths.Add("/path1", new OpenApiPathItem
{
Operations = new Dictionary<OperationType, OpenApiOperation>
{
{ OperationType.Get, new OpenApiOperation
{
Responses = new OpenApiResponses
{
{ "200", new OpenApiResponse()}
}
}
}
}
});
_document.Paths.Add("/path2", new OpenApiPathItem
{
Operations = new Dictionary<OperationType, OpenApiOperation>
{
{ OperationType.Get, new OpenApiOperation
{
Responses = new OpenApiResponses
{
{ "201", new OpenApiResponse()}
}
}
}
}
});
}
[Test]
public void DocumentWithoutAnyPathsShouldReturnEmptyList()
{
List<Endpoint> endpoints = EndpointParser.ParseAllEndpoints(_document);
Assert.IsEmpty(endpoints);
}
[Test]
public void DocumentWithTwoPathsEachHavingSingleResponseShouldReturnTwoEndpoints()
{
AddTwoTestingPaths();
List<Endpoint> endpoints = EndpointParser.ParseAllEndpoints(_document);
Assert.AreEqual(2, endpoints.Count);
}
}
}

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using NUnit.Framework;
namespace Parser.Tests
{
public class ExamplesParserTests
{
[TestCase("application/octet-stream")]
[TestCase("application/pdf")]
[TestCase("application/zip")]
public void ParsingNotSupportedContentTypeShouldReturnNull(string contentType)
{
Assert.IsNull(ExamplesParser.ParseExample(new Dictionary<string, OpenApiMediaType> { { contentType, null } }));
}
[Test]
public void ParsingPlainTextContent()
{
string plainText = "test";
Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>
{
{"text/plain", new OpenApiMediaType {Example = new OpenApiString(plainText)}}
};
string example = ExamplesParser.ParseExample(content);
Assert.AreEqual(plainText, example);
}
[Test]
public void ParsingJsonContent()
{
string jsonTemplate = "{\n \"testKey1\": \"testValue\",\n \"testKey2\": \"testValue\"\n}";
Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>
{
{
"application/json", new OpenApiMediaType
{
Example = new OpenApiObject
{
{"testKey1", new OpenApiString("testValue") },
{"testKey2", new OpenApiString("testValue") }
}
}
}
};
string example = ExamplesParser.ParseExample(content);
Assert.AreEqual(jsonTemplate, example);
}
}
}

View File

@@ -0,0 +1,88 @@
using System;
using Microsoft.OpenApi.Any;
using NUnit.Framework;
namespace Parser.Tests
{
public class OpenApiAnyConvertorTests
{
[Test]
public void ConvertStringPrimitiveShouldReturnCorrectValue()
{
string value = "test";
Assert.AreEqual(value, OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiString(value)));
}
[Test]
public void ConvertBooleanPrimitiveShouldReturnCorrectValue()
{
Assert.AreEqual("True", OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiBoolean(true)));
}
[Test]
public void ConvertIntegerPrimitiveShouldReturnCorrectValue()
{
Assert.AreEqual("5", OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiInteger(5)));
}
[Test]
public void ConvertBytePrimitiveShouldReturnCorrectValue()
{
var primitiveValue = OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiByte(new byte[] { 7, 8 }));
Assert.AreEqual("\a\b", primitiveValue);
}
[Test]
public void ConvertBinaryPrimitiveShouldReturnCorrectValue()
{
var primitiveValue = OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiBinary(new byte[] { 7, 8 }));
Assert.AreEqual("0000011100001000", primitiveValue);
}
[Test]
public void ConvertDatePrimitiveShouldReturnCorrectValue()
{
var primitiveValue = OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiDate(DateTime.UnixEpoch));
Assert.AreEqual("01/01/1970 00:00:00", primitiveValue);
}
[Test]
public void ConvertDateTimePrimitiveShouldReturnCorrectValue()
{
var primitiveValue = OpenApiAnyConvertor.GetPrimitiveValue(new OpenApiDateTime(DateTime.UnixEpoch));
Assert.AreEqual("1/1/1970 12:00:00 AM +00:00", primitiveValue);
}
[Test]
public void ConvertObjectShouldReturnCorrectJson()
{
string expectedJson = "{\n \"testKey1\": \"testValue\",\n \"testKey2\": \"testValue\"\n}";
var openApiObject = new OpenApiObject
{
{"testKey1", new OpenApiString("testValue")},
{"testKey2", new OpenApiString("testValue")}
};
var jsonValue = OpenApiAnyConvertor.GetJsonValue(openApiObject);
Assert.AreEqual(expectedJson, jsonValue);
}
[Test]
public void ConvertArrayShouldReturnCorrectJson()
{
string expectedJson = "[\n {\n \"testKey1\": \"testValue\",\n \"testKey2\": \"testValue\"\n }\n]";
var openApiObject = new OpenApiObject
{
{"testKey1", new OpenApiString("testValue")},
{"testKey2", new OpenApiString("testValue")}
};
var openApiArray = new OpenApiArray {openApiObject};
var jsonValue = OpenApiAnyConvertor.GetJsonValue(openApiArray);
Assert.AreEqual(expectedJson, jsonValue);
}
}
}

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" />
<ProjectReference Include="..\Parser\Parser.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.OpenApi">
<HintPath>..\..\..\..\..\.nuget\packages\microsoft.openapi\1.1.3\lib\netstandard2.0\Microsoft.OpenApi.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,30 @@
using System;
using NUnit.Framework;
namespace Parser.Tests
{
public class PrimitiveDataTypeExampleGeneratorTests
{
[TestCase("integer", null, "42")]
[TestCase("number", null, "42.0")]
[TestCase("boolean", null, "True")]
[TestCase("string", null, "example")]
[TestCase("string", "byte", "example")]
[TestCase("string", "binary", "01234567")]
[TestCase("string", "date", "2002-10-02")]
[TestCase("string", "date-time", "2002-10-02T10:00:00-05:00")]
[TestCase("string", "password", "example")]
public void ValidCombinationsShouldReturnValidValue(string type, string format, string expected)
{
string example = PrimitiveDataTypeExampleGenerator.GenerateExampleValueByType(type, format);
Assert.AreEqual(expected, example);
}
[Test]
public void InvalidCombinationsShouldThrowAnException()
{
Assert.Throws<NotImplementedException>(() => PrimitiveDataTypeExampleGenerator.GenerateExampleValueByType("test", null));
}
}
}

View File

@@ -0,0 +1,49 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Models;
using NUnit.Framework;
namespace Parser.Tests
{
public class RequestParserTests
{
[Test]
public void ValidRequestParsingShouldHaveCorrectValues()
{
string summary = "summary";
string example = "test";
KeyValuePair<OperationType, OpenApiOperation> operation =
new KeyValuePair<OperationType, OpenApiOperation>(OperationType.Get, new OpenApiOperation
{
Summary = summary,
Parameters = new List<OpenApiParameter> { new OpenApiParameter
{
In = ParameterLocation.Path,
Example = new OpenApiString(example),
Schema = new OpenApiSchema {Type = "string", Format = null}
} },
RequestBody = new OpenApiRequestBody
{
Content = new Dictionary<string, OpenApiMediaType>
{
{ "text/plain", new OpenApiMediaType
{
Example = new OpenApiString(example)
} }
}
}
});
Request request = RequestParser.ParseRequest(operation);
Assert.AreEqual(summary, request.Summary);
Assert.AreEqual(example, request.BodyExample);
Assert.AreEqual(example, request.UriAttributes[0].ExampleValue);
}
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Models;
using NUnit.Framework;
namespace Parser.Tests
{
public class ResponseParserTests
{
[Test]
public void ValidResponseParsingShouldHaveCorrectValues()
{
int statusCode = 200;
string example = "example";
KeyValuePair<string, OpenApiResponse> openApiResponse = new KeyValuePair<string, OpenApiResponse>(statusCode.ToString(), new OpenApiResponse
{
Content = new Dictionary<string, OpenApiMediaType>
{
{ "text/plain", new OpenApiMediaType
{
Schema = new OpenApiSchema {Type = "string", Format = null},
Example = new OpenApiString(example)
} }
}
});
Response response = ResponseParser.ParseResponse(openApiResponse);
Assert.AreEqual(statusCode, response.StatusCode);
Assert.AreEqual(example, response.Example);
}
[Test]
public void InvalidResponseStatusCodeShouldThrowException()
{
KeyValuePair<string, OpenApiResponse> openApiResponse = new KeyValuePair<string, OpenApiResponse>("invalid", new OpenApiResponse());
Assert.Throws<NotImplementedException>(() => ResponseParser.ParseResponse(openApiResponse));
}
}
}

View File

@@ -0,0 +1,164 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using NUnit.Framework;
namespace Parser.Tests
{
public class SchemaParserTests
{
[TestCase("application/octet-stream")]
[TestCase("application/pdf")]
[TestCase("application/zip")]
public void ParsingNotSupportedContentTypeShouldReturnNull(string contentType)
{
Assert.IsNull(SchemaParser.ParseSchema(new Dictionary<string, OpenApiMediaType> { { contentType, null } }));
}
[Test]
public void SchemaWithRegularProperties()
{
string testingPropertyName = "testKey";
string testingPropertyType = "string";
string testingPropertyExample = "test";
string testingPropertyFormat = null;
Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>
{
{
"application/json", new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Properties = new Dictionary<string, OpenApiSchema>
{
{
testingPropertyName, new OpenApiSchema
{
Type = testingPropertyType,
Example = new OpenApiString(testingPropertyExample),
Title = testingPropertyName,
Format = testingPropertyFormat
}
}
}
}
}
}
};
Dictionary<string, object> parsedSchema = SchemaParser.ParseSchema(content);
Dictionary<string, object> testingProperty = (Dictionary<string, object>) parsedSchema.First().Value;
Assert.AreEqual(testingPropertyName, parsedSchema.First().Key);
Assert.That(testingProperty.ContainsKey("Title"));
Assert.That(testingProperty.ContainsKey("Type"));
Assert.That(testingProperty.ContainsKey("Format"));
Assert.That(testingProperty.ContainsKey("Example"));
Assert.AreEqual(testingPropertyName, testingProperty["Title"]);
Assert.AreEqual(testingPropertyType, testingProperty["Type"]);
Assert.AreEqual(testingPropertyFormat, testingProperty["Format"]);
Assert.AreEqual(testingPropertyExample, testingProperty["Example"]);
}
[Test]
public void SchemaWithRegularArrayOfDoublesProperty()
{
string testingPropertyName = "testKey";
string testingPropertyType = "array";
string testingPropertyFormat = null;
string testingArrayItemType = "double";
string testingArrayItemFormat = "number";
Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>
{
{
"application/json", new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Properties = new Dictionary<string, OpenApiSchema>
{
{
testingPropertyName, new OpenApiSchema
{
Type = testingPropertyType,
Title = testingPropertyName,
Items = new OpenApiSchema { Type = testingArrayItemType, Format = testingArrayItemFormat },
Format = testingPropertyFormat
}
}
}
}
}
}
};
Dictionary<string, object> parsedSchema = SchemaParser.ParseSchema(content);
Dictionary<string, object> testingProperty = (Dictionary<string, object>) parsedSchema.First().Value;
Dictionary<string, object> arrayTypeDictionary = (Dictionary<string, object>) testingProperty["ArrayItemSchema"];
Assert.AreEqual(testingPropertyName, parsedSchema.First().Key);
Assert.That(arrayTypeDictionary.ContainsKey("Type"));
Assert.That(arrayTypeDictionary.ContainsKey("Format"));
Assert.AreEqual(testingArrayItemType, arrayTypeDictionary["Type"]);
Assert.AreEqual(testingArrayItemFormat, arrayTypeDictionary["Format"]);
}
[Test]
public void SchemaWithAdditionalProperties()
{
string testingPropertyName = "testKey";
string testingPropertyType = "boolean";
bool testingPropertyExample = true;
string testingPropertyFormat = null;
Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>
{
{
"application/json", new OpenApiMediaType
{
Schema = new OpenApiSchema
{
AdditionalPropertiesAllowed = true,
AdditionalProperties = new OpenApiSchema {Properties = new Dictionary<string, OpenApiSchema>
{
{
testingPropertyName, new OpenApiSchema
{
Type = testingPropertyType,
Example = new OpenApiBoolean(testingPropertyExample),
Title = testingPropertyName,
Format = testingPropertyFormat
}
}
}}
}
}
}
};
Dictionary<string, object> firstAdditionalPropertyDictionary = (Dictionary<string, object>) SchemaParser.ParseSchema(content).First().Value;
Dictionary<string, object> firstAdditionalPropertyItemDictionary = (Dictionary<string, object>)firstAdditionalPropertyDictionary.First().Value;
Assert.AreEqual(testingPropertyName, firstAdditionalPropertyDictionary.First().Key);
Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Title"));
Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Type"));
Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Format"));
Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Example"));
Assert.AreEqual(testingPropertyName, firstAdditionalPropertyItemDictionary["Title"]);
Assert.AreEqual(testingPropertyType, firstAdditionalPropertyItemDictionary["Type"]);
Assert.AreEqual(testingPropertyFormat, firstAdditionalPropertyItemDictionary["Format"]);
Assert.AreEqual(testingPropertyExample.ToString(), firstAdditionalPropertyItemDictionary["Example"]);
}
}
}