• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/device_bound_sessions/session_service.h"
6 
7 #include "crypto/scoped_mock_unexportable_key_provider.h"
8 #include "net/device_bound_sessions/unexportable_key_service_factory.h"
9 #include "net/test/test_with_task_environment.h"
10 #include "net/url_request/url_request_context_builder.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace net::device_bound_sessions {
15 
16 namespace {
17 
GetUnexportableKeyFactoryNull()18 unexportable_keys::UnexportableKeyService* GetUnexportableKeyFactoryNull() {
19   return nullptr;
20 }
21 
22 class ScopedNullUnexportableKeyFactory {
23  public:
ScopedNullUnexportableKeyFactory()24   ScopedNullUnexportableKeyFactory() {
25     UnexportableKeyServiceFactory::GetInstance()
26         ->SetUnexportableKeyFactoryForTesting(GetUnexportableKeyFactoryNull);
27   }
28   ScopedNullUnexportableKeyFactory(const ScopedNullUnexportableKeyFactory&) =
29       delete;
30   ScopedNullUnexportableKeyFactory(ScopedNullUnexportableKeyFactory&&) = delete;
~ScopedNullUnexportableKeyFactory()31   ~ScopedNullUnexportableKeyFactory() {
32     UnexportableKeyServiceFactory::GetInstance()
33         ->SetUnexportableKeyFactoryForTesting(nullptr);
34   }
35 };
36 
37 class SessionServiceTest : public TestWithTaskEnvironment {
38  protected:
SessionServiceTest()39   SessionServiceTest()
40       : context_(CreateTestURLRequestContextBuilder()->Build()) {}
41 
42   std::unique_ptr<URLRequestContext> context_;
43 };
44 
TEST_F(SessionServiceTest,HasService)45 TEST_F(SessionServiceTest, HasService) {
46   crypto::ScopedMockUnexportableKeyProvider scoped_mock_key_provider_;
47   auto service = SessionService::Create(context_.get());
48   EXPECT_TRUE(service);
49 }
50 
TEST_F(SessionServiceTest,NoService)51 TEST_F(SessionServiceTest, NoService) {
52   ScopedNullUnexportableKeyFactory null_factory;
53   auto service = SessionService::Create(context_.get());
54   EXPECT_FALSE(service);
55 }
56 }  // namespace
57 
58 }  // namespace net::device_bound_sessions
59