Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Tests TelnetServer.
      6 
      7 #include "net/base/listen_socket_unittest.h"
      8 #include "net/base/telnet_server.h"
      9 #include "testing/platform_test.h"
     10 
     11 static const char* kCRLF = "\r\n";
     12 
     13 class TelnetServerTester : public ListenSocketTester {
     14 public:
     15   virtual ListenSocket* DoListen() {
     16     return TelnetServer::Listen("127.0.0.1", kTestPort, this);
     17   }
     18 
     19   virtual void SetUp() {
     20     ListenSocketTester::SetUp();
     21     // With TelnetServer, there's some control codes sent at connect time,
     22     // so we need to eat those to avoid affecting the subsequent tests.
     23     EXPECT_EQ(ClearTestSocket(), 15);
     24   }
     25 
     26   virtual bool Send(SOCKET sock, const std::string& str) {
     27     if (ListenSocketTester::Send(sock, str)) {
     28       // TelnetServer currently calls DidRead after a CRLF, so we need to
     29       // append one to the end of the data that we send.
     30       if (ListenSocketTester::Send(sock, kCRLF)) {
     31         return true;
     32       }
     33     }
     34     return false;
     35   }
     36 
     37 private:
     38   ~TelnetServerTester() {}
     39 };
     40 
     41 class TelnetServerTest: public PlatformTest {
     42 protected:
     43   TelnetServerTest() {
     44     tester_ = NULL;
     45   }
     46 
     47   virtual void SetUp() {
     48     PlatformTest::SetUp();
     49     tester_ = new TelnetServerTester();
     50     tester_->SetUp();
     51   }
     52 
     53   virtual void TearDown() {
     54     PlatformTest::TearDown();
     55     tester_->TearDown();
     56     tester_ = NULL;
     57   }
     58 
     59   scoped_refptr<TelnetServerTester> tester_;
     60 };
     61 
     62 TEST_F(TelnetServerTest, ServerClientSend) {
     63   tester_->TestClientSend();
     64 }
     65 
     66 TEST_F(TelnetServerTest, ClientSendLong) {
     67   tester_->TestClientSendLong();
     68 }
     69 
     70 TEST_F(TelnetServerTest, ServerSend) {
     71   tester_->TestServerSend();
     72 }
     73