SEBWIN-674: Extended unit test coverage for third-party application logic.

This commit is contained in:
Damian Büchel 2023-06-02 14:08:11 +02:00
parent 9507888900
commit 627c568400
4 changed files with 118 additions and 3 deletions

View file

@ -60,13 +60,16 @@ namespace SafeExamBrowser.Applications.UnitTests
[TestMethod]
public void MustCorrectlyReadPathFromRegistry()
{
var o = default(object);
object o = @"C:\Some\Registry\Path";
var settings = new WhitelistApplication
{
DisplayName = "Windows Command Prompt",
ExecutableName = "cmd.exe",
ExecutablePath = @"C:\Some\Path"
};
registry.Setup(r => r.TryRead(It.Is<string>(s => s.Contains(RegistryValue.MachineHive.AppPaths_Key)), It.Is<string>(s => s == "Path"), out o)).Returns(true);
var result = sut.TryCreate(settings, out var application);
registry.Verify(r => r.TryRead(It.Is<string>(s => s.Contains(RegistryValue.MachineHive.AppPaths_Key)), It.Is<string>(s => s == "Path"), out o), Times.Once);
@ -91,6 +94,22 @@ namespace SafeExamBrowser.Applications.UnitTests
Assert.IsNull(application);
}
[TestMethod]
public void MustFailGracefullyWhenPathIsInvalid()
{
var settings = new WhitelistApplication
{
ExecutableName = "asdfg(/ç)&=%\"fsdg..exe..",
ExecutablePath = "[]#°§¬#°¢@tu03450'w89tz!$£äöüèé:"
};
var result = sut.TryCreate(settings, out _);
logger.Verify(l => l.Error(It.IsAny<string>(), It.IsAny<Exception>()), Times.AtLeastOnce);
Assert.AreEqual(FactoryResult.NotFound, result);
}
[TestMethod]
public void MustFailGracefullyAndIndicateThatErrorOccurred()
{

View file

@ -51,10 +51,10 @@ namespace SafeExamBrowser.Applications.UnitTests
[TestMethod]
public void GetWindows_MustCorrectlyReturnOpenWindows()
{
var sync = new AutoResetEvent(false);
var openWindows = new List<IntPtr> { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), };
var process1 = new Mock<IProcess>();
var process2 = new Mock<IProcess>();
var openWindows = new List<IntPtr> { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), };
var sync = new AutoResetEvent(false);
nativeMethods.Setup(n => n.GetOpenWindows()).Returns(openWindows);
nativeMethods.Setup(n => n.GetProcessIdFor(It.Is<IntPtr>(p => p == new IntPtr(234)))).Returns(1234);
@ -88,6 +88,7 @@ namespace SafeExamBrowser.Applications.UnitTests
public void Initialize_MustInitializeCorrectly()
{
settings.AutoStart = new Random().Next(2) == 1;
settings.Description = "Some Description";
sut.Initialize();
@ -123,6 +124,40 @@ namespace SafeExamBrowser.Applications.UnitTests
processFactory.Verify(f => f.StartNew(It.IsAny<string>(), It.IsAny<string[]>()), Times.Once);
}
[TestMethod]
public void Start_MustRemoveInstanceCorrectlyWhenTerminated()
{
var eventRaised = false;
var openWindows = new List<IntPtr> { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), };
var process = new Mock<IProcess>();
var sync = new AutoResetEvent(false);
nativeMethods.Setup(n => n.GetOpenWindows()).Returns(openWindows);
nativeMethods.Setup(n => n.GetProcessIdFor(It.Is<IntPtr>(p => p == new IntPtr(234)))).Returns(1234);
process.Setup(p => p.TryClose(It.IsAny<int>())).Returns(false);
process.Setup(p => p.TryKill(It.IsAny<int>())).Returns(true);
process.SetupGet(p => p.Id).Returns(1234);
processFactory.Setup(f => f.StartNew(It.IsAny<string>(), It.IsAny<string[]>())).Returns(process.Object);
sut.WindowsChanged += () =>
{
eventRaised = true;
sync.Set();
};
sut.Initialize();
sut.Start();
sync.WaitOne();
Assert.AreEqual(1, sut.GetWindows().Count());
process.Raise(p => p.Terminated += null, default(int));
Assert.IsTrue(eventRaised);
Assert.AreEqual(0, sut.GetWindows().Count());
}
[TestMethod]
public void Terminate_MustStopAllInstancesCorrectly()
{

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Core.Contracts.Resources.Icons;
using SafeExamBrowser.WindowsApi.Contracts;
namespace SafeExamBrowser.Applications.UnitTests
{
[TestClass]
public class ExternalApplicationWindowTests
{
private IntPtr handle;
private NativeIconResource icon;
private Mock<INativeMethods> nativeMethods;
private ExternalApplicationWindow sut;
[TestInitialize]
public void Initialize()
{
handle = new IntPtr(123);
icon = new NativeIconResource();
nativeMethods = new Mock<INativeMethods>();
sut = new ExternalApplicationWindow(icon, nativeMethods.Object, handle);
}
[TestMethod]
public void Activate_MustCorrectlyActivateWindow()
{
sut.Activate();
nativeMethods.Verify(n => n.ActivateWindow(It.Is<IntPtr>(h => h == handle)));
}
[TestMethod]
public void Update_MustCorrectlyUpdateWindow()
{
var iconChanged = false;
var titleChanged = false;
nativeMethods.Setup(m => m.GetWindowIcon(It.IsAny<IntPtr>())).Returns(new IntPtr(456));
sut.IconChanged += (_) => iconChanged = true;
sut.TitleChanged += (_) => titleChanged = true;
sut.Update();
Assert.IsTrue(iconChanged);
Assert.IsTrue(titleChanged);
}
}
}

View file

@ -84,6 +84,7 @@
<Compile Include="ApplicationFactoryTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ExternalApplicationTests.cs" />
<Compile Include="ExternalApplicationWindowTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />