Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Tao Bao | 79a0e98 | 2017-04-25 09:31:58 -0700 | [diff] [blame] | 19 | #include <signal.h> |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | |
| 22 | #include <string> |
| 23 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 24 | #include <android-base/unique_fd.h> |
Tao Bao | 79a0e98 | 2017-04-25 09:31:58 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 27 | #include "adb_io.h" |
Tao Bao | 79a0e98 | 2017-04-25 09:31:58 -0700 | [diff] [blame] | 28 | #include "fuse_adb_provider.h" |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 29 | |
| 30 | TEST(fuse_adb_provider, read_block_adb) { |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 31 | android::base::unique_fd device_socket; |
| 32 | android::base::unique_fd host_socket; |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 33 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 34 | ASSERT_TRUE(android::base::Socketpair(AF_UNIX, SOCK_STREAM, 0, &device_socket, &host_socket)); |
| 35 | FuseAdbDataProvider data(std::move(device_socket), 0, 0); |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 36 | |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 37 | fcntl(host_socket, F_SETFL, O_NONBLOCK); |
| 38 | |
| 39 | const char expected_data[] = "foobar"; |
| 40 | char block_data[sizeof(expected_data)] = {}; |
| 41 | |
| 42 | // If we write the result of read_block_adb's request before the request is |
| 43 | // actually made we can avoid needing an extra thread for this test. |
| 44 | ASSERT_TRUE(WriteFdExactly(host_socket, expected_data, |
| 45 | strlen(expected_data))); |
| 46 | |
| 47 | uint32_t block = 1234U; |
| 48 | const char expected_block[] = "00001234"; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(data.ReadBlockAlignedData(reinterpret_cast<uint8_t*>(block_data), |
| 50 | sizeof(expected_data) - 1, block)); |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 51 | |
| 52 | // Check that read_block_adb requested the right block. |
| 53 | char block_req[sizeof(expected_block)] = {}; |
| 54 | ASSERT_TRUE(ReadFdExactly(host_socket, block_req, 8)); |
| 55 | ASSERT_EQ(0, block_req[8]); |
| 56 | ASSERT_EQ(8U, strlen(block_req)); |
| 57 | ASSERT_STREQ(expected_block, block_req); |
| 58 | |
| 59 | // Check that read_block_adb returned the right data. |
| 60 | ASSERT_EQ(0, block_req[8]); |
| 61 | ASSERT_STREQ(expected_data, block_data); |
| 62 | |
| 63 | // Check that nothing else was written to the socket. |
| 64 | char tmp; |
| 65 | errno = 0; |
| 66 | ASSERT_EQ(-1, read(host_socket, &tmp, 1)); |
| 67 | ASSERT_EQ(EWOULDBLOCK, errno); |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(fuse_adb_provider, read_block_adb_fail_write) { |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 71 | android::base::unique_fd device_socket; |
| 72 | android::base::unique_fd host_socket; |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 73 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 74 | ASSERT_TRUE(android::base::Socketpair(AF_UNIX, SOCK_STREAM, 0, &device_socket, &host_socket)); |
| 75 | FuseAdbDataProvider data(std::move(device_socket), 0, 0); |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 76 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 77 | host_socket.reset(); |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 78 | |
Tao Bao | 79a0e98 | 2017-04-25 09:31:58 -0700 | [diff] [blame] | 79 | // write(2) raises SIGPIPE since the reading end has been closed. Ignore the signal to avoid |
| 80 | // failing the test. |
| 81 | signal(SIGPIPE, SIG_IGN); |
| 82 | |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 83 | char buf[1]; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 84 | ASSERT_FALSE(data.ReadBlockAlignedData(reinterpret_cast<uint8_t*>(buf), 1, 0)); |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 85 | } |