summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFeideus <erwan.ulrich@gmail.com>2018-06-15 17:41:31 +0200
committerFeideus <erwan.ulrich@gmail.com>2018-06-15 17:41:31 +0200
commit6355268eb16042d7ed193d505554ee98b7ccffdc (patch)
treee7fc89db19475ef0b673a31540d0a491c6235973 /src
parent8cd83f69c9a44ee805ea3d4d98351f68ce851798 (diff)
downloadschemafuzz-6355268eb16042d7ed193d505554ee98b7ccffdc.tar.gz
schemafuzz-6355268eb16042d7ed193d505554ee98b7ccffdc.tar.bz2
schemafuzz-6355268eb16042d7ed193d505554ee98b7ccffdc.zip
Some more buggs with the main loop ><
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/schemaspy/DBFuzzer.java168
-rw-r--r--src/main/java/org/schemaspy/model/GenericTreeNode.java38
-rw-r--r--src/test/java/org/schemaspy/model/GenericTreeNodeTest.java5
3 files changed, 106 insertions, 105 deletions
diff --git a/src/main/java/org/schemaspy/DBFuzzer.java b/src/main/java/org/schemaspy/DBFuzzer.java
index 9948b6e..af97545 100644
--- a/src/main/java/org/schemaspy/DBFuzzer.java
+++ b/src/main/java/org/schemaspy/DBFuzzer.java
@@ -31,13 +31,14 @@ public class DBFuzzer
public boolean processFirstMutation(GenericTreeNode rootMutation)
{
- boolean resQuery,returnStatus=true;
+ boolean returnStatus=true;
+ int nbUpdates;
try
{
if(rootMutation.getChosenChange() != null)
{
- resQuery = rootMutation.inject(analyzer.getSqlService(),analyzer.getDb(),false);
- if(resQuery)
+ nbUpdates = rootMutation.inject(analyzer.getSqlService(),analyzer.getDb(),false);
+ if(nbUpdates > 0)
{
LOGGER.info("GenericTreeNode was sucessfull");
}
@@ -76,7 +77,7 @@ public class DBFuzzer
public boolean fuzz (Config config)
{
boolean returnStatus = true;
- boolean resQuery;
+ int nbUpdates;
int TreeDepth = 0;
int maxDepth = Integer.parseInt(analyzer.getCommandLineArguments().getMaxDepth());
int mark = 0;
@@ -131,14 +132,14 @@ public class DBFuzzer
{
if(currentMutation.getChosenChange() != null)
{
- resQuery = currentMutation.inject(analyzer.getSqlService(),analyzer.getDb(),false);
- if(resQuery)
+ nbUpdates = currentMutation.inject(analyzer.getSqlService(),analyzer.getDb(),false);
+ if(nbUpdates > 0)
{
LOGGER.info("GenericTreeNode was sucessfull");
mutationTree.addToTree(currentMutation);
}
else
- LOGGER.info("QueryError");
+ LOGGER.info("QueryError. This update affected 0 rows");
}
}
catch(Exception e)
@@ -167,9 +168,9 @@ public class DBFuzzer
TreeDepth = mutationTree.checkMaxDepth(mutationTree.getRoot());
}
- System.out.println("success");
- printMutationTree();
- removeTemporaryCascade();
+ removeTemporaryCascade();
+ printMutationTree();
+ System.out.println("ending process");
return returnStatus;
}
@@ -178,26 +179,26 @@ public class DBFuzzer
public Row pickRandomRow()
{
Row res = null;
-
- do {
- //Table randomTable = pickRandomTable();
+ do {
+ Table randomTable = pickRandomTable();
//String theQuery = "SELECT * FROM " + randomTable.getName() + " ORDER BY RANDOM() LIMIT 1";
- String theQuery = "SELECT * FROM test_table3 ORDER BY RANDOM() LIMIT 1"; // Change test_table2 to test_table here to swap back to line finding
- QueryResponseParser qrp = new QueryResponseParser();
- ResultSet rs = null;
+ String theQuery = "SELECT * FROM "+randomTable.getName()+" ORDER BY RANDOM() LIMIT 1"; // Change test_table2 to test_table here to swap back to line finding
PreparedStatement stmt;
- try {
+ try
+ {
stmt = analyzer.getSqlService().prepareStatement(theQuery);
- rs = stmt.executeQuery();
- res = qrp.parse(rs, analyzer.getDb().getTablesMap().get("test_table3")).getRows().get(0); // randomTable should be in the get()
- } catch (Exception e) {
+ ResultSet rs = stmt.executeQuery();
+ res = new QueryResponseParser().parse(rs, analyzer.getDb().getTablesMap().get(randomTable.getName())).getRows().get(0); // randomTable should be in the get()
+ }
+ catch (Exception e)
+ {
LOGGER.info("This query threw an error" + e);
}
- }
- while(res == null);
- return res;
+ }
+ while(res == null);
+ return res;
}
public Table pickRandomTable()
@@ -218,53 +219,53 @@ public class DBFuzzer
public boolean settingTemporaryCascade(Boolean undo)
{
- Iterator i;
- ForeignKeyConstraint currentFK;
- String dropSetCascade = null;
- for(Map.Entry<String,Collection<ForeignKeyConstraint>> entry : analyzer.getDb().getLesForeignKeys().entrySet())
- {
+ Iterator i;
+ ForeignKeyConstraint currentFK;
+ String dropSetCascade = "";
+ for(Map.Entry<String,Collection<ForeignKeyConstraint>> entry : analyzer.getDb().getLesForeignKeys().entrySet())
+ {
i = entry.getValue().iterator();
while(i.hasNext())
{
- currentFK = (ForeignKeyConstraint) i.next();
- dropSetCascade = "ALTER TABLE "+currentFK.getChildTable().getName()+" DROP CONSTRAINT "+currentFK.getName()+ " CASCADE";
- try
- {
- PreparedStatement stmt = analyzer.getSqlService().prepareStatement(dropSetCascade, analyzer.getDb(),null);
- stmt.execute();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
- }
+ currentFK = (ForeignKeyConstraint) i.next();
+ dropSetCascade = "ALTER TABLE "+currentFK.getChildTable().getName()+" DROP CONSTRAINT "+currentFK.getName()+ " CASCADE";
+ try
+ {
+ PreparedStatement stmt = analyzer.getSqlService().prepareStatement(dropSetCascade, analyzer.getDb(),null);
+ stmt.execute();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
- for(Map.Entry<String,Collection<ForeignKeyConstraint>> entry : analyzer.getDb().getLesForeignKeys().entrySet())
- {
- i = entry.getValue().iterator();
- while(i.hasNext())
- {
- currentFK = (ForeignKeyConstraint) i.next();
- if(!undo)
- dropSetCascade = "ALTER TABLE "+currentFK.getChildTable().getName()+" ADD CONSTRAINT "+currentFK.getName()+" FOREIGN KEY ("+currentFK.getParentColumns().get(0).getName()+" ) REFERENCES "+currentFK.getParentTable().getName()+"("+currentFK.getChildColumns().get(0).getName()+") ON UPDATE CASCADE";
- else
- {
- dropSetCascade = "ALTER TABLE "+currentFK.getChildTable().getName()+" ADD CONSTRAINT "+currentFK.getName()+" FOREIGN KEY ("+currentFK.getParentColumns().get(0).getName()+" ) REFERENCES "+currentFK.getParentTable().getName()+"("+currentFK.getChildColumns().get(0).getName()+")";
- }
- try
- {
- PreparedStatement stmt = analyzer.getSqlService().prepareStatement(dropSetCascade, analyzer.getDb(),null);
- stmt.execute();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
+ }
+ for(Map.Entry<String,Collection<ForeignKeyConstraint>> entry : analyzer.getDb().getLesForeignKeys().entrySet())
+ {
+ i = entry.getValue().iterator();
+ while(i.hasNext())
+ {
+ currentFK = (ForeignKeyConstraint) i.next();
+ if(!undo)
+ dropSetCascade = "ALTER TABLE "+currentFK.getChildTable().getName()+" ADD CONSTRAINT "+currentFK.getName()+" FOREIGN KEY ("+currentFK.getParentColumns().get(0).getName()+" ) REFERENCES "+currentFK.getParentTable().getName()+"("+currentFK.getChildColumns().get(0).getName()+") ON UPDATE CASCADE";
+ else
+ {
+ dropSetCascade = "ALTER TABLE "+currentFK.getChildTable().getName()+" ADD CONSTRAINT "+currentFK.getName()+" FOREIGN KEY ("+currentFK.getParentColumns().get(0).getName()+" ) REFERENCES "+currentFK.getParentTable().getName()+"("+currentFK.getChildColumns().get(0).getName()+")";
+ }
+ try
+ {
+ PreparedStatement stmt = analyzer.getSqlService().prepareStatement(dropSetCascade, analyzer.getDb(),null);
+ stmt.execute();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
}
+
+ }
if(!undo)
LOGGER.info("temporary set all fk constraints to cascade");
else
@@ -306,7 +307,6 @@ public class DBFuzzer
*/
public GenericTreeNode chooseNextMutation()
{
- GenericTreeNode nextMut = null;
GenericTreeNode previousMutation = mutationTree.getLastMutation();
int markingDiff = previousMutation.getInterest_mark();
Random rand = new Random();
@@ -321,24 +321,27 @@ public class DBFuzzer
if (markingDiff > 0) //
{
int randNumber = rand.nextInt(previousMutation.getPotential_changes().size());
- nextMut = new GenericTreeNode(previousMutation.getPost_change_row(), nextId(), mutationTree.getRoot(), previousMutation,false,analyzer.getSqlService());
+ GenericTreeNode nextMut = new GenericTreeNode(previousMutation.getPost_change_row(), nextId(), mutationTree.getRoot(), previousMutation,false,analyzer.getSqlService());
nextMut.setChosenChange(previousMutation.getPotential_changes().get(randNumber));
nextMut.initPostChangeRow();
+ return nextMut;
}
else if (markingDiff == 0 || markingDiff < 0)
{
- Random changeOrDepthen = new Random();
+ Random changeOrDepthen = new Random(); // 1 is same row
if(changeOrDepthen.nextInt(2) == 1)
{
SingleChange tmp = mutationTree.getRoot().singleChangeBasedOnWeight();
- nextMut = new GenericTreeNode(tmp.getAttachedToMutation().getPost_change_row(), nextId(), mutationTree.getRoot(), tmp.getAttachedToMutation(),false,analyzer.getSqlService());
+ GenericTreeNode nextMut = new GenericTreeNode(tmp.getAttachedToMutation().getPost_change_row(), nextId(), mutationTree.getRoot(), tmp.getAttachedToMutation(),false,analyzer.getSqlService());
nextMut.setChosenChange(tmp);
nextMut.initPostChangeRow();
+ return nextMut;
}
else
{
Row nextRow;
+ GenericTreeNode nextMut;
do
{
nextRow = pickRandomRow();
@@ -348,12 +351,13 @@ public class DBFuzzer
Random nextSingleChangeId = new Random();
nextMut.setChosenChange(nextMut.getPotential_changes().get(nextSingleChangeId.nextInt(nextMut.getPotential_changes().size())));
nextMut.initPostChangeRow();
+ return nextMut;
}
}
else
System.out.println("I mean What Da Heck");
}
- return nextMut;
+ throw new Error("No mutation returned. That should not happen");
}
public boolean isNewMutation(GenericTreeNode newMut)
@@ -367,23 +371,23 @@ public class DBFuzzer
public void printMutationTree()
{
- String displayer = null ;
- for(int i = 1; i <= mutationTree.getNumberOfNodes();i++)
- {
- for(int j = 0; j < mutationTree.find(i).getDepth();j++)
- {
- displayer = displayer+("--");
- }
- displayer = displayer+(mutationTree.find(i).toString()+"\n");
- }
- System.out.println(displayer);
+ if(mutationTree.getNumberOfNodes() == 0)
+ System.out.println("tree is empty");
+ else {
+ String displayer = "";
+ for (int i = 1; i <= mutationTree.getNumberOfNodes(); i++) {
+ for (int j = 0; j < mutationTree.find(i).getDepth(); j++) {
+ displayer = displayer + ("--");
+ }
+ displayer = displayer + (mutationTree.find(i).toString() + "\n");
+ }
+ System.out.println(displayer);
+ }
}
public int nextId()
{
- int res = 0;
- res = mutationTree.getLastId()+1;
- return res;
+ return mutationTree.getLastId()+1;
}
diff --git a/src/main/java/org/schemaspy/model/GenericTreeNode.java b/src/main/java/org/schemaspy/model/GenericTreeNode.java
index bd69fbb..9d5fbff 100644
--- a/src/main/java/org/schemaspy/model/GenericTreeNode.java
+++ b/src/main/java/org/schemaspy/model/GenericTreeNode.java
@@ -268,7 +268,7 @@ public class GenericTreeNode {
case "character":
case "character varying":
- case "bytea":
+ //case "bytea":
case "varchar":
tmp = rootForThisMutation.getInitial_state_row().getContent().get(tableColumn.getName());
if(typeName.equals("bytea"))
@@ -276,17 +276,14 @@ public class GenericTreeNode {
byte[] bytes = SerializationUtils.serialize(tmp);
tmp = Arrays.toString(bytes);
}
- if(tmp != null && tmp.toString() != "" )
- {
- String tmp2 = tmp.toString().replaceAll("\\d", "");
- if (!tmp2.isEmpty())
- {
+ if(tmp != null && !tmp.toString().isEmpty() )
+ {
+ String tmp2 = tmp.toString();
char nextChar = (char) (tmp2.charAt(0) + 1);
char prevChar = (char) (tmp2.charAt(0) - 1);
oneChange.add(new SingleChange(tableColumn, this, column_value, (Character.toString(nextChar) + column_value.toString().substring(1))));
oneChange.add(new SingleChange(tableColumn, this, column_value, (Character.toString(prevChar) + column_value.toString().substring(1))));
- }
}
break;
@@ -303,10 +300,10 @@ public class GenericTreeNode {
{
Random rand = new Random();
int randNum = rand.nextInt(tmp.toString().length());
- char tmp2 = tmp.toString().charAt(randNum);
+ char tmp3 = tmp.toString().charAt(randNum);
- oneChange.add(new SingleChange(tableColumn, this, column_value, tmp.toString().substring(0,randNum) + (Character.toString(tmp2++)) + tmp.toString().substring(randNum+1)));
- oneChange.add(new SingleChange(tableColumn, this, column_value, tmp.toString().substring(0,randNum) + (Character.toString(tmp2--)) + tmp.toString().substring(randNum+1)));
+ oneChange.add(new SingleChange(tableColumn, this, column_value, tmp.toString().substring(0,randNum) + (Character.toString(tmp3++)) + tmp.toString().substring(randNum+1)));
+ oneChange.add(new SingleChange(tableColumn, this, column_value, tmp.toString().substring(0,randNum) + (Character.toString(tmp3--)) + tmp.toString().substring(randNum+1)));
break;
}
@@ -351,7 +348,7 @@ public class GenericTreeNode {
return oneChange;
}
- public boolean inject(SqlService sqlService,Database db, boolean undo)
+ public int inject(SqlService sqlService,Database db, boolean undo)
{
String theQuery = "";
if (undo)
@@ -368,14 +365,13 @@ public class GenericTreeNode {
try
{
Statement stmt = sqlService.getConnection().createStatement();
- stmt.execute(theQuery);
- System.out.println("Query success");
- return true;
+ int nbUpdates = stmt.executeUpdate(theQuery);
+ return nbUpdates;
}
catch (Exception e)
{
System.out.println("Error ! : Mutation Canceled"+e); // temporerally putting aside the Unique constraint brought by the tranfertToParent Mechanic
- return false;
+ return 0;
}
}
@@ -385,7 +381,7 @@ public class GenericTreeNode {
this.post_change_row.setValueOfColumn(chosenChange.getParentTableColumn().getName(), chosenChange.getNewValue());
}
- public boolean undo(SqlService sqlService,Database db)
+ public int undo(SqlService sqlService,Database db)
{
try
{
@@ -394,7 +390,7 @@ public class GenericTreeNode {
catch(Exception e)
{
e.printStackTrace();
- return false;
+ return 0;
}
}
@@ -524,12 +520,15 @@ public class GenericTreeNode {
for(GenericTreeNode node : goingUp )
{
- node.undo(analyzer.getSqlService(),analyzer.getDb());
+ if(node.undo(analyzer.getSqlService(),analyzer.getDb()) > 0);
+ System.out.println("success");
}
for(GenericTreeNode node : goingDown )
{
- node.inject(analyzer.getSqlService(),analyzer.getDb(), false);
+ if(node.inject(analyzer.getSqlService(),analyzer.getDb(), false) > 0);
+ System.out.println("success");
+
}
return true;
@@ -829,7 +828,6 @@ public class GenericTreeNode {
|| column.getTypeName().equals("date")
|| column.getTypeName().equals("_text")
|| column.getTypeName().equals("text")
- || column.getTypeName().equals("fulltext")
|| column.getTypeName().equals("email")
|| column.getTypeName().equals("bytea"))
return 1;
diff --git a/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java b/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java
index afe70c5..fb77f06 100644
--- a/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java
+++ b/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java
@@ -189,14 +189,13 @@ public class GenericTreeNodeTest extends AbstractTestExecutionListener {
QueryResponseParser parser = new QueryResponseParser();
QueryResponse response = parser.parse(rs,database.getTablesMap().get("actual_test_table"));
- Row row = response.getRows().get(0);
GenericTreeNode tmpMutation1 = new GenericTreeNode(response.getRows().get(0),1,sqlService);
tmpMutation1.setChosenChange(tmpMutation1.getPotential_changes().get(0));
tmpMutation1.initPostChangeRow();
- Assert.assertTrue(tmpMutation1.inject(sqlService,database,false)); //Test
+ Assert.assertTrue(tmpMutation1.inject(sqlService,database,false) > 0 ); //Test
rs = stmt.executeQuery();
response = parser.parse(rs,database.getTablesMap().get("actual_test_table"));
@@ -205,7 +204,7 @@ public class GenericTreeNodeTest extends AbstractTestExecutionListener {
System.out.println(tmpMutation1.getPost_change_row());
Assert.assertTrue(response.getRows().get(0).compare(tmpMutation1.getPost_change_row()));
- Assert.assertTrue(tmpMutation1.undo(sqlService,database)); //Test
+ Assert.assertTrue(tmpMutation1.undo(sqlService,database) > 0); //Test
rs = stmt.executeQuery();
response = parser.parse(rs,database.getTablesMap().get("actual_test_table"));