blob: 4b74adf923d945ee11faef0519708d7ff78e841d [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
Dan Albert4895c522015-02-20 17:24:58 -080066TEST(transport, kick_transport) {
Dan Albertecce5032015-05-18 16:46:31 -070067 TestTransport t;
68
Dan Albert4895c522015-02-20 17:24:58 -080069 // Mutate some member so we can test that the function is run.
70 t.kick = [](atransport* trans) { trans->fd = 42; };
Dan Albertecce5032015-05-18 16:46:31 -070071
72 TestTransport expected;
73 expected.kick = t.kick;
Dan Albert4895c522015-02-20 17:24:58 -080074 expected.fd = 42;
75 expected.kicked = 1;
Dan Albertecce5032015-05-18 16:46:31 -070076
Dan Albert4895c522015-02-20 17:24:58 -080077 kick_transport(&t);
78 ASSERT_EQ(42, t.fd);
79 ASSERT_EQ(1, t.kicked);
Dan Albertecce5032015-05-18 16:46:31 -070080 ASSERT_EQ(expected, t);
Dan Albert4895c522015-02-20 17:24:58 -080081}
82
83TEST(transport, kick_transport_already_kicked) {
84 // Ensure that the transport is not modified if the transport has already been
85 // kicked.
Dan Albertecce5032015-05-18 16:46:31 -070086 TestTransport t;
Dan Albert4895c522015-02-20 17:24:58 -080087 t.kicked = 1;
88 t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; };
Dan Albertecce5032015-05-18 16:46:31 -070089
90 TestTransport expected;
91 expected.kicked = 1;
92 expected.kick = t.kick;
93
Dan Albert4895c522015-02-20 17:24:58 -080094 kick_transport(&t);
Dan Albertecce5032015-05-18 16:46:31 -070095 ASSERT_EQ(expected, t);
Dan Albert4895c522015-02-20 17:24:58 -080096}
97
98// Disabled because the function currently segfaults for a zeroed atransport. I
99// want to make sure I understand how this is working at all before I try fixing
100// that.
101TEST(transport, DISABLED_run_transport_disconnects_zeroed_atransport) {
Dan Albertecce5032015-05-18 16:46:31 -0700102 atransport t;
Dan Albert4895c522015-02-20 17:24:58 -0800103 run_transport_disconnects(&t);
104}