blob: ecd9f384b2d8f48ab5bb221f0bbcff47d0e64fa9 [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
17#include "fuse_adb_provider.h"
18
19#include <gtest/gtest.h>
20
21#include <errno.h>
22#include <fcntl.h>
23#include <sys/socket.h>
24
25#include <string>
26
27#include "adb_io.h"
28#include "utils/file.h"
29
30TEST(fuse_adb_provider, read_block_adb) {
31 adb_data data = {};
32 int sockets[2];
33
34 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets));
35 data.sfd = sockets[0];
36
37 int host_socket = sockets[1];
38 fcntl(host_socket, F_SETFL, O_NONBLOCK);
39
40 const char expected_data[] = "foobar";
41 char block_data[sizeof(expected_data)] = {};
42
43 // If we write the result of read_block_adb's request before the request is
44 // actually made we can avoid needing an extra thread for this test.
45 ASSERT_TRUE(WriteFdExactly(host_socket, expected_data,
46 strlen(expected_data)));
47
48 uint32_t block = 1234U;
49 const char expected_block[] = "00001234";
50 ASSERT_EQ(0, read_block_adb(reinterpret_cast<void*>(&data), block,
51 reinterpret_cast<uint8_t*>(block_data),
52 sizeof(expected_data) - 1));
53
54 // Check that read_block_adb requested the right block.
55 char block_req[sizeof(expected_block)] = {};
56 ASSERT_TRUE(ReadFdExactly(host_socket, block_req, 8));
57 ASSERT_EQ(0, block_req[8]);
58 ASSERT_EQ(8U, strlen(block_req));
59 ASSERT_STREQ(expected_block, block_req);
60
61 // Check that read_block_adb returned the right data.
62 ASSERT_EQ(0, block_req[8]);
63 ASSERT_STREQ(expected_data, block_data);
64
65 // Check that nothing else was written to the socket.
66 char tmp;
67 errno = 0;
68 ASSERT_EQ(-1, read(host_socket, &tmp, 1));
69 ASSERT_EQ(EWOULDBLOCK, errno);
70
71 close(sockets[0]);
72 close(sockets[1]);
73}
74
75TEST(fuse_adb_provider, read_block_adb_fail_write) {
76 adb_data data = {};
77 int sockets[2];
78
79 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets));
80 data.sfd = sockets[0];
81
82 ASSERT_EQ(0, close(sockets[1]));
83
84 char buf[1];
85 ASSERT_EQ(-EIO, read_block_adb(reinterpret_cast<void*>(&data), 0,
86 reinterpret_cast<uint8_t*>(buf), 1));
87
88 close(sockets[0]);
89}