blob: 1533a2066c8ced1a4aa6f6cc1649236ee4494e5e [file] [log] [blame]
Anunoy Ghoshc52cd7e02024-10-31 12:03:431// 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_store.h"
6
7#include "base/files/file_path.h"
8#include "base/files/scoped_temp_dir.h"
Alex Ilin93b6e302025-03-29 15:33:259#include "crypto/scoped_fake_unexportable_key_provider.h"
Anunoy Ghoshc52cd7e02024-10-31 12:03:4310#include "net/device_bound_sessions/unexportable_key_service_factory.h"
11#include "net/test/test_with_task_environment.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace net::device_bound_sessions {
15
16namespace {
17
18unexportable_keys::UnexportableKeyService* GetUnexportableKeyFactoryNull() {
19 return nullptr;
20}
21
22class ScopedNullUnexportableKeyFactory {
23 public:
24 ScopedNullUnexportableKeyFactory() {
25 UnexportableKeyServiceFactory::GetInstance()
26 ->SetUnexportableKeyFactoryForTesting(GetUnexportableKeyFactoryNull);
27 }
28 ScopedNullUnexportableKeyFactory(const ScopedNullUnexportableKeyFactory&) =
29 delete;
30 ScopedNullUnexportableKeyFactory(ScopedNullUnexportableKeyFactory&&) = delete;
31 ~ScopedNullUnexportableKeyFactory() {
32 UnexportableKeyServiceFactory::GetInstance()
33 ->SetUnexportableKeyFactoryForTesting(nullptr);
34 }
35};
36
37class SessionStoreTest : public TestWithTaskEnvironment {
38 protected:
39 SessionStoreTest()
40 : store_file_path_(base::FilePath(FILE_PATH_LITERAL("dummy_db_path"))) {}
41
42 base::FilePath store_file_path() { return store_file_path_; }
43
44 private:
45 base::FilePath store_file_path_;
46};
47
48TEST_F(SessionStoreTest, HasStore) {
Alex Ilin93b6e302025-03-29 15:33:2549 crypto::ScopedFakeUnexportableKeyProvider scoped_fake_key_provider_;
Anunoy Ghoshc52cd7e02024-10-31 12:03:4350 auto store = SessionStore::Create(store_file_path());
51 EXPECT_TRUE(store);
52}
53
54TEST_F(SessionStoreTest, NoStore) {
55 // Empty db path not allowed.
56 {
Alex Ilin93b6e302025-03-29 15:33:2557 crypto::ScopedFakeUnexportableKeyProvider scoped_fake_key_provider_;
Anunoy Ghoshc52cd7e02024-10-31 12:03:4358 auto store = SessionStore::Create(base::FilePath());
59 EXPECT_FALSE(store);
60 }
61 // Null key service not allowed.
62 {
63 ScopedNullUnexportableKeyFactory null_factory;
64 auto store = SessionStore::Create(store_file_path());
65 EXPECT_FALSE(store);
66 }
67}
68
69} // namespace
70
71} // namespace net::device_bound_sessions