|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import java.security.Security; |
|
import java.sql.Connection; |
|
import java.sql.DriverManager; |
|
import java.sql.SQLException; |
|
|
|
import org.junit.After; |
|
import org.junit.AfterClass; |
|
import org.junit.Before; |
|
import org.junit.BeforeClass; |
|
import org.junit.Test; |
|
import org.testcontainers.containers.PostgreSQLContainer; |
|
|
|
/** |
|
* Test PGPCrypto interoperability of BouncyCastle's OpenPGP implementation and the PostgreSQL pgcrypto module. |
|
* |
|
* @author rob |
|
* |
|
*/ |
|
public class PGPCryptoTests |
|
{ |
|
private static final String Postgres_Docker_Image = "postgres:9.6"; |
|
private static final String MESSAGE = "Rob Sian Nova"; |
|
private static final String PASS = "si vous reussissez, vous serez bientôt couvert de gloire"; |
|
|
|
Connection conn; // @Rule managed |
|
static PostgreSQLContainer<?> pg; // @Rule managed |
|
|
|
@BeforeClass |
|
public static void startDocker() throws SQLException |
|
{ |
|
pg = new PostgreSQLContainer(Postgres_Docker_Image); |
|
pg.start(); |
|
createConnection().prepareStatement("CREATE EXTENSION pgcrypto;").execute(); |
|
} |
|
|
|
@AfterClass |
|
public static void stopDocker() |
|
{ |
|
pg.stop(); |
|
} |
|
|
|
private static Connection createConnection() throws SQLException |
|
{ |
|
return DriverManager.getConnection("jdbc:postgresql://" + pg.getContainerIpAddress() + ":" + pg.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT) + "/test", "test", "test"); |
|
} |
|
|
|
@Before |
|
public void setConnection() throws SQLException |
|
{ |
|
conn = createConnection(); |
|
} |
|
|
|
@After |
|
public void closeConnection() |
|
{ |
|
try { conn.close(); } |
|
catch (SQLException e) {} |
|
} |
|
|
|
@Test |
|
public void testPGCryptoForwardReverse() |
|
{ |
|
String pg_encrypted_armoured = PGPCrypto.encrypt(conn, MESSAGE, PASS); |
|
System.out.println(pg_encrypted_armoured); |
|
|
|
String plain = PGPCrypto.decrypt(conn, pg_encrypted_armoured, PASS); |
|
System.out.println(plain); |
|
|
|
assertThat(plain).isEqualTo(MESSAGE); |
|
} |
|
|
|
@Test |
|
public void testBCCryptoForwardReverse() |
|
{ |
|
String encrypted_armoured = PGPCrypto.encrypt(MESSAGE, PASS); |
|
System.out.println(encrypted_armoured); |
|
|
|
String plain = PGPCrypto.decrypt(encrypted_armoured, PASS); |
|
System.out.println(plain); |
|
|
|
assertThat(plain).isEqualTo(MESSAGE); |
|
} |
|
|
|
@Test |
|
public void testPGencBCdec() |
|
{ |
|
String encrypted_armoured = PGPCrypto.encrypt(conn, MESSAGE, PASS); |
|
System.out.println(encrypted_armoured); |
|
|
|
String plain = PGPCrypto.decrypt(encrypted_armoured, PASS); |
|
System.out.println(plain); |
|
|
|
assertThat(plain).isEqualTo(MESSAGE); |
|
} |
|
|
|
@Test |
|
public void testBCencPGdec() |
|
{ |
|
String encrypted_armoured = PGPCrypto.encrypt(MESSAGE, PASS); |
|
System.out.println(encrypted_armoured); |
|
|
|
String plain = PGPCrypto.decrypt(conn, encrypted_armoured, PASS); |
|
System.out.println(plain); |
|
|
|
assertThat(plain).isEqualTo(MESSAGE); |
|
} |
|
|
|
@Test |
|
public void testUTF8Samples() |
|
{ |
|
for(String msg : new String[]{ |
|
"მინას ვჭამ და არა მტკივა." // georgian |
|
,"ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ" // old english rune |
|
,"На берегу пустынных волн" // russian cyrillic |
|
,"நான் கண்ணாடி சாப்பிடுவேன், அதனால் எனக்கு ஒரு கேடும் வராது." // tamil |
|
,"ᐊᓕᒍᖅ ᓂᕆᔭᕌᖓᒃᑯ ᓱᕋᙱᑦᑐᓐᓇᖅᑐᖓ" // Inuktitut |
|
,"나는 유리를 먹을 수 있어요. 그래도 아프지 않아요" // korean |
|
,"私はガラスを食べられます。それは私を傷つけません。" // jp |
|
}) |
|
{ |
|
assertThat(PGPCrypto.decrypt(conn, PGPCrypto.encrypt(msg, PASS), PASS)).isEqualTo(msg); |
|
assertThat(PGPCrypto.decrypt(PGPCrypto.encrypt(conn, msg, PASS), PASS)).isEqualTo(msg); |
|
} |
|
} |
|
|
|
|
|
static { |
|
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); |
|
} |
|
} |