blob: b7109fcc2c8aa0dd0767f5a485ce4076f37f5978 [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>
24#include <android-base/test_utils.h>
Wei Wangb72a15a2017-03-02 13:46:37 -080025#include <gtest/gtest.h>
26
Tao Baoed138192017-05-01 22:30:39 -070027#include "fuse_sideload.h"
28
29TEST(SideloadTest, fuse_device) {
30 ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK));
31}
32
33TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
34 provider_vtab vtab;
Tao Bao91a7aa42017-05-01 15:57:38 -070035 vtab.close = [](void) {};
Tao Baoed138192017-05-01 22:30:39 -070036
Tao Bao91a7aa42017-05-01 15:57:38 -070037 ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, 4095));
38 ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, (1 << 22) + 1));
Tao Baoed138192017-05-01 22:30:39 -070039
40 // Too many blocks.
Tao Bao91a7aa42017-05-01 15:57:38 -070041 ASSERT_EQ(-1, run_fuse_sideload(vtab, ((1 << 18) + 1) * 4096, 4096));
42}
43
44TEST(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));
Wei Wangb72a15a2017-03-02 13:46:37 -080098}