aboutsummaryrefslogtreecommitdiff
path: root/lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
blob: 514bce1c715763505b242c8b6a26fbcdd90f52ff (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//===-- PlatformAndroidTest.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Plugins/Platform/Android/PlatformAndroid.h"
#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
#include "lldb/Utility/Connection.h"
#include "gmock/gmock.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::platform_android;
using namespace testing;

namespace {

class MockAdbClient : public AdbClient {
public:
  explicit MockAdbClient() : AdbClient() {}

  MOCK_METHOD3(ShellToFile,
               Status(const char *command, std::chrono::milliseconds timeout,
                      const FileSpec &output_file_spec));
};

class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
public:
  PlatformAndroidTest() : PlatformAndroid(false) {
    m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());

    // Set up default mock behavior to avoid uninteresting call warnings
    ON_CALL(*this, GetSyncService(_))
        .WillByDefault([](Status &error) -> std::unique_ptr<AdbSyncService> {
          error = Status::FromErrorString("Sync service unavailable");
          return nullptr;
        });
  }

  MOCK_METHOD1(GetAdbClient, AdbClientUP(Status &error));
  MOCK_METHOD0(GetPropertyPackageName, llvm::StringRef());
  MOCK_METHOD1(GetSyncService, std::unique_ptr<AdbSyncService>(Status &error));

  // Make GetSyncService public for testing
  using PlatformAndroid::GetSyncService;
};

} // namespace

TEST_F(PlatformAndroidTest,
       DownloadModuleSlice_AdbClientError_FailsGracefully) {
  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(DoAll(WithArg<0>([](auto &arg) {
                        arg = Status::FromErrorString(
                            "Failed to create AdbClient");
                      }),
                      Return(ByMove(AdbClientUP()))));

  Status result = DownloadModuleSlice(
      FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
      3600, FileSpec("/tmp/libtest.so"));

  EXPECT_TRUE(result.Fail());
  EXPECT_THAT(result.AsCString(), HasSubstr("Failed to create AdbClient"));
}

TEST_F(PlatformAndroidTest, DownloadModuleSlice_ZipFile_UsesCorrectDdCommand) {
  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(*adb_client,
              ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
                                "iflag=skip_bytes,count_bytes "
                                "skip=4096 count=3600 status=none"),
                          _, _))
      .WillOnce(Return(Status()));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(Return(ByMove(AdbClientUP(adb_client))));

  Status result = DownloadModuleSlice(
      FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
      3600, FileSpec("/tmp/libtest.so"));

  EXPECT_TRUE(result.Success());
}

TEST_F(PlatformAndroidTest,
       DownloadModuleSlice_ZipFileWithRunAs_UsesRunAsCommand) {
  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(*adb_client,
              ShellToFile(StrEq("run-as 'com.example.test' "
                                "dd if='/system/app/Test/Test.apk' "
                                "iflag=skip_bytes,count_bytes "
                                "skip=4096 count=3600 status=none"),
                          _, _))
      .WillOnce(Return(Status()));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("com.example.test")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(Return(ByMove(AdbClientUP(adb_client))));

  Status result = DownloadModuleSlice(
      FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
      3600, FileSpec("/tmp/libtest.so"));

  EXPECT_TRUE(result.Success());
}

TEST_F(PlatformAndroidTest,
       DownloadModuleSlice_LargeFile_CalculatesParametersCorrectly) {
  const uint64_t large_offset = 100 * 1024 * 1024; // 100MB offset
  const uint64_t large_size = 50 * 1024 * 1024;    // 50MB size

  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(*adb_client,
              ShellToFile(StrEq("dd if='/system/app/Large.apk' "
                                "iflag=skip_bytes,count_bytes "
                                "skip=104857600 count=52428800 status=none"),
                          _, _))
      .WillOnce(Return(Status()));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(Return(ByMove(AdbClientUP(adb_client))));

  Status result = DownloadModuleSlice(
      FileSpec("/system/app/Large.apk!/lib/arm64-v8a/large.so"), large_offset,
      large_size, FileSpec("/tmp/large.so"));

  EXPECT_TRUE(result.Success());
}

TEST_F(PlatformAndroidTest,
       GetFile_SyncServiceUnavailable_FallsBackToShellCat) {
  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(*adb_client,
              ShellToFile(StrEq("cat '/data/local/tmp/test'"), _, _))
      .WillOnce(Return(Status()));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(DoAll(WithArg<0>([](auto &arg) { arg.Clear(); }),
                      Return(ByMove(AdbClientUP(adb_client)))));

  EXPECT_CALL(*this, GetSyncService(_))
      .WillOnce([](Status &error) -> std::unique_ptr<AdbSyncService> {
        error = Status::FromErrorString("Sync service unavailable");
        return nullptr;
      });

  Status result =
      GetFile(FileSpec("/data/local/tmp/test"), FileSpec("/tmp/test"));
  EXPECT_TRUE(result.Success());
}

