forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OSCrypt] KeyStorageLinux supports provided TaskRunners
While we need to jump to a specific thread and back, OSCrypt remains a synchronous API, therefore we need to wait on the result. An exception for KeyStorageLinux is made to allow for blocking. We wrap the Init() and GetKey() methods, such that they are called on preferred TaskRunner for the targeted backend. OSCrypt itself has no expectation for which thread it's going to be called on. It might even already be the thread that the backend requires (e.g. UI). We avoid a deadlock by only posting the task if the TaskRunner is different than the current one. Bug: 782851 Change-Id: I48ae623ce04da23a5e79d0c1e52890df8a8ef79e Reviewed-on: https://chromium-review.googlesource.com/758854 Reviewed-by: Jochen Eisinger <jochen@chromium.org> Reviewed-by: Vaclav Brozek <vabr@chromium.org> Commit-Queue: Christos Froussios <cfroussios@chromium.org> Cr-Commit-Position: refs/heads/master@{#520898}
- Loading branch information
Showing
5 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/os_crypt/key_storage_linux.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/sequenced_task_runner.h" | ||
#include "base/test/test_simple_task_runner.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
// We use a fake to avoid calling a real backend. We'll make calls to it and | ||
// test that the wrapping methods post accordingly. | ||
class FakeKeyStorageLinux : public KeyStorageLinux { | ||
public: | ||
explicit FakeKeyStorageLinux(base::SequencedTaskRunner* task_runner) | ||
: task_runner_(task_runner) {} | ||
~FakeKeyStorageLinux() override = default; | ||
|
||
protected: | ||
bool Init() override { return true; } | ||
std::string GetKeyImpl() override { return std::string("1234"); } | ||
|
||
base::SequencedTaskRunner* GetTaskRunner() override { return task_runner_; } | ||
|
||
private: | ||
base::SequencedTaskRunner* task_runner_; | ||
DISALLOW_COPY_AND_ASSIGN(FakeKeyStorageLinux); | ||
}; | ||
|
||
class KeyStorageLinuxTest : public testing::Test { | ||
public: | ||
KeyStorageLinuxTest() = default; | ||
~KeyStorageLinuxTest() override = default; | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(KeyStorageLinuxTest); | ||
}; | ||
|
||
TEST_F(KeyStorageLinuxTest, SkipPostingToSameTaskRunner) { | ||
scoped_refptr<base::TestSimpleTaskRunner> task_runner( | ||
new base::TestSimpleTaskRunner()); | ||
FakeKeyStorageLinux key_storage(task_runner.get()); | ||
|
||
task_runner->PostTask(FROM_HERE, | ||
base::Bind(base::IgnoreResult(&KeyStorageLinux::GetKey), | ||
base::Unretained(&key_storage))); | ||
|
||
// This should not deadlock. | ||
task_runner->RunUntilIdle(); | ||
} | ||
|
||
TEST_F(KeyStorageLinuxTest, IgnoreTaskRunnerIfNull) { | ||
FakeKeyStorageLinux key_storage(nullptr); | ||
// This should not deadlock or crash. | ||
ASSERT_EQ(std::string("1234"), key_storage.GetKey()); | ||
} |