예제 #1
0
    def testhandle_dir_abstracts(self):
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        self.assertEquals(proto.selector, '/')
        self.assertEquals(self.logfile.getvalue(),
                          "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n")
        # Try to make this easy on us to fix.
        actualarr = self.wfile.getvalue().splitlines()
        expectedarr = [
'iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0',
'0README\t/README\tHOSTNAME\t64777\t+',
'1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
'9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
'9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
'9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
'9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
'9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
'9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
'0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
'9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
'iThis is the abstract\tfake\t(NULL)\t0',
'ifor testfile.txt.gz\tfake\t(NULL)\t0',
'9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+']
        expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                      x in expectedarr]
        self.assertEquals(len(actualarr), len(expectedarr), str(actualarr))
        for i in range(len(actualarr)):
            self.assertEquals(actualarr[i], expectedarr[i])
        # Make sure proper line endings are present.
        self.assertEquals("\r\n".join(actualarr) + "\r\n", self.wfile.getvalue())
예제 #2
0
 def testhandle_dir_abstracts(self):
     proto = GopherProtocol("", self.server, self.handler, self.rfile,
                            self.wfile, self.config)
     proto.handle()
     self.assertEqual(proto.selector, '/')
     self.assertEqual(self.logfile.getvalue(),
                      "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n")
     # Try to make this easy on us to fix.
     actualarr = self.wfile.getvalue().decode('ascii').splitlines()
     expectedarr = [
         'iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0',
         '0README\t/README\tHOSTNAME\t64777\t+',
         '1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
         '9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
         '9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
         '9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
         '9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
         '9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
         '9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
         '0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
         '9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
         'iThis is the abstract\tfake\t(NULL)\t0',
         'ifor testfile.txt.gz\tfake\t(NULL)\t0',
         '9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+'
     ]
     expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                   x in expectedarr]
     self.assertEqual(len(actualarr), len(expectedarr), str(actualarr))
     for i in range(len(actualarr)):
         self.assertEqual(actualarr[i], expectedarr[i])
     # Make sure proper line endings are present.
     self.assertEqual("\r\n".join(actualarr) + "\r\n",
                      self.wfile.getvalue())
예제 #3
0
    def testhandle_dir_noabstract(self):
        self.config.set("pygopherd", "abstract_headers", "off")
        self.config.set("pygopherd", "abstract_entries", "off")
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        actuallines = (self.wfile.getvalue().decode(
            errors="surrogateescape").splitlines())
        expectedlines = [
            "iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0",
            "iThis is the abstract\tfake\t(NULL)\t0",
            "ifor testfile.txt.gz\tfake\t(NULL)\t0",
        ]

        for line in expectedlines:
            self.assertNotIn(line, actuallines)
예제 #4
0
 def testhandle_dir_noabstract(self):
     self.config.set("pygopherd", "abstract_headers", "off")
     self.config.set("pygopherd", "abstract_entries", "off")
     proto = GopherProtocol("", self.server, self.handler, self.rfile,
                            self.wfile, self.config)
     proto.handle()
     actualarr = self.wfile.getvalue().splitlines()
     expectedarr = \
          ['0README\t/README\tHOSTNAME\t64777\t+',
           '1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
           '9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
           '9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
           '9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
           '9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
           '9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
           '9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
           '0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
           '9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
           '9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+']
     expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                    x in expectedarr]
     self.assertEquals(len(actualarr), len(expectedarr))
     for i in range(len(actualarr)):
         self.assertEquals(actualarr[i], expectedarr[i])
예제 #5
0
 def testhandle_dir_noabstract(self):
     self.config.set("pygopherd", "abstract_headers", "off")
     self.config.set("pygopherd", "abstract_entries", "off")
     proto = GopherProtocol("", self.server, self.handler, self.rfile,
                            self.wfile, self.config)
     proto.handle()
     actualarr = self.wfile.getvalue().splitlines()
     expectedarr = \
          ['0README\t/README\tHOSTNAME\t64777\t+',
           '1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
           '9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
           '9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
           '9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
           '9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
           '9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
           '9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
           '0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
           '9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
           '9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+']
     expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                    x in expectedarr]
     self.assertEqual(len(actualarr), len(expectedarr))
     for i in range(len(actualarr)):
         self.assertEqual(actualarr[i], expectedarr[i])
