1010using System . IO ;
1111using Microsoft . VisualStudio . TestTools . UnitTesting ;
1212using Moq ;
13+ using SafeExamBrowser . Communication . Contracts . Data ;
1314using SafeExamBrowser . Communication . Contracts . Hosts ;
15+ using SafeExamBrowser . Communication . Contracts . Proxies ;
1416using SafeExamBrowser . Configuration . Contracts ;
1517using SafeExamBrowser . Configuration . Contracts . Cryptography ;
1618using SafeExamBrowser . Core . Contracts . OperationModel ;
1921using SafeExamBrowser . Runtime . Communication ;
2022using SafeExamBrowser . Runtime . Operations . Session ;
2123using SafeExamBrowser . Settings ;
24+ using SafeExamBrowser . Settings . Security ;
2225using SafeExamBrowser . SystemComponents . Contracts ;
2326using SafeExamBrowser . UserInterface . Contracts ;
2427using SafeExamBrowser . UserInterface . Contracts . MessageBox ;
@@ -33,20 +36,17 @@ public class ConfigurationOperationTests
3336 private const string FILE_NAME = "SebClientSettings.seb" ;
3437
3538 private AppConfig appConfig ;
39+ private RuntimeContext context ;
40+ private Dependencies dependencies ;
41+ private SessionConfiguration currentSession ;
42+ private SessionConfiguration nextSession ;
43+
3644 private Mock < IFileSystem > fileSystem ;
3745 private Mock < IHashAlgorithm > hashAlgorithm ;
3846 private Mock < ILogger > logger ;
3947 private Mock < IConfigurationRepository > repository ;
40- private SessionConfiguration currentSession ;
4148 private Mock < IMessageBox > messageBox ;
42- private SessionConfiguration nextSession ;
43- private RuntimeContext runtimeContext ;
44- private Mock < IRuntimeHost > runtimeHost ;
45- private Mock < IRuntimeWindow > runtimeWindow ;
46- private Mock < IText > text ;
4749 private Mock < IUserInterfaceFactory > uiFactory ;
48- private ClientBridge clientBridge ;
49- private Dependencies dependencies ;
5050
5151 [ TestInitialize ]
5252 public void Initialize ( )
@@ -59,20 +59,24 @@ public void Initialize()
5959 currentSession = new SessionConfiguration ( ) ;
6060 messageBox = new Mock < IMessageBox > ( ) ;
6161 nextSession = new SessionConfiguration ( ) ;
62- runtimeContext = new RuntimeContext ( ) ;
63- runtimeHost = new Mock < IRuntimeHost > ( ) ;
64- runtimeWindow = new Mock < IRuntimeWindow > ( ) ;
65- text = new Mock < IText > ( ) ;
62+ context = new RuntimeContext ( ) ;
6663 uiFactory = new Mock < IUserInterfaceFactory > ( ) ;
6764
65+ dependencies = new Dependencies (
66+ new ClientBridge ( Mock . Of < IRuntimeHost > ( ) , context ) ,
67+ logger . Object ,
68+ messageBox . Object ,
69+ Mock . Of < IRuntimeWindow > ( ) ,
70+ context ,
71+ Mock . Of < IText > ( ) ) ;
72+
6873 appConfig . AppDataFilePath = $@ "C:\Not\Really\AppData\File.xml";
6974 appConfig . ProgramDataFilePath = $@ "C:\Not\Really\ProgramData\File.xml";
70- clientBridge = new ClientBridge ( runtimeHost . Object , runtimeContext ) ;
7175 currentSession . AppConfig = appConfig ;
72- dependencies = new Dependencies ( clientBridge , logger . Object , messageBox . Object , runtimeWindow . Object , runtimeContext , text . Object ) ;
76+ currentSession . Settings = new AppSettings ( ) ;
7377 nextSession . AppConfig = appConfig ;
74- runtimeContext . Current = currentSession ;
75- runtimeContext . Next = nextSession ;
78+ context . Current = currentSession ;
79+ context . Next = nextSession ;
7680 }
7781
7882 [ TestMethod ]
@@ -169,13 +173,13 @@ public void Perform_MustAbortIfWishedByUser()
169173 var settings = new AppSettings ( ) ;
170174 var url = @"http://www.safeexambrowser.org/whatever.seb" ;
171175
172- runtimeContext . Current = null ;
176+ context . Current = null ;
173177 settings . ConfigurationMode = ConfigurationMode . ConfigureClient ;
174178 repository . Setup ( r => r . TryLoadSettings ( It . IsAny < Uri > ( ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . Success ) ;
175179 repository . Setup ( r => r . ConfigureClientWith ( It . IsAny < Uri > ( ) , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( SaveStatus . Success ) ;
176180
177181 messageBox
178- . Setup ( m => m . Show ( It . IsAny < TextKey > ( ) , It . IsAny < TextKey > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
182+ . Setup ( m => m . Show ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
179183 . Returns ( MessageBoxResult . Yes ) ;
180184
181185 var sut = new ConfigurationOperation ( new [ ] { "abc.exe" , url } , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
@@ -195,7 +199,7 @@ public void Perform_MustNotAbortIfNotWishedByUser()
195199 repository . Setup ( r => r . ConfigureClientWith ( It . IsAny < Uri > ( ) , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( SaveStatus . Success ) ;
196200
197201 messageBox
198- . Setup ( m => m . Show ( It . IsAny < TextKey > ( ) , It . IsAny < TextKey > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
202+ . Setup ( m => m . Show ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
199203 . Returns ( MessageBoxResult . No ) ;
200204
201205 var sut = new ConfigurationOperation ( new [ ] { "abc.exe" , url } , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
@@ -216,7 +220,7 @@ public void Perform_MustInformAboutClientConfigurationError()
216220 repository . Setup ( r => r . ConfigureClientWith ( It . IsAny < Uri > ( ) , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( SaveStatus . UnexpectedError ) ;
217221
218222 messageBox
219- . Setup ( m => m . Show ( It . IsAny < TextKey > ( ) , It . IsAny < TextKey > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
223+ . Setup ( m => m . Show ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
220224 . Callback ( ( ) => informed = true ) ;
221225
222226 var sut = new ConfigurationOperation ( new [ ] { "abc.exe" , url } , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
@@ -235,7 +239,7 @@ public void Perform_MustNotAllowToAbortIfNotInConfigureClientMode()
235239 repository . Setup ( r => r . TryLoadSettings ( It . IsAny < Uri > ( ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . Success ) ;
236240
237241 messageBox
238- . Setup ( m => m . Show ( It . IsAny < TextKey > ( ) , It . IsAny < TextKey > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
242+ . Setup ( m => m . Show ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < MessageBoxAction > ( ) , It . IsAny < MessageBoxIcon > ( ) , It . IsAny < IWindow > ( ) ) )
239243 . Callback ( Assert . Fail ) ;
240244
241245 var sut = new ConfigurationOperation ( null , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
@@ -434,6 +438,7 @@ public void Perform_MustAbortAskingForAdminPasswordIfDecidedByUser()
434438 var url = @"http://www.safeexambrowser.org/whatever.seb" ;
435439
436440 appConfig . AppDataFilePath = Path . Combine ( Path . GetDirectoryName ( GetType ( ) . Assembly . Location ) , nameof ( Operations ) , "Testdata" , FILE_NAME ) ;
441+ currentSession . Settings = currentSettings ;
437442 currentSettings . Security . AdminPasswordHash = "1234" ;
438443 nextSession . Settings = nextSettings ;
439444 nextSettings . Security . AdminPasswordHash = "9876" ;
@@ -478,7 +483,7 @@ public void Repeat_MustPerformForExamWithCorrectUri()
478483 var settings = new AppSettings { ConfigurationMode = ConfigurationMode . Exam } ;
479484
480485 currentSession . Settings = currentSettings ;
481- runtimeContext . ReconfigurationFilePath = resource . LocalPath ;
486+ context . ReconfigurationFilePath = resource . LocalPath ;
482487 repository . Setup ( r => r . TryLoadSettings ( It . Is < Uri > ( u => u . Equals ( resource ) ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . Success ) ;
483488
484489 var sut = new ConfigurationOperation ( null , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
@@ -500,7 +505,7 @@ public void Repeat_MustPerformForClientConfigurationWithCorrectUri()
500505 var settings = new AppSettings { ConfigurationMode = ConfigurationMode . ConfigureClient } ;
501506
502507 currentSession . Settings = currentSettings ;
503- runtimeContext . ReconfigurationFilePath = resource . LocalPath ;
508+ context . ReconfigurationFilePath = resource . LocalPath ;
504509 repository . Setup ( r => r . TryLoadSettings ( It . Is < Uri > ( u => u . Equals ( resource ) ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . Success ) ;
505510 repository . Setup ( r => r . ConfigureClientWith ( It . Is < Uri > ( u => u . Equals ( resource ) ) , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( SaveStatus . Success ) ;
506511
@@ -526,7 +531,7 @@ public void Repeat_MustDeleteTemporaryFileAfterClientConfiguration()
526531 var order = 0 ;
527532
528533 currentSession . Settings = currentSettings ;
529- runtimeContext . ReconfigurationFilePath = resource . LocalPath ;
534+ context . ReconfigurationFilePath = resource . LocalPath ;
530535 fileSystem . Setup ( f => f . Delete ( It . IsAny < string > ( ) ) ) . Callback ( ( ) => delete = ++ order ) ;
531536 repository . Setup ( r => r . TryLoadSettings ( It . Is < Uri > ( u => u . Equals ( resource ) ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . Success ) ;
532537 repository . Setup ( r => r . ConfigureClientWith ( It . Is < Uri > ( u => u . Equals ( resource ) ) , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( SaveStatus . Success ) . Callback ( ( ) => configure = ++ order ) ;
@@ -549,7 +554,7 @@ public void Repeat_MustFailWithInvalidUri()
549554 var resource = new Uri ( "file:///C:/does/not/exist.txt" ) ;
550555 var settings = default ( AppSettings ) ;
551556
552- runtimeContext . ReconfigurationFilePath = null ;
557+ context . ReconfigurationFilePath = null ;
553558 repository . Setup ( r => r . TryLoadSettings ( It . IsAny < Uri > ( ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . Success ) ;
554559
555560 var sut = new ConfigurationOperation ( null , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
@@ -559,7 +564,7 @@ public void Repeat_MustFailWithInvalidUri()
559564 repository . Verify ( r => r . TryLoadSettings ( It . Is < Uri > ( u => u . Equals ( resource ) ) , out settings , It . IsAny < PasswordParameters > ( ) ) , Times . Never ) ;
560565 Assert . AreEqual ( OperationResult . Failed , result ) ;
561566
562- runtimeContext . ReconfigurationFilePath = resource . LocalPath ;
567+ context . ReconfigurationFilePath = resource . LocalPath ;
563568 result = sut . Repeat ( ) ;
564569
565570 fileSystem . Verify ( f => f . Delete ( It . Is < string > ( s => s == resource . LocalPath ) ) , Times . Never ) ;
@@ -577,9 +582,9 @@ public void Repeat_MustAbortForSettingsPasswordIfWishedByUser()
577582 var settings = new AppSettings { ConfigurationMode = ConfigurationMode . ConfigureClient } ;
578583
579584 currentSession . Settings = currentSettings ;
580- runtimeContext . ReconfigurationFilePath = resource . LocalPath ;
585+ context . ReconfigurationFilePath = resource . LocalPath ;
581586
582- dialog . Setup ( d => d . Show ( It . IsAny < IWindow > ( ) ) ) . Returns ( new PasswordDialogResult { Success = true } ) ;
587+ dialog . Setup ( d => d . Show ( It . IsAny < IWindow > ( ) ) ) . Returns ( new PasswordDialogResult { Success = false } ) ;
583588 repository . Setup ( r => r . TryLoadSettings ( It . Is < Uri > ( u => u . Equals ( resource ) ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . PasswordNeeded ) ;
584589 uiFactory . Setup ( f => f . CreatePasswordDialog ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( dialog . Object ) ;
585590
@@ -590,6 +595,29 @@ public void Repeat_MustAbortForSettingsPasswordIfWishedByUser()
590595 Assert . AreEqual ( OperationResult . Aborted , result ) ;
591596 }
592597
598+ [ TestMethod ]
599+ public void Repeat_MustNotWaitForPasswordViaClientIfCommunicationHasFailed ( )
600+ {
601+ var clientProxy = new Mock < IClientProxy > ( ) ;
602+ var currentSettings = new AppSettings ( ) ;
603+ var location = Path . GetDirectoryName ( GetType ( ) . Assembly . Location ) ;
604+ var resource = new Uri ( Path . Combine ( location , nameof ( Operations ) , "Testdata" , FILE_NAME ) ) ;
605+ var settings = new AppSettings { ConfigurationMode = ConfigurationMode . ConfigureClient } ;
606+
607+ context . ClientProxy = clientProxy . Object ;
608+ context . ReconfigurationFilePath = resource . LocalPath ;
609+ currentSession . Settings = currentSettings ;
610+ currentSettings . Security . KioskMode = KioskMode . CreateNewDesktop ;
611+ clientProxy . Setup ( c => c . RequestPassword ( It . IsAny < PasswordRequestPurpose > ( ) , It . IsAny < Guid > ( ) ) ) . Returns ( new CommunicationResult ( false ) ) ;
612+ repository . Setup ( r => r . TryLoadSettings ( It . Is < Uri > ( u => u . Equals ( resource ) ) , out settings , It . IsAny < PasswordParameters > ( ) ) ) . Returns ( LoadStatus . PasswordNeeded ) ;
613+
614+ var sut = new ConfigurationOperation ( null , dependencies , fileSystem . Object , hashAlgorithm . Object , repository . Object , uiFactory . Object ) ;
615+ var result = sut . Repeat ( ) ;
616+
617+ clientProxy . Verify ( c => c . RequestPassword ( It . IsAny < PasswordRequestPurpose > ( ) , It . IsAny < Guid > ( ) ) , Times . Once ) ;
618+ Assert . AreEqual ( OperationResult . Aborted , result ) ;
619+ }
620+
593621 [ TestMethod ]
594622 public void Revert_MustDoNothing ( )
595623 {
0 commit comments