|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:taskwarrior/app/utils/taskserver/pem_file_paths.dart'; |
| 4 | + |
| 5 | +class MockX509Certificate implements X509Certificate { |
| 6 | + @override |
| 7 | + final String pem; |
| 8 | + |
| 9 | + MockX509Certificate(this.pem); |
| 10 | + |
| 11 | + @override |
| 12 | + noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 13 | +} |
| 14 | + |
| 15 | +void main() { |
| 16 | + group('PemFilePaths', () { |
| 17 | + setUp(() { |
| 18 | + // Create test directory and files if they don't exist |
| 19 | + Directory('test/fixtures').createSync(recursive: true); |
| 20 | + File('test/fixtures/server.pem') |
| 21 | + .writeAsStringSync('matching-pem-content'); |
| 22 | + }); |
| 23 | + |
| 24 | + tearDown(() { |
| 25 | + // Clean up test files |
| 26 | + if (File('test/fixtures/server.pem').existsSync()) { |
| 27 | + File('test/fixtures/server.pem').deleteSync(); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + test('PemFilePaths constructor sets fields correctly', () { |
| 32 | + const pemFilePaths = PemFilePaths( |
| 33 | + ca: 'path/to/ca.pem', |
| 34 | + certificate: 'path/to/cert.pem', |
| 35 | + key: 'path/to/key.pem', |
| 36 | + serverCert: 'path/to/server.pem', |
| 37 | + ); |
| 38 | + |
| 39 | + expect(pemFilePaths.ca, 'path/to/ca.pem'); |
| 40 | + expect(pemFilePaths.certificate, 'path/to/cert.pem'); |
| 41 | + expect(pemFilePaths.key, 'path/to/key.pem'); |
| 42 | + expect(pemFilePaths.serverCert, 'path/to/server.pem'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('PemFilePaths.fromTaskrc parses taskrc correctly', () { |
| 46 | + final taskrc = { |
| 47 | + 'taskd.ca': 'path/to/ca.pem', |
| 48 | + 'taskd.certificate': 'path/to/cert.pem', |
| 49 | + 'taskd.key': 'path/to/key.pem', |
| 50 | + }; |
| 51 | + final pemFilePaths = PemFilePaths.fromTaskrc(taskrc); |
| 52 | + |
| 53 | + expect(pemFilePaths.ca, 'path/to/ca.pem'); |
| 54 | + expect(pemFilePaths.certificate, 'path/to/cert.pem'); |
| 55 | + expect(pemFilePaths.key, 'path/to/key.pem'); |
| 56 | + expect(pemFilePaths.serverCert, isNull); |
| 57 | + }); |
| 58 | + |
| 59 | + test('PemFilePaths.securityContext creates SecurityContext correctly', () { |
| 60 | + const pemFilePaths = PemFilePaths( |
| 61 | + ca: 'test/fixtures/ca.pem', |
| 62 | + certificate: 'test/fixtures/cert.pem', |
| 63 | + key: 'test/fixtures/key.pem', |
| 64 | + ); |
| 65 | + |
| 66 | + final context = pemFilePaths.securityContext(); |
| 67 | + |
| 68 | + expect(context, isA<SecurityContext>()); |
| 69 | + }); |
| 70 | + |
| 71 | + test( |
| 72 | + 'PemFilePaths.savedServerCertificateMatches returns true for matching certificates', |
| 73 | + () { |
| 74 | + const pemFilePaths = PemFilePaths( |
| 75 | + serverCert: 'test/fixtures/server.pem', |
| 76 | + ); |
| 77 | + final badServerCert = MockX509Certificate('matching-pem-content'); |
| 78 | + |
| 79 | + final result = pemFilePaths.savedServerCertificateMatches(badServerCert); |
| 80 | + |
| 81 | + expect(result, true); |
| 82 | + }); |
| 83 | + |
| 84 | + test( |
| 85 | + 'PemFilePaths.savedServerCertificateMatches returns false for non-matching certificates', |
| 86 | + () { |
| 87 | + const pemFilePaths = PemFilePaths( |
| 88 | + serverCert: 'test/fixtures/server.pem', |
| 89 | + ); |
| 90 | + final badServerCert = MockX509Certificate('non-matching-pem-content'); |
| 91 | + |
| 92 | + // Write the non-matching content to the server cert file for the test |
| 93 | + File('test/fixtures/server.pem') |
| 94 | + .writeAsStringSync('different-pem-content'); |
| 95 | + |
| 96 | + final result = pemFilePaths.savedServerCertificateMatches(badServerCert); |
| 97 | + |
| 98 | + expect(result, false); |
| 99 | + }); |
| 100 | + |
| 101 | + test('PemFilePaths.map returns correct map representation', () { |
| 102 | + const pemFilePaths = PemFilePaths( |
| 103 | + ca: 'path/to/ca.pem', |
| 104 | + certificate: 'path/to/cert.pem', |
| 105 | + key: 'path/to/key.pem', |
| 106 | + serverCert: 'path/to/server.pem', |
| 107 | + ); |
| 108 | + |
| 109 | + final map = pemFilePaths.map; |
| 110 | + |
| 111 | + expect(map, { |
| 112 | + 'taskd.ca': 'path/to/ca.pem', |
| 113 | + 'taskd.certificate': 'path/to/cert.pem', |
| 114 | + 'taskd.key': 'path/to/key.pem', |
| 115 | + 'server.cert': 'path/to/server.pem', |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + test('PemFilePaths.map omits null values', () { |
| 120 | + const pemFilePaths = PemFilePaths( |
| 121 | + ca: 'path/to/ca.pem', |
| 122 | + certificate: 'path/to/cert.pem', |
| 123 | + key: null, |
| 124 | + serverCert: null, |
| 125 | + ); |
| 126 | + |
| 127 | + final map = pemFilePaths.map; |
| 128 | + |
| 129 | + expect(map, { |
| 130 | + 'taskd.ca': 'path/to/ca.pem', |
| 131 | + 'taskd.certificate': 'path/to/cert.pem', |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +} |
0 commit comments