Anunoy Ghosh | c52cd7e0 | 2024-10-31 12:03:43 | [diff] [blame] | 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_store.h" |
| 6 | |
| 7 | #include "base/files/file_path.h" |
| 8 | #include "base/files/scoped_temp_dir.h" |
Alex Ilin | 93b6e30 | 2025-03-29 15:33:25 | [diff] [blame] | 9 | #include "crypto/scoped_fake_unexportable_key_provider.h" |
Anunoy Ghosh | c52cd7e0 | 2024-10-31 12:03:43 | [diff] [blame] | 10 | #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 | |
| 14 | namespace net::device_bound_sessions { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | unexportable_keys::UnexportableKeyService* GetUnexportableKeyFactoryNull() { |
| 19 | return nullptr; |
| 20 | } |
| 21 | |
| 22 | class 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 | |
| 37 | class 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 | |
| 48 | TEST_F(SessionStoreTest, HasStore) { |
Alex Ilin | 93b6e30 | 2025-03-29 15:33:25 | [diff] [blame] | 49 | crypto::ScopedFakeUnexportableKeyProvider scoped_fake_key_provider_; |
Anunoy Ghosh | c52cd7e0 | 2024-10-31 12:03:43 | [diff] [blame] | 50 | auto store = SessionStore::Create(store_file_path()); |
| 51 | EXPECT_TRUE(store); |
| 52 | } |
| 53 | |
| 54 | TEST_F(SessionStoreTest, NoStore) { |
| 55 | // Empty db path not allowed. |
| 56 | { |
Alex Ilin | 93b6e30 | 2025-03-29 15:33:25 | [diff] [blame] | 57 | crypto::ScopedFakeUnexportableKeyProvider scoped_fake_key_provider_; |
Anunoy Ghosh | c52cd7e0 | 2024-10-31 12:03:43 | [diff] [blame] | 58 | 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 |