TEST_F(PlatformAndroidTest, GetFile_WithRunAs_UsesRunAsInShellCommand) {
  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(
      *adb_client,
      ShellToFile(StrEq("run-as 'com.example.app' "
                        "cat '/data/data/com.example.app/lib-main/libtest.so'"),
                  _, _))
      .WillOnce(Return(Status()));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("com.example.app")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(DoAll(WithArg<0>([](auto &arg) { arg.Clear(); }),
                      Return(ByMove(AdbClientUP(adb_client)))));

  EXPECT_CALL(*this, GetSyncService(_))
      .WillOnce([](Status &error) -> std::unique_ptr<AdbSyncService> {
        error = Status::FromErrorString("Sync service unavailable");
        return nullptr;
      });

  Status result =
      GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
              FileSpec("/tmp/libtest.so"));
  EXPECT_TRUE(result.Success());
}

TEST_F(PlatformAndroidTest, GetFile_FilenameWithSingleQuotes_Rejected) {
  EXPECT_CALL(*this, GetSyncService(_))
      .WillOnce([](Status &error) -> std::unique_ptr<AdbSyncService> {
        error = Status::FromErrorString("Sync service unavailable");
        return nullptr;
      });

  Status result =
      GetFile(FileSpec("/test/file'with'quotes"), FileSpec("/tmp/output"));

  EXPECT_TRUE(result.Fail());
  EXPECT_THAT(result.AsCString(), HasSubstr("single-quotes"));
}

TEST_F(PlatformAndroidTest,
       DownloadModuleSlice_FilenameWithSingleQuotes_Rejected) {
  Status result = DownloadModuleSlice(FileSpec("/test/file'with'quotes"), 100,
                                      200, FileSpec("/tmp/output"));

  EXPECT_TRUE(result.Fail());
  EXPECT_THAT(result.AsCString(), HasSubstr("single-quotes"));
}

TEST_F(PlatformAndroidTest, GetFile_NetworkTimeout_PropagatesErrorCorrectly) {
  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(*adb_client, ShellToFile(_, _, _))
      .WillOnce(Return(Status::FromErrorString("Network timeout")));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(DoAll(WithArg<0>([](auto &arg) { arg.Clear(); }),
                      Return(ByMove(AdbClientUP(adb_client)))));

  EXPECT_CALL(*this, GetSyncService(_))
      .WillOnce([](Status &error) -> std::unique_ptr<AdbSyncService> {
        error = Status::FromErrorString("Sync service unavailable");
        return nullptr;
      });

  Status result =
      GetFile(FileSpec("/data/large/file.so"), FileSpec("/tmp/large.so"));
  EXPECT_TRUE(result.Fail());
  EXPECT_THAT(result.AsCString(), HasSubstr("Network timeout"));
}

TEST_F(PlatformAndroidTest, SyncService_ConnectionFailsGracefully) {
  // Constructor should succeed even with a failing connection
  AdbSyncService sync_service("test-device");

  // The service should report as not connected initially
  EXPECT_FALSE(sync_service.IsConnected());
  EXPECT_EQ(sync_service.GetDeviceId(), "test-device");

  // Operations should fail gracefully when connection setup fails
  FileSpec remote_file("/data/test.txt");
  FileSpec local_file("/tmp/test.txt");
  uint32_t mode, size, mtime;

  Status result = sync_service.Stat(remote_file, mode, size, mtime);
  EXPECT_TRUE(result.Fail());
}

TEST_F(PlatformAndroidTest, GetRunAs_FormatsPackageNameCorrectly) {
  // Empty package name
  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("")));
  EXPECT_EQ(this->GetRunAs(), "");

  // Valid package name
  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("com.example.test")));
  EXPECT_EQ(this->GetRunAs(), "run-as 'com.example.test' ");
}

TEST_F(PlatformAndroidTest,
       DownloadModuleSlice_ZeroOffset_CallsGetFileInsteadOfDd) {
  // When offset=0, DownloadModuleSlice calls GetFile which uses 'cat', not 'dd'
  // We need to ensure the sync service fails so GetFile falls back to shell cat
  auto *adb_client = new MockAdbClient();
  EXPECT_CALL(*adb_client,
              ShellToFile(StrEq("cat '/system/lib64/libc.so'"), _, _))
      .WillOnce(Return(Status()));

  EXPECT_CALL(*this, GetPropertyPackageName())
      .WillOnce(Return(llvm::StringRef("")));

  EXPECT_CALL(*this, GetAdbClient(_))
      .WillOnce(DoAll(WithArg<0>([](auto &arg) { arg.Clear(); }),
                      Return(ByMove(AdbClientUP(adb_client)))));

  // Mock GetSyncService to fail, forcing GetFile to use shell cat fallback
  EXPECT_CALL(*this, GetSyncService(_))
      .WillOnce(DoAll(WithArg<0>([](auto &arg) {
                        arg =
                            Status::FromErrorString("Sync service unavailable");
                      }),
                      Return(ByMove(std::unique_ptr<AdbSyncService>()))));

  Status result = DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0,
                                      FileSpec("/tmp/libc.so"));
  EXPECT_TRUE(result.Success());
}