Permalink
Browse files

fix: allowEncodingChanges should allow set client_encoding=... (#1125)

fixes #1101
  • Loading branch information...
vlsi committed Mar 8, 2018
1 parent 1ca0c58 commit af64ed2dac035c621b4aec4a0471730457725194
@@ -5,21 +5,26 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Fixed
- Avoid failure for `insert ... on conflict...update` for `reWriteBatchedInserts=true` case [PR#1130](https://github.com/pgjdbc/pgjdbc/pull/1130)
- Avoid failure for `insert ... on conflict...update` for `reWriteBatchedInserts=true` case [PR 1130](https://github.com/pgjdbc/pgjdbc/pull/1130)
- fix: allowEncodingChanges should allow set client_encoding=... [PR 1125](https://github.com/pgjdbc/pgjdbc/pull/1125)
## [42.2.1] (2018-01-25)
### Known issues
- client_encoding has to be UTF8 even with allowEncodingChanges=true
### Changed
- socksProxyHost is ignored in case it contains empty string [PR 1079](https://github.com/pgjdbc/pgjdbc/pull/1079)
### Fixed
- Avoid connection failure when `DateStyle` is set to `ISO` (~PgBouncer) [Issue 1080](https://github.com/pgjdbc/pgjdbc/issues/1080)
- Package scram:client classes, so SCRAM works when using a shaded jar [PR#1091](https://github.com/pgjdbc/pgjdbc/pull/1091) [1a89290e](https://github.com/pgjdbc/pgjdbc/commit/1a89290e110d5863b35e0a2ccf79e4292c1056f8)
- Package scram:client classes, so SCRAM works when using a shaded jar [PR 1091](https://github.com/pgjdbc/pgjdbc/pull/1091) [1a89290e](https://github.com/pgjdbc/pgjdbc/commit/1a89290e110d5863b35e0a2ccf79e4292c1056f8)
- reWriteBatchedInserts=true causes syntax error with ON CONFLICT [Issue 1045](https://github.com/pgjdbc/pgjdbc/issues/1045) [PR 1082](https://github.com/pgjdbc/pgjdbc/pull/1082)
- Avoid failure in getPGArrayType when stringType=unspecified [PR 1036](https://github.com/pgjdbc/pgjdbc/pull/1036)
## [42.2.0] (2018-01-17)
### Known issues
- SCRAM does not work as scram:client library is not packaged
- client_encoding has to be UTF8 even with allowEncodingChanges=true
### Added
- Support SCRAM-SHA-256 for PostgreSQL 10 in the JDBC 4.2 version (Java 8+) using the Ongres SCRAM library. [PR 842](https://github.com/pgjdbc/pgjdbc/pull/842)
@@ -9,6 +9,7 @@ version: 42.2.0
### Known issues
- SCRAM authentication does not work as scram client classes are not packaged
- client_encoding has to be UTF8 even with allowEncodingChanges=true
### Added
- Support SCRAM-SHA-256 for PostgreSQL 10 in the JDBC 4.2 version (Java 8+) using the Ongres SCRAM library. [PR 842](https://github.com/pgjdbc/pgjdbc/pull/842)
@@ -7,6 +7,8 @@ version: 42.2.1
---
**Notable changes**
### Known issues
- client_encoding has to be UTF8 even with allowEncodingChanges=true
### Changed
- socksProxyHost is ignored in case it contains empty string [PR 1079](https://github.com/pgjdbc/pgjdbc/pull/1079)
@@ -2596,12 +2596,21 @@ public void receiveParameterStatus() throws IOException, SQLException {
LOGGER.log(Level.FINEST, " <=BE ParameterStatus({0} = {1})", new Object[]{name, value});
}
if (name.equals("client_encoding") && !value.equalsIgnoreCase("UTF8")
&& !allowEncodingChanges) {
close(); // we're screwed now; we can't trust any subsequent string.
throw new PSQLException(GT.tr(
"The server''s client_encoding parameter was changed to {0}. The JDBC driver requires client_encoding to be UTF8 for correct operation.",
value), PSQLState.CONNECTION_FAILURE);
if (name.equals("client_encoding")) {
if (allowEncodingChanges) {
if (!value.equalsIgnoreCase("UTF8") && !value.equalsIgnoreCase("UTF-8")) {
LOGGER.log(Level.WARNING,
"pgjdbc expects client_encoding to be UTF8 for proper operation. Actual encoding is {0}",
value);
}
pgStream.setEncoding(Encoding.getDatabaseEncoding(value));
} else if (!value.equalsIgnoreCase("UTF8") && !value.equalsIgnoreCase("UTF-8")) {
close(); // we're screwed now; we can't trust any subsequent string.
throw new PSQLException(GT.tr(
"The server''s client_encoding parameter was changed to {0}. The JDBC driver requires client_encoding to be UTF8 for correct operation.",
value), PSQLState.CONNECTION_FAILURE);
}
}
if (name.equals("DateStyle") && !value.startsWith("ISO")
@@ -2635,22 +2644,7 @@ public void receiveParameterStatus() throws IOException, SQLException {
setServerVersionNum(Integer.parseInt(value));
} else if ("server_version".equals(name)) {
setServerVersion(value);
} else if ("client_encoding".equals(name)) {
if (!"UTF8".equals(value)) {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
pgStream.setEncoding(Encoding.getDatabaseEncoding("UTF8"));
} else if ("standard_conforming_strings".equals(name)) {
if ("on".equals(value)) {
setStandardConformingStrings(true);
} else if ("off".equals(value)) {
setStandardConformingStrings(false);
} else {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
} else if ("integer_datetimes".equals(name)) {
} else if ("integer_datetimes".equals(name)) {
if ("on".equals(value)) {
setIntegerDateTimes(true);
} else if ("off".equals(value)) {
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2018, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc2;
import org.postgresql.PGProperty;
import org.postgresql.test.TestUtil;
import org.postgresql.util.PSQLState;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Properties;
@RunWith(Parameterized.class)
public class ClientEncodingTest extends BaseTest4 {
@Parameterized.Parameter(0)
public boolean allowEncodingChanges;
@Override
protected void updateProperties(Properties props) {
super.updateProperties(props);
PGProperty.ALLOW_ENCODING_CHANGES.set(props, allowEncodingChanges);
}
@Parameterized.Parameters(name = "allowEncodingChanges={0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{true},
{false}
});
}
@Test
public void setEncodingUtf8() throws SQLException {
// UTF-8 is a default encoding, so it should always be safe to set encoding to UTF-8
setEncoding("UTF-8");
checkConnectionSanity();
}
@Test
public void setEncodingAscii() throws SQLException {
try {
setEncoding("sql_ascii");
if (!allowEncodingChanges) {
Assert.fail(
"allowEncodingChanges is false, thus set client_encoding=aql_ascii is expected to fail");
}
} catch (SQLException e) {
if (!allowEncodingChanges && !PSQLState.CONNECTION_FAILURE.getState()
.equals(e.getSQLState())) {
throw e;
}
Assert.assertTrue("Connection should be closed on client_encoding change", con.isClosed());
return;
}
checkConnectionSanity();
}
private void checkConnectionSanity() throws SQLException {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select 'abc' as x");
rs.next();
Assert.assertEquals("abc", rs.getString(1));
TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(st);
}
private void setEncoding(String encoding) throws SQLException {
Statement st = con.createStatement();
st.execute("set client_encoding='" + encoding + "'");
TestUtil.closeQuietly(st);
}
}
@@ -93,6 +93,7 @@
MiscTest.class,
NotifyTest.class,
DatabaseEncodingTest.class,
ClientEncodingTest.class,
BlobTest.class,
BlobTransactionTest.class,

0 comments on commit af64ed2

Please sign in to comment.