-
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.
Merge pull request #585 from atsign-foundation/unit_tests_4
- Loading branch information
Showing
10 changed files
with
285 additions
and
10 deletions.
There are no files selected for viewing
5 changes: 2 additions & 3 deletions
5
...ges/noports_core/lib/src/sshnp/util/sshnp_ssh_key_handler/sshnp_dart_ssh_key_handler.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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import 'package:noports_core/sshnp_foundation.dart'; | ||
|
||
mixin SshnpDartSshKeyHandler on SshnpCore implements SshnpKeyHandler { | ||
mixin SshnpDartSshKeyHandler implements SshnpKeyHandler { | ||
@override | ||
DartSshKeyUtil get keyUtil => _sshKeyUtil; | ||
final DartSshKeyUtil _sshKeyUtil = DartSshKeyUtil(); | ||
|
||
@override | ||
AtSshKeyPair? get identityKeyPair => _identityKeyPair; | ||
AtSshKeyPair? _identityKeyPair; | ||
AtSshKeyPair? identityKeyPair; | ||
} |
2 changes: 2 additions & 0 deletions
2
...es/noports_core/lib/src/sshnp/util/sshnp_ssh_key_handler/sshnp_local_ssh_key_handler.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
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
48 changes: 48 additions & 0 deletions
48
packages/noports_core/test/sshnp/models/sshnp_device_list_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,48 @@ | ||
import 'package:noports_core/sshnp_foundation.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('SshnpDeviceList', () { | ||
late SshnpDeviceList deviceList; | ||
setUp(() => deviceList = SshnpDeviceList()); | ||
test('public API', () { | ||
expect( | ||
deviceList.info, | ||
allOf(isA<Map<String, dynamic>>(), isEmpty), | ||
); | ||
|
||
expect( | ||
deviceList.activeDevices, | ||
allOf(isA<Set<String>>(), isEmpty), | ||
); | ||
}); // test public API | ||
|
||
test('setActive', () { | ||
expect(deviceList.info, isEmpty); | ||
|
||
deviceList.info['dev1'] = 'asdf'; | ||
deviceList.setActive('dev1'); | ||
expect( | ||
deviceList.activeDevices, | ||
allOf(hasLength(1), contains('dev1')), | ||
); | ||
|
||
deviceList.setActive('dev2'); | ||
expect( | ||
deviceList.activeDevices, | ||
allOf(hasLength(1), isNot(contains('dev2'))), | ||
); | ||
}); | ||
|
||
test('inactiveDevices', () { | ||
deviceList.info['dev1'] = 'asdf'; | ||
deviceList.info['dev2'] = 'jkl;'; | ||
deviceList.setActive('dev1'); | ||
|
||
expect( | ||
deviceList.inactiveDevices, | ||
allOf(hasLength(1), contains('dev2')), | ||
); | ||
}); // test inactiveDevices | ||
}); // group SshnpDeviceList | ||
} |
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 | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/noports_core/test/sshnp/util/sshnp_ssh_key_handler/sshnp_ssh_key_handler_mocks.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,41 @@ | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:noports_core/sshnp_foundation.dart'; | ||
|
||
class MockSshnpKeyHandler extends Mock with SshnpKeyHandler {} | ||
|
||
class MockAtSshKeyUtil extends Mock implements AtSshKeyUtil {} | ||
|
||
class MockAtSshKeyPair extends Mock implements AtSshKeyPair {} | ||
|
||
class MockSshnpDartSshKeyHandler extends Mock with SshnpDartSshKeyHandler {} | ||
|
||
class StubbedSshnp extends SshnpCore with SshnpLocalSshKeyHandler { | ||
@override | ||
LocalSshKeyUtil get keyUtil => _sshKeyUtil ?? (throw UnimplementedError()); | ||
final LocalSshKeyUtil? _sshKeyUtil; | ||
|
||
StubbedSshnp({ | ||
required super.atClient, | ||
required super.params, | ||
LocalSshKeyUtil? sshKeyUtil, | ||
SshnpdChannel? sshnpdChannel, | ||
SshrvdChannel? sshrvdChannel, | ||
}) : _sshKeyUtil = sshKeyUtil, | ||
_sshnpdChannel = sshnpdChannel, | ||
_sshrvdChannel = sshrvdChannel; | ||
|
||
@override | ||
Future<SshnpResult> run() => throw UnimplementedError(); | ||
|
||
@override | ||
SshnpdChannel get sshnpdChannel => | ||
_sshnpdChannel ?? (throw UnimplementedError()); | ||
final SshnpdChannel? _sshnpdChannel; | ||
|
||
@override | ||
SshrvdChannel get sshrvdChannel => | ||
_sshrvdChannel ?? (throw UnimplementedError()); | ||
final SshrvdChannel? _sshrvdChannel; | ||
} | ||
|
||
class MockLocalSshKeyUtil extends Mock implements LocalSshKeyUtil {} |
27 changes: 27 additions & 0 deletions
27
packages/noports_core/test/sshnp/util/sshnp_ssh_key_handler/sshnp_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,27 @@ | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:noports_core/sshnp_foundation.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'sshnp_ssh_key_handler_mocks.dart'; | ||
|
||
void main() { | ||
group('SshnpKeyHandler', () { | ||
late MockSshnpKeyHandler mockKeyHandler; | ||
late MockAtSshKeyUtil mockKeyUtil; | ||
late MockAtSshKeyPair mockKeyPair; | ||
|
||
setUp(() { | ||
mockKeyHandler = MockSshnpKeyHandler(); | ||
mockKeyUtil = MockAtSshKeyUtil(); | ||
mockKeyPair = MockAtSshKeyPair(); | ||
}); | ||
|
||
test('public API', () { | ||
when(() => mockKeyHandler.keyUtil).thenReturn(mockKeyUtil); | ||
when(() => mockKeyHandler.identityKeyPair).thenReturn(mockKeyPair); | ||
|
||
expect(mockKeyHandler.keyUtil, isA<AtSshKeyUtil>()); | ||
expect(mockKeyHandler.identityKeyPair, isA<AtSshKeyPair?>()); | ||
}); // test public API | ||
}); // group SshnpKeyHandler | ||
} |