blob: 31be2a64e91a1375387e415b9efc4841a437e138 [file] [log] [blame]
Dan Albertffd6c312015-02-26 15:33:00 -08001/*
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 Albertffd6c312015-02-26 15:33:00 -080017#include <errno.h>
18#include <fcntl.h>
Tao Bao79a0e982017-04-25 09:31:58 -070019#include <signal.h>
Dan Albertffd6c312015-02-26 15:33:00 -080020#include <sys/socket.h>
21
22#include <string>
23
Tao Bao79a0e982017-04-25 09:31:58 -070024#include <gtest/gtest.h>
25
Dan Albertffd6c312015-02-26 15:33:00 -080026#include "adb_io.h"
Tao Bao79a0e982017-04-25 09:31:58 -070027#include "fuse_adb_provider.h"
Dan Albertffd6c312015-02-26 15:33:00 -080028
29TEST(fuse_adb_provider, read_block_adb) {
30 adb_data data = {};
31 int sockets[2];
32
33 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets));
34 data.sfd = sockets[0];
35
36 int host_socket = sockets[1];
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";
Tao Bao79a0e982017-04-25 09:31:58 -070049 ASSERT_EQ(0, read_block_adb(static_cast<void*>(&data), block,
50 reinterpret_cast<uint8_t*>(block_data), sizeof(expected_data) - 1));
Dan Albertffd6c312015-02-26 15:33:00 -080051
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);
68
69 close(sockets[0]);
70 close(sockets[1]);
71}
72
73TEST(fuse_adb_provider, read_block_adb_fail_write) {
74 adb_data data = {};
75 int sockets[2];
76
77 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets));
78 data.sfd = sockets[0];
79
80 ASSERT_EQ(0, close(sockets[1]));
81
Tao Bao79a0e982017-04-25 09:31:58 -070082 // write(2) raises SIGPIPE since the reading end has been closed. Ignore the signal to avoid
83 // failing the test.
84 signal(SIGPIPE, SIG_IGN);
85
Dan Albertffd6c312015-02-26 15:33:00 -080086 char buf[1];
Tao Bao79a0e982017-04-25 09:31:58 -070087 ASSERT_EQ(-EIO, read_block_adb(static_cast<void*>(&data), 0, reinterpret_cast<uint8_t*>(buf), 1));
Dan Albertffd6c312015-02-26 15:33:00 -080088
89 close(sockets[0]);
90}