summaryrefslogtreecommitdiffstats
path: root/ppapi
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-12 23:28:59 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-12 23:28:59 +0000
commitdb567f2bfd128fad15e389356dd99eebda704823 (patch)
treedff13ed32a994920019d80064824cafa72f74c7a /ppapi
parent59906589d90c082d91f268206eadaf6b71c1aad1 (diff)
downloadchromium_src-db567f2bfd128fad15e389356dd99eebda704823.zip
chromium_src-db567f2bfd128fad15e389356dd99eebda704823.tar.gz
chromium_src-db567f2bfd128fad15e389356dd99eebda704823.tar.bz2
Implement P2P Transport Dev using P2PTransportImpl.
BUG=None TEST=Unittests Review URL: http://codereview.chromium.org/6823021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81331 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r--ppapi/tests/test_case.cc1
-rw-r--r--ppapi/tests/test_case.h4
-rw-r--r--ppapi/tests/test_transport.cc203
-rw-r--r--ppapi/tests/test_transport.h17
-rw-r--r--ppapi/tests/test_utils.h2
5 files changed, 201 insertions, 26 deletions
diff --git a/ppapi/tests/test_case.cc b/ppapi/tests/test_case.cc
index ab54d51..bfcfa8c 100644
--- a/ppapi/tests/test_case.cc
+++ b/ppapi/tests/test_case.cc
@@ -9,6 +9,7 @@
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
+// static
std::string TestCase::MakeFailureMessage(const char* file,
int line,
const char* cmd) {
diff --git a/ppapi/tests/test_case.h b/ppapi/tests/test_case.h
index b06ecb8..9ccde68 100644
--- a/ppapi/tests/test_case.h
+++ b/ppapi/tests/test_case.h
@@ -34,7 +34,8 @@ class TestCase {
// first displayed.
virtual void RunTest() = 0;
- std::string MakeFailureMessage(const char* file, int line, const char* cmd);
+ static std::string MakeFailureMessage(const char* file, int line,
+ const char* cmd);
// Returns the scriptable test object for the current test, if any.
// Internally, this uses CreateTestObject which each test overrides.
@@ -129,4 +130,3 @@ class TestCaseFactory {
#define PASS() return std::string()
#endif // PPAPI_TESTS_TEST_CASE_H_
-
diff --git a/ppapi/tests/test_transport.cc b/ppapi/tests/test_transport.cc
index 4035676..bcf9701 100644
--- a/ppapi/tests/test_transport.cc
+++ b/ppapi/tests/test_transport.cc
@@ -4,9 +4,11 @@
#include "ppapi/tests/test_transport.h"
+#include <stdlib.h>
#include <string.h>
-#include <string>
+#include <list>
+#include <map>
#include "ppapi/c/dev/ppb_testing_dev.h"
#include "ppapi/c/pp_errors.h"
@@ -19,6 +21,72 @@
REGISTER_TEST_CASE(Transport);
+#define RUN_SUBTEST(function) { \
+ std::string result = function(); \
+ if (!result.empty()) \
+ return result; \
+ }
+
+namespace {
+
+const char kTestChannelName[] = "test";
+const int kNumPackets = 100;
+const int kSendBufferSize = 1200;
+const int kReadBufferSize = 65536;
+
+class StreamReader {
+ public:
+ explicit StreamReader(pp::Transport_Dev* transport)
+ : ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)),
+ transport_(transport),
+ errors_(0) {
+ Read();
+ }
+
+ const std::list<std::vector<char> >& received() { return received_; }
+ std::list<std::string> errors() { return errors_; }
+
+ private:
+ void Read() {
+ while (true) {
+ buffer_.resize(kReadBufferSize);
+ int result = transport_->Recv(
+ &buffer_[0], buffer_.size(),
+ callback_factory_.NewCallback(&StreamReader::OnReadFinished));
+ if (result > 0)
+ DidFinishRead(result);
+ else
+ break;
+ }
+ }
+
+ void OnReadFinished(int result) {
+ DidFinishRead(result);
+ if (result > 0)
+ Read();
+ }
+
+ void DidFinishRead(int result) {
+ if (result > 0) {
+ if (result > static_cast<int>(buffer_.size())) {
+ errors_.push_back(TestCase::MakeFailureMessage(
+ __FILE__, __LINE__,
+ "Recv() returned value that is bigger than the buffer."));
+ }
+ buffer_.resize(result);
+ received_.push_back(buffer_);
+ }
+ }
+
+ pp::CompletionCallbackFactory<StreamReader> callback_factory_;
+ pp::Transport_Dev* transport_;
+ std::vector<char> buffer_;
+ std::list<std::vector<char> > received_;
+ std::list<std::string> errors_;
+};
+
+} // namespace
+
bool TestTransport::Init() {
transport_interface_ = reinterpret_cast<PPB_Transport_Dev const*>(
pp::Module::Get()->GetBrowserInterface(PPB_TRANSPORT_DEV_INTERFACE));
@@ -26,42 +94,135 @@ bool TestTransport::Init() {
}
void TestTransport::RunTest() {
- RUN_TEST(Basics);
- // TODO(juberti): more Transport tests here...
+ RUN_TEST(Create);
+ RUN_TEST(Connect);
+ RUN_TEST(SendData);
+ RUN_TEST(ConnectAndClose);
}
-std::string TestTransport::TestBasics() {
- pp::Transport_Dev transport1(instance_, "test", "");
- pp::Transport_Dev transport2(instance_, "test", "");
+std::string TestTransport::InitTargets() {
+ transport1_.reset(new pp::Transport_Dev(instance_, kTestChannelName, ""));
+ transport2_.reset(new pp::Transport_Dev(instance_, kTestChannelName, ""));
+
+ ASSERT_TRUE(transport1_.get() != NULL);
+ ASSERT_TRUE(transport2_.get() != NULL);
+ PASS();
+}
+
+std::string TestTransport::Connect() {
TestCompletionCallback connect_cb1(instance_->pp_instance());
TestCompletionCallback connect_cb2(instance_->pp_instance());
- ASSERT_EQ(transport1.Connect(connect_cb1), PP_OK_COMPLETIONPENDING);
- ASSERT_EQ(transport2.Connect(connect_cb2), PP_OK_COMPLETIONPENDING);
+ ASSERT_EQ(transport1_->Connect(connect_cb1), PP_OK_COMPLETIONPENDING);
+ ASSERT_EQ(transport2_->Connect(connect_cb2), PP_OK_COMPLETIONPENDING);
pp::Var address1;
pp::Var address2;
TestCompletionCallback next_address_cb1(instance_->pp_instance());
TestCompletionCallback next_address_cb2(instance_->pp_instance());
- ASSERT_EQ(transport1.GetNextAddress(&address1, next_address_cb1),
+ ASSERT_EQ(transport1_->GetNextAddress(&address1, next_address_cb1),
PP_OK_COMPLETIONPENDING);
- ASSERT_EQ(transport2.GetNextAddress(&address2, next_address_cb2),
+ ASSERT_EQ(transport2_->GetNextAddress(&address2, next_address_cb2),
PP_OK_COMPLETIONPENDING);
- ASSERT_EQ(next_address_cb1.WaitForResult(), 0);
- ASSERT_EQ(next_address_cb2.WaitForResult(), 0);
- ASSERT_EQ(transport1.GetNextAddress(&address1, next_address_cb1), PP_OK);
- ASSERT_EQ(transport2.GetNextAddress(&address2, next_address_cb2), PP_OK);
+ ASSERT_EQ(next_address_cb1.WaitForResult(), PP_OK);
+ ASSERT_EQ(next_address_cb2.WaitForResult(), PP_OK);
+ ASSERT_EQ(transport1_->GetNextAddress(&address1, next_address_cb1), PP_OK);
+ ASSERT_EQ(transport2_->GetNextAddress(&address2, next_address_cb2), PP_OK);
+
+ ASSERT_EQ(transport1_->ReceiveRemoteAddress(address2), PP_OK);
+ ASSERT_EQ(transport2_->ReceiveRemoteAddress(address1), PP_OK);
+
+ ASSERT_EQ(connect_cb1.WaitForResult(), PP_OK);
+ ASSERT_EQ(connect_cb2.WaitForResult(), PP_OK);
+
+ ASSERT_TRUE(transport1_->IsWritable());
+ ASSERT_TRUE(transport2_->IsWritable());
+
+ PASS();
+}
+
+std::string TestTransport::Clean() {
+ transport1_.reset();
+ transport2_.reset();
+
+ PASS();
+}
+
+std::string TestTransport::TestCreate() {
+ RUN_SUBTEST(InitTargets);
+
+ Clean();
+
+ PASS();
+}
+
+std::string TestTransport::TestConnect() {
+ RUN_SUBTEST(InitTargets);
+ RUN_SUBTEST(Connect);
+
+ Clean();
+
+ PASS();
+}
+
+std::string TestTransport::TestSendData() {
+ RUN_SUBTEST(InitTargets);
+ RUN_SUBTEST(Connect);
+
+ StreamReader reader(transport1_.get());
+
+ std::map<int, std::vector<char> > sent_packets;
+ for (int i = 0; i < kNumPackets; ++i) {
+ std::vector<char> send_buffer(kSendBufferSize);
+ for (size_t j = 0; j < send_buffer.size(); ++j) {
+ send_buffer[j] = rand() % 256;
+ }
+ // Put packet index in the beginning.
+ memcpy(&send_buffer[0], &i, sizeof(i));
+
+ TestCompletionCallback send_cb(instance_->pp_instance());
+ ASSERT_EQ(
+ transport2_->Send(&send_buffer[0], send_buffer.size(), send_cb),
+ static_cast<int>(send_buffer.size()));
+ sent_packets[i] = send_buffer;
+ }
+
+ // Wait for 1 second.
+ TestCompletionCallback wait_cb(instance_->pp_instance());
+ pp::Module::Get()->core()->CallOnMainThread(1000, wait_cb);
+ ASSERT_EQ(wait_cb.WaitForResult(), PP_OK);
+
+ ASSERT_TRUE(reader.errors().size() == 0);
+ ASSERT_TRUE(reader.received().size() > 0);
+ for (std::list<std::vector<char> >::const_iterator it =
+ reader.received().begin(); it != reader.received().end(); ++it) {
+ int index;
+ memcpy(&index, &(*it)[0], sizeof(index));
+ ASSERT_TRUE(sent_packets[index] == *it);
+ }
+
+ Clean();
+
+ PASS();
+}
+
+std::string TestTransport::TestConnectAndClose() {
+ RUN_SUBTEST(InitTargets);
+ RUN_SUBTEST(Connect);
- ASSERT_EQ(transport1.ReceiveRemoteAddress(address2), 0);
- ASSERT_EQ(transport2.ReceiveRemoteAddress(address1), 0);
+ std::vector<char> recv_buffer(kReadBufferSize);
+ TestCompletionCallback recv_cb(instance_->pp_instance());
+ ASSERT_EQ(
+ transport1_->Recv(&recv_buffer[0], recv_buffer.size(), recv_cb),
+ PP_OK_COMPLETIONPENDING);
- ASSERT_EQ(connect_cb1.WaitForResult(), 0);
- ASSERT_EQ(connect_cb2.WaitForResult(), 0);
+ // Close the transport and verify that callback is aborted.
+ ASSERT_EQ(transport1_->Close(), PP_OK);
- ASSERT_TRUE(transport1.IsWritable());
- ASSERT_TRUE(transport2.IsWritable());
+ ASSERT_EQ(recv_cb.run_count(), 1);
+ ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED);
- // TODO(sergeyu): Verify that data can be sent/received.
+ Clean();
PASS();
}
diff --git a/ppapi/tests/test_transport.h b/ppapi/tests/test_transport.h
index 43e4306..f883b6a 100644
--- a/ppapi/tests/test_transport.h
+++ b/ppapi/tests/test_transport.h
@@ -7,13 +7,14 @@
#include <string>
+#include "base/scoped_ptr.h"
#include "ppapi/tests/test_case.h"
struct PPB_Transport_Dev;
namespace pp {
-class Transport;
-}
+class Transport_Dev;
+} // namespace pp
class TestTransport : public TestCase {
public:
@@ -24,10 +25,20 @@ class TestTransport : public TestCase {
virtual void RunTest();
private:
- std::string TestBasics();
+ std::string InitTargets();
+ std::string Connect();
+ std::string Clean();
+
+ std::string TestCreate();
+ std::string TestConnect();
+ std::string TestSendData();
+ std::string TestConnectAndClose();
// Used by the tests that access the C API directly.
const PPB_Transport_Dev* transport_interface_;
+
+ scoped_ptr<pp::Transport_Dev> transport1_;
+ scoped_ptr<pp::Transport_Dev> transport2_;
};
#endif // PPAPI_TESTS_TEST_TRANSPORT_H_
diff --git a/ppapi/tests/test_utils.h b/ppapi/tests/test_utils.h
index 6fde9bf..b71cc05 100644
--- a/ppapi/tests/test_utils.h
+++ b/ppapi/tests/test_utils.h
@@ -26,6 +26,8 @@ class TestCompletionCallback {
unsigned run_count() const { return run_count_; }
void reset_run_count() { run_count_ = 0; }
+ int32_t result() const { return result_; }
+
private:
static void Handler(void* user_data, int32_t result);