File: nsUrlClassifierTestUtils.cpp

package info (click to toggle)
firefox 145.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,528 kB
  • sloc: cpp: 7,594,999; javascript: 6,459,658; ansic: 3,752,909; python: 1,403,455; xml: 629,809; asm: 438,679; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (128 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download | duplicates (2)
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
/* 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/. */

#include "nsUrlClassifierTestUtils.h"

#include "chromium/safebrowsing_v5.pb.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
#include "Entries.h"
#include "mozilla/EndianUtils.h"
#include "nsString.h"

NS_IMPL_ISUPPORTS(nsUrlClassifierTestUtils, nsIUrlClassifierTestUtils)

using namespace mozilla;
using namespace mozilla::safebrowsing;

static mozilla::StaticRefPtr<nsUrlClassifierTestUtils> gUrlClassifierTestUtils;

// static
already_AddRefed<nsUrlClassifierTestUtils>
nsUrlClassifierTestUtils::GetXPCOMSingleton() {
  if (gUrlClassifierTestUtils) {
    return do_AddRef(gUrlClassifierTestUtils);
  }

  RefPtr<nsUrlClassifierTestUtils> utils = new nsUrlClassifierTestUtils();

  // Note: This is cleared in the nsUrlClassifierUtils destructor.
  gUrlClassifierTestUtils = utils.get();
  ClearOnShutdown(&gUrlClassifierTestUtils);
  return utils.forget();
}

NS_IMETHODIMP
nsUrlClassifierTestUtils::MakeUpdateResponseV5(const nsACString& aName,
                                               uint32_t aSingleHash,
                                               nsACString& aResponse) {
  if (NS_WARN_IF(aName.IsEmpty())) {
    return NS_ERROR_INVALID_ARG;
  }

  v5::BatchGetHashListsResponse response;

  v5::HashList* hashList = response.add_hash_lists();

  hashList->set_name("test-4b");
  hashList->set_partial_update(false);
  hashList->set_version("\x00\x00\x00\x01");

  v5::RiceDeltaEncoded32Bit* riceDelta =
      hashList->mutable_additions_four_bytes();

  uint32_t prefix = BigEndian::readUint32(&aSingleHash);

  riceDelta->set_first_value(prefix);
  riceDelta->set_rice_parameter(30);
  riceDelta->set_entries_count(0);

  std::string s;
  response.SerializeToString(&s);

  nsCString out(s.c_str(), s.size());

  aResponse = out;

  return NS_OK;
}

NS_IMETHODIMP
nsUrlClassifierTestUtils::MakeFindFullHashResponseV5(
    const nsACString& aFullHash, nsACString& aResponse) {
  if (NS_WARN_IF(aFullHash.IsEmpty())) {
    return NS_ERROR_INVALID_ARG;
  }

  v5::SearchHashesResponse response;

  nsAutoCString fullHashData(aFullHash);

  v5::FullHash* fullHash = response.add_full_hashes();

  fullHash->set_full_hash(fullHashData.get(), fullHashData.Length());

  v5::Duration* duration = response.mutable_cache_duration();
  duration->set_seconds(1800);
  duration->set_nanos(0);

  std::string s;
  response.SerializeToString(&s);

  nsCString out(s.c_str(), s.size());

  aResponse = out;

  return NS_OK;
}

NS_IMETHODIMP
nsUrlClassifierTestUtils::GenerateLookupHash(const nsACString& aFragment,
                                             uint32_t* aLookupHash) {
  if (NS_WARN_IF(aFragment.IsEmpty())) {
    return NS_ERROR_INVALID_ARG;
  }

  Completion lookupHash;
  lookupHash.FromPlaintext(aFragment);

  *aLookupHash = lookupHash.ToUint32();

  return NS_OK;
}

NS_IMETHODIMP
nsUrlClassifierTestUtils::GenerateFullHash(const nsACString& aFragment,
                                           nsACString& aFullHash) {
  if (NS_WARN_IF(aFragment.IsEmpty())) {
    return NS_ERROR_INVALID_ARG;
  }

  Completion lookupHash;
  lookupHash.FromPlaintext(aFragment);

  lookupHash.ToString(aFullHash);

  return NS_OK;
}