예제 #6
0
    def testhandle_dir_abstracts(self):
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        self.assertEqual(proto.selector, "/")
        self.assertEqual(self.logfile.getvalue(),
                         "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n")
        # Try to make this easy on us to fix.
        actuallines = (self.wfile.getvalue().decode(
            errors="surrogateescape").splitlines())
        expectedlines = [
            "iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0",
            "iThis is the abstract\tfake\t(NULL)\t0",
            "ifor testfile.txt.gz\tfake\t(NULL)\t0",
        ]

        for line in expectedlines:
            self.assertIn(line, actuallines)

        # Make sure proper line endings are present.
        self.assertEqual(
            "\r\n".join(actuallines) + "\r\n",
            self.wfile.getvalue().decode(errors="surrogateescape"),
        )
예제 #7
0
class RFC1436TestCase(unittest.TestCase):
    def setUp(self):
        self.config = testutil.getconfig()
        self.rfile = StringIO("/testfile.txt\n")
        self.wfile = StringIO()
        self.logfile = testutil.getstringlogger()
        self.handler = testutil.gettestinghandler(self.rfile, self.wfile,
                                                  self.config)
        self.server = self.handler.server
        self.proto = GopherProtocol("/testfile.txt\n", self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)

    def testcanhandlerequest(self):
        assert self.proto.canhandlerequest()
        proto = GopherProtocol("/testfile.txt\tsearch\n",
                               self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        assert proto.canhandlerequest()
        self.assertEquals(proto.selector, '/testfile.txt')
        self.assertEquals(proto.searchrequest, "search")

    def testrenderobjinfo(self):
        expected = "0testfile.txt\t/testfile.txt\t%s\t%d\t+\r\n" % \
                   (self.server.server_name, self.server.server_port)
        self.assertEquals(self.proto.renderobjinfo(self.proto.gethandler().getentry()),
                          expected)

    def testhandle_file(self):
        self.proto.handle()
        self.assertEquals(self.logfile.getvalue(),
                          "10.77.77.77 [GopherProtocol/FileHandler]: /testfile.txt\n")
        self.assertEquals(self.wfile.getvalue(), "Test\n")

    def testhandle_file_zipped(self):
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", 'true')
        from pygopherd.handlers import HandlerMultiplexer
        HandlerMultiplexer.handlers = None
        handlerlist = self.config.get("handlers.HandlerMultiplexer", "handlers")
        handlerlist = handlerlist.strip()
        handlerlist = handlerlist[0] + 'ZIP.ZIPHandler, ' + handlerlist[1:]
        self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)
        self.proto = GopherProtocol("/testdata.zip/pygopherd/ziponly\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()
        self.assertEquals(self.wfile.getvalue(), "ZIPonly\n")
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", "false")

    def testhandle_dir_abstracts(self):
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        self.assertEquals(proto.selector, '/')
        self.assertEquals(self.logfile.getvalue(),
                          "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n")
        # Try to make this easy on us to fix.
        actualarr = self.wfile.getvalue().splitlines()
        expectedarr = [
'iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0',
'0README\t/README\tHOSTNAME\t64777\t+',
'1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
'9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
'9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
'9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
'9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
'9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
'9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
'0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
'9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
'iThis is the abstract\tfake\t(NULL)\t0',
'ifor testfile.txt.gz\tfake\t(NULL)\t0',
'9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+']
        expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                      x in expectedarr]
        self.assertEquals(len(actualarr), len(expectedarr), str(actualarr))
        for i in range(len(actualarr)):
            self.assertEquals(actualarr[i], expectedarr[i])
        # Make sure proper line endings are present.
        self.assertEquals("\r\n".join(actualarr) + "\r\n", self.wfile.getvalue())

    def testhandle_dir_noabstract(self):
        self.config.set("pygopherd", "abstract_headers", "off")
        self.config.set("pygopherd", "abstract_entries", "off")
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        actualarr = self.wfile.getvalue().splitlines()
        expectedarr = \
             ['0README\t/README\tHOSTNAME\t64777\t+',
              '1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
              '9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
              '9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
              '9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
              '9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
              '9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
              '9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
              '0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
              '9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
              '9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+']
        expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                       x in expectedarr]
        self.assertEquals(len(actualarr), len(expectedarr))
        for i in range(len(actualarr)):
            self.assertEquals(actualarr[i], expectedarr[i])
