@@ -4,14 +4,18 @@ import 'dart:convert';
4
4
import 'package:at_auth/at_auth.dart' ;
5
5
import 'package:at_chops/at_chops.dart' ;
6
6
import 'package:at_client_mobile/at_client_mobile.dart' ;
7
+ import 'package:at_client_mobile/src/atsign_key.dart' ;
7
8
import 'package:at_client_mobile/src/auth/at_auth_service_impl.dart' ;
8
9
import 'package:at_commons/at_builders.dart' ;
9
10
import 'package:at_lookup/at_lookup.dart' ;
10
11
import 'package:biometric_storage/biometric_storage.dart' ;
12
+ import 'package:crypton/crypton.dart' ;
11
13
import 'package:mocktail/mocktail.dart' ;
12
14
import 'package:package_info_plus/package_info_plus.dart' ;
13
15
import 'package:test/test.dart' ;
14
16
17
+ import 'at_client_service_test.dart' ;
18
+
15
19
class MockBiometricStorage extends Mock implements BiometricStorage {}
16
20
17
21
class MockEnrollmentBiometricStorageFile extends Mock
@@ -69,14 +73,14 @@ void main() {
69
73
late MockAtLookUp mockAtLookUp;
70
74
71
75
setUp (() {
72
- authServiceImpl = AtAuthServiceImpl (atSign, atClientPreference);
73
76
mockBiometricStorageEnrollmentFile = MockEnrollmentBiometricStorageFile ();
74
77
mockBiometricStorage = MockBiometricStorage ();
75
78
mockBiometricStorageKeychainFile = MockKeychainBiometricStorageFile ();
76
79
mockAtLookUp = MockAtLookUp ();
77
80
81
+ authServiceImpl =
82
+ AtAuthServiceImpl (atSign, atClientPreference, atLookUp: mockAtLookUp);
78
83
authServiceImpl.keyChainManager.biometricStorage = mockBiometricStorage;
79
- authServiceImpl.atLookUp = mockAtLookUp;
80
84
});
81
85
82
86
test ('A test to verify submission of enrollment' , () async {
@@ -464,6 +468,146 @@ void main() {
464
468
tearDown (() => tearDownMethod (mockBiometricStorageEnrollmentFile,
465
469
mockAtLookUp, mockBiometricStorage));
466
470
});
471
+
472
+ group ('A group of tests related to authenticate an atSign' , () {
473
+ String atSign = '@alice' ;
474
+ AtClientPreference atClientPreference = AtClientPreference ()
475
+ ..namespace = 'me' ;
476
+
477
+ test (
478
+ 'A test to verify AtClient initializes successfully in offline mode upon network failure when keychain manager contains keys' ,
479
+ () async {
480
+ KeyChainManager mockKeyChainManager = MockKeyChainManager ();
481
+ RSAKeypair pkamKeyPair = KeyChainManager .getInstance ().generateKeyPair ();
482
+ RSAKeypair encryptionKeyPair =
483
+ KeyChainManager .getInstance ().generateKeyPair ();
484
+ String selfEncryptionKey = KeyChainManager .getInstance ().generateAESKey ();
485
+ AtAuthService atAuthService =
486
+ AtClientMobile .authService (atSign, atClientPreference);
487
+ (atAuthService as AtAuthServiceImpl ).keyChainManager =
488
+ mockKeyChainManager;
489
+
490
+ AtsignKey atsignKey = AtsignKey (
491
+ atSign: atSign,
492
+ encryptionPublicKey: encryptionKeyPair.publicKey.toString ());
493
+
494
+ // Mock object to return keys from keychain manager
495
+ when (() => mockKeyChainManager.readAtsign (name: atSign))
496
+ .thenAnswer ((_) => Future .value (atsignKey));
497
+
498
+ AtAuthRequest atAuthRequest = AtAuthRequest (atSign);
499
+ atAuthRequest.atAuthKeys = AtAuthKeys ()
500
+ ..apkamPrivateKey = pkamKeyPair.privateKey.toString ()
501
+ ..apkamPublicKey = pkamKeyPair.publicKey.toString ()
502
+ ..defaultEncryptionPublicKey = encryptionKeyPair.publicKey.toString ()
503
+ ..defaultEncryptionPrivateKey = encryptionKeyPair.privateKey.toString ()
504
+ ..defaultSelfEncryptionKey = selfEncryptionKey
505
+ ..enrollmentId = '123' ;
506
+
507
+ AtAuthResponse atAuthResponse =
508
+ await atAuthService.authenticate (atAuthRequest);
509
+
510
+ expect (atAuthResponse.isSuccessful, true );
511
+ expect (atAuthResponse.atSign, atSign);
512
+ expect (atAuthResponse.atAuthKeys? .apkamPublicKey,
513
+ pkamKeyPair.publicKey.toString ());
514
+ expect (atAuthResponse.atAuthKeys? .apkamPrivateKey,
515
+ pkamKeyPair.privateKey.toString ());
516
+ expect (atAuthResponse.atAuthKeys? .defaultEncryptionPrivateKey,
517
+ encryptionKeyPair.privateKey.toString ());
518
+ expect (atAuthResponse.atAuthKeys? .defaultEncryptionPublicKey,
519
+ encryptionKeyPair.publicKey.toString ());
520
+ expect (atAuthResponse.atAuthKeys? .defaultSelfEncryptionKey,
521
+ selfEncryptionKey);
522
+ });
523
+
524
+ test (
525
+ 'A test to verify atClient initialization fails when network is offline and keychain manager does not have keys' ,
526
+ () async {
527
+ KeyChainManager mockKeyChainManager = MockKeyChainManager ();
528
+ RSAKeypair pkamKeyPair = KeyChainManager .getInstance ().generateKeyPair ();
529
+ RSAKeypair encryptionKeyPair =
530
+ KeyChainManager .getInstance ().generateKeyPair ();
531
+ AtAuthService atAuthService =
532
+ AtClientMobile .authService (atSign, atClientPreference);
533
+ (atAuthService as AtAuthServiceImpl ).keyChainManager =
534
+ mockKeyChainManager;
535
+
536
+ AtsignKey atsignKey = AtsignKey (atSign: atSign, encryptionPublicKey: '' );
537
+
538
+ // Mock object to return keys from keychain manager
539
+ when (() => mockKeyChainManager.readAtsign (name: atSign))
540
+ .thenAnswer ((_) => Future .value (atsignKey));
541
+
542
+ AtAuthRequest atAuthRequest = AtAuthRequest (atSign);
543
+ atAuthRequest.atAuthKeys = AtAuthKeys ()
544
+ ..apkamPrivateKey = pkamKeyPair.privateKey.toString ()
545
+ ..apkamPublicKey = pkamKeyPair.publicKey.toString ()
546
+ ..defaultEncryptionPublicKey = encryptionKeyPair.publicKey.toString ()
547
+ ..defaultEncryptionPrivateKey = encryptionKeyPair.privateKey.toString ()
548
+ ..defaultSelfEncryptionKey =
549
+ KeyChainManager .getInstance ().generateAESKey ();
550
+
551
+ AtAuthResponse atAuthResponse =
552
+ await atAuthService.authenticate (atAuthRequest);
553
+
554
+ expect (atAuthResponse.isSuccessful, false );
555
+ });
556
+
557
+ test (
558
+ 'A test to verify authentication is successful when pkamAuthentication returns true' ,
559
+ () async {
560
+ KeyChainManager mockKeyChainManager = MockKeyChainManager ();
561
+ AtLookUp mockAtLookup = MockAtLookUp ();
562
+
563
+ RSAKeypair pkamKeyPair = KeyChainManager .getInstance ().generateKeyPair ();
564
+ RSAKeypair encryptionKeyPair =
565
+ KeyChainManager .getInstance ().generateKeyPair ();
566
+ String selfEncryptionKey = KeyChainManager .getInstance ().generateAESKey ();
567
+ AtAuthService atAuthService = AtClientMobile .authService (
568
+ atSign, atClientPreference,
569
+ atLookUp: mockAtLookup);
570
+ (atAuthService as AtAuthServiceImpl ).keyChainManager =
571
+ mockKeyChainManager;
572
+
573
+ AtsignKey atsignKey = AtsignKey (
574
+ atSign: atSign,
575
+ encryptionPublicKey: encryptionKeyPair.publicKey.toString ());
576
+
577
+ // Mock object to return keys from keychain manager
578
+ when (() => mockKeyChainManager.readAtsign (name: atSign))
579
+ .thenAnswer ((_) => Future .value (atsignKey));
580
+
581
+ when (() => mockAtLookup.pkamAuthenticate (enrollmentId: '123' ))
582
+ .thenAnswer ((_) => Future .value (true ));
583
+
584
+ AtAuthRequest atAuthRequest = AtAuthRequest (atSign);
585
+ atAuthRequest.atAuthKeys = AtAuthKeys ()
586
+ ..apkamPrivateKey = pkamKeyPair.privateKey.toString ()
587
+ ..apkamPublicKey = pkamKeyPair.publicKey.toString ()
588
+ ..defaultEncryptionPublicKey = encryptionKeyPair.publicKey.toString ()
589
+ ..defaultEncryptionPrivateKey = encryptionKeyPair.privateKey.toString ()
590
+ ..defaultSelfEncryptionKey = selfEncryptionKey
591
+ ..enrollmentId = '123' ;
592
+
593
+ AtAuthResponse atAuthResponse =
594
+ await atAuthService.authenticate (atAuthRequest);
595
+
596
+ expect (atAuthResponse.isSuccessful, true );
597
+ expect (atAuthResponse.atSign, atSign);
598
+ expect (atAuthResponse.atAuthKeys? .enrollmentId, '123' );
599
+ expect (atAuthResponse.atAuthKeys? .apkamPublicKey,
600
+ pkamKeyPair.publicKey.toString ());
601
+ expect (atAuthResponse.atAuthKeys? .apkamPrivateKey,
602
+ pkamKeyPair.privateKey.toString ());
603
+ expect (atAuthResponse.atAuthKeys? .defaultEncryptionPrivateKey,
604
+ encryptionKeyPair.privateKey.toString ());
605
+ expect (atAuthResponse.atAuthKeys? .defaultEncryptionPublicKey,
606
+ encryptionKeyPair.publicKey.toString ());
607
+ expect (atAuthResponse.atAuthKeys? .defaultSelfEncryptionKey,
608
+ selfEncryptionKey);
609
+ });
610
+ });
467
611
}
468
612
469
613
void tearDownMethod (MockEnrollmentBiometricStorageFile mockBiometricStorageFile,
0 commit comments