1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/webauthn/remote_webauthn_extension_notifier.h"
#include <memory>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
namespace {
void AssertExtensionIdWakeupFilesExist(const base::FilePath& path_prefix) {
for (const auto& id :
RemoteWebAuthnExtensionNotifier::GetRemoteWebAuthnExtensionIds()) {
ASSERT_TRUE(base::PathExists(path_prefix.Append(id)));
}
}
void AssertExtensionIdWakeupFilesNotExist(const base::FilePath& path_prefix) {
for (const auto& id :
RemoteWebAuthnExtensionNotifier::GetRemoteWebAuthnExtensionIds()) {
ASSERT_FALSE(base::PathExists(path_prefix.Append(id)));
}
}
void DeleteExtensionIdWakeupFiles(const base::FilePath& path_prefix) {
for (const auto& id :
RemoteWebAuthnExtensionNotifier::GetRemoteWebAuthnExtensionIds()) {
base::FilePath file_path = path_prefix.Append(id);
base::DeleteFile(file_path);
}
}
} // namespace
class RemoteWebAuthnExtensionNotifierTest : public testing::Test {
public:
RemoteWebAuthnExtensionNotifierTest();
~RemoteWebAuthnExtensionNotifierTest() override;
protected:
void WaitForNextIoTask();
base::ScopedTempDir scoped_temp_dir_1_;
base::ScopedTempDir scoped_temp_dir_2_;
std::unique_ptr<RemoteWebAuthnExtensionNotifier> notifier_;
private:
base::test::TaskEnvironment task_environment_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
};
RemoteWebAuthnExtensionNotifierTest::RemoteWebAuthnExtensionNotifierTest() {
EXPECT_FALSE(
RemoteWebAuthnExtensionNotifier::GetRemoteWebAuthnExtensionIds().empty());
EXPECT_TRUE(scoped_temp_dir_1_.CreateUniqueTempDir());
EXPECT_TRUE(scoped_temp_dir_2_.CreateUniqueTempDir());
io_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::WithBaseSyncPrimitives()});
notifier_ = base::WrapUnique(new RemoteWebAuthnExtensionNotifier(
{
scoped_temp_dir_1_.GetPath(),
scoped_temp_dir_2_.GetPath(),
},
io_task_runner_));
}
RemoteWebAuthnExtensionNotifierTest::~RemoteWebAuthnExtensionNotifierTest() {
notifier_.reset();
// Wait for the notifier core to be deleted on the IO sequence so that ASAN
// doesn't complain about leaked memory.
WaitForNextIoTask();
}
void RemoteWebAuthnExtensionNotifierTest::WaitForNextIoTask() {
base::RunLoop run_loop;
task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&]() {
io_task_runner_->PostTaskAndReply(FROM_HERE, base::DoNothing(),
run_loop.QuitClosure());
}));
run_loop.Run();
}
TEST_F(RemoteWebAuthnExtensionNotifierTest, WritesFileInBothDirectories) {
notifier_->NotifyStateChange();
WaitForNextIoTask();
AssertExtensionIdWakeupFilesExist(scoped_temp_dir_1_.GetPath());
AssertExtensionIdWakeupFilesExist(scoped_temp_dir_2_.GetPath());
}
TEST_F(RemoteWebAuthnExtensionNotifierTest,
WritesFilesOnlyInExistingDirectory) {
base::FilePath deleted_temp_dir_1_path = scoped_temp_dir_1_.GetPath();
ASSERT_TRUE(scoped_temp_dir_1_.Delete());
notifier_->NotifyStateChange();
WaitForNextIoTask();
AssertExtensionIdWakeupFilesNotExist(deleted_temp_dir_1_path);
AssertExtensionIdWakeupFilesExist(scoped_temp_dir_2_.GetPath());
}
TEST_F(RemoteWebAuthnExtensionNotifierTest, FilesRewrittenAfterSecondCall) {
base::FilePath wakeup_file_prefix = scoped_temp_dir_1_.GetPath();
notifier_->NotifyStateChange();
WaitForNextIoTask();
AssertExtensionIdWakeupFilesExist(wakeup_file_prefix);
DeleteExtensionIdWakeupFiles(wakeup_file_prefix);
AssertExtensionIdWakeupFilesNotExist(wakeup_file_prefix);
notifier_->NotifyStateChange();
WaitForNextIoTask();
AssertExtensionIdWakeupFilesExist(wakeup_file_prefix);
}
TEST_F(RemoteWebAuthnExtensionNotifierTest,
FileWrittenWhenNotifierIsDeletedRightAfterNotifyStateChangeIsCalled) {
notifier_->NotifyStateChange();
notifier_.reset();
WaitForNextIoTask();
AssertExtensionIdWakeupFilesExist(scoped_temp_dir_1_.GetPath());
}
TEST_F(RemoteWebAuthnExtensionNotifierTest,
OnlyOneExtensionWakeupTaskIsScheduledForMultipleCalls) {
base::FilePath wakeup_file_prefix = scoped_temp_dir_1_.GetPath();
notifier_->NotifyStateChange();
notifier_->NotifyStateChange();
notifier_->NotifyStateChange();
WaitForNextIoTask();
AssertExtensionIdWakeupFilesExist(wakeup_file_prefix);
DeleteExtensionIdWakeupFiles(wakeup_file_prefix);
AssertExtensionIdWakeupFilesNotExist(wakeup_file_prefix);
WaitForNextIoTask();
WaitForNextIoTask();
for (const auto& id :
RemoteWebAuthnExtensionNotifier::GetRemoteWebAuthnExtensionIds()) {
ASSERT_FALSE(base::PathExists(wakeup_file_prefix.Append(id)));
}
}
} // namespace remoting
|