Skip to content

add non-ascii connection string tests #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions test/non_ascii_connection_strings_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import 'dart:io';

import 'package:docker_process/containers/postgres.dart';
import 'package:path/path.dart' as p;
import 'package:postgres/postgres.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

void main() {
/// This cannot be run on CI as each test requires different pg_hba.conf
/// to be mounted to the container.
if (Platform.environment.containsKey('GITHUB_ACTION')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't really checked yet, but IIRC Github Action is able to run docker run and other commands... have you tried it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I believe it's possible but not with the current setup (e.g. using services.postgres)... I'll give it another shot later and try a different setup.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: a repository can have multiple workflows, and we can differentiate tests either via directory or tags.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also: thank you for looking into this!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that what I was thinking -- to tag tests with a solo tag and exclude them from common tests and then run all solo tests with concurrency=1.

test('NO NON-ASCII CONNECTION STRINGS TESTS ARE RUNNING.', () {
// no-op
});
return;
}
final user = 'abc@def';
final password = 'pöstgrēs_üšęr_pæsswœêrd';
final db = 'postgres';

group('non-ascii tests (clear password auth)', () {
late final DockerProcess docker;
final port = 54321;

setUpAll(
() async {
docker = await startPostgres(
name: 'non_ascii_test_clear_password',
version: 'latest',
pgPort: port,
pgDatabase: db,
pgUser: user,
pgPassword: password,
cleanup: true,
pgHbaConfPath: _createTempFile(
fileName: 'pg_hba.conf',
contents: _sampleHbaConfigPassword,
),
);
},
);

tearDownAll(() {
docker.stop();
docker.kill();
});
PostgreSQLConnection? conn;

setUp(() async {
conn = PostgreSQLConnection('localhost', port, db,
username: user, password: password, allowClearTextPassword: true);
await conn!.open();
});

tearDown(() async {
await conn?.close();
});

test('- Connect with non-ascii connection string', () async {
final res = await conn!.query('select 1;');
expect(res.length, 1);
});
});

group('non-ascii tests (md5 auth)', () {
late final DockerProcess docker;
final port = 54322;
setUpAll(
() async {
docker = await startPostgres(
name: 'non_ascii_test_md5',
version: 'latest',
pgPort: port,
pgDatabase: db,
pgUser: user,
pgPassword: password,
cleanup: true,
pgHbaConfPath: _createTempFile(
fileName: 'pg_hba.conf',
contents: _sampleHbaConfigMd5,
),
);
},
);

tearDownAll(() {
docker.stop();
docker.kill();
});
PostgreSQLConnection? conn;

setUp(() async {
conn = PostgreSQLConnection(
'localhost',
port,
db,
username: user,
password: password,
);
await conn!.open();
});

tearDown(() async {
await conn?.close();
});

test('- Connect with non-ascii connection string', () async {
final res = await conn!.query('select 1;');
expect(res.length, 1);
});
});

group('non-ascii tests (scram-sha-256 auth)', () {
late final DockerProcess docker;
final port = 54323;
setUpAll(
() async {
docker = await startPostgres(
name: 'non_ascii_test_scram_sha256',
version: 'latest',
pgPort: port,
pgDatabase: db,
pgUser: user,
pgPassword: password,
cleanup: true,
pgHbaConfPath: _createTempFile(
fileName: 'pg_hba.conf',
contents: _sampleHbaConfigScramSha256,
),
);
},
);

tearDownAll(() {
docker.stop();
docker.kill();
});
PostgreSQLConnection? conn;

setUp(() async {
conn = PostgreSQLConnection(
'localhost',
port,
db,
username: user,
password: password,
);
await conn!.open();
});

tearDown(() async {
await conn?.close();
});

test('- Connect with non-ascii connection string', () async {
final res = await conn!.query('select 1;');
expect(res.length, 1);
});
});
}

/* -------------------------------------------------------------------------- */
/* helper methods and getters */
/* -------------------------------------------------------------------------- */

String _createTempFile({
required String fileName,
required String contents,
}) {
final file = File(p.join(
Directory.systemTemp.path,
DateTime.now().millisecondsSinceEpoch.toString(),
fileName,
));

file.createSync(recursive: true);
file.writeAsStringSync(contents);
return file.path;
}

String get _sampleHbaConfigPassword =>
_sampleHbaContentTrust.replaceAll('trust', 'password');

String get _sampleHbaConfigMd5 =>
_sampleHbaContentTrust.replaceAll('trust', 'md5');

String get _sampleHbaConfigScramSha256 =>
_sampleHbaContentTrust.replaceAll('trust', 'scram-sha-256');

/// METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
/// "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert".
///
/// Currently, the package only supports: 'md5', 'password', 'scram-sha-256'.
/// See [AuthenticationScheme] within `src/auth/auth.dart`
const _sampleHbaContentTrust = '''
# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust

# when using containers
host all all 0.0.0.0/0 trust
''';