1 /*
2  * Copyright 2019 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 #define LOG_TAG "AdbWifiPairingConnectionTest"
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 
23 #include <adbwifi/pairing/pairing_server.h>
24 #include <android-base/logging.h>
25 #include <gtest/gtest.h>
26 
27 #include "adb/client/pairing/tests/pairing_client.h"
28 
29 namespace adbwifi {
30 namespace pairing {
31 
32 static const std::string kTestServerCert =
33         "-----BEGIN CERTIFICATE-----\n"
34         "MIIBljCCAT2gAwIBAgIBATAKBggqhkjOPQQDAjAzMQswCQYDVQQGEwJVUzEQMA4G\n"
35         "A1UECgwHQW5kcm9pZDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTE5MTEwNzAyMDkx\n"
36         "NVoXDTI5MTEwNDAyMDkxNVowMzELMAkGA1UEBhMCVVMxEDAOBgNVBAoMB0FuZHJv\n"
37         "aWQxEjAQBgNVBAMMCWxvY2FsaG9zdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IA\n"
38         "BCXRovy3RhtK0Khle48vUmkcuI0OF7K8o9sVPE4oVnp24l+cCYr3BtrgifoHPgj4\n"
39         "vq7n105qzK7ngBHH+LBmYIijQjBAMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/\n"
40         "BAQDAgGGMB0GA1UdDgQWBBQi4eskzqVG3SCX2CwJF/aTZqUcuTAKBggqhkjOPQQD\n"
41         "AgNHADBEAiBPYvLOCIvPDtq3vMF7A2z7t7JfcCmbC7g8ftEVJucJBwIgepf+XjTb\n"
42         "L7RCE16p7iVkpHUrWAOl7zDxqD+jaji5MkQ=\n"
43         "-----END CERTIFICATE-----\n";
44 
45 static const std::string kTestServerPrivKey =
46         "-----BEGIN PRIVATE KEY-----\n"
47         "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgSCaskWPtutIgh8uQ\n"
48         "UBH6ZIea5Kxm7m6kkGNkd8FYPSOhRANCAAQl0aL8t0YbStCoZXuPL1JpHLiNDhey\n"
49         "vKPbFTxOKFZ6duJfnAmK9wba4In6Bz4I+L6u59dOasyu54ARx/iwZmCI\n"
50         "-----END PRIVATE KEY-----\n";
51 
52 static const std::string kTestClientCert =
53         "-----BEGIN CERTIFICATE-----\n"
54         "MIIBlzCCAT2gAwIBAgIBATAKBggqhkjOPQQDAjAzMQswCQYDVQQGEwJVUzEQMA4G\n"
55         "A1UECgwHQW5kcm9pZDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTE5MTEwOTAxNTAy\n"
56         "OFoXDTI5MTEwNjAxNTAyOFowMzELMAkGA1UEBhMCVVMxEDAOBgNVBAoMB0FuZHJv\n"
57         "aWQxEjAQBgNVBAMMCWxvY2FsaG9zdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IA\n"
58         "BGW+RuoEIzbt42zAuZzbXaC0bvh8n4OLFDnqkkW6kWA43GYg/mUMVc9vg/nuxyuM\n"
59         "aT0KqbTaLhm+NjCXVRnxBrajQjBAMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/\n"
60         "BAQDAgGGMB0GA1UdDgQWBBTjCaC8/NXgdBz9WlMVCNwhx7jn0jAKBggqhkjOPQQD\n"
61         "AgNIADBFAiB/xp2boj7b1KK2saS6BL59deo/TvfgZ+u8HPq4k4VP3gIhAMXswp9W\n"
62         "XdlziccQdj+0KpbUojDKeHOr4fIj/+LxsWPa\n"
63         "-----END CERTIFICATE-----\n";
64 
65 static const std::string kTestClientPrivKey =
66         "-----BEGIN PRIVATE KEY-----\n"
67         "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgFw/CWY1f6TSB70AF\n"
68         "yVe8n6QdYFu8HW5t/tij2SrXx42hRANCAARlvkbqBCM27eNswLmc212gtG74fJ+D\n"
69         "ixQ56pJFupFgONxmIP5lDFXPb4P57scrjGk9Cqm02i4ZvjYwl1UZ8Qa2\n"
70         "-----END PRIVATE KEY-----\n";
71 
72 class AdbWifiPairingConnectionTest : public testing::Test {
73   protected:
SetUp()74     virtual void SetUp() override {}
75 
TearDown()76     virtual void TearDown() override {}
77 
initPairing(const std::vector<uint8_t> server_pswd,const std::vector<uint8_t> client_pswd)78     void initPairing(const std::vector<uint8_t> server_pswd,
79                      const std::vector<uint8_t> client_pswd) {
80         std::vector<uint8_t> cert;
81         std::vector<uint8_t> key;
82         // Include the null-byte as well.
83         cert.assign(reinterpret_cast<const uint8_t*>(kTestServerCert.data()),
84                     reinterpret_cast<const uint8_t*>(kTestServerCert.data()) +
85                             kTestServerCert.size() + 1);
86         key.assign(reinterpret_cast<const uint8_t*>(kTestServerPrivKey.data()),
87                    reinterpret_cast<const uint8_t*>(kTestServerPrivKey.data()) +
88                            kTestServerPrivKey.size() + 1);
89         server_ = PairingServer::create(server_pswd, server_info_, cert, key, kDefaultPairingPort);
90         cert.assign(reinterpret_cast<const uint8_t*>(kTestClientCert.data()),
91                     reinterpret_cast<const uint8_t*>(kTestClientCert.data()) +
92                             kTestClientCert.size() + 1);
93         key.assign(reinterpret_cast<const uint8_t*>(kTestClientPrivKey.data()),
94                    reinterpret_cast<const uint8_t*>(kTestClientPrivKey.data()) +
95                            kTestClientPrivKey.size() + 1);
96         client_ = PairingClient::create(client_pswd, client_info_, cert, key, "127.0.0.1");
97     }
98 
createServer(const std::vector<uint8_t> pswd)99     std::unique_ptr<PairingServer> createServer(const std::vector<uint8_t> pswd) {
100         std::vector<uint8_t> cert;
101         std::vector<uint8_t> key;
102         // Include the null-byte as well.
103         cert.assign(reinterpret_cast<const uint8_t*>(kTestServerCert.data()),
104                     reinterpret_cast<const uint8_t*>(kTestServerCert.data()) +
105                             kTestServerCert.size() + 1);
106         key.assign(reinterpret_cast<const uint8_t*>(kTestServerPrivKey.data()),
107                    reinterpret_cast<const uint8_t*>(kTestServerPrivKey.data()) +
108                            kTestServerPrivKey.size() + 1);
109         return PairingServer::create(pswd, server_info_, cert, key, kDefaultPairingPort);
110     }
111 
createClient(const std::vector<uint8_t> pswd)112     std::unique_ptr<PairingClient> createClient(const std::vector<uint8_t> pswd) {
113         std::vector<uint8_t> cert;
114         std::vector<uint8_t> key;
115         // Include the null-byte as well.
116         cert.assign(reinterpret_cast<const uint8_t*>(kTestClientCert.data()),
117                     reinterpret_cast<const uint8_t*>(kTestClientCert.data()) +
118                             kTestClientCert.size() + 1);
119         key.assign(reinterpret_cast<const uint8_t*>(kTestClientPrivKey.data()),
120                    reinterpret_cast<const uint8_t*>(kTestClientPrivKey.data()) +
121                            kTestClientPrivKey.size() + 1);
122         return PairingClient::create(pswd, client_info_, cert, key, "127.0.0.1");
123     }
124 
125     std::unique_ptr<PairingServer> server_;
126     const PeerInfo server_info_ = {
127             .name = "my_server_name",
128             .guid = "my_server_guid",
129     };
130     std::unique_ptr<PairingClient> client_;
131     const PeerInfo client_info_ = {
132             .name = "my_client_name",
133             .guid = "my_client_guid",
134     };
135 };
136 
TEST_F(AdbWifiPairingConnectionTest,ServerCreation)137 TEST_F(AdbWifiPairingConnectionTest, ServerCreation) {
138     // All parameters bad
139     auto server = PairingServer::create({}, {}, {}, {}, -1);
140     EXPECT_EQ(nullptr, server);
141     // Bad password
142     server = PairingServer::create({}, server_info_, {0x01}, {0x01}, -1);
143     EXPECT_EQ(nullptr, server);
144     // Bad peer_info
145     server = PairingServer::create({0x01}, {}, {0x01}, {0x01}, -1);
146     EXPECT_EQ(nullptr, server);
147     // Bad certificate
148     server = PairingServer::create({0x01}, server_info_, {}, {0x01}, -1);
149     EXPECT_EQ(nullptr, server);
150     // Bad private key
151     server = PairingServer::create({0x01}, server_info_, {0x01}, {}, -1);
152     EXPECT_EQ(nullptr, server);
153     // Bad port
154     server = PairingServer::create({0x01}, server_info_, {0x01}, {0x01}, -1);
155     EXPECT_EQ(nullptr, server);
156     // Valid params
157     server = PairingServer::create({0x01}, server_info_, {0x01}, {0x01}, 7776);
158     EXPECT_NE(nullptr, server);
159 }
160 
TEST_F(AdbWifiPairingConnectionTest,ClientCreation)161 TEST_F(AdbWifiPairingConnectionTest, ClientCreation) {
162     // All parameters bad
163     auto client = PairingClient::create({}, client_info_, {}, {}, "");
164     EXPECT_EQ(nullptr, client);
165     // Bad password
166     client = PairingClient::create({}, client_info_, {0x01}, {0x01}, "127.0.0.1");
167     EXPECT_EQ(nullptr, client);
168     // Bad peer_info
169     client = PairingClient::create({0x01}, {}, {0x01}, {0x01}, "127.0.0.1");
170     EXPECT_EQ(nullptr, client);
171     // Bad certificate
172     client = PairingClient::create({0x01}, client_info_, {}, {0x01}, "127.0.0.1");
173     EXPECT_EQ(nullptr, client);
174     // Bad private key
175     client = PairingClient::create({0x01}, client_info_, {0x01}, {}, "127.0.0.1");
176     EXPECT_EQ(nullptr, client);
177     // Bad ip address
178     client = PairingClient::create({0x01}, client_info_, {0x01}, {0x01}, "");
179     EXPECT_EQ(nullptr, client);
180     // Valid params
181     client = PairingClient::create({0x01}, client_info_, {0x01}, {0x01}, "127.0.0.1");
182     EXPECT_NE(nullptr, client);
183 }
184 
TEST_F(AdbWifiPairingConnectionTest,SmokeValidPairing)185 TEST_F(AdbWifiPairingConnectionTest, SmokeValidPairing) {
186     std::vector<uint8_t> pswd{0x01, 0x03, 0x05, 0x07};
187     initPairing(pswd, pswd);
188 
189     // Start the server first, to open the port for connections
190     std::mutex server_mutex;
191     std::condition_variable server_cv;
192     std::unique_lock<std::mutex> server_lock(server_mutex);
193 
194     auto server_callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
195                                void* opaque) {
196         ASSERT_NE(nullptr, peer_info);
197         ASSERT_NE(nullptr, cert);
198         EXPECT_FALSE(cert->empty());
199         EXPECT_EQ(nullptr, opaque);
200 
201         // Verify the peer_info and cert
202         ASSERT_EQ(strlen(peer_info->name), strlen(client_info_.name));
203         EXPECT_EQ(::memcmp(peer_info->name, client_info_.name, strlen(client_info_.name)), 0);
204         ASSERT_EQ(strlen(peer_info->guid), strlen(client_info_.guid));
205         EXPECT_EQ(::memcmp(peer_info->guid, client_info_.guid, strlen(client_info_.guid)), 0);
206         ASSERT_EQ(cert->size(), kTestClientCert.size() + 1);
207         EXPECT_EQ(::memcmp(cert->data(), kTestClientCert.data(), kTestClientCert.size() + 1), 0);
208 
209         std::lock_guard<std::mutex> lock(server_mutex);
210         server_cv.notify_one();
211     };
212     ASSERT_TRUE(server_->start(server_callback, nullptr));
213 
214     // Start the client
215     bool got_valid_pairing = false;
216     std::mutex client_mutex;
217     std::condition_variable client_cv;
218     std::unique_lock<std::mutex> client_lock(client_mutex);
219     auto client_callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
220                                void* opaque) {
221         ASSERT_NE(nullptr, peer_info);
222         ASSERT_NE(nullptr, cert);
223         EXPECT_FALSE(cert->empty());
224         EXPECT_EQ(nullptr, opaque);
225 
226         // Verify the peer_info and cert
227         ASSERT_EQ(strlen(peer_info->name), strlen(server_info_.name));
228         EXPECT_EQ(::memcmp(peer_info->name, server_info_.name, strlen(server_info_.name)), 0);
229         ASSERT_EQ(strlen(peer_info->guid), strlen(server_info_.guid));
230         EXPECT_EQ(::memcmp(peer_info->guid, server_info_.guid, strlen(server_info_.guid)), 0);
231         ASSERT_EQ(cert->size(), kTestServerCert.size() + 1);
232         EXPECT_EQ(::memcmp(cert->data(), kTestServerCert.data(), kTestServerCert.size() + 1), 0);
233 
234         got_valid_pairing = (peer_info != nullptr && cert != nullptr && !cert->empty());
235         std::lock_guard<std::mutex> lock(client_mutex);
236         client_cv.notify_one();
237     };
238     ASSERT_TRUE(client_->start(client_callback, nullptr));
239     client_cv.wait(client_lock);
240 
241     // Kill server if the pairing failed, since server only shuts down when
242     // it gets a valid pairing.
243     if (!got_valid_pairing) {
244         server_lock.unlock();
245         server_.reset();
246     } else {
247         server_cv.wait(server_lock);
248     }
249 }
250 
TEST_F(AdbWifiPairingConnectionTest,CancelPairing)251 TEST_F(AdbWifiPairingConnectionTest, CancelPairing) {
252     std::vector<uint8_t> pswd{0x01, 0x03, 0x05, 0x07};
253     std::vector<uint8_t> pswd2{0x01, 0x03, 0x05, 0x06};
254     initPairing(pswd, pswd2);
255 
256     // Start the server first, to open the port for connections
257     std::mutex server_mutex;
258     std::condition_variable server_cv;
259     std::unique_lock<std::mutex> server_lock(server_mutex);
260 
261     bool server_got_valid_pairing = true;
262     auto server_callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
263                                void* opaque) {
264         // Pairing will be cancelled, which should initiate this callback with
265         // empty values.
266         ASSERT_EQ(nullptr, peer_info);
267         ASSERT_EQ(nullptr, cert);
268         EXPECT_EQ(nullptr, opaque);
269         std::lock_guard<std::mutex> lock(server_mutex);
270         server_cv.notify_one();
271         server_got_valid_pairing = false;
272     };
273     ASSERT_TRUE(server_->start(server_callback, nullptr));
274 
275     // Start the client (should fail because of different passwords).
276     bool got_valid_pairing = false;
277     std::mutex client_mutex;
278     std::condition_variable client_cv;
279     std::unique_lock<std::mutex> client_lock(client_mutex);
280     auto client_callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
281                                void* opaque) {
282         ASSERT_EQ(nullptr, peer_info);
283         ASSERT_EQ(nullptr, cert);
284         EXPECT_EQ(nullptr, opaque);
285 
286         got_valid_pairing = (peer_info != nullptr && cert != nullptr && !cert->empty());
287         std::lock_guard<std::mutex> lock(client_mutex);
288         client_cv.notify_one();
289     };
290     ASSERT_TRUE(client_->start(client_callback, nullptr));
291     client_cv.wait(client_lock);
292 
293     server_lock.unlock();
294     // This should trigger the callback to be on the same thread.
295     server_.reset();
296     EXPECT_FALSE(server_got_valid_pairing);
297 }
298 
TEST_F(AdbWifiPairingConnectionTest,MultipleClientsAllFail)299 TEST_F(AdbWifiPairingConnectionTest, MultipleClientsAllFail) {
300     std::vector<uint8_t> pswd{0x01, 0x03, 0x05, 0x07};
301     std::vector<uint8_t> pswd2{0x01, 0x03, 0x05, 0x06};
302 
303     auto server = createServer(pswd);
304     ASSERT_NE(nullptr, server);
305     // Start the server first, to open the port for connections
306     std::mutex server_mutex;
307     std::condition_variable server_cv;
308     std::unique_lock<std::mutex> server_lock(server_mutex);
309 
310     bool server_got_valid_pairing = true;
311     auto server_callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
312                                void* opaque) {
313         // Pairing will be cancelled, which should initiate this callback with
314         // empty values.
315         ASSERT_EQ(nullptr, peer_info);
316         ASSERT_EQ(nullptr, cert);
317         EXPECT_EQ(nullptr, opaque);
318         std::lock_guard<std::mutex> lock(server_mutex);
319         server_cv.notify_one();
320         server_got_valid_pairing = false;
321     };
322     ASSERT_TRUE(server->start(server_callback, nullptr));
323 
324     // Start multiple clients, all with bad passwords
325     std::vector<std::unique_ptr<PairingClient>> clients;
326     int num_clients_done = 0;
327     int test_num_clients = 5;
328     std::mutex client_mutex;
329     std::condition_variable client_cv;
330     std::unique_lock<std::mutex> client_lock(client_mutex);
331     while (clients.size() < test_num_clients) {
332         auto client = createClient(pswd2);
333         ASSERT_NE(nullptr, client);
334         auto callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
335                             void* opaque) {
336             ASSERT_EQ(nullptr, peer_info);
337             ASSERT_EQ(nullptr, cert);
338             EXPECT_EQ(nullptr, opaque);
339 
340             {
341                 std::lock_guard<std::mutex> lock(client_mutex);
342                 num_clients_done++;
343             }
344             client_cv.notify_one();
345         };
346         ASSERT_TRUE(client->start(callback, nullptr));
347         clients.push_back(std::move(client));
348     }
349 
350     client_cv.wait(client_lock, [&]() { return (num_clients_done == test_num_clients); });
351     EXPECT_EQ(num_clients_done, test_num_clients);
352 
353     server_lock.unlock();
354     // This should trigger the callback to be on the same thread.
355     server.reset();
356     EXPECT_FALSE(server_got_valid_pairing);
357 }
358 
TEST_F(AdbWifiPairingConnectionTest,MultipleClientsOnePass)359 TEST_F(AdbWifiPairingConnectionTest, MultipleClientsOnePass) {
360     // Send multiple clients with bad passwords, but send the last one with the
361     // correct password.
362     std::vector<uint8_t> pswd{0x01, 0x03, 0x05, 0x07};
363     std::vector<uint8_t> pswd2{0x01, 0x03, 0x05, 0x06};
364 
365     auto server = createServer(pswd);
366     ASSERT_NE(nullptr, server);
367     // Start the server first, to open the port for connections
368     std::mutex server_mutex;
369     std::condition_variable server_cv;
370     std::unique_lock<std::mutex> server_lock(server_mutex);
371 
372     bool server_got_valid_pairing = false;
373     auto server_callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
374                                void* opaque) {
375         // Pairing will be cancelled, which should initiate this callback with
376         // empty values.
377 
378         ASSERT_NE(nullptr, peer_info);
379         ASSERT_NE(nullptr, cert);
380         EXPECT_FALSE(cert->empty());
381         EXPECT_EQ(nullptr, opaque);
382 
383         // Verify the peer_info and cert
384         ASSERT_EQ(strlen(peer_info->name), strlen(client_info_.name));
385         EXPECT_EQ(::memcmp(peer_info->name, client_info_.name, strlen(client_info_.name)), 0);
386         ASSERT_EQ(strlen(peer_info->guid), strlen(client_info_.guid));
387         EXPECT_EQ(::memcmp(peer_info->guid, client_info_.guid, strlen(client_info_.guid)), 0);
388         ASSERT_EQ(cert->size(), kTestClientCert.size() + 1);
389         EXPECT_EQ(::memcmp(cert->data(), kTestClientCert.data(), kTestClientCert.size() + 1), 0);
390 
391         std::lock_guard<std::mutex> lock(server_mutex);
392         server_got_valid_pairing = true;
393         server_cv.notify_one();
394     };
395     ASSERT_TRUE(server->start(server_callback, nullptr));
396 
397     // Start multiple clients, all with bad passwords (except for the last one)
398     std::vector<std::unique_ptr<PairingClient>> clients;
399     int num_clients_done = 0;
400     int test_num_clients = 5;
401     std::mutex client_mutex;
402     std::condition_variable client_cv;
403     std::unique_lock<std::mutex> client_lock(client_mutex);
404     bool got_valid_pairing = false;
405     while (clients.size() < test_num_clients) {
406         std::unique_ptr<PairingClient> client;
407         if (clients.size() == test_num_clients - 1) {
408             // Make this one have the valid password
409             client = createClient(pswd);
410             ASSERT_NE(nullptr, client);
411             auto callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
412                                 void* opaque) {
413                 ASSERT_NE(nullptr, peer_info);
414                 ASSERT_NE(nullptr, cert);
415                 EXPECT_FALSE(cert->empty());
416                 EXPECT_EQ(nullptr, opaque);
417 
418                 // Verify the peer_info and cert
419                 ASSERT_EQ(strlen(peer_info->name), strlen(server_info_.name));
420                 EXPECT_EQ(::memcmp(peer_info->name, server_info_.name, strlen(server_info_.name)),
421                           0);
422                 ASSERT_EQ(strlen(peer_info->guid), strlen(server_info_.guid));
423                 EXPECT_EQ(::memcmp(peer_info->guid, server_info_.guid, strlen(server_info_.guid)),
424                           0);
425                 ASSERT_EQ(cert->size(), kTestServerCert.size() + 1);
426                 EXPECT_EQ(
427                         ::memcmp(cert->data(), kTestServerCert.data(), kTestServerCert.size() + 1),
428                         0);
429                 got_valid_pairing = (peer_info != nullptr && cert != nullptr && !cert->empty());
430 
431                 {
432                     std::lock_guard<std::mutex> lock(client_mutex);
433                     num_clients_done++;
434                 }
435                 client_cv.notify_one();
436             };
437             ASSERT_TRUE(client->start(callback, nullptr));
438         } else {
439             client = createClient(pswd2);
440             ASSERT_NE(nullptr, client);
441             auto callback = [&](const PeerInfo* peer_info, const std::vector<uint8_t>* cert,
442                                 void* opaque) {
443                 ASSERT_EQ(nullptr, peer_info);
444                 ASSERT_EQ(nullptr, cert);
445                 EXPECT_EQ(nullptr, opaque);
446 
447                 {
448                     std::lock_guard<std::mutex> lock(client_mutex);
449                     num_clients_done++;
450                 }
451                 client_cv.notify_one();
452             };
453             ASSERT_TRUE(client->start(callback, nullptr));
454         }
455         clients.push_back(std::move(client));
456     }
457 
458     client_cv.wait(client_lock, [&]() { return (num_clients_done == test_num_clients); });
459     EXPECT_EQ(num_clients_done, test_num_clients);
460 
461     // Kill server if the pairing failed, since server only shuts down when
462     // it gets a valid pairing.
463     if (!got_valid_pairing) {
464         server_lock.unlock();
465         server_.reset();
466     } else {
467         server_cv.wait(server_lock);
468     }
469     EXPECT_TRUE(server_got_valid_pairing);
470 }
471 
472 }  // namespace pairing
473 }  // namespace adbwifi
474