def test_invalidport(self):
        time.sleep(1)
        try:
            lock = threading.Lock()
            self.telnet = telnet_reader.server_plugin(lock, PORT)

        except Exception as e:
            self.fail("Server Failed to Start")

        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect(("localhost", PORT-1))
            connection = True

        except Exception as e:
            connection = False

        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect(("localhost", PORT+1))
            connection = True

        except Exception as e:
            connection = False

        finally:
            self.assertFalse(connection)
            self.telnet.s.close()
            conn.close()
            time.sleep(1)
    def test_run(self):

        try:
            lock = threading.Lock()
            telnet = telnet_reader.server_plugin(lock, PORT)
        except Exception as e:
            self.fail("Server Failed to Start")



        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect(("localhost", 23))

            conn.recv(1024)
            conn.recv(1024)
            conn.recv(1024)
            conn.recv(1024)
            conn.recv(1024)
            conn.recv(1024)
            conn.sendall('Username \r\n')
            conn.recv(1024)
            conn.send('Password \r\n')
            conn.recv(1024)
            conn.send('Command test \r\n')
            conn.send('\x03')

            connection = True
        except Exception as e:
            print e
            connection = False
        finally:
            self.assertTrue(connection)
            conn.close()
    def test_nonacsii(self):
        time.sleep(2)
        try:
            lock = threading.Lock()
            self.telnet = telnet_reader.server_plugin(lock, PORT)

        except Exception as e:
            self.fail("Server Failed to Start")

        time.sleep(2)

        try:
            self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.conn.connect(("localhost", PORT))

        except Exception as e:
            print e
            self.fail("Failed to make a connection")

        try:
            self.conn.recv(1024)
            self.conn.sendall('Username \r\n')
            self.conn.recv(1024)
            self.conn.send('Password \r\n')
            self.conn.recv(1024)
            self.conn.send('café \r\n')
            self.conn.recv(1024)
            self.conn.send('ñóǹ äŝçíì 汉语/漢語  华语/華語 Huáyǔ; 中文 Zhōngwén 漢字仮名交じり文 Lech Wałęsa æøå \r\n')
            self.conn.recv(1024)
            self.conn.send(
                '\x00 \x01 \x02 \x04 \x05 \x06 \x07 \x08 \x09 \x0A \x0B \x0C \x0D \x0E \x0F'
                '\x20 \x21 \x22 \x23 \x24 \x25 \x26 \x27 \x28 \x29 \x2A \x2B \x2C \x2D \x2E \x2F'
                '  \r\n')
            self.conn.recv(1024)
            self.conn.send('\x03')
            self.conn.recv(1024)
            nonAscii = True

        except Exception as e:
            print e
            nonAscii = False

        finally:
            self.telnet.s.close()
            self.assertTrue(nonAscii)
            self.conn.close()
            time.sleep(1)
    def test_shellshock(self):
        shellshockfail = False
        time.sleep(1)
        try:
            lock = threading.Lock()
            self.telnet = telnet_reader.server_plugin(lock, PORT)

        except Exception as e:
            self.fail("Server Failed to Start")
        time.sleep(1)

        try:
            self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.conn.connect(("localhost", PORT))
            msg = self.conn.recv(1024)
            # print msg
            self.conn.sendall('Username \r\n')
            msg = self.conn.recv(1024)
            # print msg
            self.conn.send('Password \r\n')
            msg = self.conn.recv(1024)
            # print msg
            self.conn.send('env z="() { :; }; echo vulnerable" bash -c "echo foo"'' \r\n')
            msg = self.conn.recv(1024)
            # print msg

            if msg in 'vulnerable':
                shellshockfail = False
                self.fail("ShellShock occured: " + msg)

            else:
                shellshockfail = True

            self.conn.send('\x03')
            self.conn.recv(1024)
            self.telnet.s.close()
            connection = True

        except Exception as e:
            print e
            connection = False

        finally:
            self.assertTrue(connection & shellshockfail)
            self.conn.close()
            time.sleep(1)
    def test_startUp(self):
        try:
            lock = threading.Lock()
            telnet = telnet_reader.server_plugin(lock, PORT)
        except Exception as e:
            self.fail("Server Failed to Start")

        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect(("localhost", 23))
            connection = True
        except Exception as e:
            print e
            connection = False
        finally:
            self.assertTrue(connection)
            conn.close()
    def test_teardown(self):
        connection = False
        try:
              lock = threading.Lock()
              self.telnet = telnet_reader.server_plugin(lock, PORT)

        except Exception as e:
              self.fail("Server Failed to Start")

        time.sleep(1)

        try:
            self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.conn.connect(("localhost", PORT))
            self.conn.recv(1024)
            self.conn.sendall('Username \r\n')
            self.conn.recv(1024)
            self.conn.send('Password \r\n')
            self.conn.recv(1024)
            self.conn.send('Command test \r\n')
            self.conn.recv(1024)
            self.conn.send('\x03')
            self.conn.recv(1024)
            connection = True

        except Exception as e:
            print e
            self.fail("client failed to connect")

        try:
            self.telnet.tear_down()
            self.conn.connect(("localhost",PORT))
            self.fail("Server Failed to shutdown")

        except Exception as e:
            self.assertTrue(True)

        finally:
            self.telnet.s.close()
            self.conn.close()
            time.sleep(1)
    def test_mulithreads(self):
        try:
             lock = threading.Lock()
             telnet = telnet_reader.server_plugin(lock, PORT)
        except Exception as e:
            self.fail("Server Failed to Start")

        try:
            threads = []
            for num in range(0, 4):
                thread = telent_client()
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()
            connection = True
        except Exception as e:
            print e
            connection = False
        finally:
            self.assertTrue(connection)
    def test_bufferoverflow(self):
        buff = '\x41'* 16793598 # sends 16,793,598 A
        bufferOverflow = False
        time.sleep(1)

        try:
            lock = threading.Lock()
            self.telnet = telnet_reader.server_plugin(lock, PORT)

        except Exception as e:
            self.fail("Server Failed to Start")

        time.sleep(1)

        try:
            self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.conn.connect(("localhost", PORT))
            # self.conn.settimeout(5.0)
            msg = self.conn.recv(1024)
            # print msg
            self.conn.sendall('Username \r\n')
            msg = self.conn.recv(1024)
            # print msg
            self.conn.send('Password \r\n')
            msg = self.conn.recv(1024)
            # print msg
            # self.conn.settimeout(30.0)
            self.conn.sendall('' + buff + buff + ' \r\n')
            self.conn.sendall('\r\n')
            msg = self.conn.recv(1024)

            if msg != 'Invalid command\n>> ':
                bufferOverflow = True

            else:
                bufferOverflow = False

            self.conn.send('\x03')
            self.conn.recv(1024)
            # self.telnet.s.close()
            connection = True

        except Exception as e:

            if e.message == 'timed out':
                self.fail('Client had to timeout: 30 seconds')
            # We want the connection to close when a buffer Overflow attack happens.

            if e.strerror == 'An existing connection was forcibly closed by the remote host':
                bufferOverflow = True
                pass

            if e.message == '':
                bufferOverflow = True
                pass

            else:
                print e
                bufferOverflow = False

        try:
            self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.conn.connect(("localhost", PORT))
            self.conn.recv(1024)
            self.conn.sendall('Username \r\n')
            msg = self.conn.recv(1024)
            # print msg
            self.conn.send('Password \r\n')
            msg = self.conn.recv(1024)
            # print msg
            self.conn.send('Command test \r\n')
            msg = self.conn.recv(1024)
            # print msg
            self.conn.send('\x03')
            msg = self.conn.recv(1024)
            # print msg
            self.telnet.s.close()
            connection = True

        except Exception as e:
            print e
            connection = False

        finally:
            self.assertTrue(bufferOverflow)
            self.conn.close()
            time.sleep(1)