Search in sources :

Example 1 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class SimplePacketReaderTest method basicHeaderRead.

// the basic operation: make sure header bytes are interpreted properly
@Test
public void basicHeaderRead() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    maxAllowedPacket.setValue(100000);
    // mix up the bits so we know they're interpreted correctly
    SocketConnection connection = new FixedBufferSocketConnection(new byte[] { 3, 2, 1, 42 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    assertEquals(-1, reader.getMessageSequence());
    NativePacketHeader hdr = reader.readHeader();
    assertEquals(65536 + 512 + 3, hdr.getMessageSize());
    assertEquals(42, hdr.getMessageSequence());
    assertEquals(42, reader.getMessageSequence());
    reader.resetMessageSequence();
    assertEquals(0, reader.getMessageSequence());
}
Also used : SocketConnection(com.mysql.cj.protocol.SocketConnection) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Example 2 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class SimplePacketReaderTest method exceedMaxAllowedPacketHeaderRead.

// test checking of maxAllowedPacket
@Test
public void exceedMaxAllowedPacketHeaderRead() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    maxAllowedPacket.setValue(1024);
    // read header with packet size = maxAllowedPacket => SUCCESS
    MockSocketConnection connection = new FixedBufferSocketConnection(new byte[] { 0, 4, 0, 42 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    NativePacketHeader hdr = reader.readHeader();
    assertEquals(1024, hdr.getMessageSize());
    // read header with packet size = maxAllowedPacket + 1 => ERROR
    connection = new FixedBufferSocketConnection(new byte[] { 1, 4, 0, 42 });
    reader = new SimplePacketReader(connection, maxAllowedPacket);
    try {
        reader.readHeader();
        fail("Should throw exception as packet size exceeds maxAllowedPacket");
    } catch (CJPacketTooBigException ex) {
        assertTrue(connection.forceClosed, "Connection should be force closed when maxAllowedPacket is exceeded");
    }
}
Also used : CJPacketTooBigException(com.mysql.cj.exceptions.CJPacketTooBigException) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Example 3 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class SimplePacketReaderTest method truncatedPacketHeaderRead.

// we only supply 3 bytes when 4 are needed
@Test
public void truncatedPacketHeaderRead() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    MockSocketConnection connection = new FixedBufferSocketConnection(new byte[] { 3, 2, 1 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    try {
        reader.readHeader();
        fail("Should throw an exception when we can't read the full header");
    } catch (EOFException ex) {
        assertTrue(connection.forceClosed, "Connection should be force closed when header read fails");
    }
}
Also used : EOFException(java.io.EOFException) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Example 4 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class SimplePacketReaderTest method readBasicPayload.

// trivial payload test
@Test
public void readBasicPayload() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    SocketConnection connection = new FixedBufferSocketConnection(new byte[] { 3, 2, 1, 6, 5, 4 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    NativePacketPayload b = reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
    assertEquals(3, b.getByteBuffer()[0]);
    assertEquals(2, b.getByteBuffer()[1]);
    assertEquals(1, b.getByteBuffer()[2]);
    // make sure the first only consumed the requested 3 bytes
    b = reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
    assertEquals(6, b.getByteBuffer()[0]);
    assertEquals(5, b.getByteBuffer()[1]);
    assertEquals(4, b.getByteBuffer()[2]);
}
Also used : SocketConnection(com.mysql.cj.protocol.SocketConnection) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Example 5 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class ResultSetRegressionTest method tstBug24525461testBytes.

private void tstBug24525461testBytes(Properties props, boolean testJSON, Statement st) throws Exception {
    st.executeUpdate("truncate table testBug24525461");
    String fGeomFromText = versionMeetsMinimum(5, 6, 1) ? "ST_GeomFromText" : "GeomFromText";
    StringBuilder sb = new StringBuilder("INSERT INTO testBug24525461 values(0, 1, 1, 1, 1, 1, 1, 1, '2000-01-01 00:00:00', 1, 1, '2000-01-01', '12:00:00', '2000-01-01 00:00:00', 2000, 'aaa'," + " 1, 1, 'x', 'a', 1, '1', 1 , '1', 1, '1', 1, '1', '1', 1, " + fGeomFromText + "('POINT(1 1)'), " + fGeomFromText + "('POINT(2 2)')," + " _utf8 'aaa', _utf8 'aaa', 'aaa', 'aaa', 1, 1, 'aaa', 'aaa', 'aaa', _utf8 'aaa', _utf8 'aaa', '1', null");
    if (testJSON) {
        sb.append(", '{\"key1\": \"value1\"}'");
    }
    sb.append(")");
    st.executeUpdate(sb.toString());
    System.out.println(" with params = " + props);
    Connection con = getConnectionWithProps(props);
    PreparedStatement testPstmt = con.prepareStatement("SELECT * FROM testBug24525461", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs1 = testPstmt.executeQuery();
    assertTrue(rs1.next());
    // check that other fields are refreshed properly
    rs1.updateInt(2, 10);
    rs1.updateRow();
    tstBug24525461assertResults1(testJSON, st);
    // check that all fields are set as expected
    Calendar cal = Calendar.getInstance();
    cal.set(2002, 01, 02, 10, 30, 0);
    cal.set(Calendar.MILLISECOND, 0);
    rs1.updateBigDecimal(2, BigDecimal.valueOf(20));
    rs1.updateInt(3, 2);
    rs1.updateBoolean(4, false);
    rs1.updateShort(5, (short) 2);
    rs1.updateInt(6, 2);
    rs1.updateFloat(7, 2);
    rs1.updateDouble(8, 2);
    // f08 TIMESTAMP
    rs1.updateTimestamp(9, new Timestamp(cal.getTimeInMillis()));
    rs1.updateLong(10, 2L);
    rs1.updateInt(11, 2);
    // f11 DATE
    rs1.updateDate(12, new Date(cal.getTimeInMillis()));
    // f12 TIME
    rs1.updateTime(13, new Time(cal.getTimeInMillis()));
    // f13 DATETIME
    rs1.updateTimestamp(14, new Timestamp(cal.getTimeInMillis()));
    // f14 YEAR
    rs1.updateInt(15, 2002);
    rs1.updateNString(16, "bbb");
    // f16 VARBINARY(30)
    rs1.updateBytes(17, new byte[] { 50 });
    // f17 BIT
    rs1.updateByte(18, (byte) 0);
    rs1.updateString(19, "y");
    rs1.updateString(20, "b");
    rs1.updateBlob(21, new com.mysql.cj.jdbc.Blob("2".getBytes(), null));
    rs1.updateClob(22, new com.mysql.cj.jdbc.Clob("2", null));
    rs1.updateBlob(23, new ByteArrayInputStream(new byte[] { 50 }));
    rs1.updateClob(24, new StringReader("2"));
    rs1.updateBlob(25, new ByteArrayInputStream(new byte[] { 50, 51, 52 }), 1);
    rs1.updateClob(26, new StringReader("2222"), 1);
    rs1.updateObject(27, "2", MysqlType.BLOB);
    rs1.updateNClob(28, new com.mysql.cj.jdbc.NClob("2", null));
    rs1.updateString(29, "2");
    rs1.updateBytes(30, new byte[] { 50 });
    Object p1 = rs1.getObject(31);
    Object p2 = rs1.getObject(32);
    rs1.updateObject(31, p2);
    rs1.updateObject(32, p1);
    rs1.updateNClob(33, new StringReader("bbb"));
    rs1.updateNClob(34, new StringReader("bbbbbb"), 3);
    rs1.updateAsciiStream(35, new ByteArrayInputStream("bbb".getBytes()));
    rs1.updateAsciiStream(36, new ByteArrayInputStream("bbbbbb".getBytes()), 3);
    rs1.updateBinaryStream(37, new ByteArrayInputStream(new byte[] { 50 }));
    rs1.updateBinaryStream(38, new ByteArrayInputStream(new byte[] { 50, 51, 52 }), 1);
    rs1.updateCharacterStream(39, new StringReader("bbb"));
    rs1.updateCharacterStream(40, new StringReader("bbbbbb"), 3);
    rs1.updateCharacterStream(41, new StringReader("bbbbbb"), 3L);
    rs1.updateNCharacterStream(42, new StringReader("bbb"));
    rs1.updateNCharacterStream(43, new StringReader("bbbbbb"), 3);
    rs1.updateNull(44);
    SQLXML xml = new MysqlSQLXML(null, new JdbcPropertySetImpl());
    xml.setString("<doc/>");
    rs1.updateSQLXML(45, xml);
    if (testJSON) {
        // f18 JSON
        rs1.updateObject(46, "{\"key2\": \"value2\"}");
    }
    rs1.updateRow();
    tstBug24525461assertResults2(testJSON, st);
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) PreparedStatement(java.sql.PreparedStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) Time(java.sql.Time) ZonedDateTime(java.time.ZonedDateTime) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) Timestamp(java.sql.Timestamp) InternalDate(com.mysql.cj.protocol.InternalDate) LocalDate(java.time.LocalDate) Date(java.sql.Date) SQLXML(java.sql.SQLXML) MysqlSQLXML(com.mysql.cj.jdbc.MysqlSQLXML) MysqlSQLXML(com.mysql.cj.jdbc.MysqlSQLXML) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) UpdatableResultSet(com.mysql.cj.jdbc.result.UpdatableResultSet) StringReader(java.io.StringReader) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl)

Aggregations

JdbcPropertySetImpl (com.mysql.cj.jdbc.JdbcPropertySetImpl)8 Test (org.junit.jupiter.api.Test)6 SocketConnection (com.mysql.cj.protocol.SocketConnection)2 EOFException (java.io.EOFException)2 MysqlConnection (com.mysql.cj.MysqlConnection)1 CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)1 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)1 JdbcPropertySet (com.mysql.cj.jdbc.JdbcPropertySet)1 MysqlSQLXML (com.mysql.cj.jdbc.MysqlSQLXML)1 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)1 UpdatableResultSet (com.mysql.cj.jdbc.result.UpdatableResultSet)1 InternalDate (com.mysql.cj.protocol.InternalDate)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ByteBuffer (java.nio.ByteBuffer)1 Connection (java.sql.Connection)1 Date (java.sql.Date)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1