Home | History | Annotate | Download | only in adb
      1 /*
      2  * Copyright (C) 2016 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 #include <unistd.h>
     19 #include <atomic>
     20 
     21 #include "adb_io.h"
     22 #include "sysdeps.h"
     23 
     24 static void increment_atomic_int(void* c) {
     25     sleep(1);
     26     reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
     27 }
     28 
     29 TEST(sysdeps_thread, smoke) {
     30     std::atomic<int> counter(0);
     31 
     32     for (int i = 0; i < 100; ++i) {
     33         ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter));
     34     }
     35 
     36     sleep(2);
     37     ASSERT_EQ(100, counter.load());
     38 }
     39 
     40 TEST(sysdeps_thread, join) {
     41     std::atomic<int> counter(0);
     42     std::vector<adb_thread_t> threads(500);
     43     for (size_t i = 0; i < threads.size(); ++i) {
     44         ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i]));
     45     }
     46 
     47     int current = counter.load();
     48     ASSERT_GE(current, 0);
     49     // Make sure that adb_thread_create actually creates threads, and doesn't do something silly
     50     // like synchronously run the function passed in. The sleep in increment_atomic_int should be
     51     // enough to keep this from being flakey.
     52     ASSERT_LT(current, 500);
     53 
     54     for (const auto& thread : threads) {
     55         ASSERT_TRUE(adb_thread_join(thread));
     56     }
     57 
     58     ASSERT_EQ(500, counter.load());
     59 }
     60 
     61 TEST(sysdeps_thread, exit) {
     62     adb_thread_t thread;
     63     ASSERT_TRUE(adb_thread_create(
     64         [](void*) {
     65             adb_thread_exit();
     66             for (;;) continue;
     67         },
     68         nullptr, &thread));
     69     ASSERT_TRUE(adb_thread_join(thread));
     70 }
     71 
     72 TEST(sysdeps_socketpair, smoke) {
     73     int fds[2];
     74     ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
     75     ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4));
     76     ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4));
     77 
     78     char buf[4];
     79     ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4));
     80     ASSERT_STREQ(buf, "foo");
     81     ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4));
     82     ASSERT_STREQ(buf, "bar");
     83     ASSERT_EQ(0, adb_close(fds[0]));
     84     ASSERT_EQ(0, adb_close(fds[1]));
     85 }
     86 
     87 TEST(sysdeps_fd, exhaustion) {
     88     std::vector<int> fds;
     89     int socketpair[2];
     90 
     91     while (adb_socketpair(socketpair) == 0) {
     92         fds.push_back(socketpair[0]);
     93         fds.push_back(socketpair[1]);
     94     }
     95 
     96     ASSERT_EQ(EMFILE, errno) << strerror(errno);
     97     for (int fd : fds) {
     98         ASSERT_EQ(0, adb_close(fd));
     99     }
    100     ASSERT_EQ(0, adb_socketpair(socketpair));
    101     ASSERT_EQ(socketpair[0], fds[0]);
    102     ASSERT_EQ(socketpair[1], fds[1]);
    103     ASSERT_EQ(0, adb_close(socketpair[0]));
    104     ASSERT_EQ(0, adb_close(socketpair[1]));
    105 }
    106 
    107 class sysdeps_poll : public ::testing::Test {
    108   protected:
    109     int fds[2];
    110     void SetUp() override {
    111         ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
    112     }
    113 
    114     void TearDown() override {
    115         if (fds[0] >= 0) {
    116             ASSERT_EQ(0, adb_close(fds[0]));
    117         }
    118         if (fds[1] >= 0) {
    119             ASSERT_EQ(0, adb_close(fds[1]));
    120         }
    121     }
    122 };
    123 
    124 TEST_F(sysdeps_poll, smoke) {
    125     adb_pollfd pfd[2] = {};
    126     pfd[0].fd = fds[0];
    127     pfd[0].events = POLLRDNORM;
    128     pfd[1].fd = fds[1];
    129     pfd[1].events = POLLWRNORM;
    130 
    131     pfd[0].revents = -1;
    132     pfd[1].revents = -1;
    133     EXPECT_EQ(1, adb_poll(pfd, 2, 0));
    134     EXPECT_EQ(0, pfd[0].revents);
    135     EXPECT_EQ(POLLWRNORM, pfd[1].revents);
    136 
    137     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
    138 
    139     // Wait for the socketpair to be flushed.
    140     pfd[0].revents = -1;
    141     EXPECT_EQ(1, adb_poll(pfd, 1, 100));
    142     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    143     pfd[0].revents = -1;
    144     pfd[1].revents = -1;
    145     EXPECT_EQ(2, adb_poll(pfd, 2, 0));
    146     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    147     EXPECT_EQ(POLLWRNORM, pfd[1].revents);
    148 }
    149 
    150 TEST_F(sysdeps_poll, timeout) {
    151     adb_pollfd pfd = {};
    152     pfd.fd = fds[0];
    153     pfd.events = POLLRDNORM;
    154 
    155     EXPECT_EQ(0, adb_poll(&pfd, 1, 100));
    156     EXPECT_EQ(0, pfd.revents);
    157 
    158     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
    159 
    160     EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
    161     EXPECT_EQ(POLLRDNORM, pfd.revents);
    162 }
    163 
    164 TEST_F(sysdeps_poll, invalid_fd) {
    165     adb_pollfd pfd[3] = {};
    166     pfd[0].fd = fds[0];
    167     pfd[0].events = POLLRDNORM;
    168     pfd[1].fd = INT_MAX;
    169     pfd[1].events = POLLRDNORM;
    170     pfd[2].fd = fds[1];
    171     pfd[2].events = POLLWRNORM;
    172 
    173     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
    174 
    175     // Wait for the socketpair to be flushed.
    176     EXPECT_EQ(1, adb_poll(pfd, 1, 100));
    177     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    178 
    179     EXPECT_EQ(3, adb_poll(pfd, 3, 0));
    180     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    181     EXPECT_EQ(POLLNVAL, pfd[1].revents);
    182     EXPECT_EQ(POLLWRNORM, pfd[2].revents);
    183 }
    184 
    185 TEST_F(sysdeps_poll, duplicate_fd) {
    186     adb_pollfd pfd[2] = {};
    187     pfd[0].fd = fds[0];
    188     pfd[0].events = POLLRDNORM;
    189     pfd[1] = pfd[0];
    190 
    191     EXPECT_EQ(0, adb_poll(pfd, 2, 0));
    192     EXPECT_EQ(0, pfd[0].revents);
    193     EXPECT_EQ(0, pfd[1].revents);
    194 
    195     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
    196 
    197     EXPECT_EQ(2, adb_poll(pfd, 2, 100));
    198     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
    199     EXPECT_EQ(POLLRDNORM, pfd[1].revents);
    200 }
    201 
    202 TEST_F(sysdeps_poll, disconnect) {
    203     adb_pollfd pfd = {};
    204     pfd.fd = fds[0];
    205     pfd.events = POLLIN;
    206 
    207     EXPECT_EQ(0, adb_poll(&pfd, 1, 0));
    208     EXPECT_EQ(0, pfd.revents);
    209 
    210     EXPECT_EQ(0, adb_close(fds[1]));
    211     fds[1] = -1;
    212 
    213     EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
    214 
    215     // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
    216     EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
    217 }
    218 
    219 TEST_F(sysdeps_poll, fd_count) {
    220     // https://code.google.com/p/android/issues/detail?id=12141
    221     static constexpr int num_sockets = 512;
    222     std::vector<int> sockets;
    223     std::vector<adb_pollfd> pfds;
    224     sockets.resize(num_sockets * 2);
    225     for (int32_t i = 0; i < num_sockets; ++i) {
    226         ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno);
    227         ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i)));
    228         adb_pollfd pfd;
    229         pfd.events = POLLIN;
    230         pfd.fd = sockets[i * 2 + 1];
    231         pfds.push_back(pfd);
    232     }
    233 
    234     ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0));
    235     for (int i = 0; i < num_sockets; ++i) {
    236         ASSERT_NE(0, pfds[i].revents & POLLIN);
    237 
    238         int32_t buf[2] = { -1, -1 };
    239         ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t)));
    240         ASSERT_EQ(i, buf[0]);
    241     }
    242 
    243     for (int fd : sockets) {
    244         adb_close(fd);
    245     }
    246 }
    247 
    248 #include "sysdeps/mutex.h"
    249 TEST(sysdeps_mutex, mutex_smoke) {
    250     static std::atomic<bool> finished(false);
    251     static std::mutex &m = *new std::mutex();
    252     m.lock();
    253     ASSERT_FALSE(m.try_lock());
    254     adb_thread_create([](void*) {
    255         ASSERT_FALSE(m.try_lock());
    256         m.lock();
    257         finished.store(true);
    258         adb_sleep_ms(200);
    259         m.unlock();
    260     }, nullptr);
    261 
    262     ASSERT_FALSE(finished.load());
    263     adb_sleep_ms(100);
    264     ASSERT_FALSE(finished.load());
    265     m.unlock();
    266     adb_sleep_ms(100);
    267     m.lock();
    268     ASSERT_TRUE(finished.load());
    269     m.unlock();
    270 }
    271 
    272 // Our implementation on Windows aborts on double lock.
    273 #if defined(_WIN32)
    274 TEST(sysdeps_mutex, mutex_reentrant_lock) {
    275     std::mutex &m = *new std::mutex();
    276 
    277     m.lock();
    278     ASSERT_FALSE(m.try_lock());
    279     EXPECT_DEATH(m.lock(), "non-recursive mutex locked reentrantly");
    280 }
    281 #endif
    282 
    283 TEST(sysdeps_mutex, recursive_mutex_smoke) {
    284     static std::recursive_mutex &m = *new std::recursive_mutex();
    285 
    286     m.lock();
    287     ASSERT_TRUE(m.try_lock());
    288     m.unlock();
    289 
    290     adb_thread_create([](void*) {
    291         ASSERT_FALSE(m.try_lock());
    292         m.lock();
    293         adb_sleep_ms(500);
    294         m.unlock();
    295     }, nullptr);
    296 
    297     adb_sleep_ms(100);
    298     m.unlock();
    299     adb_sleep_ms(100);
    300     ASSERT_FALSE(m.try_lock());
    301     m.lock();
    302     m.unlock();
    303 }
    304