1 //===-- PlatformAndroidTest.cpp -------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "Plugins/Platform/Android/PlatformAndroid.h"
10 #include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
11 #include "TestingSupport/SubsystemRAII.h"
12 #include "TestingSupport/TestUtilities.h"
13 #include "lldb/Utility/Connection.h"
14 #include "gmock/gmock.h"
17 using namespace lldb_private
;
18 using namespace lldb_private::platform_android
;
19 using namespace testing
;
23 class MockSyncService
: public AdbClient::SyncService
{
25 MockSyncService() : SyncService(std::unique_ptr
<Connection
>()) {}
27 MOCK_METHOD2(PullFile
,
28 Status(const FileSpec
&remote_file
, const FileSpec
&local_file
));
29 MOCK_METHOD4(Stat
, Status(const FileSpec
&remote_file
, uint32_t &mode
,
30 uint32_t &size
, uint32_t &mtime
));
33 typedef std::unique_ptr
<AdbClient::SyncService
> SyncServiceUP
;
35 class MockAdbClient
: public AdbClient
{
37 explicit MockAdbClient() : AdbClient("mock") {}
39 MOCK_METHOD3(ShellToFile
,
40 Status(const char *command
, std::chrono::milliseconds timeout
,
41 const FileSpec
&output_file_spec
));
42 MOCK_METHOD1(GetSyncService
, SyncServiceUP(Status
&error
));
45 class PlatformAndroidTest
: public PlatformAndroid
, public ::testing::Test
{
47 PlatformAndroidTest() : PlatformAndroid(false) {
48 m_remote_platform_sp
= PlatformSP(new PlatformAndroidRemoteGDBServer());
51 MOCK_METHOD1(GetAdbClient
, AdbClientUP(Status
&error
));
52 MOCK_METHOD0(GetPropertyPackageName
, llvm::StringRef());
57 TEST_F(PlatformAndroidTest
, DownloadModuleSliceWithAdbClientError
) {
58 EXPECT_CALL(*this, GetAdbClient(_
))
60 .WillOnce(DoAll(WithArg
<0>([](auto &arg
) {
61 arg
= Status("Failed to create AdbClient");
63 Return(ByMove(AdbClientUP()))));
67 FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
72 TEST_F(PlatformAndroidTest
, DownloadModuleSliceWithNormalFile
) {
73 auto sync_service
= new MockSyncService();
74 EXPECT_CALL(*sync_service
, Stat(FileSpec("/system/lib64/libc.so"), _
, _
, _
))
76 .WillOnce(DoAll(SetArgReferee
<1>(1), Return(Status())));
77 EXPECT_CALL(*sync_service
, PullFile(FileSpec("/system/lib64/libc.so"), _
))
79 .WillOnce(Return(Status()));
81 auto adb_client
= new MockAdbClient();
82 EXPECT_CALL(*adb_client
, GetSyncService(_
))
84 .WillOnce(Return(ByMove(SyncServiceUP(sync_service
))));
86 EXPECT_CALL(*this, GetAdbClient(_
))
88 .WillOnce(Return(ByMove(AdbClientUP(adb_client
))));
91 DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
95 TEST_F(PlatformAndroidTest
, DownloadModuleSliceWithZipFile
) {
96 auto adb_client
= new MockAdbClient();
97 EXPECT_CALL(*adb_client
,
98 ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
99 "iflag=skip_bytes,count_bytes "
100 "skip=4096 count=3600 status=none"),
103 .WillOnce(Return(Status()));
105 EXPECT_CALL(*this, GetAdbClient(_
))
107 .WillOnce(Return(ByMove(AdbClientUP(adb_client
))));
111 FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
116 TEST_F(PlatformAndroidTest
, DownloadModuleSliceWithZipFileAndRunAs
) {
117 auto adb_client
= new MockAdbClient();
118 EXPECT_CALL(*adb_client
,
119 ShellToFile(StrEq("run-as 'com.example.test' "
120 "dd if='/system/app/Test/Test.apk' "
121 "iflag=skip_bytes,count_bytes "
122 "skip=4096 count=3600 status=none"),
125 .WillOnce(Return(Status()));
127 EXPECT_CALL(*this, GetPropertyPackageName())
129 .WillOnce(Return(llvm::StringRef("com.example.test")));
131 EXPECT_CALL(*this, GetAdbClient(_
))
133 .WillOnce(Return(ByMove(AdbClientUP(adb_client
))));
137 FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
142 TEST_F(PlatformAndroidTest
, GetFileWithNormalFile
) {
143 auto sync_service
= new MockSyncService();
144 EXPECT_CALL(*sync_service
, Stat(FileSpec("/data/local/tmp/test"), _
, _
, _
))
146 .WillOnce(DoAll(SetArgReferee
<1>(1), Return(Status())));
147 EXPECT_CALL(*sync_service
, PullFile(FileSpec("/data/local/tmp/test"), _
))
149 .WillOnce(Return(Status()));
151 auto adb_client
= new MockAdbClient();
152 EXPECT_CALL(*adb_client
, GetSyncService(_
))
154 .WillOnce(Return(ByMove(SyncServiceUP(sync_service
))));
156 EXPECT_CALL(*this, GetAdbClient(_
))
158 .WillOnce(Return(ByMove(AdbClientUP(adb_client
))));
160 EXPECT_TRUE(GetFile(FileSpec("/data/local/tmp/test"), FileSpec()).Success());
163 TEST_F(PlatformAndroidTest
, GetFileWithCatFallback
) {
164 auto sync_service
= new MockSyncService();
167 Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _
, _
, _
))
169 .WillOnce(DoAll(SetArgReferee
<1>(0), Return(Status())));
171 auto adb_client0
= new MockAdbClient();
172 EXPECT_CALL(*adb_client0
, GetSyncService(_
))
174 .WillOnce(Return(ByMove(SyncServiceUP(sync_service
))));
176 auto adb_client1
= new MockAdbClient();
179 ShellToFile(StrEq("cat '/data/data/com.example.app/lib-main/libtest.so'"),
182 .WillOnce(Return(Status()));
184 EXPECT_CALL(*this, GetAdbClient(_
))
186 .WillOnce(Return(ByMove(AdbClientUP(adb_client0
))))
187 .WillOnce(Return(ByMove(AdbClientUP(adb_client1
))));
190 GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
195 TEST_F(PlatformAndroidTest
, GetFileWithCatFallbackAndRunAs
) {
196 auto sync_service
= new MockSyncService();
199 Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _
, _
, _
))
201 .WillOnce(DoAll(SetArgReferee
<1>(0), Return(Status())));
203 auto adb_client0
= new MockAdbClient();
204 EXPECT_CALL(*adb_client0
, GetSyncService(_
))
206 .WillOnce(Return(ByMove(SyncServiceUP(sync_service
))));
208 auto adb_client1
= new MockAdbClient();
211 ShellToFile(StrEq("run-as 'com.example.app' "
212 "cat '/data/data/com.example.app/lib-main/libtest.so'"),
215 .WillOnce(Return(Status()));
217 EXPECT_CALL(*this, GetPropertyPackageName())
219 .WillOnce(Return(llvm::StringRef("com.example.app")));
221 EXPECT_CALL(*this, GetAdbClient(_
))
223 .WillOnce(Return(ByMove(AdbClientUP(adb_client0
))))
224 .WillOnce(Return(ByMove(AdbClientUP(adb_client1
))));
227 GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),