Add some basic tests to adb.

Change-Id: I946b5b1e5650540db3b4f75892214c4218b3baf3
This commit is contained in:
Dan Albert 2015-02-20 17:24:58 -08:00
parent d75d6c91d2
commit 055f1aa4ff
3 changed files with 107 additions and 18 deletions

View File

@ -26,7 +26,9 @@ LIBADB_SRC_FILES := \
transport_usb.c \
LIBADB_CFLAGS := \
-Wall -Werror -Wno-unused-parameter \
-Wall -Werror \
-Wno-unused-parameter \
-Wno-missing-field-initializers \
-D_XOPEN_SOURCE -D_GNU_SOURCE \
-fvisibility=hidden \
@ -67,6 +69,34 @@ endif
include $(BUILD_HOST_STATIC_LIBRARY)
LIBADB_TEST_SRCS := \
transport_test.cpp \
include $(CLEAR_VARS)
LOCAL_CLANG := $(ADB_CLANG)
LOCAL_MODULE := adbd_test
LOCAL_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS)
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
LOCAL_STATIC_LIBRARIES := libadbd
LOCAL_SHARED_LIBRARIES := liblog
include $(BUILD_NATIVE_TEST)
include $(CLEAR_VARS)
LOCAL_CLANG := $(ADB_CLANG)
LOCAL_MODULE := adb_test
LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS)
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.c
LOCAL_STATIC_LIBRARIES := \
libadb \
libcrypto_static \
libcutils \
ifeq ($(HOST_OS),linux)
LOCAL_LDLIBS += -lrt -ldl -lpthread
endif
include $(BUILD_HOST_NATIVE_TEST)
# adb host tool
# =========================================================
include $(CLEAR_VARS)

View File

@ -18,6 +18,7 @@
#include "transport.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -69,8 +70,7 @@ static void dump_hex( const unsigned char* ptr, size_t len )
}
#endif
void
kick_transport(atransport* t)
void kick_transport(atransport* t)
{
if (t && !t->kicked)
{
@ -87,8 +87,25 @@ kick_transport(atransport* t)
}
}
void
run_transport_disconnects(atransport* t)
// Each atransport contains a list of adisconnects (t->disconnects).
// An adisconnect contains a link to the next/prev adisconnect, a function
// pointer to a disconnect callback which takes a void* piece of user data and
// the atransport, and some user data for the callback (helpfully named
// "opaque").
//
// The list is circular. New items are added to the entry member of the list
// (t->disconnects) by add_transport_disconnect.
//
// run_transport_disconnects invokes each function in the list.
//
// Gotchas:
// * run_transport_disconnects assumes that t->disconnects is non-null, so
// this can't be run on a zeroed atransport.
// * The callbacks in this list are not removed when called, and this function
// is not guarded against running more than once. As such, ensure that this
// function is not called multiple times on the same atransport.
// TODO(danalbert): Just fix this so that it is guarded once you have tests.
void run_transport_disconnects(atransport* t)
{
adisconnect* dis = t->disconnects.next;
@ -753,17 +770,6 @@ void remove_transport_disconnect(atransport* t, adisconnect* dis)
dis->next = dis->prev = dis;
}
static int qual_char_is_invalid(char ch)
{
if ('A' <= ch && ch <= 'Z')
return 0;
if ('a' <= ch && ch <= 'z')
return 0;
if ('0' <= ch && ch <= '9')
return 0;
return 1;
}
static int qual_match(const char *to_test,
const char *prefix, const char *qual, int sanitize_qual)
{
@ -783,7 +789,7 @@ static int qual_match(const char *to_test,
while (*qual) {
char ch = *qual++;
if (sanitize_qual && qual_char_is_invalid(ch))
if (sanitize_qual && isalnum(ch))
ch = '_';
if (ch != *to_test++)
return 0;
@ -923,7 +929,7 @@ static void add_qual(char **buf, size_t *buf_size,
if (sanitize_qual) {
char *cp;
for (cp = *buf + prefix_len; cp < *buf + len; cp++) {
if (qual_char_is_invalid(*cp))
if (isalnum(*cp))
*cp = '_';
}
}

53
adb/transport_test.cpp Normal file
View File

@ -0,0 +1,53 @@
/*
* 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 "transport.h"
#include <gtest/gtest.h>
#include "adb.h"
TEST(transport, kick_transport) {
atransport t = {};
// Mutate some member so we can test that the function is run.
t.kick = [](atransport* trans) { trans->fd = 42; };
atransport expected = t;
expected.fd = 42;
expected.kicked = 1;
kick_transport(&t);
ASSERT_EQ(42, t.fd);
ASSERT_EQ(1, t.kicked);
ASSERT_EQ(0, memcmp(&expected, &t, sizeof(atransport)));
}
TEST(transport, kick_transport_already_kicked) {
// Ensure that the transport is not modified if the transport has already been
// kicked.
atransport t = {};
t.kicked = 1;
t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; };
atransport expected = t;
kick_transport(&t);
ASSERT_EQ(0, memcmp(&expected, &t, sizeof(atransport)));
}
// Disabled because the function currently segfaults for a zeroed atransport. I
// want to make sure I understand how this is working at all before I try fixing
// that.
TEST(transport, DISABLED_run_transport_disconnects_zeroed_atransport) {
atransport t = {};
run_transport_disconnects(&t);
}