chromium/components/policy/core/common/cloud/device_management_service_unittest.cc

// Copyright 2012 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/policy/core/common/cloud/device_management_service.h"

#include <memory>
#include <optional>
#include <ostream>
#include <utility>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/statistics_recorder.h"
#include "base/run_loop.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/dm_auth.h"
#include "components/policy/core/common/cloud/mock_device_management_service.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/base/proxy_chain.h"
#include "net/base/proxy_server.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

_;
DoAll;
Invoke;
Mock;

em;

namespace policy {

const char kServiceUrl[] =;

// Encoded empty response messages for testing the error code paths.
const char kResponseEmpty[] =;

#define PROTO_STRING(name)

// Some helper constants.
const char kOAuthToken[] =;
const char kDMToken[] =;
const char kClientID[] =;
const char kRobotAuthCode[] =;
const char kEnrollmentToken[] =;
const char kProfileID[] =;
const char kIdToken[] =;
#if BUILDFLAG(IS_IOS)
const char kOAuthAuthorizationHeaderPrefix[] = "OAuth ";
#endif

// Helper function which generates a DMServer response and populates the
// `error_detail` field.
std::string GenerateResponseWithErrorDetail(
    em::DeviceManagementErrorDetail error_detail) {}

// Unit tests for the device management policy service. The tests are run
// against a TestURLLoaderFactory that is used to short-circuit the request
// without calling into the actual network stack.
class DeviceManagementServiceTestBase : public testing::Test {};

struct FailedRequestParams {};

void PrintTo(const FailedRequestParams& params, std::ostream* os) {}

// A parameterized test case for erroneous response situations, they're mostly
// the same for all kinds of requests.
class DeviceManagementServiceFailedRequestTest
    : public DeviceManagementServiceTestBase,
      public testing::WithParamInterface<FailedRequestParams> {};

TEST_P(DeviceManagementServiceFailedRequestTest, RegisterRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest, CertBasedRegisterRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest, BrowserRegistrationRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest,
       PolicyAgentRegistrationRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest, OidcEnrollmentRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest, ApiAuthCodeFetchRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest, PolicyRequest) {}

TEST_P(DeviceManagementServiceFailedRequestTest, AutoEnrollmentRequest) {}

INSTANTIATE_TEST_SUITE_P();

// Simple query parameter parser for testing.
class QueryParams {};

class DeviceManagementServiceTest : public DeviceManagementServiceTestBase {};

TEST_F(DeviceManagementServiceTest, RegisterRequest) {}

TEST_F(DeviceManagementServiceTest, CriticalRequest) {}

TEST_F(DeviceManagementServiceTest, CertBasedRegisterRequest) {}

TEST_F(DeviceManagementServiceTest, BrowserRegistrationRequest) {}

TEST_F(DeviceManagementServiceTest, PolicyAgentRegistrationRequest) {}

TEST_F(DeviceManagementServiceTest, OidcEnrollmentRequest) {}

TEST_F(DeviceManagementServiceTest, ApiAuthCodeFetchRequest) {}

TEST_F(DeviceManagementServiceTest, RequestWithProfileId) {}

TEST_F(DeviceManagementServiceTest, CancelRegisterRequest) {}

TEST_F(DeviceManagementServiceTest, CancelCertBasedRegisterRequest) {}

TEST_F(DeviceManagementServiceTest, CancelBrowserRegistrationRequest) {}

TEST_F(DeviceManagementServiceTest, CancelOidcEnrollmentRequest) {}

TEST_F(DeviceManagementServiceTest, CancelApiAuthCodeFetch) {}

TEST_F(DeviceManagementServiceTest, CancelPolicyRequest) {}

TEST_F(DeviceManagementServiceTest, JobQueueing) {}

TEST_F(DeviceManagementServiceTest, CancelRequestAfterShutdown) {}

ACTION_P(ResetPointer, pointer) {}

TEST_F(DeviceManagementServiceTest, CancelDuringCallback) {}

TEST_F(DeviceManagementServiceTest, RetryOnProxyError) {}

TEST_F(DeviceManagementServiceTest, RetryOnBadResponseFromProxy) {}

TEST_F(DeviceManagementServiceTest, AcceptMimeTypeFromProxy) {}

TEST_F(DeviceManagementServiceTest, RetryOnNetworkChanges) {}

TEST_F(DeviceManagementServiceTest, PolicyFetchRetryImmediately) {}

TEST_F(DeviceManagementServiceTest, RetryLimit) {}

TEST_F(DeviceManagementServiceTest, CancelDuringRetry) {}

// Tests that authorization data is correctly added to the request.
class DeviceManagementRequestAuthTest : public DeviceManagementServiceTestBase {};

TEST_F(DeviceManagementRequestAuthTest, OnlyOAuthToken) {}

TEST_F(DeviceManagementRequestAuthTest, OnlyDMToken) {}

TEST_F(DeviceManagementRequestAuthTest, OnlyEnrollmentToken) {}

#if !BUILDFLAG(IS_IOS)
// Cannot test requests with an oauth token and another authorization token on
// iOS because they both use the "Authorization" header.

TEST_F(DeviceManagementRequestAuthTest, OAuthAndDMToken) {}

TEST_F(DeviceManagementRequestAuthTest, OAuthAndEnrollmentToken) {}

TEST_F(DeviceManagementRequestAuthTest, OidcAuthAndIdToken) {}
#endif

#if defined(GTEST_HAS_DEATH_TEST)
TEST_F(DeviceManagementRequestAuthTest, CannotUseOAuthTokenAsAuthData) {}
#endif  // GTEST_HAS_DEATH_TEST

class DeviceManagementServiceTestWithTimeManipulation
    : public DeviceManagementServiceTestBase {};

TEST_F(DeviceManagementServiceTestWithTimeManipulation,
       BrowserRegistrationRequestWithTimeout) {}

TEST_F(DeviceManagementServiceTestWithTimeManipulation,
       OidcEnrollmentRequestWithTimeout) {}

}  // namespace policy