From 0da587e521c479aae08be98836d47d87b12a81b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Mon, 18 Oct 2021 12:06:10 +0200 Subject: [PATCH] SEBWIN-508: Implemented basic JavaScript API. --- .../Handlers/RequestHandlerTests.cs | 5 +- .../Handlers/ResourceHandlerTests.cs | 14 +++- SafeExamBrowser.Browser/BrowserApplication.cs | 39 +++++++--- .../BrowserApplicationInstance.cs | 25 ++++--- SafeExamBrowser.Browser/BrowserControl.cs | 18 +++-- SafeExamBrowser.Browser/Content/Api.js | 8 ++ .../{Pages => Content}/BlockedContent.html | 0 .../{Pages => Content}/BlockedPage.html | 0 .../ContentLoader.cs} | 38 ++++++++-- .../Handlers/RenderProcessMessageHandler.cs | 54 +++++++++++++ .../Handlers/ResourceHandler.cs | 66 +++++----------- .../SafeExamBrowser.Browser.csproj | 10 ++- SafeExamBrowser.Client/CompositionRoot.cs | 13 +++- .../Cryptography/IKeyGenerator.cs | 26 +++++++ ...ExamBrowser.Configuration.Contracts.csproj | 1 + .../DataMapping/BrowserDataMapper.cs | 4 +- .../Cryptography/KeyGenerator.cs | 75 +++++++++++++++++++ .../SafeExamBrowser.Configuration.csproj | 1 + .../Browser/BrowserSettings.cs | 14 ++-- 19 files changed, 311 insertions(+), 100 deletions(-) create mode 100644 SafeExamBrowser.Browser/Content/Api.js rename SafeExamBrowser.Browser/{Pages => Content}/BlockedContent.html (100%) rename SafeExamBrowser.Browser/{Pages => Content}/BlockedPage.html (100%) rename SafeExamBrowser.Browser/{Pages/HtmlLoader.cs => Content/ContentLoader.cs} (55%) create mode 100644 SafeExamBrowser.Browser/Handlers/RenderProcessMessageHandler.cs create mode 100644 SafeExamBrowser.Configuration.Contracts/Cryptography/IKeyGenerator.cs create mode 100644 SafeExamBrowser.Configuration/Cryptography/KeyGenerator.cs diff --git a/SafeExamBrowser.Browser.UnitTests/Handlers/RequestHandlerTests.cs b/SafeExamBrowser.Browser.UnitTests/Handlers/RequestHandlerTests.cs index 1b528c93..903d3d86 100644 --- a/SafeExamBrowser.Browser.UnitTests/Handlers/RequestHandlerTests.cs +++ b/SafeExamBrowser.Browser.UnitTests/Handlers/RequestHandlerTests.cs @@ -13,6 +13,7 @@ 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; @@ -29,6 +30,7 @@ namespace SafeExamBrowser.Browser.UnitTests.Handlers { private AppConfig appConfig; private Mock filter; + private Mock keyGenerator; private Mock logger; private BrowserSettings settings; private WindowSettings windowSettings; @@ -41,11 +43,12 @@ namespace SafeExamBrowser.Browser.UnitTests.Handlers { 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, logger.Object, settings, windowSettings, text.Object); + resourceHandler = new ResourceHandler(appConfig, filter.Object, keyGenerator.Object, logger.Object, settings, windowSettings, text.Object); sut = new TestableRequestHandler(appConfig, filter.Object, logger.Object, resourceHandler, settings, windowSettings, text.Object); } diff --git a/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs b/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs index 45cf3a33..b848f5de 100644 --- a/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs +++ b/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs @@ -15,6 +15,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Browser.Contracts.Filters; using SafeExamBrowser.Configuration.Contracts; +using SafeExamBrowser.Configuration.Contracts.Cryptography; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Settings.Browser; @@ -30,6 +31,7 @@ namespace SafeExamBrowser.Browser.UnitTests.Handlers { private AppConfig appConfig; private Mock filter; + private Mock keyGenerator; private Mock logger; private BrowserSettings settings; private WindowSettings windowSettings; @@ -41,12 +43,13 @@ namespace SafeExamBrowser.Browser.UnitTests.Handlers { appConfig = new AppConfig(); filter = new Mock(); + keyGenerator = new Mock(); logger = new Mock(); settings = new BrowserSettings(); windowSettings = new WindowSettings(); text = new Mock(); - sut = new TestableResourceHandler(appConfig, filter.Object, logger.Object, settings, windowSettings, text.Object); + sut = new TestableResourceHandler(appConfig, filter.Object, keyGenerator.Object, logger.Object, settings, windowSettings, text.Object); } [TestMethod] @@ -57,11 +60,13 @@ namespace SafeExamBrowser.Browser.UnitTests.Handlers var request = new Mock(); browser.SetupGet(b => b.Address).Returns("http://www.host.org"); + keyGenerator.Setup(g => g.CalculateBrowserExamKeyHash(It.IsAny())).Returns(new Random().Next().ToString()); + keyGenerator.Setup(g => g.CalculateConfigurationKeyHash(It.IsAny())).Returns(new Random().Next().ToString()); request.SetupGet(r => r.Headers).Returns(new NameValueCollection()); request.SetupGet(r => r.Url).Returns("http://www.host.org"); request.SetupSet(r => r.Headers = It.IsAny()).Callback((h) => headers = h); settings.SendConfigurationKey = true; - settings.SendExamKey = true; + settings.SendBrowserExamKey = true; var result = sut.OnBeforeResourceLoad(browser.Object, Mock.Of(), Mock.Of