File: shared_storage_test_utils.h

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (124 lines) | stat: -rw-r--r-- 4,744 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_NETWORK_SHARED_STORAGE_SHARED_STORAGE_TEST_UTILS_H_
#define SERVICES_NETWORK_SHARED_STORAGE_SHARED_STORAGE_TEST_UTILS_H_

#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/mojom/shared_storage.mojom.h"

namespace network {

static constexpr char kSharedStoragePathPrefix[] = "/shared_storage";
static constexpr char kSharedStorageWritePathSuffix[] = "/write.html";
static constexpr char kSharedStorageTestPath[] = "/test";
static constexpr char kSharedStorageBypassPath[] = "/bypass";
static constexpr char kSharedStorageRedirectPath[] = "/redirect";
static constexpr char kSharedStorageResponseData[] =
    "shared storage response data";

std::string MakeSharedStorageTestPath();
std::string MakeSharedStorageBypassPath();
std::string MakeSharedStorageRedirectPrefix();

class SharedStorageRequestCount {
 public:
  static size_t Get();
  static size_t Increment();
  static void Reset();

 private:
  static size_t count_;
};

mojom::SharedStorageModifierMethodWithOptionsPtr MojomSetMethod(
    const std::u16string& key,
    const std::u16string& value,
    bool ignore_if_present,
    std::optional<std::string> with_lock = std::nullopt);

mojom::SharedStorageModifierMethodWithOptionsPtr MojomAppendMethod(
    const std::u16string& key,
    const std::u16string& value,
    std::optional<std::string> with_lock = std::nullopt);

mojom::SharedStorageModifierMethodWithOptionsPtr MojomDeleteMethod(
    const std::u16string& key,
    std::optional<std::string> with_lock = std::nullopt);

mojom::SharedStorageModifierMethodWithOptionsPtr MojomClearMethod(
    std::optional<std::string> with_lock = std::nullopt);

// Wraps `mojom::SharedStorageModifierMethodWithOptionsPtr` to use gmock
// matchers.
struct SharedStorageMethodWrapper {
  explicit SharedStorageMethodWrapper(
      mojom::SharedStorageModifierMethodWithOptionsPtr method_with_options);

  SharedStorageMethodWrapper(const SharedStorageMethodWrapper& other);
  SharedStorageMethodWrapper& operator=(
      const SharedStorageMethodWrapper& other);

  ~SharedStorageMethodWrapper();

  friend bool operator==(const SharedStorageMethodWrapper& a,
                         const SharedStorageMethodWrapper& b) = default;

  mojom::SharedStorageModifierMethodWithOptionsPtr method_with_options;
};

std::ostream& operator<<(std::ostream& os,
                         const SharedStorageMethodWrapper& wrapper);

class SharedStorageResponse : public net::test_server::BasicHttpResponse {
 public:
  explicit SharedStorageResponse(std::string shared_storage_write);
  SharedStorageResponse(net::HttpStatusCode code, std::string new_location);
  SharedStorageResponse(std::string shared_storage_write,
                        net::HttpStatusCode code,
                        std::string new_location);

  SharedStorageResponse(const SharedStorageResponse&) = delete;
  SharedStorageResponse& operator=(const SharedStorageResponse&) = delete;

  ~SharedStorageResponse() override;

  void SendResponse(
      base::WeakPtr<net::test_server::HttpResponseDelegate> delegate) override;

 private:
  std::optional<std::string> shared_storage_write_;
  net::HttpStatusCode code_ = net::HTTP_OK;
  std::optional<std::string> new_location_;
};

// Sends a response with the "Shared-Storage-Write" header, with value
// `shared_storage_write`, to any request whose path starts with the
// `kSharedStoragePathPrefix` prefix and which has the
// "Sec-Shared-Storage-Writable: ?1" request header or whose full path is
// `MakeSharedStorageBypassPath()`.
std::unique_ptr<net::test_server::HttpResponse>
HandleSharedStorageRequestSimple(std::string shared_storage_write,
                                 const net::test_server::HttpRequest& request);

// Sends a response with the "Shared-Storage-Write" header, with the next
// available value in `shared_storage_write_headers` as tracked by
// `SharedStorageRequestCount`, to any request whose path starts with the
// `kSharedStoragePathPrefix` prefix and ends with the
// `kSharedStorageWritePathSuffix` and which has the
// "Sec-Shared-Storage-Writable: ?1" request header, as long as
// `shared_storage_write_headers` has not yet been fully iterated through..
std::unique_ptr<net::test_server::HttpResponse>
HandleSharedStorageRequestMultiple(
    std::vector<std::string> shared_storage_write_headers,
    const net::test_server::HttpRequest& request);

}  // namespace network

#endif  // SERVICES_NETWORK_SHARED_STORAGE_SHARED_STORAGE_TEST_UTILS_H_