blob: ea895038c20851d9b73d44443a60b2727711dc45 [file] [log] [blame]
Wei Wangb72a15a2017-03-02 13:46:37 -08001/*
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 */
Tao Baoed138192017-05-01 22:30:39 -070016
Wei Wangb72a15a2017-03-02 13:46:37 -080017#include <unistd.h>
Tao Baoed138192017-05-01 22:30:39 -070018
xunchang5e6832a2019-03-15 16:04:32 -070019#include <memory>
Tao Bao91a7aa42017-05-01 15:57:38 -070020#include <string>
21#include <vector>
22
23#include <android-base/file.h>
24#include <android-base/strings.h>
Wei Wangb72a15a2017-03-02 13:46:37 -080025#include <gtest/gtest.h>
26
xunchang5e6832a2019-03-15 16:04:32 -070027#include "fuse_provider.h"
Tao Baoed138192017-05-01 22:30:39 -070028#include "fuse_sideload.h"
29
30TEST(SideloadTest, fuse_device) {
31 ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK));
32}
33
Tao Bao2be97372019-04-15 12:45:50 -070034class FuseTestDataProvider : public FuseDataProvider {
35 public:
36 FuseTestDataProvider(uint64_t file_size, uint32_t block_size)
37 : FuseDataProvider(file_size, block_size) {}
38
39 private:
40 bool ReadBlockAlignedData(uint8_t*, uint32_t, uint32_t) const override {
41 return true;
42 }
Tianjie Xuf6158eb2019-06-11 16:09:07 -070043
44 bool Valid() const override {
45 return true;
46 }
Tao Bao2be97372019-04-15 12:45:50 -070047};
48
Tao Baoed138192017-05-01 22:30:39 -070049TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
Tao Bao2be97372019-04-15 12:45:50 -070050 auto provider_small_block = std::make_unique<FuseTestDataProvider>(4096, 4095);
xunchang5e6832a2019-03-15 16:04:32 -070051 ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_small_block)));
Tao Baoed138192017-05-01 22:30:39 -070052
Tao Bao2be97372019-04-15 12:45:50 -070053 auto provider_large_block = std::make_unique<FuseTestDataProvider>(4096, (1 << 22) + 1);
xunchang5e6832a2019-03-15 16:04:32 -070054 ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_large_block)));
Tao Baoed138192017-05-01 22:30:39 -070055
Tao Bao2be97372019-04-15 12:45:50 -070056 auto provider_too_many_blocks =
57 std::make_unique<FuseTestDataProvider>(((1 << 18) + 1) * 4096, 4096);
xunchang5e6832a2019-03-15 16:04:32 -070058 ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_too_many_blocks)));
Tao Bao91a7aa42017-05-01 15:57:38 -070059}
60
61TEST(SideloadTest, run_fuse_sideload) {
62 const std::vector<std::string> blocks = {
63 std::string(2048, 'a') + std::string(2048, 'b'),
64 std::string(2048, 'c') + std::string(2048, 'd'),
65 std::string(2048, 'e') + std::string(2048, 'f'),
66 std::string(2048, 'g') + std::string(2048, 'h'),
67 };
68 const std::string content = android::base::Join(blocks, "");
69 ASSERT_EQ(16384U, content.size());
70
xunchang5e6832a2019-03-15 16:04:32 -070071 TemporaryFile temp_file;
72 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
Tao Bao91a7aa42017-05-01 15:57:38 -070073
xunchang5e6832a2019-03-15 16:04:32 -070074 auto provider = std::make_unique<FuseFileDataProvider>(temp_file.path, 4096);
75 ASSERT_TRUE(provider->Valid());
Tao Bao91a7aa42017-05-01 15:57:38 -070076 TemporaryDir mount_point;
77 pid_t pid = fork();
78 if (pid == 0) {
xunchang5e6832a2019-03-15 16:04:32 -070079 ASSERT_EQ(0, run_fuse_sideload(std::move(provider), mount_point.path));
Tao Bao91a7aa42017-05-01 15:57:38 -070080 _exit(EXIT_SUCCESS);
81 }
82
83 std::string package = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_FILENAME;
84 int status;
85 static constexpr int kSideloadInstallTimeout = 10;
86 for (int i = 0; i < kSideloadInstallTimeout; ++i) {
87 ASSERT_NE(-1, waitpid(pid, &status, WNOHANG));
88
89 struct stat sb;
90 if (stat(package.c_str(), &sb) == 0) {
91 break;
92 }
93
94 if (errno == ENOENT && i < kSideloadInstallTimeout - 1) {
95 sleep(1);
96 continue;
97 }
98 FAIL() << "Timed out waiting for the fuse-provided package.";
99 }
100
101 std::string content_via_fuse;
102 ASSERT_TRUE(android::base::ReadFileToString(package, &content_via_fuse));
103 ASSERT_EQ(content, content_via_fuse);
104
105 std::string exit_flag = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_EXIT_FLAG;
106 struct stat sb;
107 ASSERT_EQ(0, stat(exit_flag.c_str(), &sb));
108
109 waitpid(pid, &status, 0);
110 ASSERT_EQ(0, WEXITSTATUS(status));
111 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
Wei Wangb72a15a2017-03-02 13:46:37 -0800112}