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());
}
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");
}
}
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");
}
}
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]);
}
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);
}
Aggregations