blob: 49deb73d2ea4ac507ef01b8657699a932a450ee3 [file] [log] [blame]
Dan Albert4895c522015-02-20 17:24:58 -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 "transport.h"
18
19#include <gtest/gtest.h>
20
21#include "adb.h"
22
Dan Albertecce5032015-05-18 16:46:31 -070023class TestTransport : public atransport {
24public:
25 bool operator==(const atransport& rhs) const {
26 EXPECT_EQ(read_from_remote, rhs.read_from_remote);
27 EXPECT_EQ(write_to_remote, rhs.write_to_remote);
28 EXPECT_EQ(close, rhs.close);
29 EXPECT_EQ(kick, rhs.kick);
30
31 EXPECT_EQ(fd, rhs.fd);
32 EXPECT_EQ(transport_socket, rhs.transport_socket);
33
34 EXPECT_EQ(
35 0, memcmp(&transport_fde, &rhs.transport_fde, sizeof(fdevent)));
36
37 EXPECT_EQ(ref_count, rhs.ref_count);
38 EXPECT_EQ(sync_token, rhs.sync_token);
39 EXPECT_EQ(connection_state, rhs.connection_state);
40 EXPECT_EQ(online, rhs.online);
41 EXPECT_EQ(type, rhs.type);
42
43 EXPECT_EQ(usb, rhs.usb);
44 EXPECT_EQ(sfd, rhs.sfd);
45
46 EXPECT_EQ(serial, rhs.serial);
47 EXPECT_EQ(product, rhs.product);
48 EXPECT_EQ(model, rhs.model);
49 EXPECT_EQ(device, rhs.device);
50 EXPECT_EQ(devpath, rhs.devpath);
51 EXPECT_EQ(adb_port, rhs.adb_port);
52 EXPECT_EQ(kicked, rhs.kicked);
53
54 EXPECT_EQ(
55 0, memcmp(&disconnects, &rhs.disconnects, sizeof(adisconnect)));
56
57 EXPECT_EQ(key, rhs.key);
58 EXPECT_EQ(0, memcmp(token, rhs.token, TOKEN_SIZE));
59 EXPECT_EQ(0, memcmp(&auth_fde, &rhs.auth_fde, sizeof(fdevent)));
60 EXPECT_EQ(failed_auth_attempts, rhs.failed_auth_attempts);
61
62 return true;
63 }
64};
65
Spencer Lowc9ddd812015-05-24 15:36:28 -070066class TransportSetup {
67public:
68 TransportSetup() {
69#ifdef _WIN32
70 // Use extern instead of including sysdeps.h which brings in various macros
71 // that conflict with APIs used in this file.
72 extern void adb_sysdeps_init(void);
73 adb_sysdeps_init();
74#else
75 // adb_sysdeps_init() is an inline function that we cannot link against.
76#endif
77 }
78};
79
80// Static initializer will call adb_sysdeps_init() before main() to initialize
81// the transport mutex before it is used in the tests. Alternatives would be to
82// use __attribute__((constructor)) here or to use that or a static initializer
83// for adb_sysdeps_init() itself in sysdeps_win32.cpp (caveats of unclear
84// init order), or to use a test fixture whose SetUp() could do the init once.
85static TransportSetup g_TransportSetup;
86
Dan Albert4895c522015-02-20 17:24:58 -080087TEST(transport, kick_transport) {
Dan Albertecce5032015-05-18 16:46:31 -070088 TestTransport t;
89
Dan Albert4895c522015-02-20 17:24:58 -080090 // Mutate some member so we can test that the function is run.
91 t.kick = [](atransport* trans) { trans->fd = 42; };
Dan Albertecce5032015-05-18 16:46:31 -070092
93 TestTransport expected;
94 expected.kick = t.kick;
Dan Albert4895c522015-02-20 17:24:58 -080095 expected.fd = 42;
96 expected.kicked = 1;
Dan Albertecce5032015-05-18 16:46:31 -070097
Dan Albert4895c522015-02-20 17:24:58 -080098 kick_transport(&t);
99 ASSERT_EQ(42, t.fd);
100 ASSERT_EQ(1, t.kicked);
Dan Albertecce5032015-05-18 16:46:31 -0700101 ASSERT_EQ(expected, t);
Dan Albert4895c522015-02-20 17:24:58 -0800102}
103
104TEST(transport, kick_transport_already_kicked) {
105 // Ensure that the transport is not modified if the transport has already been
106 // kicked.
Dan Albertecce5032015-05-18 16:46:31 -0700107 TestTransport t;
Dan Albert4895c522015-02-20 17:24:58 -0800108 t.kicked = 1;
109 t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; };
Dan Albertecce5032015-05-18 16:46:31 -0700110
111 TestTransport expected;
112 expected.kicked = 1;
113 expected.kick = t.kick;
114
Dan Albert4895c522015-02-20 17:24:58 -0800115 kick_transport(&t);
Dan Albertecce5032015-05-18 16:46:31 -0700116 ASSERT_EQ(expected, t);
Dan Albert4895c522015-02-20 17:24:58 -0800117}
118
119// Disabled because the function currently segfaults for a zeroed atransport. I
120// want to make sure I understand how this is working at all before I try fixing
121// that.
122TEST(transport, DISABLED_run_transport_disconnects_zeroed_atransport) {
Dan Albertecce5032015-05-18 16:46:31 -0700123 atransport t;
Dan Albert4895c522015-02-20 17:24:58 -0800124 run_transport_disconnects(&t);
125}