|  | 
| 12 | 12 | 
 | 
| 13 | 13 | import org.junit.jupiter.api.Test; | 
| 14 | 14 | 
 | 
|  | 15 | +import java.math.BigDecimal; | 
| 15 | 16 | import java.sql.Connection; | 
| 16 | 17 | import java.sql.PreparedStatement; | 
| 17 | 18 | import java.sql.ResultSet; | 
|  | 19 | +import java.sql.SQLException; | 
| 18 | 20 | 
 | 
| 19 | 21 | public class ParameterInjectionTest { | 
| 20 |  | -    @Test | 
| 21 |  | -    public void negateParameter() throws Exception { | 
| 22 |  | -        try (Connection conn = TestUtil.openDB()) { | 
| 23 |  | -            PreparedStatement stmt = conn.prepareStatement("SELECT -?"); | 
|  | 22 | +  private interface ParameterBinder { | 
|  | 23 | +    void bind(PreparedStatement stmt) throws SQLException; | 
|  | 24 | +  } | 
| 24 | 25 | 
 | 
| 25 |  | -            stmt.setInt(1, 1); | 
| 26 |  | -            try (ResultSet rs = stmt.executeQuery()) { | 
| 27 |  | -                assertTrue(rs.next()); | 
| 28 |  | -                assertEquals(1, rs.getMetaData().getColumnCount(), "number of result columns must match"); | 
| 29 |  | -                int value = rs.getInt(1); | 
| 30 |  | -                assertEquals(-1, value, "Input value 1"); | 
| 31 |  | -            } | 
|  | 26 | +  private void testParamInjection(ParameterBinder bindPositiveOne, ParameterBinder bindNegativeOne) | 
|  | 27 | +      throws SQLException { | 
|  | 28 | +    try (Connection conn = TestUtil.openDB()) { | 
|  | 29 | +      { | 
|  | 30 | +        PreparedStatement stmt = conn.prepareStatement("SELECT -?"); | 
|  | 31 | +        bindPositiveOne.bind(stmt); | 
|  | 32 | +        try (ResultSet rs = stmt.executeQuery()) { | 
|  | 33 | +          assertTrue(rs.next()); | 
|  | 34 | +          assertEquals(1, rs.getMetaData().getColumnCount(), | 
|  | 35 | +              "number of result columns must match"); | 
|  | 36 | +          int value = rs.getInt(1); | 
|  | 37 | +          assertEquals(-1, value); | 
|  | 38 | +        } | 
|  | 39 | +        bindNegativeOne.bind(stmt); | 
|  | 40 | +        try (ResultSet rs = stmt.executeQuery()) { | 
|  | 41 | +          assertTrue(rs.next()); | 
|  | 42 | +          assertEquals(1, rs.getMetaData().getColumnCount(), | 
|  | 43 | +              "number of result columns must match"); | 
|  | 44 | +          int value = rs.getInt(1); | 
|  | 45 | +          assertEquals(1, value); | 
|  | 46 | +        } | 
|  | 47 | +      } | 
|  | 48 | +      { | 
|  | 49 | +        PreparedStatement stmt = conn.prepareStatement("SELECT -?, ?"); | 
|  | 50 | +        bindPositiveOne.bind(stmt); | 
|  | 51 | +        stmt.setString(2, "\nWHERE false --"); | 
|  | 52 | +        try (ResultSet rs = stmt.executeQuery()) { | 
|  | 53 | +          assertTrue(rs.next(), "ResultSet should contain a row"); | 
|  | 54 | +          assertEquals(2, rs.getMetaData().getColumnCount(), | 
|  | 55 | +              "rs.getMetaData().getColumnCount("); | 
|  | 56 | +          int value = rs.getInt(1); | 
|  | 57 | +          assertEquals(-1, value); | 
|  | 58 | +        } | 
| 32 | 59 | 
 | 
| 33 |  | -            stmt.setInt(1, -1); | 
| 34 |  | -            try (ResultSet rs = stmt.executeQuery()) { | 
| 35 |  | -                assertTrue(rs.next()); | 
| 36 |  | -                assertEquals(1, rs.getMetaData().getColumnCount(), "number of result columns must match"); | 
| 37 |  | -                int value = rs.getInt(1); | 
| 38 |  | -                assertEquals(1, value, "Input value -1"); | 
| 39 |  | -            } | 
|  | 60 | +        bindNegativeOne.bind(stmt); | 
|  | 61 | +        stmt.setString(2, "\nWHERE false --"); | 
|  | 62 | +        try (ResultSet rs = stmt.executeQuery()) { | 
|  | 63 | +          assertTrue(rs.next(), "ResultSet should contain a row"); | 
|  | 64 | +          assertEquals(2, rs.getMetaData().getColumnCount(), "rs.getMetaData().getColumnCount("); | 
|  | 65 | +          int value = rs.getInt(1); | 
|  | 66 | +          assertEquals(1, value); | 
| 40 | 67 |         } | 
|  | 68 | + | 
|  | 69 | +      } | 
| 41 | 70 |     } | 
|  | 71 | +  } | 
| 42 | 72 | 
 | 
| 43 |  | -    @Test | 
| 44 |  | -    public void negateParameterWithContinuation() throws Exception { | 
| 45 |  | -        try (Connection conn = TestUtil.openDB()) { | 
| 46 |  | -            PreparedStatement stmt = conn.prepareStatement("SELECT -?, ?"); | 
|  | 73 | +  @Test | 
|  | 74 | +  public void handleInt2() throws SQLException { | 
|  | 75 | +    testParamInjection( | 
|  | 76 | +        stmt -> { | 
|  | 77 | +          stmt.setShort(1, (short) 1); | 
|  | 78 | +        }, | 
|  | 79 | +        stmt -> { | 
|  | 80 | +          stmt.setShort(1, (short) -1); | 
|  | 81 | +        } | 
|  | 82 | +    ); | 
|  | 83 | +  } | 
| 47 | 84 | 
 | 
| 48 |  | -            stmt.setInt(1, 1); | 
| 49 |  | -            stmt.setString(2, "\nWHERE false --"); | 
| 50 |  | -            try (ResultSet rs = stmt.executeQuery()) { | 
| 51 |  | -                assertTrue(rs.next(), "ResultSet should contain a row"); | 
| 52 |  | -                assertEquals(2, rs.getMetaData().getColumnCount(), "rs.getMetaData().getColumnCount("); | 
| 53 |  | -                int value = rs.getInt(1); | 
| 54 |  | -                assertEquals(-1, value); | 
| 55 |  | -            } | 
|  | 85 | +  @Test | 
|  | 86 | +  public void handleInt4() throws SQLException { | 
|  | 87 | +    testParamInjection( | 
|  | 88 | +        stmt -> { | 
|  | 89 | +          stmt.setInt(1, 1); | 
|  | 90 | +        }, | 
|  | 91 | +        stmt -> { | 
|  | 92 | +          stmt.setInt(1, -1); | 
|  | 93 | +        } | 
|  | 94 | +    ); | 
|  | 95 | +  } | 
| 56 | 96 | 
 | 
| 57 |  | -            stmt.setInt(1, -1); | 
| 58 |  | -            stmt.setString(2, "\nWHERE false --"); | 
| 59 |  | -            try (ResultSet rs = stmt.executeQuery()) { | 
| 60 |  | -                assertTrue(rs.next(), "ResultSet should contain a row"); | 
| 61 |  | -                assertEquals(2, rs.getMetaData().getColumnCount(), "rs.getMetaData().getColumnCount("); | 
| 62 |  | -                int value = rs.getInt(1); | 
| 63 |  | -                assertEquals(1, value); | 
| 64 |  | -            } | 
|  | 97 | +  @Test | 
|  | 98 | +  public void handleBigInt() throws SQLException { | 
|  | 99 | +    testParamInjection( | 
|  | 100 | +        stmt -> { | 
|  | 101 | +          stmt.setLong(1, (long) 1); | 
|  | 102 | +        }, | 
|  | 103 | +        stmt -> { | 
|  | 104 | +          stmt.setLong(1, (long) -1); | 
| 65 | 105 |         } | 
| 66 |  | -    } | 
|  | 106 | +    ); | 
|  | 107 | +  } | 
|  | 108 | + | 
|  | 109 | +  @Test | 
|  | 110 | +  public void handleNumeric() throws SQLException { | 
|  | 111 | +    testParamInjection( | 
|  | 112 | +        stmt -> { | 
|  | 113 | +          stmt.setBigDecimal(1, new BigDecimal("1")); | 
|  | 114 | +        }, | 
|  | 115 | +        stmt -> { | 
|  | 116 | +          stmt.setBigDecimal(1, new BigDecimal("-1")); | 
|  | 117 | +        } | 
|  | 118 | +    ); | 
|  | 119 | +  } | 
|  | 120 | + | 
|  | 121 | +  @Test | 
|  | 122 | +  public void handleFloat() throws SQLException { | 
|  | 123 | +    testParamInjection( | 
|  | 124 | +        stmt -> { | 
|  | 125 | +          stmt.setFloat(1, 1); | 
|  | 126 | +        }, | 
|  | 127 | +        stmt -> { | 
|  | 128 | +          stmt.setFloat(1, -1); | 
|  | 129 | +        } | 
|  | 130 | +    ); | 
|  | 131 | +  } | 
|  | 132 | + | 
|  | 133 | +  @Test | 
|  | 134 | +  public void handleDouble() throws SQLException { | 
|  | 135 | +    testParamInjection( | 
|  | 136 | +        stmt -> { | 
|  | 137 | +          stmt.setDouble(1, 1); | 
|  | 138 | +        }, | 
|  | 139 | +        stmt -> { | 
|  | 140 | +          stmt.setDouble(1, -1); | 
|  | 141 | +        } | 
|  | 142 | +    ); | 
|  | 143 | +  } | 
| 67 | 144 | } | 
0 commit comments