|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | +import 'dart:typed_data'; |
| 4 | + |
| 5 | +import 'package:dartssh2/dartssh2.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('SSHClient.ident', () { |
| 10 | + test('uses default ident when not provided', () async { |
| 11 | + final socket = _FakeSSHSocket(); |
| 12 | + final client = SSHClient( |
| 13 | + socket, |
| 14 | + username: 'demo', |
| 15 | + ); |
| 16 | + |
| 17 | + await Future<void>.delayed(Duration.zero); |
| 18 | + |
| 19 | + expect(client.ident, 'DartSSH_2.0'); |
| 20 | + expect(socket.writes, contains('SSH-2.0-DartSSH_2.0\r\n')); |
| 21 | + |
| 22 | + client.close(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('uses custom ident when provided', () async { |
| 26 | + final socket = _FakeSSHSocket(); |
| 27 | + final client = SSHClient( |
| 28 | + socket, |
| 29 | + username: 'demo', |
| 30 | + ident: 'MyClient_1.0', |
| 31 | + ); |
| 32 | + |
| 33 | + await Future<void>.delayed(Duration.zero); |
| 34 | + |
| 35 | + expect(client.ident, 'MyClient_1.0'); |
| 36 | + expect(socket.writes, contains('SSH-2.0-MyClient_1.0\r\n')); |
| 37 | + |
| 38 | + client.close(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('throws when ident is empty', () { |
| 42 | + expect( |
| 43 | + () => SSHClient( |
| 44 | + _FakeSSHSocket(), |
| 45 | + username: 'demo', |
| 46 | + ident: '', |
| 47 | + ), |
| 48 | + throwsA(isA<ArgumentError>()), |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + test('throws when ident contains newline characters', () { |
| 53 | + expect( |
| 54 | + () => SSHClient( |
| 55 | + _FakeSSHSocket(), |
| 56 | + username: 'demo', |
| 57 | + ident: 'Bad\nIdent', |
| 58 | + ), |
| 59 | + throwsA(isA<ArgumentError>()), |
| 60 | + ); |
| 61 | + |
| 62 | + expect( |
| 63 | + () => SSHClient( |
| 64 | + _FakeSSHSocket(), |
| 65 | + username: 'demo', |
| 66 | + ident: 'Bad\rIdent', |
| 67 | + ), |
| 68 | + throwsA(isA<ArgumentError>()), |
| 69 | + ); |
| 70 | + }); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +class _FakeSSHSocket implements SSHSocket { |
| 75 | + final _inputController = StreamController<Uint8List>(); |
| 76 | + final _doneCompleter = Completer<void>(); |
| 77 | + final writes = <String>[]; |
| 78 | + |
| 79 | + @override |
| 80 | + Stream<Uint8List> get stream => _inputController.stream; |
| 81 | + |
| 82 | + @override |
| 83 | + StreamSink<List<int>> get sink => _RecordingSink(writes); |
| 84 | + |
| 85 | + @override |
| 86 | + Future<void> get done => _doneCompleter.future; |
| 87 | + |
| 88 | + @override |
| 89 | + Future<void> close() async { |
| 90 | + if (!_doneCompleter.isCompleted) { |
| 91 | + _doneCompleter.complete(); |
| 92 | + } |
| 93 | + await _inputController.close(); |
| 94 | + } |
| 95 | + |
| 96 | + @override |
| 97 | + void destroy() { |
| 98 | + if (!_doneCompleter.isCompleted) { |
| 99 | + _doneCompleter.complete(); |
| 100 | + } |
| 101 | + unawaited(_inputController.close()); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class _RecordingSink implements StreamSink<List<int>> { |
| 106 | + _RecordingSink(this._writes); |
| 107 | + |
| 108 | + final List<String> _writes; |
| 109 | + |
| 110 | + @override |
| 111 | + void add(List<int> data) { |
| 112 | + _writes.add(latin1.decode(data)); |
| 113 | + } |
| 114 | + |
| 115 | + @override |
| 116 | + void addError(Object error, [StackTrace? stackTrace]) {} |
| 117 | + |
| 118 | + @override |
| 119 | + Future<void> addStream(Stream<List<int>> stream) async { |
| 120 | + await for (final data in stream) { |
| 121 | + add(data); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + Future<void> close() async {} |
| 127 | + |
| 128 | + @override |
| 129 | + Future<void> get done async {} |
| 130 | +} |
0 commit comments