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" 9 #include "crypto/scoped_mock_unexportable_key_provider.h" 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 GetUnexportableKeyFactoryNull()18unexportable_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 SessionStoreTest : public TestWithTaskEnvironment { 38 protected: SessionStoreTest()39 SessionStoreTest() 40 : store_file_path_(base::FilePath(FILE_PATH_LITERAL("dummy_db_path"))) {} 41 store_file_path()42 base::FilePath store_file_path() { return store_file_path_; } 43 44 private: 45 base::FilePath store_file_path_; 46 }; 47 TEST_F(SessionStoreTest,HasStore)48TEST_F(SessionStoreTest, HasStore) { 49 crypto::ScopedMockUnexportableKeyProvider scoped_mock_key_provider_; 50 auto store = SessionStore::Create(store_file_path()); 51 EXPECT_TRUE(store); 52 } 53 TEST_F(SessionStoreTest,NoStore)54TEST_F(SessionStoreTest, NoStore) { 55 // Empty db path not allowed. 56 { 57 crypto::ScopedMockUnexportableKeyProvider scoped_mock_key_provider_; 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 72