예제 #8
0
class RFC1436TestCase(unittest.TestCase):
    def setUp(self):
        self.config = testutil.getconfig()
        self.rfile = BytesIO(b"/testfile.txt\n")
        self.wfile = BytesIO()
        self.logfile = testutil.getstringlogger()
        self.handler = testutil.gettestinghandler(self.rfile, self.wfile,
                                                  self.config)
        self.server = self.handler.server
        self.proto = GopherProtocol("/testfile.txt\n", self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)

    def testcanhandlerequest(self):
        assert self.proto.canhandlerequest()
        proto = GopherProtocol("/testfile.txt\tsearch\n", self.server,
                               self.handler, self.rfile, self.wfile,
                               self.config)
        assert proto.canhandlerequest()
        self.assertEqual(proto.selector, '/testfile.txt')
        self.assertEqual(proto.searchrequest, "search")

    def testrenderobjinfo(self):
        expected = "0testfile.txt\t/testfile.txt\t%s\t%d\t+\r\n" % \
                   (self.server.server_name, self.server.server_port)
        self.assertEqual(
            self.proto.renderobjinfo(self.proto.gethandler().getentry()),
            expected)

    def testhandle_file(self):
        self.proto.handle()
        self.assertEqual(
            self.logfile.getvalue(),
            b"10.77.77.77 [GopherProtocol/FileHandler]: /testfile.txt\n")
        self.assertEqual(self.wfile.getvalue(), b"Test\n")

    def testhandle_dir_abstracts(self):
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        self.assertEqual(proto.selector, '/')
        self.assertEqual(self.logfile.getvalue(),
                         "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n")
        # Try to make this easy on us to fix.
        actualarr = self.wfile.getvalue().decode('ascii').splitlines()
        expectedarr = [
            'iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0',
            '0README\t/README\tHOSTNAME\t64777\t+',
            '1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
            '9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
            '9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
            '9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
            '9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
            '9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
            '9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
            '0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
            '9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
            'iThis is the abstract\tfake\t(NULL)\t0',
            'ifor testfile.txt.gz\tfake\t(NULL)\t0',
            '9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+'
        ]
        expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                      x in expectedarr]
        self.assertEqual(len(actualarr), len(expectedarr), str(actualarr))
        for i in range(len(actualarr)):
            self.assertEqual(actualarr[i], expectedarr[i])
        # Make sure proper line endings are present.
        self.assertEqual("\r\n".join(actualarr) + "\r\n",
                         self.wfile.getvalue())

    def testhandle_dir_noabstract(self):
        self.config.set("pygopherd", "abstract_headers", "off")
        self.config.set("pygopherd", "abstract_entries", "off")
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        actualarr = self.wfile.getvalue().splitlines()
        expectedarr = \
             ['0README\t/README\tHOSTNAME\t64777\t+',
              '1pygopherd\t/pygopherd\tHOSTNAME\t64777\t+',
              '9symlinktest\t/symlinktest.zip\tHOSTNAME\t64777\t+',
              '9testarchive\t/testarchive.tar\tHOSTNAME\t64777\t+',
              '9testarchive.tar.gz\t/testarchive.tar.gz\tHOSTNAME\t64777\t+',
              '9testarchive.tgz\t/testarchive.tgz\tHOSTNAME\t64777\t+',
              '9testdata\t/testdata.zip\tHOSTNAME\t64777\t+',
              '9testdata2\t/testdata2.zip\tHOSTNAME\t64777\t+',
              '0testfile\t/testfile.txt\tHOSTNAME\t64777\t+',
              '9testfile.txt.gz\t/testfile.txt.gz\tHOSTNAME\t64777\t+',
              '9ziptorture\t/ziptorture.zip\tHOSTNAME\t64777\t+']
        expectedarr = [re.sub('HOSTNAME', self.server.server_name, x) for \
                       x in expectedarr]
        self.assertEqual(len(actualarr), len(expectedarr))
        for i in range(len(actualarr)):
            self.assertEqual(actualarr[i], expectedarr[i])
