1 /*
2 * Copyright (C) 2020 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 <gtest/gtest.h>
18
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <thread>
22
23 #include "app_test_base.h"
24 #include "chpp/app.h"
25 #include "chpp/clients/discovery.h"
26 #include "chpp/clients/loopback.h"
27 #include "chpp/clients/timesync.h"
28 #include "chpp/log.h"
29 #include "chpp/platform/platform_link.h"
30 #include "chpp/transport.h"
31
32 /*
33 * Test suite for the CHPP Loopback client/service
34 */
35 namespace chpp {
36 namespace {
37
38 class ChppAppTest : public AppTestBase {};
39
TEST_F(ChppAppTest,SimpleStartStop)40 TEST_F(ChppAppTest, SimpleStartStop) {
41 // Simple test to make sure start/stop work threads work without crashing
42 ASSERT_TRUE(mClientLinkContext.linkEstablished);
43 ASSERT_TRUE(mServiceLinkContext.linkEstablished);
44 }
45
TEST_F(ChppAppTest,TransportLayerLoopback)46 TEST_F(ChppAppTest, TransportLayerLoopback) {
47 // This tests the more limited transport-layer-looopback. In contrast,
48 // the regular application-layer loopback test provides a more thorough test
49 // and test results.
50 constexpr size_t kTestLen =
51 CHPP_LINUX_LINK_TX_MTU_BYTES - CHPP_TRANSPORT_ENCODING_OVERHEAD_BYTES;
52 uint8_t buf[kTestLen];
53 for (size_t i = 0; i < kTestLen; i++) {
54 buf[i] = (uint8_t)(i + 100);
55 }
56
57 std::this_thread::sleep_for(std::chrono::milliseconds(2000));
58 CHPP_LOGI("Starting transport-layer loopback test (max buffer = %zu)...",
59 kTestLen);
60
61 EXPECT_EQ(CHPP_APP_ERROR_NONE,
62 chppRunTransportLoopback(mClientAppContext.transportContext, buf,
63 kTestLen));
64 std::this_thread::sleep_for(std::chrono::milliseconds(300));
65 EXPECT_EQ(CHPP_APP_ERROR_NONE,
66 mClientAppContext.transportContext->loopbackResult);
67
68 EXPECT_EQ(
69 CHPP_APP_ERROR_NONE,
70 chppRunTransportLoopback(mClientAppContext.transportContext, buf, 100));
71 std::this_thread::sleep_for(std::chrono::milliseconds(300));
72 EXPECT_EQ(CHPP_APP_ERROR_NONE,
73 mClientAppContext.transportContext->loopbackResult);
74
75 EXPECT_EQ(
76 CHPP_APP_ERROR_NONE,
77 chppRunTransportLoopback(mClientAppContext.transportContext, buf, 1));
78 std::this_thread::sleep_for(std::chrono::milliseconds(300));
79 EXPECT_EQ(CHPP_APP_ERROR_NONE,
80 mClientAppContext.transportContext->loopbackResult);
81
82 EXPECT_EQ(
83 CHPP_APP_ERROR_INVALID_LENGTH,
84 chppRunTransportLoopback(mClientAppContext.transportContext, buf, 0));
85 std::this_thread::sleep_for(std::chrono::milliseconds(300));
86 EXPECT_EQ(CHPP_APP_ERROR_INVALID_LENGTH,
87 mClientAppContext.transportContext->loopbackResult);
88 }
89
TEST_F(ChppAppTest,SimpleLoopback)90 TEST_F(ChppAppTest, SimpleLoopback) {
91 constexpr size_t kTestLen = CHPP_LINUX_LINK_TX_MTU_BYTES -
92 CHPP_TRANSPORT_ENCODING_OVERHEAD_BYTES -
93 CHPP_LOOPBACK_HEADER_LEN;
94 uint8_t buf[kTestLen];
95 for (size_t i = 0; i < kTestLen; i++) {
96 buf[i] = (uint8_t)(i + 100);
97 }
98
99 CHPP_LOGI(
100 "Starting loopback test without fragmentation (max buffer = %zu)...",
101 kTestLen);
102
103 struct ChppLoopbackTestResult result =
104 chppRunLoopbackTest(&mClientAppContext, buf, kTestLen);
105
106 EXPECT_EQ(result.error, CHPP_APP_ERROR_NONE);
107
108 result = chppRunLoopbackTest(&mClientAppContext, buf, 10);
109 EXPECT_EQ(result.error, CHPP_APP_ERROR_NONE);
110
111 result = chppRunLoopbackTest(&mClientAppContext, buf, 1);
112 EXPECT_EQ(result.error, CHPP_APP_ERROR_NONE);
113
114 result = chppRunLoopbackTest(&mClientAppContext, buf, 0);
115 EXPECT_EQ(result.error, CHPP_APP_ERROR_INVALID_LENGTH);
116 }
117
TEST_F(ChppAppTest,FragmentedLoopback)118 TEST_F(ChppAppTest, FragmentedLoopback) {
119 constexpr size_t kTestLen = UINT16_MAX;
120 uint8_t buf[kTestLen];
121 for (size_t i = 0; i < kTestLen; i++) {
122 // Arbitrary data. A modulus of 251, a prime number, reduces the chance of
123 // alignment with the MTU.
124 buf[i] = (uint8_t)((i % 251) + 64);
125 }
126
127 CHPP_LOGI("Starting loopback test with fragmentation (max buffer = %zu)...",
128 kTestLen);
129
130 struct ChppLoopbackTestResult result =
131 chppRunLoopbackTest(&mClientAppContext, buf, kTestLen);
132 EXPECT_EQ(result.error, CHPP_APP_ERROR_NONE);
133
134 result = chppRunLoopbackTest(&mClientAppContext, buf, 50000);
135 EXPECT_EQ(result.error, CHPP_APP_ERROR_NONE);
136
137 result = chppRunLoopbackTest(
138 &mClientAppContext, buf,
139 chppTransportTxMtuSize(mClientAppContext.transportContext) -
140 CHPP_LOOPBACK_HEADER_LEN + 1);
141 EXPECT_EQ(result.error, CHPP_APP_ERROR_NONE);
142 }
143
TEST_F(ChppAppTest,Timesync)144 TEST_F(ChppAppTest, Timesync) {
145 // Upper bound for the RTT (response received - request sent).
146 constexpr uint64_t kMaxRttNs = 20 * CHPP_NSEC_PER_MSEC;
147 // The offset is defined as (Time when the service sent the response) - (Time
148 // when the client got the response).
149 // We use half the RTT as the upper bound.
150 constexpr int64_t kMaxOffsetNs = kMaxRttNs / 2;
151
152 CHPP_LOGI("Starting timesync test...");
153
154 std::this_thread::sleep_for(std::chrono::milliseconds(100));
155 EXPECT_TRUE(chppTimesyncMeasureOffset(&mClientAppContext));
156 std::this_thread::sleep_for(std::chrono::milliseconds(100));
157
158 EXPECT_EQ(chppTimesyncGetResult(&mClientAppContext)->error,
159 CHPP_APP_ERROR_NONE);
160
161 EXPECT_LT(chppTimesyncGetResult(&mClientAppContext)->rttNs, kMaxRttNs);
162 EXPECT_NE(chppTimesyncGetResult(&mClientAppContext)->rttNs, 0);
163
164 EXPECT_LT(chppTimesyncGetResult(&mClientAppContext)->offsetNs, kMaxOffsetNs);
165 EXPECT_GT(chppTimesyncGetResult(&mClientAppContext)->offsetNs, -kMaxOffsetNs);
166 EXPECT_NE(chppTimesyncGetResult(&mClientAppContext)->offsetNs, 0);
167 }
168
TEST_F(ChppAppTest,DiscoveryMatched)169 TEST_F(ChppAppTest, DiscoveryMatched) {
170 constexpr uint64_t kTimeoutMs = 5000;
171 EXPECT_TRUE(chppWaitForDiscoveryComplete(&mClientAppContext, kTimeoutMs));
172 EXPECT_TRUE(chppAreAllClientsMatched(&mClientAppContext));
173 }
174
175 } // namespace
176 } // namespace chpp
177