-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Dart and Local SshKeyHandler tests
- Loading branch information
1 parent
7f732e3
commit 5b4fe9f
Showing
5 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...s/noports_core/test/sshnp/util/sshnp_ssh_key_handler/sshnp_dart_ssh_key_handler_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:noports_core/sshnp_foundation.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'sshnp_ssh_key_handler_mocks.dart'; | ||
|
||
void main() { | ||
group('SshnpDartSshKeyHandler', () { | ||
late MockSshnpDartSshKeyHandler keyHandler; | ||
late MockAtSshKeyPair mockKeyPair; | ||
|
||
setUp(() { | ||
keyHandler = MockSshnpDartSshKeyHandler(); | ||
mockKeyPair = MockAtSshKeyPair(); | ||
}); | ||
|
||
test('public API', () { | ||
/// The Dart key handler requires that there is a setter for | ||
/// identityKeyPair, in addition to a getter | ||
keyHandler.identityKeyPair = mockKeyPair; | ||
expect(keyHandler.identityKeyPair, mockKeyPair); | ||
|
||
expect(MockSshnpDartSshKeyHandler(), isA<SshnpKeyHandler>()); | ||
}); // test initialization | ||
}); // group SshnpDartKeyHandler | ||
} |
131 changes: 131 additions & 0 deletions
131
.../noports_core/test/sshnp/util/sshnp_ssh_key_handler/sshnp_local_ssh_key_handler_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import 'package:at_client/at_client.dart'; | ||
import 'package:noports_core/sshnp_foundation.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../sshnp_mocks.dart'; | ||
import 'sshnp_ssh_key_handler_mocks.dart'; | ||
|
||
void main() { | ||
group('SshnpLocalSshKeyHandler', () { | ||
late MockAtClient mockAtClient; | ||
late MockSshnpParams mockParams; | ||
late MockLocalSshKeyUtil keyUtil; | ||
late MockAtSshKeyPair keyPair; | ||
|
||
late MockSshnpdChannel mockSshnpdChannel; | ||
late MockSshrvdChannel mockSshrvdChannel; | ||
|
||
setUp(() { | ||
mockAtClient = MockAtClient(); | ||
mockParams = MockSshnpParams(); | ||
keyUtil = MockLocalSshKeyUtil(); | ||
keyPair = MockAtSshKeyPair(); | ||
|
||
mockSshnpdChannel = MockSshnpdChannel(); | ||
mockSshrvdChannel = MockSshrvdChannel(); | ||
registerFallbackValue(AtClientPreference()); | ||
}); | ||
|
||
whenConstructor() { | ||
when(() => mockParams.device).thenReturn('mydevice'); | ||
when(() => mockParams.localPort).thenReturn(0); | ||
when(() => mockParams.verbose).thenReturn(false); | ||
when(() => mockAtClient.getPreferences()).thenReturn(null); | ||
when(() => mockAtClient.setPreferences(any())).thenReturn(null); | ||
} | ||
|
||
whenInitialization({AtSshKeyPair? identityKeyPair}) { | ||
when(() => mockSshnpdChannel.callInitialization()) | ||
.thenAnswer((_) async {}); | ||
when(() => mockSshnpdChannel.resolveRemoteUsername()) | ||
.thenAnswer((_) async => 'myRemoteUsername'); | ||
when(() => mockSshnpdChannel.sharePublicKeyIfRequired(identityKeyPair)) | ||
.thenAnswer((_) async {}); | ||
when(() => mockSshrvdChannel.callInitialization()) | ||
.thenAnswer((_) async {}); | ||
} | ||
|
||
test('public API', () { | ||
whenConstructor(); | ||
final sshnp = StubbedSshnp( | ||
atClient: mockAtClient, | ||
params: mockParams, | ||
); | ||
expect(sshnp, isA<SshnpKeyHandler>()); | ||
}); // test public API | ||
|
||
test('initialization', () async { | ||
whenConstructor(); | ||
|
||
final sshnp = StubbedSshnp( | ||
atClient: mockAtClient, | ||
params: mockParams, | ||
sshKeyUtil: keyUtil, | ||
sshnpdChannel: mockSshnpdChannel, | ||
sshrvdChannel: mockSshrvdChannel, | ||
); | ||
|
||
whenInitialization(identityKeyPair: keyPair); | ||
final identityFile = '.ssh/asdf'; | ||
when(() => keyUtil.isValidPlatform).thenReturn(true); | ||
when(() => mockParams.identityFile).thenReturn(identityFile); | ||
when(() => keyUtil.getKeyPair(identifier: identityFile)) | ||
.thenAnswer((_) async => keyPair); | ||
|
||
/// normally we would call [callInitialization()] but it's fine to call | ||
/// initialize directly for testing purposes, since we avoid weird | ||
/// lifecycle issues that could be caused by mocking | ||
await sshnp.initialize(); | ||
|
||
/// We don't care about [SshnpCore] initialization here, we only care that | ||
/// the [keyPair] is set correctly, since [SshnpCore] is tested elsewhere | ||
expect(sshnp.identityKeyPair, keyPair); | ||
}); // test initialization | ||
|
||
test('initialization - no identityFile', () async { | ||
whenConstructor(); | ||
|
||
final sshnp = StubbedSshnp( | ||
atClient: mockAtClient, | ||
params: mockParams, | ||
sshKeyUtil: keyUtil, | ||
sshnpdChannel: mockSshnpdChannel, | ||
sshrvdChannel: mockSshrvdChannel, | ||
); | ||
|
||
whenInitialization(identityKeyPair: keyPair); | ||
when(() => keyUtil.isValidPlatform).thenReturn(true); | ||
when(() => mockParams.identityFile).thenReturn(null); | ||
when(() => mockSshnpdChannel.sharePublicKeyIfRequired(null)) | ||
.thenAnswer((_) async {}); | ||
when(() => keyUtil.getKeyPair(identifier: '.ssh/asdf')) | ||
.thenAnswer((_) async => keyPair); | ||
|
||
/// normally we would call [callInitialization()] but it's fine to call | ||
/// initialize directly for testing purposes, since we avoid weird | ||
/// lifecycle issues that could be caused by mocking | ||
await sshnp.initialize(); | ||
|
||
/// We don't care about [SshnpCore] initialization here, we only care that | ||
/// the [keyPair] is set correctly, since [SshnpCore] is tested elsewhere | ||
expect(sshnp.identityKeyPair, null); | ||
}); // test initialization - no identityFile | ||
|
||
test('initialization - invalid platform', () { | ||
whenConstructor(); | ||
|
||
final sshnp = StubbedSshnp( | ||
atClient: mockAtClient, | ||
params: mockParams, | ||
sshKeyUtil: keyUtil, | ||
sshnpdChannel: mockSshnpdChannel, | ||
sshrvdChannel: mockSshrvdChannel, | ||
); | ||
|
||
whenInitialization(); | ||
when(() => keyUtil.isValidPlatform).thenReturn(false); | ||
expect(sshnp.initialize(), throwsA(isA<SshnpError>())); | ||
}); | ||
}); // group SshnpLocalSshKeyHandler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters