diff options
author | Dan Albert <danalbert@google.com> | 2015-02-27 16:37:40 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-02-27 16:37:40 +0000 |
commit | 3deba524f9d1e94b613d9c13f4c76595f0ec99ab (patch) | |
tree | e63321cd9a8715a3500e1fe7c70ee40db7fdc18e | |
parent | 735a0f0fc3de936db3ce5a99dbcf02e970d47ba0 (diff) | |
parent | ecead276f068cb53bddb2458b200aa96473ddfb3 (diff) | |
download | bootable_recovery-3deba524f9d1e94b613d9c13f4c76595f0ec99ab.zip bootable_recovery-3deba524f9d1e94b613d9c13f4c76595f0ec99ab.tar.gz bootable_recovery-3deba524f9d1e94b613d9c13f4c76595f0ec99ab.tar.bz2 |
am ecead276: Merge "Add tests for read_block_adb."
* commit 'ecead276f068cb53bddb2458b200aa96473ddfb3':
Add tests for read_block_adb.
-rw-r--r-- | minadbd/Android.mk | 29 | ||||
-rw-r--r-- | minadbd/fuse_adb_provider.c | 11 | ||||
-rw-r--r-- | minadbd/fuse_adb_provider.h | 11 | ||||
-rw-r--r-- | minadbd/fuse_adb_provider_test.cpp | 89 |
4 files changed, 124 insertions, 16 deletions
diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 24b043e..79fe96b 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -2,6 +2,12 @@ LOCAL_PATH:= $(call my-dir) +minadbd_cflags := \ + -Wall -Werror \ + -Wno-unused-parameter \ + -Wno-missing-field-initializers \ + -DADB_HOST=0 \ + include $(CLEAR_VARS) LOCAL_SRC_FILES := \ @@ -9,15 +15,22 @@ LOCAL_SRC_FILES := \ fuse_adb_provider.c \ services.c \ -LOCAL_CFLAGS := \ - -Wall -Werror \ - -Wno-unused-parameter \ - -Wimplicit-function-declaration \ - -DADB_HOST=0 \ - +LOCAL_MODULE := libminadbd +LOCAL_CFLAGS := $(minadbd_cflags) +LOCAL_CONLY_FLAGS := -Wimplicit-function-declaration LOCAL_C_INCLUDES := bootable/recovery system/core/adb LOCAL_WHOLE_STATIC_LIBRARIES := libadbd -LOCAL_MODULE := libminadbd - include $(BUILD_STATIC_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_CLANG := true +LOCAL_MODULE := minadbd_test +LOCAL_SRC_FILES := fuse_adb_provider_test.cpp +LOCAL_CFLAGS := $(minadbd_cflags) +LOCAL_C_INCLUDES := $(LOCAL_PATH) system/core/adb +LOCAL_STATIC_LIBRARIES := libminadbd +LOCAL_SHARED_LIBRARIES := liblog libutils + +include $(BUILD_NATIVE_TEST) diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c index 0d71072..5da7fd7 100644 --- a/minadbd/fuse_adb_provider.c +++ b/minadbd/fuse_adb_provider.c @@ -23,16 +23,11 @@ #include "adb.h" #include "adb_io.h" +#include "fuse_adb_provider.h" #include "fuse_sideload.h" -struct adb_data { - int sfd; // file descriptor for the adb channel - - uint64_t file_size; - uint32_t block_size; -}; - -static int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, + uint32_t fetch_size) { struct adb_data* ad = (struct adb_data*)cookie; char buf[10]; diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index 23de44a..b88ce49 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -17,10 +17,21 @@ #ifndef __FUSE_ADB_PROVIDER_H #define __FUSE_ADB_PROVIDER_H +#include <stdint.h> + #ifdef __cplusplus extern "C" { #endif +struct adb_data { + int sfd; // file descriptor for the adb channel + + uint64_t file_size; + uint32_t block_size; +}; + +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, + uint32_t fetch_size); int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size); #ifdef __cplusplus diff --git a/minadbd/fuse_adb_provider_test.cpp b/minadbd/fuse_adb_provider_test.cpp new file mode 100644 index 0000000..ecd9f38 --- /dev/null +++ b/minadbd/fuse_adb_provider_test.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fuse_adb_provider.h" + +#include <gtest/gtest.h> + +#include <errno.h> +#include <fcntl.h> +#include <sys/socket.h> + +#include <string> + +#include "adb_io.h" +#include "utils/file.h" + +TEST(fuse_adb_provider, read_block_adb) { + adb_data data = {}; + int sockets[2]; + + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)); + data.sfd = sockets[0]; + + int host_socket = sockets[1]; + fcntl(host_socket, F_SETFL, O_NONBLOCK); + + const char expected_data[] = "foobar"; + char block_data[sizeof(expected_data)] = {}; + + // If we write the result of read_block_adb's request before the request is + // actually made we can avoid needing an extra thread for this test. + ASSERT_TRUE(WriteFdExactly(host_socket, expected_data, + strlen(expected_data))); + + uint32_t block = 1234U; + const char expected_block[] = "00001234"; + ASSERT_EQ(0, read_block_adb(reinterpret_cast<void*>(&data), block, + reinterpret_cast<uint8_t*>(block_data), + sizeof(expected_data) - 1)); + + // Check that read_block_adb requested the right block. + char block_req[sizeof(expected_block)] = {}; + ASSERT_TRUE(ReadFdExactly(host_socket, block_req, 8)); + ASSERT_EQ(0, block_req[8]); + ASSERT_EQ(8U, strlen(block_req)); + ASSERT_STREQ(expected_block, block_req); + + // Check that read_block_adb returned the right data. + ASSERT_EQ(0, block_req[8]); + ASSERT_STREQ(expected_data, block_data); + + // Check that nothing else was written to the socket. + char tmp; + errno = 0; + ASSERT_EQ(-1, read(host_socket, &tmp, 1)); + ASSERT_EQ(EWOULDBLOCK, errno); + + close(sockets[0]); + close(sockets[1]); +} + +TEST(fuse_adb_provider, read_block_adb_fail_write) { + adb_data data = {}; + int sockets[2]; + + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)); + data.sfd = sockets[0]; + + ASSERT_EQ(0, close(sockets[1])); + + char buf[1]; + ASSERT_EQ(-EIO, read_block_adb(reinterpret_cast<void*>(&data), 0, + reinterpret_cast<uint8_t*>(buf), 1)); + + close(sockets[0]); +} |