From 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:33 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- .../contentanalysis/tests/browser/browser.toml | 3 + .../browser/browser_content_analysis_policies.js | 127 ++++++++++++ .../contentanalysis/tests/browser/moz.build | 7 + .../tests/gtest/TestContentAnalysis.cpp | 214 ++++++++++----------- .../tests/gtest/TestContentAnalysis.h | 24 --- .../tests/gtest/TestContentAnalysisAgent.cpp | 132 +++++++++++++ .../tests/gtest/TestContentAnalysisAgent.h | 24 +++ .../tests/gtest/TestContentAnalysisMisbehaving.cpp | 2 +- .../tests/gtest/TestContentAnalysisUtils.cpp | 2 +- .../contentanalysis/tests/gtest/moz.build | 4 + toolkit/components/contentanalysis/tests/moz.build | 7 + 11 files changed, 410 insertions(+), 136 deletions(-) create mode 100644 toolkit/components/contentanalysis/tests/browser/browser.toml create mode 100644 toolkit/components/contentanalysis/tests/browser/browser_content_analysis_policies.js create mode 100644 toolkit/components/contentanalysis/tests/browser/moz.build delete mode 100644 toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.h create mode 100644 toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.cpp create mode 100644 toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.h create mode 100644 toolkit/components/contentanalysis/tests/moz.build (limited to 'toolkit/components/contentanalysis/tests') diff --git a/toolkit/components/contentanalysis/tests/browser/browser.toml b/toolkit/components/contentanalysis/tests/browser/browser.toml new file mode 100644 index 0000000000..0e21090299 --- /dev/null +++ b/toolkit/components/contentanalysis/tests/browser/browser.toml @@ -0,0 +1,3 @@ +[DEFAULT] + +["browser_content_analysis_policies.js"] diff --git a/toolkit/components/contentanalysis/tests/browser/browser_content_analysis_policies.js b/toolkit/components/contentanalysis/tests/browser/browser_content_analysis_policies.js new file mode 100644 index 0000000000..e2e001e9d1 --- /dev/null +++ b/toolkit/components/contentanalysis/tests/browser/browser_content_analysis_policies.js @@ -0,0 +1,127 @@ +/* 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/. */ + +// Check that CA is active if and only if: +// 1. browser.contentanalysis.enabled is true and +// 2. Either browser.contentanalysis.enabled was set by an enteprise +// policy or the "-allow-content-analysis" command line arg was present +// We can't really test command line arguments so we instead use a test-only +// method to set the value the command-line is supposed to update. + +"use strict"; + +const { EnterprisePolicyTesting } = ChromeUtils.importESModule( + "resource://testing-common/EnterprisePolicyTesting.sys.mjs" +); + +const kEnabledPref = "enabled"; +const kPipeNamePref = "pipe_path_name"; +const kTimeoutPref = "agent_timeout"; +const kAllowUrlPref = "allow_url_regex_list"; +const kDenyUrlPref = "deny_url_regex_list"; +const kPerUserPref = "is_per_user"; +const kShowBlockedPref = "show_blocked_result"; +const kDefaultAllowPref = "default_allow"; + +const ca = Cc["@mozilla.org/contentanalysis;1"].getService( + Ci.nsIContentAnalysis +); + +add_task(async function test_ca_active() { + ok(!ca.isActive, "CA is inactive when pref and cmd line arg are missing"); + + // Set the pref without enterprise policy. CA should not be active. + Services.prefs.setBoolPref("browser.contentanalysis." + kEnabledPref, true); + ok( + !ca.isActive, + "CA is inactive when pref is set but cmd line arg is missing" + ); + + // Set the pref without enterprise policy but also set command line arg + // property. CA should be active. + ca.testOnlySetCACmdLineArg(true); + ok(ca.isActive, "CA is active when pref is set and cmd line arg is present"); + + // Undo test-only value before later tests. + ca.testOnlySetCACmdLineArg(false); + ok(!ca.isActive, "properly unset cmd line arg value"); + + // Disabled the pref with enterprise policy. CA should not be active. + await EnterprisePolicyTesting.setupPolicyEngineWithJson({ + policies: { + ContentAnalysis: { Enabled: false }, + }, + }); + ok(!ca.isActive, "CA is inactive when disabled by enterprise policy pref"); + + // Enabled the pref with enterprise policy. CA should be active. + await EnterprisePolicyTesting.setupPolicyEngineWithJson({ + policies: { + ContentAnalysis: { Enabled: true }, + }, + }); + ok(ca.isActive, "CA is active when enabled by enterprise policy pref"); +}); + +add_task(async function test_ca_enterprise_config() { + const string1 = "this is a string"; + const string2 = "this is another string"; + + await EnterprisePolicyTesting.setupPolicyEngineWithJson({ + policies: { + ContentAnalysis: { + PipePathName: "abc", + AgentTimeout: 99, + AllowUrlRegexList: string1, + DenyUrlRegexList: string2, + IsPerUser: true, + ShowBlockedResult: false, + DefaultAllow: true, + }, + }, + }); + + is( + Services.prefs.getStringPref("browser.contentanalysis." + kPipeNamePref), + "abc", + "pipe name match" + ); + is( + Services.prefs.getIntPref("browser.contentanalysis." + kTimeoutPref), + 99, + "timeout match" + ); + is( + Services.prefs.getStringPref("browser.contentanalysis." + kAllowUrlPref), + string1, + "allow urls match" + ); + is( + Services.prefs.getStringPref("browser.contentanalysis." + kDenyUrlPref), + string2, + "deny urls match" + ); + is( + Services.prefs.getBoolPref("browser.contentanalysis." + kPerUserPref), + true, + "per user match" + ); + is( + Services.prefs.getBoolPref("browser.contentanalysis." + kShowBlockedPref), + false, + "show blocked match" + ); + is( + Services.prefs.getBoolPref("browser.contentanalysis." + kDefaultAllowPref), + true, + "default allow match" + ); +}); + +add_task(async function test_cleanup() { + ca.testOnlySetCACmdLineArg(false); + await EnterprisePolicyTesting.setupPolicyEngineWithJson({ + policies: {}, + }); +}); diff --git a/toolkit/components/contentanalysis/tests/browser/moz.build b/toolkit/components/contentanalysis/tests/browser/moz.build new file mode 100644 index 0000000000..cfd5452a0e --- /dev/null +++ b/toolkit/components/contentanalysis/tests/browser/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +BROWSER_CHROME_MANIFESTS += ["browser.toml"] diff --git a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.cpp b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.cpp index 1cf6d8fc22..cd083a7779 100644 --- a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.cpp +++ b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.cpp @@ -6,127 +6,121 @@ #include "gtest/gtest.h" #include "mozilla/ArrayUtils.h" #include "mozilla/Assertions.h" -#include "mozilla/CmdLineAndEnvUtils.h" -#include "content_analysis/sdk/analysis_client.h" -#include "TestContentAnalysis.h" +#include "mozilla/Logging.h" +#include "mozilla/Preferences.h" +#include "nsNetUtil.h" +#include "ContentAnalysis.h" #include #include -using namespace content_analysis::sdk; +const char* kAllowUrlPref = "browser.contentanalysis.allow_url_regex_list"; +const char* kDenyUrlPref = "browser.contentanalysis.deny_url_regex_list"; -MozAgentInfo LaunchAgentNormal(const wchar_t* aToBlock) { - nsString cmdLineArguments; - if (aToBlock && aToBlock[0] != 0) { - cmdLineArguments.Append(L" --toblock=.*"); - cmdLineArguments.Append(aToBlock); - cmdLineArguments.Append(L".*"); +using namespace mozilla; +using namespace mozilla::contentanalysis; + +class ContentAnalysisTest : public testing::Test { + protected: + ContentAnalysisTest() { + auto* logmodule = LogModule::Get("contentanalysis"); + logmodule->SetLevel(LogLevel::Verbose); + + nsCOMPtr caSvc = + do_GetService("@mozilla.org/contentanalysis;1"); + MOZ_ASSERT(caSvc); + mContentAnalysis = static_cast(caSvc.get()); + + // Tests run earlier could have altered these values + mContentAnalysis->mParsedUrlLists = false; + mContentAnalysis->mAllowUrlList = {}; + mContentAnalysis->mDenyUrlList = {}; + + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kAllowUrlPref, "")); + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kDenyUrlPref, "")); + } + + void TearDown() override { + mContentAnalysis->mParsedUrlLists = false; + mContentAnalysis->mAllowUrlList = {}; + mContentAnalysis->mDenyUrlList = {}; + + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kAllowUrlPref, "")); + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kDenyUrlPref, "")); } - cmdLineArguments.Append(L" --user"); - cmdLineArguments.Append(L" --path="); - nsString pipeName; - GeneratePipeName(L"contentanalysissdk-gtest-", pipeName); - cmdLineArguments.Append(pipeName); - MozAgentInfo agentInfo; - LaunchAgentWithCommandLineArguments(cmdLineArguments, pipeName, agentInfo); - return agentInfo; + + already_AddRefed CreateRequest(const char* aUrl) { + nsCOMPtr uri; + MOZ_ALWAYS_SUCCEEDS(NS_NewURI(getter_AddRefs(uri), aUrl)); + // We will only use the URL and, implicitly, the analysisType + // (behavior differs for download vs other types). + return RefPtr(new ContentAnalysisRequest( + nsIContentAnalysisRequest::AnalysisType::eFileTransfer, + EmptyString(), false, EmptyCString(), uri, + nsIContentAnalysisRequest::OperationType::eDroppedText, + nullptr)) + .forget(); + } + + RefPtr mContentAnalysis; + + // Proxies for private members of ContentAnalysis. TEST_F + // creates new subclasses -- they do not inherit `friend`s. + // (FRIEND_TEST is another more verbose solution.) + using UrlFilterResult = ContentAnalysis::UrlFilterResult; + UrlFilterResult FilterByUrlLists(nsIContentAnalysisRequest* aReq) { + return mContentAnalysis->FilterByUrlLists(aReq); + } +}; + +TEST_F(ContentAnalysisTest, AllowUrlList) { + MOZ_ALWAYS_SUCCEEDS( + Preferences::SetCString(kAllowUrlPref, ".*\\.org/match.*")); + RefPtr car = + CreateRequest("https://example.org/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow); + car = CreateRequest("https://example.com/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck); } -TEST(ContentAnalysis, TextShouldNotBeBlocked) -{ - auto MozAgentInfo = LaunchAgentNormal(L"block"); - // Exit the test early if the process failed to launch - ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); - ASSERT_NE(nullptr, MozAgentInfo.client.get()); - - ContentAnalysisRequest request; - request.set_request_token("request token"); - request.set_text_content("should succeed"); - ContentAnalysisResponse response; - ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); - ASSERT_STREQ("request token", response.request_token().c_str()); - ASSERT_EQ(1, response.results().size()); - ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, - response.results().Get(0).status()); - ASSERT_EQ(0, response.results().Get(0).triggered_rules_size()); - - BOOL terminateResult = - ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); - ASSERT_NE(FALSE, terminateResult) - << "Failed to terminate content_analysis_sdk_agent process"; +TEST_F(ContentAnalysisTest, MultipleAllowUrlList) { + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString( + kAllowUrlPref, ".*\\.org/match.* .*\\.net/match.*")); + RefPtr car = + CreateRequest("https://example.org/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow); + car = CreateRequest("https://example.net/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow); + car = CreateRequest("https://example.com/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck); } -TEST(ContentAnalysis, TextShouldBeBlocked) -{ - auto MozAgentInfo = LaunchAgentNormal(L"block"); - // Exit the test early if the process failed to launch - ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); - ASSERT_NE(nullptr, MozAgentInfo.client.get()); - - ContentAnalysisRequest request; - request.set_request_token("request token"); - request.set_text_content("should be blocked"); - ContentAnalysisResponse response; - ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); - ASSERT_STREQ("request token", response.request_token().c_str()); - ASSERT_EQ(1, response.results().size()); - ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, - response.results().Get(0).status()); - ASSERT_EQ(1, response.results().Get(0).triggered_rules_size()); - ASSERT_EQ(ContentAnalysisResponse_Result_TriggeredRule_Action_BLOCK, - response.results().Get(0).triggered_rules(0).action()); - - BOOL terminateResult = - ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); - ASSERT_NE(FALSE, terminateResult) - << "Failed to terminate content_analysis_sdk_agent process"; +TEST_F(ContentAnalysisTest, DenyUrlList) { + MOZ_ALWAYS_SUCCEEDS( + Preferences::SetCString(kDenyUrlPref, ".*\\.com/match.*")); + RefPtr car = + CreateRequest("https://example.org/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck); + car = CreateRequest("https://example.com/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny); } -TEST(ContentAnalysis, FileShouldNotBeBlocked) -{ - auto MozAgentInfo = LaunchAgentNormal(L"block"); - // Exit the test early if the process failed to launch - ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); - ASSERT_NE(nullptr, MozAgentInfo.client.get()); - - ContentAnalysisRequest request; - request.set_request_token("request token"); - request.set_file_path("..\\..\\_tests\\gtest\\allowedFile.txt"); - ContentAnalysisResponse response; - ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); - ASSERT_STREQ("request token", response.request_token().c_str()); - ASSERT_EQ(1, response.results().size()); - ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, - response.results().Get(0).status()); - ASSERT_EQ(0, response.results().Get(0).triggered_rules_size()); - - BOOL terminateResult = - ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); - ASSERT_NE(FALSE, terminateResult) - << "Failed to terminate content_analysis_sdk_agent process"; +TEST_F(ContentAnalysisTest, MultipleDenyUrlList) { + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString( + kDenyUrlPref, ".*\\.com/match.* .*\\.biz/match.*")); + RefPtr car = + CreateRequest("https://example.org/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck); + car = CreateRequest("https://example.com/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny); + car = CreateRequest("https://example.biz/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny); } -TEST(ContentAnalysis, FileShouldBeBlocked) -{ - auto MozAgentInfo = LaunchAgentNormal(L"block"); - // Exit the test early if the process failed to launch - ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); - ASSERT_NE(nullptr, MozAgentInfo.client.get()); - - ContentAnalysisRequest request; - request.set_request_token("request token"); - request.set_file_path("..\\..\\_tests\\gtest\\blockedFile.txt"); - ContentAnalysisResponse response; - ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); - ASSERT_STREQ("request token", response.request_token().c_str()); - ASSERT_EQ(1, response.results().size()); - ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, - response.results().Get(0).status()); - ASSERT_EQ(1, response.results().Get(0).triggered_rules_size()); - ASSERT_EQ(ContentAnalysisResponse_Result_TriggeredRule_Action_BLOCK, - response.results().Get(0).triggered_rules(0).action()); - - BOOL terminateResult = - ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); - ASSERT_NE(FALSE, terminateResult) - << "Failed to terminate content_analysis_sdk_agent process"; +TEST_F(ContentAnalysisTest, DenyOverridesAllowUrlList) { + MOZ_ALWAYS_SUCCEEDS( + Preferences::SetCString(kAllowUrlPref, ".*\\.org/match.*")); + MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kDenyUrlPref, ".*.org/match.*")); + RefPtr car = + CreateRequest("https://example.org/matchme/"); + ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny); } diff --git a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.h b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.h deleted file mode 100644 index 9e31036262..0000000000 --- a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysis.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* 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/. */ -#ifndef mozilla_testcontentanalysis_h -#define mozilla_testcontentanalysis_h - -#include - -#include "content_analysis/sdk/analysis_client.h" -#include "gtest/gtest.h" -#include "nsString.h" - -struct MozAgentInfo { - PROCESS_INFORMATION processInfo; - std::unique_ptr client; -}; - -void GeneratePipeName(const wchar_t* prefix, nsString& pipeName); -void LaunchAgentWithCommandLineArguments(const nsString& cmdLineArguments, - const nsString& pipeName, - MozAgentInfo& agentInfo); -#endif diff --git a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.cpp b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.cpp new file mode 100644 index 0000000000..5b2b76b963 --- /dev/null +++ b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.cpp @@ -0,0 +1,132 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 https://mozilla.org/MPL/2.0/. */ + +#include "gtest/gtest.h" +#include "mozilla/ArrayUtils.h" +#include "mozilla/Assertions.h" +#include "mozilla/CmdLineAndEnvUtils.h" +#include "content_analysis/sdk/analysis_client.h" +#include "TestContentAnalysisAgent.h" +#include +#include + +using namespace content_analysis::sdk; + +MozAgentInfo LaunchAgentNormal(const wchar_t* aToBlock) { + nsString cmdLineArguments; + if (aToBlock && aToBlock[0] != 0) { + cmdLineArguments.Append(L" --toblock=.*"); + cmdLineArguments.Append(aToBlock); + cmdLineArguments.Append(L".*"); + } + cmdLineArguments.Append(L" --user"); + cmdLineArguments.Append(L" --path="); + nsString pipeName; + GeneratePipeName(L"contentanalysissdk-gtest-", pipeName); + cmdLineArguments.Append(pipeName); + MozAgentInfo agentInfo; + LaunchAgentWithCommandLineArguments(cmdLineArguments, pipeName, agentInfo); + return agentInfo; +} + +TEST(ContentAnalysisAgent, TextShouldNotBeBlocked) +{ + auto MozAgentInfo = LaunchAgentNormal(L"block"); + // Exit the test early if the process failed to launch + ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); + ASSERT_NE(nullptr, MozAgentInfo.client.get()); + + ContentAnalysisRequest request; + request.set_request_token("request token"); + request.set_text_content("should succeed"); + ContentAnalysisResponse response; + ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); + ASSERT_STREQ("request token", response.request_token().c_str()); + ASSERT_EQ(1, response.results().size()); + ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, + response.results().Get(0).status()); + ASSERT_EQ(0, response.results().Get(0).triggered_rules_size()); + + BOOL terminateResult = + ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); + ASSERT_NE(FALSE, terminateResult) + << "Failed to terminate content_analysis_sdk_agent process"; +} + +TEST(ContentAnalysisAgent, TextShouldBeBlocked) +{ + auto MozAgentInfo = LaunchAgentNormal(L"block"); + // Exit the test early if the process failed to launch + ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); + ASSERT_NE(nullptr, MozAgentInfo.client.get()); + + ContentAnalysisRequest request; + request.set_request_token("request token"); + request.set_text_content("should be blocked"); + ContentAnalysisResponse response; + ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); + ASSERT_STREQ("request token", response.request_token().c_str()); + ASSERT_EQ(1, response.results().size()); + ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, + response.results().Get(0).status()); + ASSERT_EQ(1, response.results().Get(0).triggered_rules_size()); + ASSERT_EQ(ContentAnalysisResponse_Result_TriggeredRule_Action_BLOCK, + response.results().Get(0).triggered_rules(0).action()); + + BOOL terminateResult = + ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); + ASSERT_NE(FALSE, terminateResult) + << "Failed to terminate content_analysis_sdk_agent process"; +} + +TEST(ContentAnalysisAgent, FileShouldNotBeBlocked) +{ + auto MozAgentInfo = LaunchAgentNormal(L"block"); + // Exit the test early if the process failed to launch + ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); + ASSERT_NE(nullptr, MozAgentInfo.client.get()); + + ContentAnalysisRequest request; + request.set_request_token("request token"); + request.set_file_path("..\\..\\_tests\\gtest\\allowedFile.txt"); + ContentAnalysisResponse response; + ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); + ASSERT_STREQ("request token", response.request_token().c_str()); + ASSERT_EQ(1, response.results().size()); + ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, + response.results().Get(0).status()); + ASSERT_EQ(0, response.results().Get(0).triggered_rules_size()); + + BOOL terminateResult = + ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); + ASSERT_NE(FALSE, terminateResult) + << "Failed to terminate content_analysis_sdk_agent process"; +} + +TEST(ContentAnalysisAgent, FileShouldBeBlocked) +{ + auto MozAgentInfo = LaunchAgentNormal(L"block"); + // Exit the test early if the process failed to launch + ASSERT_NE(MozAgentInfo.processInfo.dwProcessId, 0UL); + ASSERT_NE(nullptr, MozAgentInfo.client.get()); + + ContentAnalysisRequest request; + request.set_request_token("request token"); + request.set_file_path("..\\..\\_tests\\gtest\\blockedFile.txt"); + ContentAnalysisResponse response; + ASSERT_EQ(0, MozAgentInfo.client->Send(request, &response)); + ASSERT_STREQ("request token", response.request_token().c_str()); + ASSERT_EQ(1, response.results().size()); + ASSERT_EQ(ContentAnalysisResponse_Result_Status_SUCCESS, + response.results().Get(0).status()); + ASSERT_EQ(1, response.results().Get(0).triggered_rules_size()); + ASSERT_EQ(ContentAnalysisResponse_Result_TriggeredRule_Action_BLOCK, + response.results().Get(0).triggered_rules(0).action()); + + BOOL terminateResult = + ::TerminateProcess(MozAgentInfo.processInfo.hProcess, 0); + ASSERT_NE(FALSE, terminateResult) + << "Failed to terminate content_analysis_sdk_agent process"; +} diff --git a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.h b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.h new file mode 100644 index 0000000000..9e31036262 --- /dev/null +++ b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisAgent.h @@ -0,0 +1,24 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ +#ifndef mozilla_testcontentanalysis_h +#define mozilla_testcontentanalysis_h + +#include + +#include "content_analysis/sdk/analysis_client.h" +#include "gtest/gtest.h" +#include "nsString.h" + +struct MozAgentInfo { + PROCESS_INFORMATION processInfo; + std::unique_ptr client; +}; + +void GeneratePipeName(const wchar_t* prefix, nsString& pipeName); +void LaunchAgentWithCommandLineArguments(const nsString& cmdLineArguments, + const nsString& pipeName, + MozAgentInfo& agentInfo); +#endif diff --git a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisMisbehaving.cpp b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisMisbehaving.cpp index 0b005e1f6c..7c944ed6e3 100644 --- a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisMisbehaving.cpp +++ b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisMisbehaving.cpp @@ -8,7 +8,7 @@ #include "mozilla/Assertions.h" #include "mozilla/CmdLineAndEnvUtils.h" #include "content_analysis/sdk/analysis_client.h" -#include "TestContentAnalysis.h" +#include "TestContentAnalysisAgent.h" #include #include #include diff --git a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisUtils.cpp b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisUtils.cpp index fc0cca5acd..0e14de6b81 100644 --- a/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisUtils.cpp +++ b/toolkit/components/contentanalysis/tests/gtest/TestContentAnalysisUtils.cpp @@ -3,7 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "TestContentAnalysis.h" +#include "TestContentAnalysisAgent.h" #include #include #include diff --git a/toolkit/components/contentanalysis/tests/gtest/moz.build b/toolkit/components/contentanalysis/tests/gtest/moz.build index ce701987a4..549e1e0e39 100644 --- a/toolkit/components/contentanalysis/tests/gtest/moz.build +++ b/toolkit/components/contentanalysis/tests/gtest/moz.build @@ -12,6 +12,10 @@ LOCAL_INCLUDES += [ if CONFIG["OS_TARGET"] == "WINNT": UNIFIED_SOURCES += [ "TestContentAnalysis.cpp", + ] + SOURCES += [ + # Agent SDK usings conflicts with Gecko usings + "TestContentAnalysisAgent.cpp", "TestContentAnalysisMisbehaving.cpp", "TestContentAnalysisUtils.cpp", ] diff --git a/toolkit/components/contentanalysis/tests/moz.build b/toolkit/components/contentanalysis/tests/moz.build new file mode 100644 index 0000000000..e8087ba5ed --- /dev/null +++ b/toolkit/components/contentanalysis/tests/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +TEST_DIRS += ["gtest", "browser"] -- cgit v1.2.3