File: safe_browsing_api_handler_test_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,943 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/safe_browsing/android/safe_browsing_api_handler_test_util.h"

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/containers/heap_array.h"
#include "base/run_loop.h"
#include "components/safe_browsing/android/safe_browsing_api_handler_bridge.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/safe_browsing/android/native_j_unittests_jni_headers/SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_jni.h"

namespace safe_browsing::test {

void WithMockSafeBrowsingApiHandler::SetUp() {
  env_ = base::android::AttachCurrentThread();
  ASSERT_TRUE(env_);
  Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setUp(env_);
}

void WithMockSafeBrowsingApiHandler::TearDown() {
  SafeBrowsingApiHandlerBridge::GetInstance()
      .ResetSafeBrowsingApiAvailableForTesting();
  SafeBrowsingApiHandlerBridge::GetInstance().ResetSafetyNetIdForTesting();
  Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_tearDown(env_);
}

void WithMockSafeBrowsingApiHandler::SetSafetyNetApiInitializationState(
    SafetyNetApiInitializationState state) {
  Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setSafetyNetApiInitializationState(
      env_, static_cast<int>(state));
}

void WithMockSafeBrowsingApiHandler::SetVerifyAppsResult(
    VerifyAppsEnabledResult result) {
  Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setVerifyAppsResult(
      env_, static_cast<int>(result));
}

void WithMockSafeBrowsingApiHandler::SetSafetyNetIdResultEmpty() {
  Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setSafetyNetIdResultEmpty(
      env_);
}

void WithMockSafeBrowsingApiHandler::AddLocalAllowlistEntry(
    const GURL& url,
    bool is_download_allowlist,
    bool is_match) {
  base::android::ScopedJavaLocalRef<jstring> j_url =
      base::android::ConvertUTF8ToJavaString(env_, url.spec());

  if (is_download_allowlist) {
    Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setCsdDownloadAllowlistMatch(
        env_, j_url, is_match);
  } else {
    Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setCsdAllowlistMatch(
        env_, j_url, is_match);
  }
}

void WithMockSafeBrowsingApiHandler::AddSafeBrowsingResponse(
    const GURL& url,
    const SafeBrowsingApiLookupResult& returned_lookup_result,
    const SafeBrowsingJavaThreatType& returned_threat_type,
    const std::vector<SafeBrowsingJavaThreatAttribute>&
        returned_threat_attributes,
    const SafeBrowsingJavaResponseStatus& returned_response_status,
    const std::vector<SafeBrowsingJavaThreatType>& expected_threat_types,
    const SafeBrowsingJavaProtocol& expected_protocol) {
  base::android::ScopedJavaLocalRef<jstring> j_url =
      base::android::ConvertUTF8ToJavaString(env_, url.spec());
  auto int_threat_types =
      base::HeapArray<int>::WithSize(expected_threat_types.size());
  auto itr = int_threat_types.begin();
  for (auto expected_threat_type : expected_threat_types) {
    *itr++ = static_cast<int>(expected_threat_type);
  }
  auto int_threat_attributes =
      base::HeapArray<int>::WithSize(returned_threat_attributes.size());
  itr = int_threat_attributes.begin();
  for (auto returned_threat_attribute : returned_threat_attributes) {
    *itr++ = static_cast<int>(returned_threat_attribute);
  }
  Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_setSafeBrowsingApiHandlerResponse(
      env_, j_url, base::android::ToJavaIntArray(env_, int_threat_types),
      static_cast<int>(expected_protocol),
      static_cast<int>(returned_lookup_result),
      static_cast<int>(returned_threat_type),
      base::android::ToJavaIntArray(env_, int_threat_attributes),
      static_cast<int>(returned_response_status));
}

void WithMockSafeBrowsingApiHandler::RunHashDatabaseUrlCheck(
    const GURL& url,
    const SBThreatTypeSet& threat_types,
    SBThreatType expected_threat_type,
    SubresourceFilterMatch expected_subresource_filter_match) {
  base::RunLoop run_loop;
  auto callback = SafeBrowsingApiHandlerBridge::ResponseCallback(base::BindOnce(
      [](base::RunLoop* run_loop, SBThreatType expected_threat_type,
         SubresourceFilterMatch expected_subresource_filter_match,
         SBThreatType returned_threat_type,
         const ThreatMetadata& returned_metadata) {
        EXPECT_EQ(returned_threat_type, expected_threat_type);
        EXPECT_EQ(returned_metadata.subresource_filter_match,
                  expected_subresource_filter_match);
        run_loop->Quit();
      },
      &run_loop, expected_threat_type, expected_subresource_filter_match));
  SafeBrowsingApiHandlerBridge::GetInstance().StartHashDatabaseUrlCheck(
      std::move(callback), url, threat_types);
  run_loop.Run();
}

void WithMockSafeBrowsingApiHandler::RunHashRealTimeUrlCheck(
    const GURL& url,
    const SBThreatTypeSet& threat_types,
    SBThreatType expected_threat_type) {
  base::RunLoop run_loop;
  auto callback = SafeBrowsingApiHandlerBridge::ResponseCallback(base::BindOnce(
      [](base::RunLoop* run_loop, SBThreatType expected_threat_type,
         SBThreatType returned_threat_type,
         const ThreatMetadata& returned_metadata) {
        EXPECT_EQ(returned_threat_type, expected_threat_type);
        run_loop->Quit();
      },
      &run_loop, expected_threat_type));
  SafeBrowsingApiHandlerBridge::GetInstance().StartHashRealTimeUrlCheck(
      std::move(callback), url, threat_types);
  run_loop.Run();
  EXPECT_EQ(
      Java_SafeBrowsingApiHandlerBridgeNativeUnitTestHelper_getSafeBrowsingApiUrlCheckTimeObserverResult(
          env_),
      kExpectedSafeBrowsingCheckDeltaMicroseconds);
}

}  // namespace safe_browsing::test