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 <memory>
20 #include <string>
21 #include <vector>
22
23 #include <android-base/file.h>
24 #include <android-base/strings.h>
25 #include <gtest/gtest.h>
26
27 #include "fuse_provider.h"
28 #include "fuse_sideload.h"
29
TEST(SideloadTest,fuse_device)30 TEST(SideloadTest, fuse_device) {
31 ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK));
32 }
33
34 class FuseTestDataProvider : public FuseDataProvider {
35 public:
FuseTestDataProvider(uint64_t file_size,uint32_t block_size)36 FuseTestDataProvider(uint64_t file_size, uint32_t block_size)
37 : FuseDataProvider(file_size, block_size) {}
38
39 private:
ReadBlockAlignedData(uint8_t *,uint32_t,uint32_t) const40 bool ReadBlockAlignedData(uint8_t*, uint32_t, uint32_t) const override {
41 return true;
42 }
43 };
44
TEST(SideloadTest,run_fuse_sideload_wrong_parameters)45 TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
46 auto provider_small_block = std::make_unique<FuseTestDataProvider>(4096, 4095);
47 ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_small_block)));
48
49 auto provider_large_block = std::make_unique<FuseTestDataProvider>(4096, (1 << 22) + 1);
50 ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_large_block)));
51
52 auto provider_too_many_blocks =
53 std::make_unique<FuseTestDataProvider>(((1 << 18) + 1) * 4096, 4096);
54 ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_too_many_blocks)));
55 }
56
TEST(SideloadTest,run_fuse_sideload)57 TEST(SideloadTest, run_fuse_sideload) {
58 const std::vector<std::string> blocks = {
59 std::string(2048, 'a') + std::string(2048, 'b'),
60 std::string(2048, 'c') + std::string(2048, 'd'),
61 std::string(2048, 'e') + std::string(2048, 'f'),
62 std::string(2048, 'g') + std::string(2048, 'h'),
63 };
64 const std::string content = android::base::Join(blocks, "");
65 ASSERT_EQ(16384U, content.size());
66
67 TemporaryFile temp_file;
68 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
69
70 auto provider = std::make_unique<FuseFileDataProvider>(temp_file.path, 4096);
71 ASSERT_TRUE(provider->Valid());
72 TemporaryDir mount_point;
73 pid_t pid = fork();
74 if (pid == 0) {
75 ASSERT_EQ(0, run_fuse_sideload(std::move(provider), mount_point.path));
76 _exit(EXIT_SUCCESS);
77 }
78
79 std::string package = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_FILENAME;
80 int status;
81 static constexpr int kSideloadInstallTimeout = 10;
82 for (int i = 0; i < kSideloadInstallTimeout; ++i) {
83 ASSERT_NE(-1, waitpid(pid, &status, WNOHANG));
84
85 struct stat sb;
86 if (stat(package.c_str(), &sb) == 0) {
87 break;
88 }
89
90 if (errno == ENOENT && i < kSideloadInstallTimeout - 1) {
91 sleep(1);
92 continue;
93 }
94 FAIL() << "Timed out waiting for the fuse-provided package.";
95 }
96
97 std::string content_via_fuse;
98 ASSERT_TRUE(android::base::ReadFileToString(package, &content_via_fuse));
99 ASSERT_EQ(content, content_via_fuse);
100
101 std::string exit_flag = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_EXIT_FLAG;
102 struct stat sb;
103 ASSERT_EQ(0, stat(exit_flag.c_str(), &sb));
104
105 waitpid(pid, &status, 0);
106 ASSERT_EQ(0, WEXITSTATUS(status));
107 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
108 }
109