File: embedded_policy_test_server_test_base.h

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 (98 lines) | stat: -rw-r--r-- 3,603 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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_POLICY_TEST_SUPPORT_EMBEDDED_POLICY_TEST_SERVER_TEST_BASE_H_
#define COMPONENTS_POLICY_TEST_SUPPORT_EMBEDDED_POLICY_TEST_SERVER_TEST_BASE_H_

#include <memory>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/task_environment.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/policy/test_support/embedded_policy_test_server.h"
#include "components/policy/test_support/remote_commands_state.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
struct ResourceRequest;
}  // namespace network

namespace policy {

class ClientStorage;
class PolicyStorage;
class RemoteCommandsState;

class EmbeddedPolicyTestServerTestBase : public testing::Test {
 public:
  EmbeddedPolicyTestServerTestBase();
  EmbeddedPolicyTestServerTestBase(const EmbeddedPolicyTestServerTestBase&) =
      delete;
  EmbeddedPolicyTestServerTestBase& operator=(
      const EmbeddedPolicyTestServerTestBase&) = delete;
  ~EmbeddedPolicyTestServerTestBase() override;

  void SetUp() override;

  // Helper functions to set request components.
  void SetURL(const GURL& url);
  void SetMethod(const std::string& method);
  void SetAppType(const std::string& app_type);
  void SetDeviceIdParam(const std::string& device_id);
  void SetProfileIdParam(const std::string& profile_id);
  void SetDeviceType(const std::string& device_type);
  void SetOAuthToken(const std::string& oauth_token);
  void SetRequestTypeParam(const std::string& request_type);
  void SetEnrollmentTokenHeader(const std::string& enrollment_token);
  void SetDeviceTokenHeader(const std::string& device_token);
  void SetGoogleLoginTokenHeader(const std::string& user_email);
  void SetPayload(const enterprise_management::DeviceManagementRequest&
                      device_management_request);

  // Makes a request to the test server and waits until a response is ready.
  void StartRequestAndWait();

  // Helper functions for accessing response data.
  int GetResponseCode() const;
  bool HasResponseBody() const;
  std::string GetResponseBody() const;
  enterprise_management::DeviceManagementResponse GetDeviceManagementResponse()
      const;

  EmbeddedPolicyTestServer* test_server() { return &test_server_; }

  ClientStorage* client_storage() { return test_server_.client_storage(); }

  PolicyStorage* policy_storage() { return test_server_.policy_storage(); }

  RemoteCommandsState* remote_commands_state() {
    return test_server_.remote_commands_state();
  }

 private:
  // Adds a query param to the |resource_request_|.
  void AddQueryParam(const std::string& key, const std::string& value);

  // Callback to be provided for the network request. Invokes |callback| when
  // done.
  void DownloadedToString(base::OnceClosure callback,
                          std::unique_ptr<std::string> response_body);

  base::test::TaskEnvironment task_environment_;
  EmbeddedPolicyTestServer test_server_;
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  std::unique_ptr<network::SimpleURLLoader> url_loader_;
  std::unique_ptr<network::ResourceRequest> resource_request_;
  std::string payload_;
  std::unique_ptr<std::string> response_body_;
  bool done_ = false;
};

}  // namespace policy

#endif  // COMPONENTS_POLICY_TEST_SUPPORT_EMBEDDED_POLICY_TEST_SERVER_TEST_BASE_H_