Skip to content
Snippets Groups Projects
Commit 0ea31623 authored by Jueun Park's avatar Jueun Park
Browse files

Adapted InferParameter

parent fa454295
No related branches found
No related tags found
1 merge request!6Resolve "check and fix random request generator"
Pipeline #13271 passed
/*
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
aspect InferParameter{ aspect InferParameter{
syn String PropertyItem.writePropertyName(SchemaOb schema,PropertyItem p)=((SchemaReference) schema).getRef().substring(((SchemaReference) schema).getRef().lastIndexOf("/") + 1) + p.getName();
syn String PropertyItem.writePropertyValue(JsonNode node,PropertyItem p)=node.get(p.getName()).toString().startsWith("\"") && node.get(p.getName()).toString().endsWith("\"") ? node.get(p.getName()).toString().substring(1, node.get(p.getName()).toString().length() - 1) : node.get(p.getName()).toString(); syn String ParameterObject.dictName(String dict) = dict.substring(0, dict.indexOf("?"));
syn String ParameterObject.dictValue(String dict) = dict.substring(dict.indexOf("?")+1);
coll Set<InferredParameter> OpenAPIObject.collectInferredParameters()[new HashSet<InferredParameter>()]; inh List<String> PathsObject.inferUrl(List<String> dict);
InferredParameter contributes this eq OpenAPIObject.getP(int i).inferUrl(List<String> dict){
to OpenAPIObject.collectInferredParameters(); List<String> paths = new ArrayList<>();
PathItemObject p = getP(i).getP().pathItemObject();
syn String InferredParameter.name()=getParameter().substring(0, getParameter().indexOf("?")); String path = getServ(0).getUrl();
syn String InferredParameter.value()=getParameter().substring(getParameter().indexOf("?") + 1);
public void OpenAPIObject.generateRequestsWithInferredParameters()throws Exception{
Set<String> urls = new HashSet<>();
generateRequests();
for (PathsObject p : getPathsObjects())
p.inferUrl();
}
inh boolean PathsObject.inferUrl();
eq OpenAPIObject.getPathsObject(int i).inferUrl(){
PathItemObject p = getPathsObject(i).getPathItemObject();
String path = getServerObject(0).getUrl();
if (p.hasGet()) if (p.hasG())
p.getGet().inferRandomUrl(path + getPathsObject(i).getRef(), p.getGet().getOperationObject()); paths.addAll(p.getG().inferRandomUrl(path + getP(i).getRef(), p.getG().getO(), dict));
else if (p.hasPost()) else if (p.hasPostOb())
p.getPost().inferRandomUrl(path + getPathsObject(i).getRef(), p.getPost().getOperationObject()); paths.addAll(p.getPostOb().inferRandomUrl(path + getP(i).getRef(), p.getPostOb().getO(), dict));
return true; return paths;
} }
syn String Get.inferRandomUrl(String pathRef,OperationObject operationObject){ syn List<String> Get.inferRandomUrl(String pathRef,OperationObject operationObject, List<String> dict){
List<String> paths = new ArrayList<>(); List<String> paths = new ArrayList<>();
for (ParameterOb o : operationObject.getParameterObs()) { for (ParameterOb o : operationObject.getPList()) {
ParameterObject p = o.parameterObject(); ParameterObject p = o.parameterObject();
SchemaObject s = p.getSchemaOb().schemaObject(); SchemaObject s = p.getSchema().schemaObject();
// check if the parameter is in type 'path'. // check if the parameter is in type 'path'.
if (p.getIn().equals("path")) if (p.getIn().equals("path"))
paths = p.addinfPathParameters(pathRef, paths); paths = p.addinfPathParameters(pathRef, dict);
// check if the parameter is in type 'query' // check if the parameter is in type 'query'
else if (p.getIn().equals("query")) else if (p.getIn().equals("query"))
paths = p.addinfQueryParameters(pathRef, paths); paths = p.addinfQueryParameters(pathRef, dict);
System.out.println(paths.size() + " Paths are inferred.");
} }
for (String path : paths)
connectGET(path); System.out.println(paths.size() + " paths for " + pathRef + " GET request are inferred");
return pathRef; return paths;
} }
syn String Post.inferRandomUrl(String pathRef,OperationObject operationObject){ syn List<String> Post.inferRandomUrl(String pathRef,OperationObject operationObject, List<String> dict){
List<String> paths = new ArrayList<>(); List<String> paths = new ArrayList<>();
for (ParameterOb o : operationObject.getParameterObs()) { for (ParameterOb o : operationObject.getPList()) {
ParameterObject p = o.parameterObject(); ParameterObject p = o.parameterObject();
SchemaObject s = p.getSchemaOb().schemaObject(); SchemaObject s = p.getSchema().schemaObject();
// check if the parameter is in type 'path'. // check if the parameter is in type 'path'.
if (p.getIn().equals("path")) if (p.getIn().equals("path"))
paths = p.addinfPathParameters(pathRef, paths); paths = p.addinfPathParameters(pathRef, dict);
// check if the parameter is in type 'query' // check if the parameter is in type 'query'
else if (p.getIn().equals("query")) else if (p.getIn().equals("query"))
paths = p.addinfQueryParameters(pathRef, paths); paths = p.addinfQueryParameters(pathRef, dict);
System.out.println(paths.size() + " Paths are inferred.");
} }
for (String path : paths)
connectPOST(path); System.out.println(paths.size() + " paths for " + pathRef + " POST request are inferred");
return pathRef; return paths;
} }
syn List<String> ParameterObject.addinfPathParameters(String pathRef,List<String> paths){ syn List<String> ParameterObject.addinfPathParameters(String pathRef,List<String> dict){
for (InferredParameter i : root().collectInferredParameters()) { List<String> paths = new ArrayList<>();
for (String d : dict){
// get the field which must be modified // get the field which must be modified
String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}")+1);
// case insensitive comparison of parameter name and name of inferred parameters // case insensitive comparison of parameter name and name of inferred parameters
if (getName().equalsIgnoreCase(i.name())) if (getName().equalsIgnoreCase(d.substring(0, d.indexOf("?"))))
// add inferred parameter in url // add inferred parameter in url
paths.add(pathRef.replace(pathPart, i.value())); paths = root().addInList(paths, pathRef.replace(pathPart, d.substring(d.indexOf("?")+1)));
} }
return paths; return paths;
} }
syn List<String> ParameterObject.addinfQueryParameters(String pathRef,List<String> paths){ syn List<String> ParameterObject.addinfQueryParameters(String pathRef,List<String> dict){
SchemaObject s = getSchemaOb().schemaObject(); List<String> paths = new ArrayList<>();
SchemaObject s = getSchema().schemaObject();
// check if query parameter is in type 'array' // check if query parameter is in type 'array'
if (s.getType().equals("array")) { if (s.getType().equals("array")) {
for (InferredParameter i : root().collectInferredParameters()) { for (String d : dict){
// case insensitive comparison of parameter name and name of inferred parameters // case insensitive comparison of parameter name and name of inferred parameters
if (getName().equalsIgnoreCase(i.name())) if (getName().equalsIgnoreCase(dictName(d)) && !pathRef.contains(getName() + "=" + dictValue(d)))
// add inferred parameter in url // add inferred parameter in url
pathRef = pathRef + "&" + getName() + "=" + i.value(); pathRef = pathRef + "&" + getName() + "=" + dictValue(d);
} }
paths.add(pathRef.replaceFirst("&", "?")); paths = root().addInList(paths, pathRef.replaceFirst("&", "?"));
} else { } else {
for (InferredParameter i : root().collectInferredParameters()) { for (String d : dict){
// case insensitive comparison of parameter name and name of inferred parameters // case insensitive comparison of parameter name and name of inferred parameters
if (getName().equalsIgnoreCase(i.name())) if (getName().equalsIgnoreCase(dictName(d)))
// add inferred parameter in url // add inferred parameter in url
paths.add(pathRef + "?" + getName() + "=" + i.value()); paths = root().addInList(paths, pathRef + "?" + getName() + "=" + dictValue(d));
} }
} }
return paths; return paths;
} }
public void OperationObject.writeDictionary(SchemaOb schema,String resp)throws Exception{ public List<String> OperationObject.writeDictionary(SchemaOb schema,String resp, List<String> dict)throws Exception{
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode respNode = mapper.readTree(resp); JsonNode respNode = mapper.readTree(resp);
String value; String value;
if (schema instanceof SchemaReference) { if (schema instanceof SchemaReference) {
for (PropertyItem p : schema.schemaObject().getPropertyItems()) { for (PropertyItem p : schema.schemaObject().getPList()) {
String infName = p.writePropertyName(schema, p); String infName = ((SchemaReference) schema).getRef().substring(((SchemaReference) schema).getRef().lastIndexOf("/") + 1) + p.getName();
value = p.writePropertyValue(respNode, p); value = respNode.get(p.getName()).toString().startsWith("\"") &&
respNode.get(p.getName()).toString().endsWith("\"") ?
root().addInferredParameter(new InferredParameter(infName + "?" + value)); respNode.get(p.getName()).toString().substring(1, respNode.get(p.getName()).toString().length() - 1) : respNode.get(p.getName()).toString();
root().addInferredParameter(new InferredParameter(p.getName() + "?" + value));
dict = root().addInList(dict, infName + "?" + value);
dict = root().addInList(dict, p.getName() + "?" + value);
} }
} else { } else {
for (PropertyItem p : schema.schemaObject().getPropertyItems()) { for (PropertyItem p : schema.schemaObject().getPList()) {
value = p.writePropertyValue(respNode, p); value = respNode.get(p.getName()).toString().startsWith("\"") &&
respNode.get(p.getName()).toString().endsWith("\"") ?
respNode.get(p.getName()).toString().substring(1, respNode.get(p.getName()).toString().length() - 1) : respNode.get(p.getName()).toString();
root().addInferredParameter(new InferredParameter(p.getName() + "?" + respNode.get(p.getName()).textValue())); dict = root().addInList(dict, p.getName() + "?" + respNode.get(p.getName()).textValue());
} }
} }
return dict;
} }
public void OperationObject.writeDictionaryWithArray(SchemaOb schema,String resp)throws Exception{ public List<String> OperationObject.writeDictionaryWithArray(SchemaOb schema,String resp, List<String> dict)throws Exception{
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
ArrayNode respNode = ((ArrayNode) mapper.readTree(resp)); ArrayNode respNode = ((ArrayNode) mapper.readTree(resp));
Iterator<JsonNode> props = respNode.elements(); Iterator<JsonNode> props = respNode.elements();
while (props.hasNext()) while (props.hasNext())
writeDictionary(schema.schemaObject().getItemsSchema().getSchemaOb(), props.next().toString()); dict = writeDictionary(schema.schemaObject().getI().getSchema(), props.next().toString(), dict);
return dict;
}
public List<String> OpenAPIObject.addInList(List<String> list, String value){
if( !list.contains(value) )
list.add(value);
return list;
} }
} }
*/ \ No newline at end of file
\ No newline at end of file
aspect RandomRequestGenerator{ aspect RandomRequestGenerator{
syn String ParameterObject.randomPathParameter(String pathRef){ syn String ParameterObject.randomPathParameter(String pathRef){
Random rand = new Random();
SchemaObject s = getSchema().schemaObject(); SchemaObject s = getSchema().schemaObject();
String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1);
if (s.getType().equals("string")) if (s.getType().equals("string"))
pathRef = pathRef.replace(pathPart, generateRandomString(rand, s.getEList())); pathRef = pathRef.replace(pathPart, generateRandomString(s.getEList()));
else if (s.getType().equals("integer")) else if (s.getType().equals("integer"))
pathRef = pathRef.replace(pathPart, generateRandomInt(rand, pathRef = pathRef.replace(pathPart, generateRandomInt(
-1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1,
10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1 10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1
)); ));
...@@ -16,22 +15,21 @@ aspect RandomRequestGenerator{ ...@@ -16,22 +15,21 @@ aspect RandomRequestGenerator{
} }
syn String ParameterObject.randomQueryParameter(String pathRef){ syn String ParameterObject.randomQueryParameter(String pathRef){
Random rand = new Random();
SchemaObject s = getSchema().schemaObject(); SchemaObject s = getSchema().schemaObject();
if (s.getType().equals("string")) if (s.getType().equals("string"))
pathRef = pathRef + "?" + getName() + "=" + generateRandomString(rand, s.getEList()); pathRef = pathRef + "?" + getName() + "=" + generateRandomString(s.getEList());
else if (s.getType().equals("integer")) else if (s.getType().equals("integer"))
pathRef = pathRef + "?" + getName() + "=" + generateRandomInt(rand, pathRef = pathRef + "?" + getName() + "=" + generateRandomInt(
-1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1,
10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1
else if (s.getType().equals("array")) { else if (s.getType().equals("array")) {
if (s.getI().getSchema().schemaObject().getType().equals("string")) { if (s.getI().getSchema().schemaObject().getType().equals("string")) {
for (EnumObj e : s.getI().getSchema().schemaObject().getEList()) for (EnumObj e : s.getI().getSchema().schemaObject().getEList())
pathRef = rand.nextDouble() < 0.5 ? pathRef + "&" + getName() + "=" + e.getEnumOb() : pathRef; pathRef = pathWithEnum(e, pathRef);
} else if (s.getI().getSchema().schemaObject().getType().equals("integer")) { } else if (s.getI().getSchema().schemaObject().getType().equals("integer")) {
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
pathRef = pathRef + "&" + getName() + "=" + generateRandomInt(rand, pathRef = pathRef + "&" + getName() + "=" + generateRandomInt(
-1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1,
10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1
} }
...@@ -104,7 +102,8 @@ aspect RandomRequestGenerator{ ...@@ -104,7 +102,8 @@ aspect RandomRequestGenerator{
} }
} }
syn String ParameterObject.generateRandomString(Random rand,JastAddList<EnumObj> objs){ public String ParameterObject.generateRandomString(JastAddList<EnumObj> objs){
Random rand = new Random();
if (objs.getNumChild() != 0) if (objs.getNumChild() != 0)
return objs.getChild(rand.nextInt(objs.getNumChild())).getEnumOb().toString(); return objs.getChild(rand.nextInt(objs.getNumChild())).getEnumOb().toString();
...@@ -115,7 +114,8 @@ aspect RandomRequestGenerator{ ...@@ -115,7 +114,8 @@ aspect RandomRequestGenerator{
.toString(); .toString();
} }
syn String ParameterObject.generateRandomInt(Random rand,int minimum,int maximum){ public String ParameterObject.generateRandomInt(int minimum,int maximum){
Random rand = new Random();
if (minimum > -1 && maximum > 0) if (minimum > -1 && maximum > 0)
return String.valueOf(rand.nextInt(minimum + maximum) - minimum); return String.valueOf(rand.nextInt(minimum + maximum) - minimum);
else if (minimum > -1) else if (minimum > -1)
...@@ -124,4 +124,18 @@ aspect RandomRequestGenerator{ ...@@ -124,4 +124,18 @@ aspect RandomRequestGenerator{
return String.valueOf(rand.nextInt(maximum)); return String.valueOf(rand.nextInt(maximum));
return String.valueOf(rand.nextInt()); return String.valueOf(rand.nextInt());
} }
public String ParameterObject.pathWithEnum(EnumObj e, String pathRef){
Random rand = new Random();
return rand.nextDouble() < 0.5 ?
pathRef + "&" + this.getName() + "=" + e.getEnumOb() : pathRef;
}
syn String OpenAPIObject.getURLFromGeneratedURLs(List<String> urls, String ref){
for( String url : urls ){
if( url.contains(ref) )
return url;
}
return null;
}
} }
\ No newline at end of file
...@@ -2,7 +2,7 @@ package de.tudresden.inf.st.openapi; ...@@ -2,7 +2,7 @@ package de.tudresden.inf.st.openapi;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import de.tudresden.inf.st.openapi.ast.OpenAPIObject; import de.tudresden.inf.st.openapi.ast.*;
import io.swagger.models.reader.SwaggerParser; import io.swagger.models.reader.SwaggerParser;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
import io.swagger.report.MessageBuilder; import io.swagger.report.MessageBuilder;
...@@ -22,9 +22,11 @@ import javax.net.ssl.HttpsURLConnection; ...@@ -22,9 +22,11 @@ import javax.net.ssl.HttpsURLConnection;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class OpenAPIMain { public class OpenAPIMain {
static List<String> successfulUrls = new ArrayList<>();
/** /**
* main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd * main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd
...@@ -34,12 +36,20 @@ public class OpenAPIMain { ...@@ -34,12 +36,20 @@ public class OpenAPIMain {
OpenAPIObject jastAddObject; OpenAPIObject jastAddObject;
SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null); SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null);
OpenAPI openAPI = result.getOpenAPI(); OpenAPI openAPI = result.getOpenAPI();
List<String> generatedURLs = new ArrayList<>(); List<String> generatedURLs, dictionary;
List<String> dictionary = new ArrayList<>(); List<String> inferredURLs = new ArrayList<>();
jastAddObject = OpenAPIObject.parseOpenAPI(openAPI); jastAddObject = OpenAPIObject.parseOpenAPI(openAPI);
generatedURLs = jastAddObject.generateRequests(); generatedURLs = jastAddObject.generateRequests();
dictionary = sendRandomRequests(jastAddObject, generatedURLs);
for ( PathsObject p : jastAddObject.getPList() )
inferredURLs.addAll(p.inferUrl(dictionary));
sendInferredRequests(jastAddObject, inferredURLs);
System.out.println("Requests generated by Parameter Inference are :");
for ( String url : successfulUrls )
System.out.println("--> " + url);
URL expUrl = OpenAPIMain.class.getClassLoader().getResource(fileName); URL expUrl = OpenAPIMain.class.getClassLoader().getResource(fileName);
File file = null; File file = null;
...@@ -59,16 +69,34 @@ public class OpenAPIMain { ...@@ -59,16 +69,34 @@ public class OpenAPIMain {
} }
} }
public void sendRandomRequests() {} public static List<String> sendRandomRequests(OpenAPIObject api, List<String> urls) {
List<String> dict = new ArrayList<>();
if( api.getNumP() != 0 ){
for( PathsObject p : api.getPList() ){
PathItemObject item = p.getP().pathItemObject();
String url = api.getURLFromGeneratedURLs(urls, p.getRef());
if( item.hasG() )
dict = connectGET(item.getG().getO(), url, dict);
if( item.hasPostOb() )
dict = connectPOST(item.getPostOb().getO(), url, dict);
}
}
return dict;
}
public void sendInferredRequests(List<String> randomRequests) {} public static void sendInferredRequests(OpenAPIObject api, List<String> inferredURLs) {
if( api.getNumP() != 0 ){
for( PathsObject p : api.getPList() ){
connectInferredURLs(p, inferredURLs);
}
}
}
public void connectGET(String path){ public static List<String> connectGET(OperationObject op, String path,List<String> dict){
try { try {
URL url = new URL(path); URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); // request header
con.setRequestMethod("GET"); // optional default is GET con.setRequestMethod("GET"); // optional default is GET
int responseCode = con.getResponseCode(); int responseCode = con.getResponseCode();
...@@ -82,14 +110,25 @@ public class OpenAPIMain { ...@@ -82,14 +110,25 @@ public class OpenAPIMain {
in.close(); in.close();
// print result // print result
System.out.println("connected path : " + path); //System.out.println("connected path : " + path);
System.out.println("HTTP status code (GET) : " + responseCode); //System.out.println("HTTP status code (GET) : " + con.getResponseCode());
successfulUrls.add(path);
for (ResponseTuple t : op.getR().getRList()) {
if (t.getKey().equals("200") && responseCode == 200) {
SchemaOb respSchema = t.getO().responseObject().getC(0).getM().getS();
if (respSchema.schemaObject().getType().equals("array"))
dict.addAll(op.writeDictionaryWithArray(respSchema, response.toString(), dict));
else
dict.addAll(op.writeDictionary(respSchema, response.toString(), dict));
}
}
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.toString()); //System.out.println(e.toString());
} }
return dict;
} }
public void connectPOST(String path){ public static List<String> connectPOST(OperationObject op, String path, List<String> dict) {
try { try {
URL url = new URL(path); URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
...@@ -109,10 +148,34 @@ public class OpenAPIMain { ...@@ -109,10 +148,34 @@ public class OpenAPIMain {
in.close(); in.close();
// print result // print result
System.out.println("connected path : " + path); //System.out.println("connected path : " + path);
System.out.println("HTTP status code (POST) : " + responseCode); //System.out.println("HTTP status code (POST) : " + responseCode);
successfulUrls.add(path);
for (ResponseTuple t : op.getR().getRList()) {
if (t.getKey().equals("200") && responseCode == 200) {
SchemaOb respSchema = t.getO().responseObject().getC(0).getM().getS();
if (respSchema.schemaObject().getType().equals("array"))
dict.addAll(op.writeDictionaryWithArray(respSchema, response.toString(), dict));
else
dict.addAll(op.writeDictionary(respSchema, response.toString(), dict));
}
}
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.toString()); //System.out.println(e.toString());
}
return dict;
}
public static void connectInferredURLs(PathsObject p, List<String> urls){
PathItemObject item = p.getP().pathItemObject();
System.out.println("Connect with inferred URLs is running...");
for ( String url : urls ){
if( url.contains(p.getRef()) ){
if( item.hasG() )
connectGET(item.getG().getO(), url, new ArrayList<>());
if( item.hasPostOb() )
connectPOST(item.getPostOb().getO(), url, new ArrayList<>());
}
} }
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment