Skip to content

Commit

Permalink
Added test.
Browse files Browse the repository at this point in the history
  • Loading branch information
boocmp committed Nov 26, 2024
1 parent cb708f8 commit 030307f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
64 changes: 64 additions & 0 deletions browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/thread_test_helper.h"
#include "base/threading/thread_restrictions.h"
Expand Down Expand Up @@ -238,6 +239,32 @@ base::FilePath AdBlockServiceTest::MakeTestDataCopy(
return temp_path.Append(source_location.BaseName());
}

brave_shields::AdBlockResourceProvider*
AdBlockServiceTest::IntsallDefaultAdBlockResources(
const base::FilePath& component_path) {
brave_shields::AdBlockService* service =
g_brave_browser_process->ad_block_service();
auto* resource_provider =
static_cast<brave_shields::AdBlockDefaultResourceProvider*>(
service->resource_provider());
base::RunLoop run_loop;
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindLambdaForTesting([&component_path]() {
return component_updater::CreateComponentContentsAccessor(
true, component_path);
}),
base::BindLambdaForTesting(
[resource_provider, &run_loop](
scoped_refptr<brave_component_updater::ComponentContentsAccessor>
accessor) {
resource_provider->OnComponentReady(std::move(accessor));
run_loop.Quit();
}));
run_loop.Run();
return resource_provider;
}

void AdBlockServiceTest::UpdateAdBlockResources(const std::string& resources) {
auto component_path = MakeFileInTempDir("resources.json", resources);

Expand Down Expand Up @@ -1595,6 +1622,43 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Custom1pBlocking) {
EXPECT_EQ(profile()->GetPrefs()->GetUint64(kAdsBlocked), 2ULL);
}

#if BUILDFLAG(ENABLE_EXTENSIONS)
#define MAYBE_SafeResourcesComponentAccess SafeResourcesComponentAccess
#else
#define MAYBE_SafeResourcesComponentAccess DISABLED_SafeResourcesComponentAccess
#endif
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, MAYBE_SafeResourcesComponentAccess) {
const auto components_path =
GetTestDataDir().AppendASCII("adblock-components");
{
auto* resource_provider = IntsallDefaultAdBlockResources(
components_path.AppendASCII("resources_ok"));
base::RunLoop run_loop;
resource_provider->LoadResources(
base::BindLambdaForTesting([&run_loop](const std::string& resources) {
constexpr const char kExpectedResources[] =
"[{\"name\":\"brave-fix.js\",\"aliases\":[],\"kind\":{\"mime\":"
"\"application/javascript\"}]";
EXPECT_EQ(kExpectedResources, resources);
run_loop.Quit();
}));
run_loop.Run();
}

{
auto* resource_provider = IntsallDefaultAdBlockResources(
components_path.AppendASCII("resources_corrupted"));
base::RunLoop run_loop;
resource_provider->LoadResources(
base::BindLambdaForTesting([&run_loop](const std::string& resources) {
constexpr const char kExpectedResources[] = "[]";
EXPECT_EQ(kExpectedResources, resources);
run_loop.Quit();
}));
run_loop.Run();
}
}

// Load a page with a script which uses a redirect data URL.
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectRulesAreRespected) {
UpdateAdBlockResources(R"(
Expand Down
3 changes: 3 additions & 0 deletions browser/brave_shields/ad_block_service_browsertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class HostContentSettingsMap;

namespace brave_shields {
class AdBlockService;
class AdBlockResourceProvider;
class FilterListCatalogEntry;
} // namespace brave_shields

Expand All @@ -47,6 +48,8 @@ class AdBlockServiceTest : public PlatformBrowserTest {
void AddNewRules(const std::string& rules,
uint8_t permission_mask = 0,
bool first_party_protections = false);
brave_shields::AdBlockResourceProvider* IntsallDefaultAdBlockResources(
const base::FilePath& component_path);
void UpdateAdBlockResources(const std::string& resources);
void UpdateAdBlockInstanceWithRules(const std::string& rules);
void UpdateCustomAdBlockInstanceWithRules(const std::string& rules);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"description":"treehash per file","signed_content":{"payload":"eyJjb250ZW50X2hhc2hlcyI6W3siYmxvY2tfc2l6ZSI6NDA5NiwiZGlnZXN0Ijoic2hhMjU2IiwiZmlsZXMiOlt7InBhdGgiOiJyZXNvdXJjZXMuanNvbiIsInJvb3RfaGFzaCI6Ik9oM3pERVJjT3U0NWE2N21WQkFqeWcyNzEyUGxvek04UWtBeVNnZzVUMlkifV0sImZvcm1hdCI6InRyZWVoYXNoIiwiaGFzaF9ibG9ja19zaXplIjo0MDk2fV0sIml0ZW1faWQiOiJtZmRkaWJtYmxtYmNjcGFkZm5kZ2FraW9wbW1oZWJvcCIsIml0ZW1fdmVyc2lvbiI6IjEuMC4xMDMiLCJwcm90b2NvbF92ZXJzaW9uIjoxfQ","signatures":[{"protected":"eyJhbGciOiJSUzI1NiJ9","header":{"kid":"webstore"},"signature":"gxnzPORvPtzdFbXbaOD-MOa93ED7LFNA8bAswg7dCXBXe9CP5Loyjsj3ch1XxVSzCfyLQ-lai0LN7TlTjxTZocbXD7MRfwo8hR9Qn2iyTjc8QswSiWl_F0tuV7fg6b6oTiX7Cc7-EPoyngWAA9nX7_HqjfG8XspnoEWs_-lER-DPDCX7sbWdSZuZaPoEOgDaujILJgcYr264n0nZNzc213wVzKxwfQeZAPUZAnTofulO03QQquKuzjZURS8ZZdSW2vlc72duxylo3Xr40lERk60VGv35ZwYW1UZpt8PZH_XOame3vfyBrbrxylRySu7nAQ8GuyEuDP8ucIAHUpf5Hg"}]}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"malware.js","aliases":[],"kind":{"mime":"application/javascript"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"description":"treehash per file","signed_content":{"payload":"eyJjb250ZW50X2hhc2hlcyI6W3siYmxvY2tfc2l6ZSI6NDA5NiwiZGlnZXN0Ijoic2hhMjU2IiwiZmlsZXMiOlt7InBhdGgiOiJyZXNvdXJjZXMuanNvbiIsInJvb3RfaGFzaCI6Ik9oM3pERVJjT3U0NWE2N21WQkFqeWcyNzEyUGxvek04UWtBeVNnZzVUMlkifV0sImZvcm1hdCI6InRyZWVoYXNoIiwiaGFzaF9ibG9ja19zaXplIjo0MDk2fV0sIml0ZW1faWQiOiJtZmRkaWJtYmxtYmNjcGFkZm5kZ2FraW9wbW1oZWJvcCIsIml0ZW1fdmVyc2lvbiI6IjEuMC4xMDMiLCJwcm90b2NvbF92ZXJzaW9uIjoxfQ","signatures":[{"protected":"eyJhbGciOiJSUzI1NiJ9","header":{"kid":"webstore"},"signature":"gxnzPORvPtzdFbXbaOD-MOa93ED7LFNA8bAswg7dCXBXe9CP5Loyjsj3ch1XxVSzCfyLQ-lai0LN7TlTjxTZocbXD7MRfwo8hR9Qn2iyTjc8QswSiWl_F0tuV7fg6b6oTiX7Cc7-EPoyngWAA9nX7_HqjfG8XspnoEWs_-lER-DPDCX7sbWdSZuZaPoEOgDaujILJgcYr264n0nZNzc213wVzKxwfQeZAPUZAnTofulO03QQquKuzjZURS8ZZdSW2vlc72duxylo3Xr40lERk60VGv35ZwYW1UZpt8PZH_XOame3vfyBrbrxylRySu7nAQ8GuyEuDP8ucIAHUpf5Hg"}]}}]
1 change: 1 addition & 0 deletions test/data/adblock-components/resources_ok/resources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"brave-fix.js","aliases":[],"kind":{"mime":"application/javascript"}]

0 comments on commit 030307f

Please sign in to comment.