blob: 8d290dbcd1edebfbec1d56f3e713585b361d9d10 [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
Tao Bao91a7aa42017-05-01 15:57:38 -070019#include <string>
20#include <vector>
21
22#include <android-base/file.h>
23#include <android-base/strings.h>
Wei Wangb72a15a2017-03-02 13:46:37 -080024#include <gtest/gtest.h>
25
Tao Baoed138192017-05-01 22:30:39 -070026#include "fuse_sideload.h"
27
28TEST(SideloadTest, fuse_device) {
29 ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK));
30}
31
32TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
33 provider_vtab vtab;
Tao Bao91a7aa42017-05-01 15:57:38 -070034 vtab.close = [](void) {};
Tao Baoed138192017-05-01 22:30:39 -070035
Tao Bao91a7aa42017-05-01 15:57:38 -070036 ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, 4095));
37 ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, (1 << 22) + 1));
Tao Baoed138192017-05-01 22:30:39 -070038
39 // Too many blocks.
Tao Bao91a7aa42017-05-01 15:57:38 -070040 ASSERT_EQ(-1, run_fuse_sideload(vtab, ((1 << 18) + 1) * 4096, 4096));
41}
42
43TEST(SideloadTest, run_fuse_sideload) {
44 const std::vector<std::string> blocks = {
45 std::string(2048, 'a') + std::string(2048, 'b'),
46 std::string(2048, 'c') + std::string(2048, 'd'),
47 std::string(2048, 'e') + std::string(2048, 'f'),
48 std::string(2048, 'g') + std::string(2048, 'h'),
49 };
50 const std::string content = android::base::Join(blocks, "");
51 ASSERT_EQ(16384U, content.size());
52
53 provider_vtab vtab;
54 vtab.close = [](void) {};
55 vtab.read_block = [&blocks](uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
xunchangea2912f2019-03-17 16:45:12 -070056 if (block >= 4) return false;
Tao Bao91a7aa42017-05-01 15:57:38 -070057 blocks[block].copy(reinterpret_cast<char*>(buffer), fetch_size);
xunchangea2912f2019-03-17 16:45:12 -070058 return true;
Tao Bao91a7aa42017-05-01 15:57:38 -070059 };
60
61 TemporaryDir mount_point;
62 pid_t pid = fork();
63 if (pid == 0) {
64 ASSERT_EQ(0, run_fuse_sideload(vtab, 16384, 4096, mount_point.path));
65 _exit(EXIT_SUCCESS);
66 }
67
68 std::string package = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_FILENAME;
69 int status;
70 static constexpr int kSideloadInstallTimeout = 10;
71 for (int i = 0; i < kSideloadInstallTimeout; ++i) {
72 ASSERT_NE(-1, waitpid(pid, &status, WNOHANG));
73
74 struct stat sb;
75 if (stat(package.c_str(), &sb) == 0) {
76 break;
77 }
78
79 if (errno == ENOENT && i < kSideloadInstallTimeout - 1) {
80 sleep(1);
81 continue;
82 }
83 FAIL() << "Timed out waiting for the fuse-provided package.";
84 }
85
86 std::string content_via_fuse;
87 ASSERT_TRUE(android::base::ReadFileToString(package, &content_via_fuse));
88 ASSERT_EQ(content, content_via_fuse);
89
90 std::string exit_flag = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_EXIT_FLAG;
91 struct stat sb;
92 ASSERT_EQ(0, stat(exit_flag.c_str(), &sb));
93
94 waitpid(pid, &status, 0);
95 ASSERT_EQ(0, WEXITSTATUS(status));
96 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
Wei Wangb72a15a2017-03-02 13:46:37 -080097}