|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:docker_process/containers/postgres.dart'; |
| 4 | +import 'package:path/path.dart' as p; |
| 5 | +import 'package:postgres/postgres.dart'; |
| 6 | +import 'package:test/expect.dart'; |
| 7 | +import 'package:test/scaffolding.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + /// This cannot be run on CI as each test requires different pg_hba.conf |
| 11 | + /// to be mounted to the container. |
| 12 | + if (Platform.environment.containsKey('GITHUB_ACTION')) { |
| 13 | + test('NO NON-ASCII CONNECTION STRINGS TESTS ARE RUNNING.', () { |
| 14 | + // no-op |
| 15 | + }); |
| 16 | + return; |
| 17 | + } |
| 18 | + final user = 'abc@def'; |
| 19 | + final password = 'pöstgrēs_üšęr_pæsswœêrd'; |
| 20 | + final db = 'postgres'; |
| 21 | + |
| 22 | + group('non-ascii tests (clear password auth)', () { |
| 23 | + late final DockerProcess docker; |
| 24 | + final port = 54321; |
| 25 | + |
| 26 | + setUpAll( |
| 27 | + () async { |
| 28 | + docker = await startPostgres( |
| 29 | + name: 'non_ascii_test_clear_password', |
| 30 | + version: 'latest', |
| 31 | + pgPort: port, |
| 32 | + pgDatabase: db, |
| 33 | + pgUser: user, |
| 34 | + pgPassword: password, |
| 35 | + cleanup: true, |
| 36 | + pgHbaConfPath: _createTempFile( |
| 37 | + fileName: 'pg_hba.conf', |
| 38 | + contents: _sampleHbaConfigPassword, |
| 39 | + ), |
| 40 | + ); |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + tearDownAll(() { |
| 45 | + docker.stop(); |
| 46 | + docker.kill(); |
| 47 | + }); |
| 48 | + PostgreSQLConnection? conn; |
| 49 | + |
| 50 | + setUp(() async { |
| 51 | + conn = PostgreSQLConnection('localhost', port, db, |
| 52 | + username: user, password: password, allowClearTextPassword: true); |
| 53 | + await conn!.open(); |
| 54 | + }); |
| 55 | + |
| 56 | + tearDown(() async { |
| 57 | + await conn?.close(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('- Connect with non-ascii connection string', () async { |
| 61 | + final res = await conn!.query('select 1;'); |
| 62 | + expect(res.length, 1); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + group('non-ascii tests (md5 auth)', () { |
| 67 | + late final DockerProcess docker; |
| 68 | + final port = 54322; |
| 69 | + setUpAll( |
| 70 | + () async { |
| 71 | + docker = await startPostgres( |
| 72 | + name: 'non_ascii_test_md5', |
| 73 | + version: 'latest', |
| 74 | + pgPort: port, |
| 75 | + pgDatabase: db, |
| 76 | + pgUser: user, |
| 77 | + pgPassword: password, |
| 78 | + cleanup: true, |
| 79 | + pgHbaConfPath: _createTempFile( |
| 80 | + fileName: 'pg_hba.conf', |
| 81 | + contents: _sampleHbaConfigMd5, |
| 82 | + ), |
| 83 | + ); |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + tearDownAll(() { |
| 88 | + docker.stop(); |
| 89 | + docker.kill(); |
| 90 | + }); |
| 91 | + PostgreSQLConnection? conn; |
| 92 | + |
| 93 | + setUp(() async { |
| 94 | + conn = PostgreSQLConnection( |
| 95 | + 'localhost', |
| 96 | + port, |
| 97 | + db, |
| 98 | + username: user, |
| 99 | + password: password, |
| 100 | + ); |
| 101 | + await conn!.open(); |
| 102 | + }); |
| 103 | + |
| 104 | + tearDown(() async { |
| 105 | + await conn?.close(); |
| 106 | + }); |
| 107 | + |
| 108 | + test('- Connect with non-ascii connection string', () async { |
| 109 | + final res = await conn!.query('select 1;'); |
| 110 | + expect(res.length, 1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + group('non-ascii tests (scram-sha-256 auth)', () { |
| 115 | + late final DockerProcess docker; |
| 116 | + final port = 54323; |
| 117 | + setUpAll( |
| 118 | + () async { |
| 119 | + docker = await startPostgres( |
| 120 | + name: 'non_ascii_test_scram_sha256', |
| 121 | + version: 'latest', |
| 122 | + pgPort: port, |
| 123 | + pgDatabase: db, |
| 124 | + pgUser: user, |
| 125 | + pgPassword: password, |
| 126 | + cleanup: true, |
| 127 | + pgHbaConfPath: _createTempFile( |
| 128 | + fileName: 'pg_hba.conf', |
| 129 | + contents: _sampleHbaConfigScramSha256, |
| 130 | + ), |
| 131 | + ); |
| 132 | + }, |
| 133 | + ); |
| 134 | + |
| 135 | + tearDownAll(() { |
| 136 | + docker.stop(); |
| 137 | + docker.kill(); |
| 138 | + }); |
| 139 | + PostgreSQLConnection? conn; |
| 140 | + |
| 141 | + setUp(() async { |
| 142 | + conn = PostgreSQLConnection( |
| 143 | + 'localhost', |
| 144 | + port, |
| 145 | + db, |
| 146 | + username: user, |
| 147 | + password: password, |
| 148 | + ); |
| 149 | + await conn!.open(); |
| 150 | + }); |
| 151 | + |
| 152 | + tearDown(() async { |
| 153 | + await conn?.close(); |
| 154 | + }); |
| 155 | + |
| 156 | + test('- Connect with non-ascii connection string', () async { |
| 157 | + final res = await conn!.query('select 1;'); |
| 158 | + expect(res.length, 1); |
| 159 | + }); |
| 160 | + }); |
| 161 | +} |
| 162 | + |
| 163 | +/* -------------------------------------------------------------------------- */ |
| 164 | +/* helper methods and getters */ |
| 165 | +/* -------------------------------------------------------------------------- */ |
| 166 | + |
| 167 | +String _createTempFile({ |
| 168 | + required String fileName, |
| 169 | + required String contents, |
| 170 | +}) { |
| 171 | + final file = File(p.join( |
| 172 | + Directory.systemTemp.path, |
| 173 | + DateTime.now().millisecondsSinceEpoch.toString(), |
| 174 | + fileName, |
| 175 | + )); |
| 176 | + |
| 177 | + file.createSync(recursive: true); |
| 178 | + file.writeAsStringSync(contents); |
| 179 | + return file.path; |
| 180 | +} |
| 181 | + |
| 182 | +String get _sampleHbaConfigPassword => |
| 183 | + _sampleHbaContentTrust.replaceAll('trust', 'password'); |
| 184 | + |
| 185 | +String get _sampleHbaConfigMd5 => |
| 186 | + _sampleHbaContentTrust.replaceAll('trust', 'md5'); |
| 187 | + |
| 188 | +String get _sampleHbaConfigScramSha256 => |
| 189 | + _sampleHbaContentTrust.replaceAll('trust', 'scram-sha-256'); |
| 190 | + |
| 191 | +/// METHOD can be "trust", "reject", "md5", "password", "scram-sha-256", |
| 192 | +/// "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert". |
| 193 | +/// |
| 194 | +/// Currently, the package only supports: 'md5', 'password', 'scram-sha-256'. |
| 195 | +/// See [AuthenticationScheme] within `src/auth/auth.dart` |
| 196 | +const _sampleHbaContentTrust = ''' |
| 197 | +# TYPE DATABASE USER ADDRESS METHOD |
| 198 | +
|
| 199 | +# "local" is for Unix domain socket connections only |
| 200 | +local all all trust |
| 201 | +# IPv4 local connections: |
| 202 | +host all all 127.0.0.1/32 trust |
| 203 | +# IPv6 local connections: |
| 204 | +host all all ::1/128 trust |
| 205 | +
|
| 206 | +# when using containers |
| 207 | +host all all 0.0.0.0/0 trust |
| 208 | +'''; |
0 commit comments