⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 session_unittest.cc

📁 本人收集整理的一份c/c++跨平台网络库
💻 CC
📖 第 1 页 / 共 3 页
字号:
  manhandler2->CheckNoStanza();  // Deliver the transport-accept if one exists.  if (stanza1.get() != NULL) {    manager1->OnIncomingMessage(stanza1.get());    stanza1.reset(manhandler1->CheckNextStanza(      "<cli:iq to=\"bar@baz.com\" id=\"2\" type=\"result\" from=\"foo@baz.com\""      " xmlns:cli=\"jabber:client\"/>"));    manhandler1->CheckNoStanza();    // The first session should now have a transport.    ASSERT(session1->transport() != NULL);    ASSERT(session1->transport()->name() == kNsP2pTransport);  }  // Deliver the candidates.  If client2 is old (or is acting old because  // client1 is), then client1 will correct its earlier mistake of sending  // transport-info by sending a candidates message.  If client1 is supposed to  // be old, then it sent candidates earlier, so we drop this.  manager1->OnIncomingMessage(stanza2.get());  if (old1 || old2)  {    stanza2.reset(manhandler1->CheckNextStanza(      "<cli:iq to=\"bar@baz.com\" type=\"set\" from=\"foo@baz.com\" id=\"4\""      " xmlns:cli=\"jabber:client\">"      "<session xmlns=\"http://www.google.com/session\" type=\"candidates\""      " id=\"2154761789\" initiator=\"foo@baz.com\">"      "<candidate name=\"a\" address=\"127.0.0.1\" port=\"28653\""      " preference=\"1\" username=\"h0ISP4S5SJKH/9EY\" protocol=\"udp\""      " generation=\"0\" password=\"UhnAmO5C89dD2dZ+\" type=\"local\""      " network=\"network\"/>"      "<candidate name=\"a\" address=\"127.0.0.1\" port=\"28658\""      " preference=\"1\" username=\"yid4vfB3zXPvrRB9\" protocol=\"udp\""      " generation=\"0\" password=\"SqLXTvcEyriIo+Mj\" type=\"local\""      " network=\"network\"/>"      "<candidate name=\"b\" address=\"127.0.0.1\" port=\"28663\""      " preference=\"1\" username=\"NvT78D7WxPWM1KL8\" protocol=\"udp\""      " generation=\"0\" password=\"+mV/QhOapXu4caPX\" type=\"local\""      " network=\"network\"/>"      "<candidate name=\"b\" address=\"127.0.0.1\" port=\"28668\""      " preference=\"1\" username=\"8EzB7MH+TYpIlSp/\" protocol=\"udp\""      " generation=\"0\" password=\"h+MelLXupoK5aYqC\" type=\"local\""      " network=\"network\"/>"      "</session>"      "</cli:iq>"));  } else {    GetNextID();  // Advance the ID count to be the same in all cases.    stanza2.reset(NULL);  }  if (old1) {    stanza2.reset(NULL);  }  stanza1.reset(manhandler1->CheckNextStanza(    "<cli:iq to=\"bar@baz.com\" id=\"3\" type=\"result\" from=\"foo@baz.com\""    " xmlns:cli=\"jabber:client\"/>"));  manhandler1->CheckNoStanza();  // The first session must have a transport in either case now.  ASSERT(session1->transport() != NULL);  ASSERT(session1->transport()->name() == kNsP2pTransport);  // If client1 just generated a candidates message, then we must deliver it.  if (stanza2.get() != NULL) {    manager2->OnIncomingMessage(stanza2.get());    stanza2.reset(manhandler2->CheckNextStanza(      "<cli:iq to=\"foo@baz.com\" id=\"4\" type=\"result\" from=\"bar@baz.com\""      " xmlns:cli=\"jabber:client\"/>"));    manhandler2->CheckNoStanza();  }  // The channels should be able to become writable at this point.  This  // requires pinging, so it may take a little while.  signaling_thread->ProcessMessages(500);  ASSERT(chan1a->writable() && chan1a->readable());  ASSERT(chan1b->writable() && chan1b->readable());  ASSERT(chan2a->writable() && chan2a->readable());  ASSERT(chan2b->writable() && chan2b->readable());  ASSERT(chanhandler1a->last_writable);  ASSERT(chanhandler1b->last_writable);  ASSERT(chanhandler2a->last_writable);  ASSERT(chanhandler2b->last_writable);  // Accept the session.  TestAccept(signaling_thread, session1, session2,             handler1.get(), handler2.get(),             manager1.get(), manager2.get(),             manhandler1.get(), manhandler2.get());  // Send a bunch of data between them.  TestSendRecv(chanhandler1a.get(), chanhandler1b.get(), chanhandler2a.get(),               chanhandler2b.get(), signaling_thread, false);  manager1->DestroySession(session1);  manager2->DestroySession(session2);  ASSERT(manhandler1->create_count == 1);  ASSERT(manhandler2->create_count == 1);  ASSERT(manhandler1->destroy_count == 1);  ASSERT(manhandler2->destroy_count == 1);  worker_thread->Stop();  std::cout << "P2P Compatibility: " << test_name << ": PASS" << std::endl;}// Tests the P2P transport.  The flags indicate whether they clients will// advertise support for raw as well.void TestP2P(const std::string& test_name, bool raw1, bool raw2) {  InitTest();  talk_base::Thread* signaling_thread = talk_base::Thread::Current();  scoped_ptr<talk_base::Thread> worker_thread(new talk_base::Thread());  worker_thread->Start();  scoped_ptr<PortAllocator> allocator(      new TestPortAllocator(worker_thread.get(), NULL));  scoped_ptr<MySessionClient> client1(new MySessionClient());  client1->SetTransports(true, raw1);  scoped_ptr<MySessionClient> client2(new MySessionClient());  client2->SetTransports(true, raw2);  scoped_ptr<SessionManager> manager1(      new SessionManager(allocator.get(), worker_thread.get()));  scoped_ptr<SessionManagerHandler> manhandler1(      new SessionManagerHandler(manager1.get(), "foo@baz.com"));  client1->AddManager(manager1.get());  Session* session1 = manager1->CreateSession("foo@baz.com", kSessionType);  ASSERT(manhandler1->create_count == 1);  ASSERT(manhandler1->last_id == session1->id());  scoped_ptr<SessionHandler> handler1(new SessionHandler(session1));  ASSERT(client1->create_count == 1);  TransportChannel* chan1a = client1->a;  ASSERT(chan1a->name() == "a");  ASSERT(session1->GetChannel("a") == chan1a);  scoped_ptr<ChannelHandler> chanhandler1a(new ChannelHandler(chan1a));  TransportChannel* chan1b = client1->b;  ASSERT(chan1b->name() == "b");  ASSERT(session1->GetChannel("b") == chan1b);  scoped_ptr<ChannelHandler> chanhandler1b(new ChannelHandler(chan1b));  SessionDescription* desc1 = new SessionDescription();  ASSERT(session1->state() == Session::STATE_INIT);  bool valid = session1->Initiate("bar@baz.com", NULL, desc1);  ASSERT(valid);  handler1->PrepareTransport();  signaling_thread->ProcessMessages(100);  ASSERT(handler1->last_state == Session::STATE_SENTINITIATE);  scoped_ptr<XmlElement> stanza1, stanza2;  if (raw1) {    stanza1.reset(manhandler1->CheckNextStanza(      "<cli:iq to=\"bar@baz.com\" type=\"set\" from=\"foo@baz.com\" id=\"0\""      " xmlns:cli=\"jabber:client\">"      "<session xmlns=\"http://www.google.com/session\" type=\"initiate\""      " id=\"2154761789\" initiator=\"foo@baz.com\">"      "<ses:description xmlns:ses=\"http://oink.splat/session\"/>"      "<p:transport xmlns:p=\"http://www.google.com/transport/p2p\"/>"      "<raw:transport xmlns:raw=\"http://www.google.com/transport/raw\"/>"      "</session>"      "</cli:iq>"));  } else {    stanza1.reset(manhandler1->CheckNextStanza(      "<cli:iq to=\"bar@baz.com\" type=\"set\" from=\"foo@baz.com\" id=\"0\""      " xmlns:cli=\"jabber:client\">"      "<session xmlns=\"http://www.google.com/session\" type=\"initiate\""      " id=\"2154761789\" initiator=\"foo@baz.com\">"      "<ses:description xmlns:ses=\"http://oink.splat/session\"/>"      "<p:transport xmlns:p=\"http://www.google.com/transport/p2p\"/>"      "</session>"      "</cli:iq>"));  }  stanza2.reset(manhandler1->CheckNextStanza(    "<cli:iq to=\"bar@baz.com\" type=\"set\" from=\"foo@baz.com\" id=\"1\""    " xmlns:cli=\"jabber:client\">"    "<session xmlns=\"http://www.google.com/session\" type=\"transport-info\""    " id=\"2154761789\" initiator=\"foo@baz.com\">"    "<p:transport xmlns:p=\"http://www.google.com/transport/p2p\">"    "<candidate name=\"a\" address=\"127.0.0.1\" port=\"28653\""    " preference=\"1\" username=\"h0ISP4S5SJKH/9EY\" protocol=\"udp\""    " generation=\"0\" password=\"UhnAmO5C89dD2dZ+\" type=\"local\""    " network=\"network\"/>"    "<candidate name=\"a\" address=\"127.0.0.1\" port=\"28658\""    " preference=\"1\" username=\"yid4vfB3zXPvrRB9\" protocol=\"udp\""    " generation=\"0\" password=\"SqLXTvcEyriIo+Mj\" type=\"local\""    " network=\"network\"/>"    "<candidate name=\"b\" address=\"127.0.0.1\" port=\"28663\""    " preference=\"1\" username=\"NvT78D7WxPWM1KL8\" protocol=\"udp\""    " generation=\"0\" password=\"+mV/QhOapXu4caPX\" type=\"local\""    " network=\"network\"/>"    "<candidate name=\"b\" address=\"127.0.0.1\" port=\"28668\""    " preference=\"1\" username=\"8EzB7MH+TYpIlSp/\" protocol=\"udp\""    " generation=\"0\" password=\"h+MelLXupoK5aYqC\" type=\"local\""    " network=\"network\"/>"    "</p:transport>"    "</session>"    "</cli:iq>"));  manhandler1->CheckNoStanza();  scoped_ptr<SessionManager> manager2(      new SessionManager(allocator.get(), worker_thread.get()));  scoped_ptr<SessionManagerHandler> manhandler2(      new SessionManagerHandler(manager2.get(), "bar@baz.com"));  client2->AddManager(manager2.get());  // Deliver the initiate.  manager2->OnIncomingMessage(stanza1.get());  stanza1.reset(manhandler2->CheckNextStanza(    "<cli:iq to=\"foo@baz.com\" id=\"0\" type=\"result\" from=\"bar@baz.com\""    " xmlns:cli=\"jabber:client\"/>"));  stanza1.reset(manhandler2->CheckNextStanza(    "<cli:iq to=\"foo@baz.com\" type=\"set\" from=\"bar@baz.com\" id=\"2\""    " xmlns:cli=\"jabber:client\">"    "<session xmlns=\"http://www.google.com/session\""    " type=\"transport-accept\" id=\"2154761789\" initiator=\"foo@baz.com\">"    "<p:transport xmlns:p=\"http://www.google.com/transport/p2p\"/>"    "</session>"    "</cli:iq>"));  manhandler2->CheckNoStanza();  ASSERT(manhandler2->create_count == 1);  ASSERT(manhandler2->last_id == session1->id());  Session* session2 = manager2->GetSession(session1->id());  ASSERT(session2);  ASSERT(session1->id() == session2->id());  ASSERT(manhandler2->last_id == session2->id());  ASSERT(session2->state() == Session::STATE_RECEIVEDINITIATE);  scoped_ptr<SessionHandler> handler2(new SessionHandler(session2));  handler2->PrepareTransport();  ASSERT(session2->name() == session1->remote_name());  ASSERT(session1->name() == session2->remote_name());  ASSERT(session2->transport() != NULL);  ASSERT(session2->transport()->name() == kNsP2pTransport);  ASSERT(client2->create_count == 1);  TransportChannel* chan2a = client2->a;  scoped_ptr<ChannelHandler> chanhandler2a(new ChannelHandler(chan2a));  TransportChannel* chan2b = client2->b;  scoped_ptr<ChannelHandler> chanhandler2b(new ChannelHandler(chan2b));  // Deliver the candidates.  manager2->OnIncomingMessage(stanza2.get());  stanza2.reset(manhandler2->CheckNextStanza(    "<cli:iq to=\"foo@baz.com\" id=\"1\" type=\"result\" from=\"bar@baz.com\""    " xmlns:cli=\"jabber:client\"/>"));  signaling_thread->ProcessMessages(10);  stanza2.reset(manhandler2->CheckNextStanza(    "<cli:iq to=\"foo@baz.com\" type=\"set\" from=\"bar@baz.com\" id=\"3\""    " xmlns:cli=\"jabber:client\">"    "<session xmlns=\"http://www.google.com/session\" type=\"transport-info\""    " id=\"2154761789\" initiator=\"foo@baz.com\">"    "<p:transport xmlns:p=\"http://www.google.com/transport/p2p\">"    "<candidate name=\"a\" address=\"127.0.0.1\" port=\"28673\""    " preference=\"1\" username=\"FJDz3iuXjbQJDRjs\" protocol=\"udp\""    " generation=\"0\" password=\"Ca5daV9m6G91qhlM\" type=\"local\""    " network=\"network\"/>"    "<candidate name=\"a\" address=\"127.0.0.1\" port=\"28678\""    " preference=\"1\" username=\"xlN53r3Jn/R5XuCt\" protocol=\"udp\""    " generation=\"0\" password=\"rgik2pKsjaPSUdJd\" type=\"local\""    " network=\"network\"/>"    "<candidate name=\"b\" address=\"127.0.0.1\" port=\"28683\""    " preference=\"1\" username=\"IBZ8CSq8ot2+pSMp\" protocol=\"udp\""    " generation=\"0\" password=\"i7RcDsGntMI6fzdd\" type=\"local\""    " network=\"network\"/>"    "<candidate name=\"b\" address=\"127.0.0.1\" port=\"28688\""    " preference=\"1\" username=\"SEtih9PYtMHCAlMI\" protocol=\"udp\""    " generation=\"0\" password=\"wROrHJ3+gDxUUMp1\" type=\"local\""    " network=\"network\"/>"    "</p:transport>"    "</session>"    "</cli:iq>"));  manhandler2->CheckNoStanza();  // Deliver the transport-accept.  manager1->OnIncomingMessage(stanza1.get());  stanza1.reset(manhandler1->CheckNextStanza(    "<cli:iq to=\"bar@baz.com\" id=\"2\" type=\"result\" from=\"foo@baz.com\""    " xmlns:cli=\"jabber:client\"/>"));  manhandler1->CheckNoStanza();  // The first session should now have a transport.  ASSERT(session1->transport() != NULL);  ASSERT(session1->transport()->name() == kNsP2pTransport);  // Deliver the candidates.  manager1->OnIncomingMessage(stanza2.get());  stanza1.reset(manhandler1->CheckNextStanza(    "<cli:iq to=\"bar@baz.com\" id=\"3\" type=\"result\" from=\"foo@baz.com\""    " xmlns:cli=\"jabber:client\"/>"));  manhandler1->CheckNoStanza();  // The channels should be able to become writable at this point.  This  // requires pinging, so it may take a little while.  signaling_thread->ProcessMessages(500);  ASSERT(chan1a->writable() && chan1a->readable());  ASSERT(chan1b->writable() && chan1b->readable());  ASSERT(chan2a->writable() && chan2a->readable());  ASSERT(chan2b->writable() && chan2b->readable());  ASSERT(chanhandler1a->last_writable);  ASSERT(chanhandler1b->last_writable);  ASSERT(chanhandler2a->last_writable);  ASSERT(chanhandler2b->last_writable);  // Accept the session.  TestAccept(signaling_thread, session1, session2,             handler1.get(), handler2.get(),             manager1.get(), manager2.get(),             manhandler1.get(), manhandler2.get());  // Send a bunch of data between them.  TestSendRecv(chanhandler1a.get(), chanhandler1b.get(), chanhandler2a.get(),               chanhandler2b.get(), signaling_thread, false);  manager1->DestroySession(session1);  manager2->DestroySession(session2);  ASSERT(manhandler1->create_count == 1);  ASSERT(manhandler2->create_count == 1);  ASSERT(manhandler1->destroy_count == 1);  ASSERT(manhandler2->destroy_count == 1);  worker_thread->Stop();  std::cout << "P2P: " << test_name << ": PASS" << std::endl;}//int main(int argc, char* argv[]) {  talk_base::LogMessage::LogToDebug(talk_base::LS_WARNING);  TestP2P("{p2p} => {p2p}", false, false);  TestP2P("{p2p} => {p2p,raw}", false, true);  TestP2P("{p2p,raw} => {p2p}", true, false);  TestP2P("{p2p,raw} => {p2p,raw}", true, true);  TestP2PCompatibility("New => New", false, false);  TestP2PCompatibility("Old => New", true, false);  TestP2PCompatibility("New => Old", false, true);  TestP2PCompatibility("Old => Old", true, true);  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -