/* * 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 CefSharp; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Browser.Contracts.Filters; using SafeExamBrowser.Browser.Handlers; using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.Configuration.Contracts.Cryptography; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Settings.Browser; using SafeExamBrowser.Settings.Browser.Filter; using SafeExamBrowser.Settings.Browser.Proxy; using BrowserSettings = SafeExamBrowser.Settings.Browser.BrowserSettings; using Request = SafeExamBrowser.Browser.Contracts.Filters.Request; using ResourceHandler = SafeExamBrowser.Browser.Handlers.ResourceHandler; namespace SafeExamBrowser.Browser.UnitTests.Handlers { [TestClass] public class RequestHandlerTests { private AppConfig appConfig; private Mock filter; private Mock keyGenerator; private Mock logger; private BrowserSettings settings; private WindowSettings windowSettings; private ResourceHandler resourceHandler; private Mock text; private TestableRequestHandler sut; [TestInitialize] public void Initialize() { appConfig = new AppConfig(); filter = new Mock(); keyGenerator = new Mock(); logger = new Mock(); settings = new BrowserSettings(); windowSettings = new WindowSettings(); text = new Mock(); resourceHandler = new ResourceHandler(appConfig, filter.Object, keyGenerator.Object, logger.Object, default, settings, windowSettings, text.Object); sut = new TestableRequestHandler(appConfig, filter.Object, logger.Object, resourceHandler, settings, windowSettings); } [TestMethod] public void MustBlockSpecialWindowDispositions() { Assert.IsTrue(sut.OnOpenUrlFromTab(default, default, default, default, WindowOpenDisposition.NewBackgroundTab, default)); Assert.IsTrue(sut.OnOpenUrlFromTab(default, default, default, default, WindowOpenDisposition.NewPopup, default)); Assert.IsTrue(sut.OnOpenUrlFromTab(default, default, default, default, WindowOpenDisposition.NewWindow, default)); Assert.IsTrue(sut.OnOpenUrlFromTab(default, default, default, default, WindowOpenDisposition.SaveToDisk, default)); } [TestMethod] public void MustDetectQuitUrl() { var eventFired = false; var quitUrl = "http://www.byebye.com"; var request = new Mock(); appConfig.ConfigurationFileMimeType = "application/seb"; request.SetupGet(r => r.Url).Returns(quitUrl); settings.QuitUrl = quitUrl; sut.QuitUrlVisited += (url) => eventFired = true; var blocked = sut.OnBeforeBrowse(Mock.Of(), Mock.Of(), Mock.Of