// Copyright 2020 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 "device/fido/virtual_u2f_device.h" #include #include "base/callback.h" #include "base/memory/scoped_refptr.h" #include "base/test/bind_test_util.h" #include "base/test/task_environment.h" #include "device/fido/fido_parsing_utils.h" #include "device/fido/fido_test_data.h" #include "device/fido/test_callback_receiver.h" #include "testing/gtest/include/gtest/gtest.h" namespace device { namespace { using TestCallbackReceiver = test::ValueCallbackReceiver>>; void SendCommand(VirtualU2fDevice* device, base::span command) { device->DeviceTransact(fido_parsing_utils::Materialize(command), base::DoNothing()); } } // namespace class VirtualU2fDeviceTest : public ::testing::Test { protected: void MakeSelfDestructingDevice() { auto state = base::MakeRefCounted(); device_ = std::make_unique(state); state->simulate_press_callback = base::BindLambdaForTesting([&](VirtualFidoDevice* _) { device_.reset(); return true; }); } std::unique_ptr device_; base::test::TaskEnvironment task_environment_; }; // Tests that destroying the virtual device from the |simulate_press_callback| // does not crash. TEST_F(VirtualU2fDeviceTest, DestroyInsideSimulatePressCallback) { MakeSelfDestructingDevice(); SendCommand(device_.get(), test_data::kU2fRegisterCommandApdu); ASSERT_FALSE(device_); MakeSelfDestructingDevice(); SendCommand(device_.get(), test_data::kU2fSignCommandApduWithKeyAlpha); ASSERT_FALSE(device_); } } // namespace device