// Copyright (C) 2021 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
TEST_F(PosixSocketTest, socketSendDoesNotGenerateSigPipe) { // Check that writing to a broken pipe does not generate a SIGPIPE // signal.
SigPipeSignalHandler handler;
ASSERT_EQ(-1, handler.signaled()); auto [sock1, sock2] = connectPair();
// s1 and s2 are now connected. Close s1 immediately, then try to // send data through s2.
sock1->Close();
ASSERT_FALSE(sock1->Connected()); // The EPIPE might not happen on the first send due to // TCP packet buffering in the kernel. Perform multiple send() // in a loop to work-around this.
errno = 0; constint kMaxSendCount = 1000; int n = 0; while (n < kMaxSendCount) { int ret = sock2->Send((uint8_t*)"xxxx", 4); if (ret < 0) { #ifdef __APPLE__ // On OS X, errno is sometimes EPROTOTYPE instead of EPIPE // when this happens.
ASSERT_TRUE(errno == EPIPE || errno == EPROTOTYPE) << strerror(errno); #else
ASSERT_EQ(EPIPE, errno) << strerror(errno); #endif break;
}
n++;
}
// On MacOS you usually have n < 30
ASSERT_LT(n, kMaxSendCount);
// No signals were raised.
ASSERT_EQ(-1, handler.signaled());
}
TEST_F(PosixSocketTest, can_send_data_around_poll) { auto [sock1, sock2] = connectPair();
std::string word = "Hello World";
std::string input = " ";
uint8_t* buffer = (uint8_t*)input.data(); int buflen = input.size();
// Poll for at most 250ms.
clock::time_point until = clock::now() + 250ms; do { int recv = sock2->Recv(buffer, buflen); if (recv > 0) {
buflen -= recv;
buffer += recv;
}
} while (buflen > 0 && clock::now() < until);
ASSERT_EQ(word, input);
}
TEST_F(PosixSocketTest, data_results_in_read_event) { auto [sock1, sock2] = connectPair();
std::mutex m;
std::condition_variable cv;
std::string word = "Hello World";
std::string input = " ";
bool received = false;
// Register a callback that only gets called once..
sock2->WatchForNonBlockingRead([&](auto sock) {
std::unique_lock<std::mutex> guard(m);
received = true; // Unregister, to prevent surprises..
sock->StopWatching();
cv.notify_all();
});
// The callback will be called within 250ms.
ASSERT_TRUE(cv.wait_for(lk, 250ms, [&] { return received; }));
uint8_t* buffer = (uint8_t*)input.data(); int buflen = input.size();
// At least 1 byte is coming in. (Note, we might get just a few // bytes. vs the whole thing as you never know what happens in the // ip stack.)
ASSERT_GT(sock2->Recv(buffer, buflen), 0);
}
}
TEST_F(PosixSocketTest, connectFails) { int port = pass_.port();
// Close the port, we should not be able to connect
pass_.Close();
ASSERT_FALSE(pass_.Connected());
// Max 250ms to go to nowhere... auto socket = pasc_.ConnectToRemoteServer("localhost", port, 250ms);
ASSERT_FALSE(socket->Connected());
}
TEST_F(PosixSocketTest, canConnectMultiple) { int port = pass_.port(); int CONNECTION_COUNT = 10;
std::mutex m;
std::condition_variable cv;
std::vector<std::shared_ptr<AsyncDataChannel>> connections; bool connected = false;
// After the first connection there was no call to startListening, and hence // no new sockets should be accepted.
{
connected = false; auto socket = pasc_.ConnectToRemoteServer("localhost", port, 250ms);
// We should have a partial connection, so we don't know yet that it is not // working..
ASSERT_TRUE(socket->Connected());
std::unique_lock<std::mutex> lk(m);
// Should timeout, as we never invoke the callback that accepts the socket.
ASSERT_FALSE(cv.wait_for(lk, 250ms, [&] { return connected; }));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.