Skip to content

Commit b473259

Browse files
committed
Test if inserted data is correctly retrieved after being escaped
1 parent e6bf23a commit b473259

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Diff for: driver_test.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ func TestSqlInjection(t *testing.T) {
15631563
} else if err == nil {
15641564
dbt.Errorf("Sql injection successful with arg: %s", arg)
15651565
} else {
1566-
dbt.Errorf("Error running query with arg: %s; err: %s", err.Error())
1566+
dbt.Errorf("Error running query with arg: %s; err: %s", arg, err.Error())
15671567
}
15681568
}
15691569
}
@@ -1577,3 +1577,32 @@ func TestSqlInjection(t *testing.T) {
15771577
runTests(t, testdsn, createTest("' OR '1'='1"))
15781578
}
15791579
}
1580+
1581+
// Test if inserted data is correctly retrieved after being escaped
1582+
func TestInsertRetrieveEscapedData(t *testing.T) {
1583+
testData := func(dbt *DBTest) {
1584+
dbt.mustExec("CREATE TABLE test (v VARCHAR(255))")
1585+
1586+
// All sequences that are escaped by EscapeQuotes and EscapeString
1587+
v := "foo \x00\n\r\x1a\"'\\"
1588+
dbt.mustExec("INSERT INTO test VALUES (?)", v)
1589+
1590+
var out string
1591+
err := dbt.db.QueryRow("SELECT v FROM test").Scan(&out)
1592+
if err != nil {
1593+
dbt.Fatalf("%s", err.Error())
1594+
}
1595+
1596+
if out != v {
1597+
dbt.Errorf("%q != %q", out, v)
1598+
}
1599+
}
1600+
1601+
dsns := []string{
1602+
dsn,
1603+
dsn + "&sql_mode=NO_BACKSLASH_ESCAPES",
1604+
}
1605+
for _, testdsn := range dsns {
1606+
runTests(t, testdsn, testData)
1607+
}
1608+
}

0 commit comments

Comments
 (0)