Wei Wang | b72a15a | 2017-03-02 13:46:37 -0800 | [diff] [blame] | 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 | */ |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 16 | |
Wei Wang | b72a15a | 2017-03-02 13:46:37 -0800 | [diff] [blame] | 17 | #include <unistd.h> |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 18 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 19 | #include <memory> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <android-base/file.h> |
| 24 | #include <android-base/strings.h> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 25 | #include <android-base/unique_fd.h> |
Wei Wang | b72a15a | 2017-03-02 13:46:37 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 28 | #include "fuse_provider.h" |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 29 | #include "fuse_sideload.h" |
| 30 | |
| 31 | TEST(SideloadTest, fuse_device) { |
| 32 | ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK)); |
| 33 | } |
| 34 | |
| 35 | TEST(SideloadTest, run_fuse_sideload_wrong_parameters) { |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 36 | auto provider_small_block = |
| 37 | std::make_unique<FuseFileDataProvider>(android::base::unique_fd(), 4096, 4095); |
| 38 | ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_small_block))); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 39 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 40 | auto provider_large_block = |
| 41 | std::make_unique<FuseFileDataProvider>(android::base::unique_fd(), 4096, (1 << 22) + 1); |
| 42 | ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_large_block))); |
Tao Bao | ed13819 | 2017-05-01 22:30:39 -0700 | [diff] [blame] | 43 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 44 | auto provider_too_many_blocks = std::make_unique<FuseFileDataProvider>( |
| 45 | android::base::unique_fd(), ((1 << 18) + 1) * 4096, 4096); |
| 46 | ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_too_many_blocks))); |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(SideloadTest, run_fuse_sideload) { |
| 50 | const std::vector<std::string> blocks = { |
| 51 | std::string(2048, 'a') + std::string(2048, 'b'), |
| 52 | std::string(2048, 'c') + std::string(2048, 'd'), |
| 53 | std::string(2048, 'e') + std::string(2048, 'f'), |
| 54 | std::string(2048, 'g') + std::string(2048, 'h'), |
| 55 | }; |
| 56 | const std::string content = android::base::Join(blocks, ""); |
| 57 | ASSERT_EQ(16384U, content.size()); |
| 58 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 59 | TemporaryFile temp_file; |
| 60 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path)); |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 61 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 62 | auto provider = std::make_unique<FuseFileDataProvider>(temp_file.path, 4096); |
| 63 | ASSERT_TRUE(provider->Valid()); |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 64 | TemporaryDir mount_point; |
| 65 | pid_t pid = fork(); |
| 66 | if (pid == 0) { |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 67 | ASSERT_EQ(0, run_fuse_sideload(std::move(provider), mount_point.path)); |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 68 | _exit(EXIT_SUCCESS); |
| 69 | } |
| 70 | |
| 71 | std::string package = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_FILENAME; |
| 72 | int status; |
| 73 | static constexpr int kSideloadInstallTimeout = 10; |
| 74 | for (int i = 0; i < kSideloadInstallTimeout; ++i) { |
| 75 | ASSERT_NE(-1, waitpid(pid, &status, WNOHANG)); |
| 76 | |
| 77 | struct stat sb; |
| 78 | if (stat(package.c_str(), &sb) == 0) { |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | if (errno == ENOENT && i < kSideloadInstallTimeout - 1) { |
| 83 | sleep(1); |
| 84 | continue; |
| 85 | } |
| 86 | FAIL() << "Timed out waiting for the fuse-provided package."; |
| 87 | } |
| 88 | |
| 89 | std::string content_via_fuse; |
| 90 | ASSERT_TRUE(android::base::ReadFileToString(package, &content_via_fuse)); |
| 91 | ASSERT_EQ(content, content_via_fuse); |
| 92 | |
| 93 | std::string exit_flag = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_EXIT_FLAG; |
| 94 | struct stat sb; |
| 95 | ASSERT_EQ(0, stat(exit_flag.c_str(), &sb)); |
| 96 | |
| 97 | waitpid(pid, &status, 0); |
| 98 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 99 | ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); |
Wei Wang | b72a15a | 2017-03-02 13:46:37 -0800 | [diff] [blame] | 100 | } |