예제 #9
0
class DISABLED_TestVFS_Zip_huge:
    def setUp(self):
        from pygopherd import testutil
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.config = testutil.getconfig()
        self.rfile = StringIO("/testfile.txt\n")
        self.wfile = StringIO()
        self.logfile = testutil.getstringlogger()
        self.handler = testutil.gettestinghandler(self.rfile, self.wfile,
                                                  self.config)
        self.server = self.handler.server
        self.proto = GopherProtocol("/testfile.txt\n", self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", 'true')
        from pygopherd.handlers import HandlerMultiplexer
        HandlerMultiplexer.handlers = None
        handlerlist = self.config.get("handlers.HandlerMultiplexer",
                                      "handlers")
        handlerlist = handlerlist.strip()
        handlerlist = handlerlist[0] + 'ZIP.ZIPHandler, ' + handlerlist[1:]
        self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)

    def testlistdir1(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip\n", self.server, self.handler,
                                    self.rfile, self.wfile, self.config)
        self.proto.handle()

    def testlistdir2(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/lib\n", self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()

    def testlistdir3(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/lib/dpkg/info\n", self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()

    def testopen1(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/lib/dpkg/info/dpkg.list\n",
                                    self.server, self.handler, self.rfile,
                                    self.wfile, self.config)
        self.proto.handle()

    def testopen2(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/games/bsdgames/snake.log\n",
                                    self.server, self.handler, self.rfile,
                                    self.wfile, self.config)
        self.proto.handle()

    def testopen3(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol(
            "/foo.zip/www/apache2-default/manual/platforms/index.html\n",
            self.server, self.handler, self.rfile, self.wfile, self.config)
        self.proto.handle()
예제 #10
0
파일: ZIP.py 프로젝트: Almad/pygopherd
class DISABLED_TestVFS_Zip_huge:
    def setUp(self):
        from pygopherd import testutil
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.config = testutil.getconfig()
        self.rfile = StringIO("/testfile.txt\n")
        self.wfile = StringIO()
        self.logfile = testutil.getstringlogger()
        self.handler = testutil.gettestinghandler(self.rfile, self.wfile,
                                                  self.config)
        self.server = self.handler.server
        self.proto = GopherProtocol("/testfile.txt\n", self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", 'true')
        from pygopherd.handlers import HandlerMultiplexer
        HandlerMultiplexer.handlers = None
        handlerlist = self.config.get("handlers.HandlerMultiplexer", "handlers")
        handlerlist = handlerlist.strip()
        handlerlist = handlerlist[0] + 'ZIP.ZIPHandler, ' + handlerlist[1:]
        self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)


    def testlistdir1(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()

    def testlistdir2(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/lib\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()

    def testlistdir3(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/lib/dpkg/info\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()
        
    def testopen1(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/lib/dpkg/info/dpkg.list\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()

    def testopen2(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/games/bsdgames/snake.log\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()

    def testopen3(self):
        from pygopherd.protocols.rfc1436 import GopherProtocol
        self.proto = GopherProtocol("/foo.zip/www/apache2-default/manual/platforms/index.html\n",
                                    self.server,
                                    self.handler, self.rfile, self.wfile,
                                    self.config)
        self.proto.handle()
예제 #11
0
class RFC1436TestCase(unittest.TestCase):
    def setUp(self):
        self.config = testutil.get_config()
        self.rfile = BytesIO(b"/testfile.txt\n")
        self.wfile = BytesIO()
        self.logfile = testutil.get_string_logger()
        self.handler = testutil.get_testing_handler(self.rfile, self.wfile,
                                                    self.config)
        self.server = self.handler.server
        self.proto = GopherProtocol(
            "/testfile.txt\n",
            self.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

    def inject_handler(self,
                       handler_name: str,
                       index: typing.Optional[int] = None):
        """
        Add a handler to the HandlerMultiplexer in the configuration.
        """
        handlerlist = self.config.get("handlers.HandlerMultiplexer",
                                      "handlers")
        handlers = [x.strip() for x in handlerlist.strip("[] \n").split(",")]
        if index is not None:
            handlers.insert(index, handler_name)
        else:
            handlers.append(handler_name)

        handlerlist = "[" + ",".join(handlers) + "]"
        self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)

        # Clear the cache
        HandlerMultiplexer.handlers = None

    def testcanhandlerequest(self):
        assert self.proto.canhandlerequest()
        proto = GopherProtocol(
            "/testfile.txt\tsearch\n",
            self.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )
        assert proto.canhandlerequest()
        self.assertEqual(proto.selector, "/testfile.txt")
        self.assertEqual(proto.searchrequest, "search")

    def testrenderobjinfo(self):
        expected = "0testfile.txt\t/testfile.txt\t%s\t%d\t+\r\n" % (
            self.server.server_name,
            self.server.server_port,
        )
        self.assertEqual(
            self.proto.renderobjinfo(self.proto.gethandler().getentry()),
            expected)

    def testhandle_file(self):
        self.proto.handle()
        self.assertEqual(
            self.logfile.getvalue(),
            "10.77.77.77 [GopherProtocol/FileHandler]: /testfile.txt\n",
        )
        self.assertEqual(self.wfile.getvalue(), b"Test\n")

    def testhandle_file_zipped(self):
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", "true")
        self.inject_handler("ZIP.ZIPHandler", index=0)

        self.proto = GopherProtocol(
            "/testdata.zip/pygopherd/ziponly\n",
            self.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )
        self.proto.handle()
        self.assertEqual(self.wfile.getvalue(), b"ZIPonly\n")
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", "false")

    def testhandle_dir_abstracts(self):
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        self.assertEqual(proto.selector, "/")
        self.assertEqual(self.logfile.getvalue(),
                         "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n")
        # Try to make this easy on us to fix.
        actuallines = (self.wfile.getvalue().decode(
            errors="surrogateescape").splitlines())
        expectedlines = [
            "iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0",
            "iThis is the abstract\tfake\t(NULL)\t0",
            "ifor testfile.txt.gz\tfake\t(NULL)\t0",
        ]

        for line in expectedlines:
            self.assertIn(line, actuallines)

        # Make sure proper line endings are present.
        self.assertEqual(
            "\r\n".join(actuallines) + "\r\n",
            self.wfile.getvalue().decode(errors="surrogateescape"),
        )

    def testhandle_dir_noabstract(self):
        self.config.set("pygopherd", "abstract_headers", "off")
        self.config.set("pygopherd", "abstract_entries", "off")
        proto = GopherProtocol("", self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        proto.handle()
        actuallines = (self.wfile.getvalue().decode(
            errors="surrogateescape").splitlines())
        expectedlines = [
            "iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0",
            "iThis is the abstract\tfake\t(NULL)\t0",
            "ifor testfile.txt.gz\tfake\t(NULL)\t0",
        ]

        for line in expectedlines:
            self.assertNotIn(line, actuallines)