1 /*
2 * Copyright (C) 2017 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 <unistd.h>
18
19 #include <string>
20 #include <vector>
21
22 #include <android-base/file.h>
23 #include <android-base/strings.h>
24 #include <android-base/test_utils.h>
25 #include <gtest/gtest.h>
26
27 #include "fuse_sideload.h"
28
TEST(SideloadTest,fuse_device)29 TEST(SideloadTest, fuse_device) {
30 ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK));
31 }
32
TEST(SideloadTest,run_fuse_sideload_wrong_parameters)33 TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
34 provider_vtab vtab;
35 vtab.close = [](void) {};
36
37 ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, 4095));
38 ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, (1 << 22) + 1));
39
40 // Too many blocks.
41 ASSERT_EQ(-1, run_fuse_sideload(vtab, ((1 << 18) + 1) * 4096, 4096));
42 }
43
TEST(SideloadTest,run_fuse_sideload)44 TEST(SideloadTest, run_fuse_sideload) {
45 const std::vector<std::string> blocks = {
46 std::string(2048, 'a') + std::string(2048, 'b'),
47 std::string(2048, 'c') + std::string(2048, 'd'),
48 std::string(2048, 'e') + std::string(2048, 'f'),
49 std::string(2048, 'g') + std::string(2048, 'h'),
50 };
51 const std::string content = android::base::Join(blocks, "");
52 ASSERT_EQ(16384U, content.size());
53
54 provider_vtab vtab;
55 vtab.close = [](void) {};
56 vtab.read_block = [&blocks](uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
57 if (block >= 4) return -1;
58 blocks[block].copy(reinterpret_cast<char*>(buffer), fetch_size);
59 return 0;
60 };
61
62 TemporaryDir mount_point;
63 pid_t pid = fork();
64 if (pid == 0) {
65 ASSERT_EQ(0, run_fuse_sideload(vtab, 16384, 4096, mount_point.path));
66 _exit(EXIT_SUCCESS);
67 }
68
69 std::string package = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_FILENAME;
70 int status;
71 static constexpr int kSideloadInstallTimeout = 10;
72 for (int i = 0; i < kSideloadInstallTimeout; ++i) {
73 ASSERT_NE(-1, waitpid(pid, &status, WNOHANG));
74
75 struct stat sb;
76 if (stat(package.c_str(), &sb) == 0) {
77 break;
78 }
79
80 if (errno == ENOENT && i < kSideloadInstallTimeout - 1) {
81 sleep(1);
82 continue;
83 }
84 FAIL() << "Timed out waiting for the fuse-provided package.";
85 }
86
87 std::string content_via_fuse;
88 ASSERT_TRUE(android::base::ReadFileToString(package, &content_via_fuse));
89 ASSERT_EQ(content, content_via_fuse);
90
91 std::string exit_flag = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_EXIT_FLAG;
92 struct stat sb;
93 ASSERT_EQ(0, stat(exit_flag.c_str(), &sb));
94
95 waitpid(pid, &status, 0);
96 ASSERT_EQ(0, WEXITSTATUS(status));
97 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
98 }
99