Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Unified Diff: net/socket/ssl_server_socket_unittest.cc

Issue 7399025: Fix instability in SSL client/server sockets (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update FakeSocketTest. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/socket/ssl_server_socket_unittest.cc
diff --git a/net/socket/ssl_server_socket_unittest.cc b/net/socket/ssl_server_socket_unittest.cc
index de89c207117b900a92f2f4b89a90a12ea692821f..457ee134708497b7301d5c373fee2d26890e3039 100644
--- a/net/socket/ssl_server_socket_unittest.cc
+++ b/net/socket/ssl_server_socket_unittest.cc
@@ -15,10 +15,13 @@
#include "net/socket/ssl_server_socket.h"
+#include <stdlib.h>
+
#include <queue>
#include "base/file_path.h"
#include "base/file_util.h"
+#include "base/message_loop.h"
#include "base/path_service.h"
#include "crypto/nss_util.h"
#include "crypto/rsa_private_key.h"
@@ -46,7 +49,10 @@ namespace {
class FakeDataChannel {
public:
- FakeDataChannel() : read_callback_(NULL), read_buf_len_(0) {
+ FakeDataChannel()
+ : read_callback_(NULL),
+ read_buf_len_(0),
+ ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) {
willchan no longer on Chromium 2011/07/21 08:16:06 Need compiler_specific.h for this macro.
Sergey Ulanov 2011/07/21 17:11:12 Done.
}
virtual int Read(IOBuffer* buf, int buf_len,
@@ -63,13 +69,15 @@ class FakeDataChannel {
virtual int Write(IOBuffer* buf, int buf_len,
CompletionCallback* callback) {
data_.push(new net::DrainableIOBuffer(buf, buf_len));
- DoReadCallback();
+ MessageLoop::current()->PostTask(
+ FROM_HERE, task_factory_.NewRunnableMethod(
+ &FakeDataChannel::DoReadCallback));
wtc 2011/07/21 00:27:12 willchan: can you review the use of ScopedRunnable
Sergey Ulanov 2011/07/21 00:51:15 Because DoReadCallback() calls callback on the soc
willchan no longer on Chromium 2011/07/21 08:16:06 Looks reasonable to me.
return buf_len;
}
private:
void DoReadCallback() {
- if (!read_callback_)
+ if (!read_callback_ || !data_.size())
wtc 2011/07/21 00:27:12 Nit: some people prefer data_.empty(). Why do you
Sergey Ulanov 2011/07/21 00:51:15 Done.
return;
int copied = PropogateData(read_buf_, read_buf_len_);
@@ -97,6 +105,8 @@ class FakeDataChannel {
std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
+ ScopedRunnableMethodFactory<FakeDataChannel> task_factory_;
+
DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
};
@@ -114,11 +124,15 @@ class FakeSocket : public StreamSocket {
virtual int Read(IOBuffer* buf, int buf_len,
CompletionCallback* callback) {
+ // Read random number of bytes.
+ buf_len = rand() % buf_len + 1;
return incoming_->Read(buf, buf_len, callback);
}
virtual int Write(IOBuffer* buf, int buf_len,
CompletionCallback* callback) {
+ // Write random number of bytes.
+ buf_len = rand() % buf_len + 1;
wtc 2011/07/21 00:27:12 Are you using rand() instead of base::RandInt() be
return outgoing_->Write(buf, buf_len, callback);
}
@@ -204,17 +218,28 @@ TEST(FakeSocketTest, DataTransfer) {
scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
// Write then read.
- EXPECT_EQ(kTestDataSize, server.Write(write_buf, kTestDataSize, NULL));
- EXPECT_EQ(kTestDataSize, client.Read(read_buf, kReadBufSize, NULL));
- EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
+ int written = server.Write(write_buf, kTestDataSize, NULL);
+ EXPECT_GT(written, 0);
+ EXPECT_LE(written, kTestDataSize);
+
+ int read = client.Read(read_buf, kReadBufSize, NULL);
wtc 2011/07/21 00:27:12 Nit: name these local variables nwritten and nread
+ EXPECT_GT(read, 0);
+ EXPECT_LE(read, written);
+ EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
// Read then write.
TestCompletionCallback callback;
EXPECT_EQ(net::ERR_IO_PENDING,
server.Read(read_buf, kReadBufSize, &callback));
- EXPECT_EQ(kTestDataSize, client.Write(write_buf, kTestDataSize, NULL));
- EXPECT_EQ(kTestDataSize, callback.WaitForResult());
- EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize));
+
+ written = client.Write(write_buf, kTestDataSize, NULL);
+ EXPECT_GT(written, 0);
+ EXPECT_LE(written, kTestDataSize);
+
+ read = callback.WaitForResult();
+ EXPECT_GT(read, 0);
+ EXPECT_LE(read, written);
+ EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
}
class SSLServerSocketTest : public PlatformTest {

Powered by Google App Engine
This is Rietveld 408576698