1 /*
2  *  Copyright 2022 The Android Open Source Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at:
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20 
21 #include <future>
22 
23 #include "common/init_flags.h"
24 #include "module.h"
25 #include "os/handler.h"
26 #include "os/system_properties.h"
27 #include "os/thread.h"
28 #include "shim/dumpsys.h"
29 #include "stack_manager.h"
30 #include "storage/storage_module.h"
31 
32 using namespace bluetooth;
33 using namespace testing;
34 
35 namespace {
36 
37 constexpr char kTrue[] = "1";
38 constexpr char kFalse[] = "0";
39 constexpr char kReadOnlyDebuggableProperty[] = "ro.debuggable";
40 
41 }  // namespace
42 
43 class MainShimDumpsysTest : public testing::Test {
44  public:
45  protected:
SetUp()46   void SetUp() override {
47     bluetooth::common::InitFlags::SetAllForTesting();
48 
49     ModuleList modules;
50     modules.add<shim::Dumpsys>();
51 
52     os::Thread* thread = new os::Thread("thread", os::Thread::Priority::NORMAL);
53     stack_manager_.StartUp(&modules, thread);
54   }
TearDown()55   void TearDown() override { stack_manager_.ShutDown(); }
56   StackManager stack_manager_;
57 
58   os::Thread* thread_{nullptr};
59   os::Handler* handler_{nullptr};
60 };
61 
TEST_F(MainShimDumpsysTest,dumpsys_developer)62 TEST_F(MainShimDumpsysTest, dumpsys_developer) {
63   ASSERT_TRUE(os::SetSystemProperty(kReadOnlyDebuggableProperty, kTrue));
64 
65   std::promise<void> promise;
66   auto future = promise.get_future();
67   stack_manager_.GetInstance<shim::Dumpsys>()->Dump(STDOUT_FILENO, nullptr,
68                                                     std::move(promise));
69   future.get();
70 }
71 
TEST_F(MainShimDumpsysTest,dumpsys_user)72 TEST_F(MainShimDumpsysTest, dumpsys_user) {
73   ASSERT_TRUE(os::SetSystemProperty(kReadOnlyDebuggableProperty, kFalse));
74 
75   std::promise<void> promise;
76   auto future = promise.get_future();
77   stack_manager_.GetInstance<shim::Dumpsys>()->Dump(STDOUT_FILENO, nullptr,
78                                                     std::move(promise));
79   future.get();
80 }
81