Restore SEBPatch

This commit is contained in:
2025-06-01 11:44:20 +02:00
commit 8c656e3137
1297 changed files with 142172 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Communication.Contracts;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Operations;
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Core.UnitTests.Operations
{
[TestClass]
public class CommunicationHostOperationTests
{
private Mock<ICommunicationHost> hostMock;
private Mock<ILogger> loggerMock;
private CommunicationHostOperation sut;
[TestInitialize]
public void Initialize()
{
hostMock = new Mock<ICommunicationHost>();
loggerMock = new Mock<ILogger>();
sut = new CommunicationHostOperation(hostMock.Object, loggerMock.Object);
}
[TestMethod]
public void MustRestartHostOnRepeat()
{
var order = 0;
var stop = 0;
var start = 0;
hostMock.Setup(h => h.Stop()).Callback(() => stop = ++order);
hostMock.Setup(h => h.Start()).Callback(() => start = ++order);
var result = sut.Repeat();
hostMock.Verify(h => h.Stop(), Times.Once);
hostMock.Verify(h => h.Start(), Times.Once);
Assert.AreEqual(stop, 1);
Assert.AreEqual(start, 2);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustOnlyRestartHostOnRepeatIfNotRunning()
{
hostMock.SetupGet(h => h.IsRunning).Returns(true);
var result = sut.Repeat();
hostMock.Verify(h => h.Start(), Times.Never);
hostMock.Verify(h => h.Stop(), Times.Never);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustStartHostOnPerform()
{
var result = sut.Perform();
hostMock.Verify(h => h.Start(), Times.Once);
hostMock.Verify(h => h.Stop(), Times.Never);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustStopHostOnRevert()
{
sut.Revert();
hostMock.Verify(h => h.Stop(), Times.Once);
hostMock.Verify(h => h.Start(), Times.Never);
}
[TestMethod]
public void MustFireStatusChangedEvent()
{
var fired = 0;
sut.StatusChanged += (_) => fired++;
sut.Perform();
sut.Repeat();
sut.Revert();
Assert.AreEqual(3, fired);
}
[TestMethod]
public void MustNotFireActionRequiredEvent()
{
sut.ActionRequired += (_) => Assert.Fail();
sut.Perform();
sut.Repeat();
sut.Revert();
}
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 Microsoft.VisualStudio.TestTools.UnitTesting;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Operations;
namespace SafeExamBrowser.Core.UnitTests.Operations
{
[TestClass]
public class DelegateOperationTests
{
[TestMethod]
public void MustExecutePerformAction()
{
var performed = false;
void perform() => performed = true;
var sut = new DelegateOperation(perform);
sut.Perform();
Assert.IsTrue(performed);
}
[TestMethod]
public void MustExecuteRepeatAction()
{
var repeated = false;
void repeat() => repeated = true;
var sut = new DelegateOperation(() => { }, repeat);
sut.Repeat();
Assert.IsTrue(repeated);
}
[TestMethod]
public void MustExecuteRevertAction()
{
var reverted = false;
void revert() => reverted = true;
var sut = new DelegateOperation(() => { }, revert: revert);
sut.Revert();
Assert.IsTrue(reverted);
}
[TestMethod]
public void MustNotFailIfActionsAreNull()
{
var sut = new DelegateOperation(null, null, null);
var perform = sut.Perform();
var repeat = sut.Repeat();
var revert = sut.Revert();
Assert.AreEqual(OperationResult.Success, perform);
Assert.AreEqual(OperationResult.Success, repeat);
Assert.AreEqual(OperationResult.Success, revert);
}
[TestMethod]
public void MustNotFireEvents()
{
var sut = new DelegateOperation(default, default, default);
sut.ActionRequired += (_) => Assert.Fail();
sut.StatusChanged += (_) => Assert.Fail();
sut.Perform();
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Operations;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Core.UnitTests.Operations
{
[TestClass]
public class I18nOperationTests
{
private Mock<ILogger> logger;
private Mock<IText> text;
private I18nOperation sut;
[TestInitialize]
public void Initialize()
{
logger = new Mock<ILogger>();
text = new Mock<IText>();
sut = new I18nOperation(logger.Object, text.Object);
}
[TestMethod]
public void MustPerformCorrectly()
{
var result = sut.Perform();
text.Verify(t => t.Initialize(), Times.Once);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustDoNothingOnRevert()
{
sut.Revert();
text.VerifyNoOtherCalls();
}
[TestMethod]
public void MustNotFireEvents()
{
sut.ActionRequired += (_) => Assert.Fail();
sut.StatusChanged += (_) => Assert.Fail();
sut.Perform();
}
}
}

View File

@@ -0,0 +1,163 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
using SafeExamBrowser.Core.Operations;
using SafeExamBrowser.I18n.Contracts;
namespace SafeExamBrowser.Core.UnitTests.Operations
{
[TestClass]
public class LazyInitializationOperationTests
{
private Mock<IOperation> operationMock;
[TestInitialize]
public void Initialize()
{
operationMock = new Mock<IOperation>();
}
[TestMethod]
public void MustInstantiateOperationOnPerform()
{
var initialized = false;
IOperation initialize()
{
initialized = true;
return operationMock.Object;
};
var sut = new LazyInitializationOperation(initialize);
sut.Perform();
Assert.IsTrue(initialized);
}
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void MustNotInstantiateOperationOnRevert()
{
IOperation initialize()
{
return operationMock.Object;
};
var sut = new LazyInitializationOperation(initialize);
sut.Revert();
}
[TestMethod]
public void MustReturnCorrectOperationResult()
{
IOperation initialize()
{
return operationMock.Object;
};
operationMock.Setup(o => o.Perform()).Returns(OperationResult.Success);
operationMock.Setup(o => o.Revert()).Returns(OperationResult.Failed);
var sut = new LazyInitializationOperation(initialize);
var perform = sut.Perform();
var revert = sut.Revert();
Assert.AreEqual(OperationResult.Success, perform);
Assert.AreEqual(OperationResult.Failed, revert);
}
[TestMethod]
public void MustCorrectlyHandleEventSubscription()
{
IOperation initialize()
{
return operationMock.Object;
};
var actionRequired = 0;
var actionRequiredHandler = new ActionRequiredEventHandler(args => actionRequired++);
var statusChanged = 0;
var statusChangedHandler = new StatusChangedEventHandler(t => statusChanged++);
var sut = new LazyInitializationOperation(initialize);
sut.ActionRequired += actionRequiredHandler;
sut.StatusChanged += statusChangedHandler;
sut.Perform();
operationMock.Raise(o => o.ActionRequired += null, new Mock<ActionRequiredEventArgs>().Object);
operationMock.Raise(o => o.StatusChanged += null, default(TextKey));
Assert.AreEqual(1, actionRequired);
Assert.AreEqual(1, statusChanged);
sut.ActionRequired -= actionRequiredHandler;
sut.StatusChanged -= statusChangedHandler;
operationMock.Raise(o => o.ActionRequired += null, new Mock<ActionRequiredEventArgs>().Object);
operationMock.Raise(o => o.StatusChanged += null, default(TextKey));
Assert.AreEqual(1, actionRequired);
Assert.AreEqual(1, statusChanged);
sut.ActionRequired += actionRequiredHandler;
sut.StatusChanged += statusChangedHandler;
operationMock.Raise(o => o.ActionRequired += null, new Mock<ActionRequiredEventArgs>().Object);
operationMock.Raise(o => o.StatusChanged += null, default(TextKey));
Assert.AreEqual(2, actionRequired);
Assert.AreEqual(2, statusChanged);
}
[TestMethod]
public void MustUseSameInstanceForAllOperations()
{
var first = true;
var operation = operationMock.Object;
IOperation initialize()
{
if (first)
{
first = false;
return operation;
}
return new Mock<IOperation>().Object;
};
var sut = new LazyInitializationOperation(initialize);
sut.Perform();
sut.Revert();
operationMock.Verify(o => o.Perform(), Times.Once);
operationMock.Verify(o => o.Revert(), Times.Once);
}
[TestMethod]
public void MustNotFailOnEventRegistrationWithoutOperation()
{
var sut = new LazyInitializationOperation(() => default);
sut.ActionRequired += default;
sut.StatusChanged += default;
sut.ActionRequired -= default;
sut.StatusChanged -= default;
}
}
}