def test_streamreader_extract_bytes():
    # Empty bytes
    stream = StringStream(b'')
    reader = StreamReader(stream, False)
    assert reader.extract_bytes(10) == b''

    # Small bytes object, small reads
    stream = StringStream(b'abcd')
    reader = StreamReader(stream, False)
    assert reader.extract_bytes(2) == b'ab'
    assert reader.extract_bytes(2) == b'cd'
    assert reader.extract_bytes(2) == b''

    # Embedded null bytes
    stream = StringStream(b'a\x00b\x00c')
    reader = StreamReader(stream, False)
    assert reader.extract_bytes(5) == b'a\x00b\x00c'

    # Not enough data in stream to fill buffer
    stream = StringStream(b'abcdefghijklmnop')
    reader = StreamReader(stream, False)
    assert reader.extract_bytes(10) == b'abcdefghij'
    assert reader.extract_bytes(10) == b'klmnop'
    assert reader.extract_bytes(10) == b''

    # Read of 0 bytes
    stream = StringStream(b'abcd')
    reader = StreamReader(stream, False)
    assert reader.extract_bytes(0) == b''
    assert reader.extract_bytes(0) == b''

    # Very large read (8 MiB buffer allocation)
    assert reader.extract_bytes(8 * 1024 * 1024) == b'abcd'
    def create_miraidata(self):
        # Create a temporary _miraidata.py and throw it on the path somewhere...

        # First, we need the minified DC file contents:
        from panda3d.core import StringStream
        dcStream = StringStream()
        self.dcf.write(dcStream, True)
        dcData = dcStream.getData()

        # Next we need config files...
        configData = []
        with open(self.config_file) as f:
            fd = f.read()
            fd = fd.replace('SERVER_VERSION_HERE', self.version)
            fd = fd.replace('LANGUAGE_HERE', self.language)
            configData.append(fd)

        md = 'CONFIG = %r\nDC = %r\n' % (configData, dcData)

        # Now we use tempfile to dump md:
        td = tempfile.mkdtemp()
        with open(os.path.join(td, '_miraidata.py'), 'w') as f:
            f.write(md)

        self.mf.path.append(td)

        atexit.register(shutil.rmtree, td)
def test_streamreader_z_string():
    # Empty stream
    stream = StringStream(b'')
    reader = StreamReader(stream, False)
    assert reader.get_z_string() == ''

    # Empty string
    stream = StringStream(b'\x00')
    reader = StreamReader(stream, False)
    assert reader.get_z_string() == ''

    # String of length 1
    stream = StringStream(b'A\x00')
    reader = StreamReader(stream, False)
    assert reader.get_z_string() == 'A'

    # String with excess data
    stream = StringStream(b'ABC\x00AB')
    reader = StreamReader(stream, False)
    assert reader.get_z_string() == 'ABC'

    # EOF before end of string
    stream = StringStream(b'ABC')
    reader = StreamReader(stream, False)
    assert reader.get_z_string() == 'ABC'
def test_streamreader_readline():
    # Empty stream
    stream = StringStream(b'')
    reader = StreamReader(stream, False)
    assert reader.readline() == b''
    assert reader.readline() == b''

    # Single line without newline
    stream = StringStream(b'A')
    reader = StreamReader(stream, False)
    assert reader.readline() == b'A'
    assert reader.readline() == b''

    # Single newline
    stream = StringStream(b'\n')
    reader = StreamReader(stream, False)
    assert reader.readline() == b'\n'
    assert reader.readline() == b''

    # Line with text followed by empty line
    stream = StringStream(b'A\n\n')
    reader = StreamReader(stream, False)
    assert reader.readline() == b'A\n'
    assert reader.readline() == b'\n'
    assert reader.readline() == b''

    # Preserve null byte
    stream = StringStream(b'\x00\x00')
    reader = StreamReader(stream, False)
    assert reader.readline() == b'\x00\x00'
Beispiel #5
0
    def __test_loader(self, filename, use_loader_class):
        store = DNAStorage()

        if use_loader_class:
            np = self.loader.loadDNAFile(store, filename)
            expected_repr = '''PandaNode dna
  PandaNode test
    PandaNode 1000
      PandaNode subgroup
        ModelNode prop_test T:(pos -12 5 7 hpr 180 15 0) S:(ColorScaleAttrib)
        PandaNode tb3:test_block [DNACode]
        PandaNode sb3:test_block'''

        else:
            np = NodePath(loadDNAFile(store, filename))
            expected_repr = '''PandaNode test
  PandaNode 1000
    PandaNode subgroup
      ModelNode prop_test T:(pos -12 5 7 hpr 180 15 0) S:(ColorScaleAttrib)
      PandaNode tb3:test_block [DNACode]
      PandaNode sb3:test_block'''

        self.check_store(store)

        ss = StringStream()
        np.ls(ss)

        self.assertEqual(ss.getData().strip(), expected_repr)
Beispiel #6
0
    def create_miraidata(self):
        # Create a temporary _miraidata.py and throw it on the path somewhere...

        # First, we need the minified DC file contents:
        from panda3d.core import StringStream
        dcStream = StringStream()
        self.dcf.write(dcStream, True)
        dcData = dcStream.getData()

        # Next we need config files...
        configData = []
        with open(self.config_file) as f:
            fd = f.read()
            fd = fd.replace('SERVER_VERSION_HERE', self.version)
            fd = fd.replace('LANGUAGE_HERE', self.language)
            configData.append(fd)

        md = 'CONFIG = %r\nDC = %r\n' % (configData, dcData)

        # Now we use tempfile to dump md:
        td = tempfile.mkdtemp()
        with open(os.path.join(td, '_miraidata.py'), 'w') as f:
            f.write(md)

        self.mf.path.append(td)

        atexit.register(shutil.rmtree, td)
Beispiel #7
0
def test_zip_replace_subfile(tmp_path):
    stream = StringStream()
    zf = zipfile.ZipFile(StreamIOWrapper(stream), mode='w', allowZip64=True)
    zf.writestr("test1.txt", b"contents of first file")
    zf.writestr("test2.txt", b"")
    zf.writestr("test3.txt", b"contents of third file")
    zf.close()

    zip = ZipArchive()
    zip.open_read_write(stream)

    assert zip.is_read_valid()
    assert zip.is_write_valid()
    assert not zip.needs_repack()

    assert zip.verify()

    sf = zip.find_subfile("test2.txt")
    assert sf >= 0
    zip.add_subfile("test2.txt", StringStream(b"contents of second file"), 6)
    zip.close()

    with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
        assert zf.testzip() is None
        assert zf.read("test1.txt") == b"contents of first file"
        assert zf.read("test2.txt") == b"contents of second file"
        assert zf.read("test3.txt") == b"contents of third file"
Beispiel #8
0
 def __init__(self, *args):
     InteractiveConsole.__init__(self, *args)
     self.stream = StringStream()
     self.stringio = StringIO()
     self.input_string = ""
     self.output_string = ""
     self.more = 0
     self.prompt = ">>> "
Beispiel #9
0
    def __init__(self, client, name):
        self.client = client
        self.name = name

        self.notify = directNotify.newCategory('ToontownRPCMethod[%s]' % name)

        self.channel = None
        self.stream = StringStream()
        self.callback = None
        self.errback = None
        self.extraArgs = None
Beispiel #10
0
 def _logClsend(self, senderId, dataStr):
     msgStream = StringStream()
     simbase.air.describeMessage(msgStream, '', dataStr)
     readableStr = msgStream.getData()
     sstream = StringStream()
     PyDatagram(dataStr).dumpHex(sstream)
     hexDump = sstream.getData()
     self.clsendNotify.info('%s [%s]: %s%s' % (self.doId,
      self._clsendCounter,
      readableStr,
      hexDump))
Beispiel #11
0
def test_zip_write():
    stream = StringStream()
    zip = ZipArchive()
    zip.open_write(stream)
    zip.add_subfile("test.txt", StringStream(b"test deflated"), 6)
    zip.add_subfile("test2.txt", StringStream(b"test stored"), 0)
    zip.close()

    with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
        assert zf.testzip() is None

    assert tuple(sorted(zf.namelist())) == ("test.txt", "test2.txt")
def test_istream_iter():
    istream = StringStream(b'a')
    assert tuple(istream) == (b'a', )
    assert tuple(istream) == ()

    istream = StringStream(b'a\nb\nc\n')
    assert tuple(istream) == (b'a\n', b'b\n', b'c\n')

    istream = StringStream(b'\na\nb\nc')
    assert tuple(istream) == (b'\n', b'a\n', b'b\n', b'c')

    istream = StringStream(b'\n\n\n')
    assert tuple(istream) == (b'\n', b'\n', b'\n')
Beispiel #13
0
 def traverse(self, nodePath, dnaStorage):
     node = nodePath.attachNewNode('baseline', 0)
     node.setPosHpr(self.pos, self.hpr)
     node.setPos(node, 0, -0.1, 0)
     if self.data:
         bf = BamFile()
         ss = StringStream()
         ss.setData(self.data)
         bf.openRead(ss)
         signText = NodePath(bf.readNode())
         signText.reparentTo(node)
     node.flattenStrong()
     for child in self.children:
         child.traverse
Beispiel #14
0
 def traverse(self, nodePath, dnaStorage):
     node = nodePath.attachNewNode('baseline', 0)
     node.setPosHpr(self.pos, self.hpr)
     node.setPos(node, 0, -0.4, 0)
     if self.data:
         bf = BamFile()
         ss = StringStream()
         ss.setData(self.data)
         bf.openRead(ss)
         signText = NodePath(bf.readNode())
         signText.reparentTo(node)
     node.flattenStrong()
     for child in self.children_:
         child.traverse(nodePath, dnaStorage)
Beispiel #15
0
def getScreenshot(p3dApp):

    p3dApp.taskMgr.step()
    p3dApp.taskMgr.step()
    pnmss = PNMImage()
    p3dApp.win.getScreenshot(pnmss)
    resulting_ss = StringStream()
    pnmss.write(resulting_ss, "screenshot.png")
    screenshot_buffer = resulting_ss.getData()
    pilimage = Image.open(StringIO(screenshot_buffer))
    pilimage.load()

    #pnmimage will sometimes output as palette mode for 8-bit png so convert
    pilimage = pilimage.convert('RGBA')
    return pilimage
Beispiel #16
0
    def takePictureRaw(self):
        img = PNMImage()
        tex = self.takePicture()
        tex.store(img)

        ss = StringStream()
        img.write(ss, 'jpg')

        if 1:
            # Test it
            img2 = PNMImage()
            img2.read(ss)
            img2.write(Filename("test_viewfinder.jpg"))

        return ss.getData()
Beispiel #17
0
def getScreenshot(p3dApp):

    p3dApp.taskMgr.step()
    p3dApp.taskMgr.step()
    pnmss = PNMImage()
    p3dApp.win.getScreenshot(pnmss)
    resulting_ss = StringStream()
    pnmss.write(resulting_ss, "screenshot.png")
    screenshot_buffer = resulting_ss.getData()
    pilimage = Image.open(StringIO(screenshot_buffer))
    pilimage.load()
    
    #pnmimage will sometimes output as palette mode for 8-bit png so convert
    pilimage = pilimage.convert('RGBA')
    return pilimage
Beispiel #18
0
    def __init__(self, filename):
        vfs = VirtualFileSystem.getGlobalPtr()
        #self.material = BSPMaterial.getFromFile(filename)
        self.material = TexturePool.loadTexture(filename)
        self.filename = filename
        if self.material:  #.hasKeyvalue("$basetexture"):
            # baseTexturePath = self.material.getKeyvalue("$basetexture")

            if True:  #vfs.exists(baseTexturePath):
                #imageData = bytes(VirtualFileSystem.getGlobalPtr().readFile(baseTexturePath, True))

                pimage = PNMImage()
                self.material.store(pimage, 0, 0)
                ss = StringStream()
                pimage.write(ss, "tmp.png")
                imageData = bytes(ss)

                byteArray = QtCore.QByteArray.fromRawData(imageData)
                image = QtGui.QImage.fromData(byteArray)
                self.pixmap = QtGui.QPixmap.fromImage(image)
                self.icon = QtGui.QIcon(self.pixmap)
                self.size = LVector2i(pimage.getXSize(), pimage.getYSize())

                print(pimage, self.size)
            else:
                self.size = LVector2i(64, 64)
                self.icon = None
                self.pixmap = None
        else:
            self.size = LVector2i(64, 64)
            self.icon = None
            self.pixmap = None
Beispiel #19
0
def test_zip_read_extract(tmp_path):
    stream = StringStream()
    zf = zipfile.ZipFile(StreamIOWrapper(stream), mode='w', allowZip64=True)
    zf.writestr("test.txt", b"test stored", compress_type=zipfile.ZIP_STORED)
    zf.writestr("test2.txt",
                b"test deflated",
                compress_type=zipfile.ZIP_DEFLATED)
    zf.writestr("dir/dtest.txt", b"test in dir")
    zf.writestr("dir1/dir2/test.txt", b"test nested dir")
    zf.writestr("emptydir/", b"", compress_type=zipfile.ZIP_STORED)
    zf.close()

    wrapper = IStreamWrapper(stream)
    zip = ZipArchive()
    zip.open_read(wrapper)

    assert zip.is_read_valid()
    assert not zip.is_write_valid()
    assert not zip.needs_repack()

    assert zip.verify()

    assert zip.find_subfile("nonexistent.txt") == -1

    sf = zip.find_subfile("test.txt")
    assert sf >= 0
    assert zip.read_subfile(sf) == b"test stored"
    assert zip.extract_subfile(sf, tmp_path / "test.txt")
    assert open(tmp_path / "test.txt", 'rb').read() == b"test stored"

    sf = zip.find_subfile("test2.txt")
    assert sf >= 0
    assert zip.read_subfile(sf) == b"test deflated"
    assert zip.extract_subfile(sf, tmp_path / "test2.txt")
    assert open(tmp_path / "test2.txt", 'rb').read() == b"test deflated"
Beispiel #20
0
    def __init__(self, name, lens):
        panda3d.core.Camera.__init__(self, name, lens)
        self.name = name
        self.path = NodePath(self)

        self.height, self.width = 512, 512
        self.dataSize = self.height * self.width * NUM_CHANNELS

        self.buffer = base.win.makeTextureBuffer(name + 'Buffer', self.height,
                                                 self.width)

        # Set the sort order to a negative number so this buffer will be rendered before the
        # main window.
        self.buffer.setSort(-100)
        self.tex = self.buffer.getTexture()
        self.tex.setRenderToTexture(True)
        self.tex.makeRamImage()

        # Attach this camera to the buffer.
        base.makeCamera(self.buffer, useCamera=self.path)
        self.setCameraMask(BitMask32(2))

        # Make a PNMImage into which we'll copy screenshots from the buffer.
        self.image = PNMImage()
        self.stringStream = StringStream()
        self.gfxEngine = GraphicsEngine.getGlobalPtr()
        self.gsg = base.win.getGsg()

        self.currentFrame = 0

        # Only initialize IPC and the copy out task if the posix_ipc module
        # was loaded successfully.
        if POSIX_IPC_LOADED:
            self.initializeIpc()
Beispiel #21
0
def test_zip_repack(tmp_path):
    zip_path = tmp_path / "test_zip_repack.zip"
    zf = zipfile.ZipFile(zip_path, mode='w', allowZip64=True)
    zf.writestr("test1.txt", b"contents of first file")
    zf.writestr("test2.txt", b"contents of second file")
    zf.close()

    zip = ZipArchive()
    zip.open_read_write(zip_path)

    assert zip.is_read_valid()
    assert zip.is_write_valid()
    assert not zip.needs_repack()

    assert zip.verify()

    removed = zip.remove_subfile("test2.txt")
    assert removed

    zip.add_subfile("test3.txt", StringStream(b"contents of third file"), 6)

    assert zip.needs_repack()
    result = zip.repack()
    assert result
    assert not zip.needs_repack()

    assert zip.verify()

    zip.close()

    with zipfile.ZipFile(zip_path, 'r') as zf:
        assert zf.testzip() is None
        assert zf.read("test1.txt") == b"contents of first file"
        assert "test2.txt" not in zf.namelist()
        assert zf.read("test3.txt") == b"contents of third file"
Beispiel #22
0
def test_zip_remove_subfile(tmp_path):
    stream = StringStream()
    zf = zipfile.ZipFile(StreamIOWrapper(stream), mode='w', allowZip64=True)
    zf.writestr("test1.txt", b"contents of first file")
    zf.writestr("test2.txt", b"contents of second file")
    zf.writestr("test3.txt", b"contents of third file")
    zf.close()

    zip = ZipArchive()
    zip.open_read_write(stream)

    assert zip.is_read_valid()
    assert zip.is_write_valid()
    assert not zip.needs_repack()

    assert zip.verify()

    removed = zip.remove_subfile("test2.txt")
    assert removed
    zip.close()

    with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
        assert zf.testzip() is None
        names = zf.namelist()
        assert "test1.txt" in names
        assert "test2.txt" not in names
        assert "test3.txt" in names
Beispiel #23
0
def getTextureRAM(mesh):
    total_image_area = 0
    for cimg in mesh.images:
        pilimg = cimg.pilimage

        if pilimg:
            total_image_area += pilimg.size[0] * pilimg.size[1] * len(
                pilimg.getbands())
        else:
            #PIL doesn't support DDS, so if loading failed, try and load it as a DDS with panda3d
            imgdata = cimg.data

            #if we can't even load the image's data, can't convert
            if imgdata is None:
                continue

            try:
                from panda3d.core import Texture
                from panda3d.core import StringStream
            except ImportError:
                #if panda3d isn't installed and PIL failed, can't convert
                continue

            t = Texture()
            try:
                success = t.readDds(StringStream(imgdata))
            except:
                success = 1
            if success == 0:
                #failed to load as DDS, so let's give up
                continue
            total_image_area += t.getXSize() * t.getYSize() * 3

    return total_image_area
    def test_loader(self):
        store = DNAStorage()
        np = self.loader.loadDNAFile(store, Filename('test.pdna'))

        self.check_store(store)

        expected_repr = '''PandaNode dna
  PandaNode root
    PandaNode test
      PandaNode 1000
        PandaNode subgroup
          ModelNode prop_test T:(pos -12 5 7 hpr 180 15 0) S:(ColorScaleAttrib)
          PandaNode tb3:test_block [DNACode]
          PandaNode sb3:test_block'''
        ss = StringStream()
        np.ls(ss)
        self.assertEqual(ss.getData().strip(), expected_repr)
Beispiel #25
0
    def test_loader(self):
        store = DNAStorage()
        np = self.loader.loadDNAFile(store, Filename('test.pdna'))

        self.check_store(store)

        expected_repr = '''PandaNode dna
  PandaNode root
    PandaNode test
      PandaNode 1000
        PandaNode subgroup
          ModelNode prop_test T:(pos -12 5 7 hpr 180 15 0) S:(ColorScaleAttrib)
          PandaNode tb3:test_block [DNACode]
          PandaNode sb3:test_block'''
        ss = StringStream()
        np.ls(ss)
        self.assertEqual(ss.getData().strip(), expected_repr)
Beispiel #26
0
    def loadTexture(self, texname, container):

        # Note that we reference the texture files by name. This works under the
        # assumption that texture names are unique!
        if self.findTexture(texname) == True:
            return  # texture already loaded before

        # print 'loading texture:', texname
        t_type = ''
        s3dentry = container.s3d_file_obj.getFile(texname)
        if s3dentry != None:
            texfile = s3dentry.data
            (magic, ) = struct.unpack('<2s', texfile[0:2])
            if magic == 'BM':
                t_type = 'IMG'  # raw 32 bit rgba (actually argb)
                # Generic BMP file
                # patch up the sometimes irregular bmp headers
                texfile = self.checkBmp(texfile, texname)
                if texfile == None:
                    return

                # turn bmp into a 32bit rgba true color image
                # returns a tuple of (img, width, height)
                img = self.createAlphaBMP(texfile, texname)
                texfile = img[
                    0]  # so that the addTexture() at the bottom works

                t = Texture()  # create empty texture object
                component_type = Texture.TUnsignedByte
                format = Texture.FRgba
                t.setup2dTexture(img[1], img[2], component_type, format)
                t.setRamImage(img[0])
                #t.load(ti)                  # load texture from pnmimage

            elif magic == 'DD':
                t_type = 'DDS'
                # DDS file
                dds = DDSFile(texfile)
                dds.patchHeader()
                # dds.dumpHeader()
                # dds.uncompressToBmp()
                # dds.save(texname+'.dds')

                ts = StringStream(dds.buf)  # turn into an istream
                t = Texture()  # create texture object
                t.readDds(ts)  # load texture from dds ram image
            else:
                print 'Error unsupported texture: %s magic:%s referenced in fragment: %i' % (
                    texname, magic, f.id)
                return
        else:
            print 'Error: texture %s not found in s3d archive' % (texname)
            return

        # t.setWrapU(Texture.WMClamp)
        # t.setWrapV(Texture.WMClamp)

        self.addTexture(texname, t, texfile, t_type)
Beispiel #27
0
    def makeICNS(self, fn):
        """ Writes the images to an Apple ICNS file.  Returns True on success. """

        if not isinstance(fn, Filename):
            fn = Filename.fromOsSpecific(fn)
        fn.setBinary()

        icns = open(fn, 'wb')
        icns.write(b'icns\0\0\0\0')

        icon_types = {16: b'is32', 32: b'il32', 48: b'ih32', 128: b'it32'}
        mask_types = {16: b's8mk', 32: b'l8mk', 48: b'h8mk', 128: b't8mk'}
        png_types = {256: b'ic08', 512: b'ic09', 1024: b'ic10'}

        pngtype = PNMFileTypeRegistry.getGlobalPtr().getTypeFromExtension(
            "png")

        for size, image in sorted(self.images.items(),
                                  key=lambda item: item[0]):
            if size in png_types and pngtype is not None:
                stream = StringStream()
                image.write(stream, "", pngtype)
                pngdata = stream.data

                icns.write(png_types[size])
                icns.write(struct.pack('>I', len(pngdata)))
                icns.write(pngdata)

            elif size in icon_types:
                # If it has an alpha channel, we write out a mask too.
                if image.hasAlpha():
                    icns.write(mask_types[size])
                    icns.write(struct.pack('>I', size * size + 8))

                    for y in range(size):
                        for x in range(size):
                            icns.write(
                                struct.pack('<B',
                                            int(image.getAlpha(x, y) * 255)))

                icns.write(icon_types[size])
                icns.write(struct.pack('>I', size * size * 4 + 8))

                for y in range(size):
                    for x in range(size):
                        r, g, b = image.getXel(x, y)
                        icns.write(
                            struct.pack('>BBBB', 0, int(r * 255), int(g * 255),
                                        int(b * 255)))

        length = icns.tell()
        icns.seek(4)
        icns.write(struct.pack('>I', length))
        icns.close()

        return True
Beispiel #28
0
def test_multifile_read_empty():
    stream = StringStream(
        b'pmf\x00\n\r\x01\x00\x01\x00\x01\x00\x00\x00\xdb\x9d7\\\x00\x00\x00\x00'
    )
    wrapper = IStreamWrapper(stream)

    m = Multifile()
    assert m.open_read(wrapper)
    assert m.is_read_valid()
    assert m.get_num_subfiles() == 0
    m.close()
    def __init__(self, client, name):
        self.client = client
        self.name = name

        self.notify = directNotify.newCategory('ToontownRPCMethod[%s]' % name)

        self.channel = None
        self.stream = StringStream()
        self.callback = None
        self.errback = None
        self.extraArgs = None
Beispiel #30
0
def textureFromData(image_data, filename=""):
    tex = None

    if image_data:
        myTexture = Texture()

        myImage = PNMImage()
        success = myImage.read(StringStream(image_data), filename)

        if success == 1:
            #PNMImage can handle most texture formats
            myTexture.load(myImage)
        else:
            #Except for DDS, which PNMImage.read will return 0, so try to load as DDS
            success = myTexture.readDds(StringStream(image_data))

        if success != 0:
            tex = myTexture
            tex.setMinfilter(Texture.FTLinearMipmapLinear)

    return tex
def test_streamreader_fixed_string():
    # Zero-length string
    stream = StringStream(b'ABC')
    reader = StreamReader(stream, False)
    assert reader.get_fixed_string(0) == ''

    # Empty stream
    stream = StringStream(b'')
    reader = StreamReader(stream, False)
    assert reader.get_fixed_string(1) == ''

    # Empty string
    stream = StringStream(b'\x00')
    reader = StreamReader(stream, False)
    assert reader.get_fixed_string(1) == ''

    # String of length 1
    stream = StringStream(b'A')
    reader = StreamReader(stream, False)
    assert reader.get_fixed_string(1) == 'A'

    # String of length 1, excess data
    stream = StringStream(b'ABC\x00')
    reader = StreamReader(stream, False)
    assert reader.get_fixed_string(1) == 'A'

    # EOF before end of string
    stream = StringStream(b'AB')
    reader = StreamReader(stream, False)
    assert reader.get_fixed_string(4) == 'AB'
Beispiel #32
0
def test_streamreader_readlines():
    # Empty stream
    stream = StringStream(b'')
    reader = StreamReader(stream, False)
    assert reader.readlines() == []

    # Single line without newline
    stream = StringStream(b'A')
    reader = StreamReader(stream, False)
    assert reader.readlines() == [b'A']
    assert reader.readlines() == []

    # Single newline
    stream = StringStream(b'\n')
    reader = StreamReader(stream, False)
    assert reader.readlines() == [b'\n']
    assert reader.readlines() == []

    # Line with text followed by empty line
    stream = StringStream(b'A\n\n')
    reader = StreamReader(stream, False)
    assert reader.readlines() == [b'A\n', b'\n']
    assert reader.readlines() == []

    # Multiple lines
    stream = StringStream(b'A\nB\nC')
    reader = StreamReader(stream, False)
    assert reader.readlines() == [b'A\n', b'B\n', b'C']
    assert reader.readlines() == []

    # Preserve null byte
    stream = StringStream(b'\x00\x00')
    reader = StreamReader(stream, False)
    assert reader.readlines() == [b'\x00\x00']
def test_streamreader_string():
    # Empty string
    stream = StringStream(b'\x00\x00')
    reader = StreamReader(stream, False)
    assert reader.get_string() == ''

    # String size but no string contents
    stream = StringStream(b'\x01\x00')
    reader = StreamReader(stream, False)
    assert reader.get_string() == ''

    # String of length 1
    stream = StringStream(b'\x01\x00A')
    reader = StreamReader(stream, False)
    assert reader.get_string() == 'A'

    # String with excess data
    stream = StringStream(b'\x01\x00AB')
    reader = StreamReader(stream, False)
    assert reader.get_string() == 'A'

    # EOF before end of string
    stream = StringStream(b'\x03\x00AB')
    reader = StreamReader(stream, False)
    assert reader.get_string() == 'AB'

    # Preserves null bytes
    stream = StringStream(b'\x02\x00\x00\x00')
    reader = StreamReader(stream, False)
    assert reader.get_string() == '\x00\x00'
Beispiel #34
0
def test_zip_write_empty():
    stream = StringStream()
    zip = ZipArchive()
    zip.open_write(stream)

    assert not zip.is_read_valid()
    assert zip.is_write_valid()
    assert not zip.needs_repack()

    zip.close()

    assert stream.data == EMPTY_ZIP
    with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
        assert zf.testzip() is None
Beispiel #35
0
def test_zip_read_empty():
    stream = StringStream(EMPTY_ZIP)
    wrapper = IStreamWrapper(stream)

    zip = ZipArchive()
    zip.open_read(wrapper)

    assert zip.is_read_valid()
    assert not zip.is_write_valid()
    assert not zip.needs_repack()

    assert zip.get_num_subfiles() == 0

    zip.close()
Beispiel #36
0
 def _logClsend(self, senderId, dataStr):
     msgStream = StringStream()
     simbase.air.describeMessage(msgStream, '', dataStr)
     readableStr = msgStream.getData()
     sstream = StringStream()
     PyDatagram(dataStr).dumpHex(sstream)
     hexDump = sstream.getData()
     self.clsendNotify.info(
         '%s [%s]: %s%s' %
         (self.doId, self._clsendCounter, readableStr, hexDump))
class ToontownRPCMethod:
    def __init__(self, client, name):
        self.client = client
        self.name = name

        self.notify = directNotify.newCategory('ToontownRPCMethod[%s]' % name)

        self.channel = None
        self.stream = StringStream()
        self.callback = None
        self.errback = None
        self.extraArgs = None

    def __call__(self, *args, **kwargs):
        self.callback = kwargs.pop('_callback', None)
        self.errback = kwargs.pop('_errback', None)
        self.extraArgs = kwargs.pop('_extraArgs', [])

        if (len(args) > 0) and (len(kwargs) > 0):
            raise ProtocolError('Cannot use both positional and keyword arguments.')

        token = self.generateToken(700)
        if kwargs:
            kwargs['token'] = token
            self.send(kwargs)
        else:
            self.send((token,) + args)

    @staticmethod
    def generateToken(accessLevel):
        data = json.dumps({'timestamp': int(time.mktime(time.gmtime())),
                           'accesslevel': accessLevel})
        iv = os.urandom(AES.block_size)
        webRpcSecret = config.GetString('web-rpc-secret', '6163636f756e7473')
        cipher = AES.new(webRpcSecret, mode=AES.MODE_CBC, IV=iv)
        data += '\x00' * (16 - (len(data) % AES.block_size))
        token = cipher.encrypt(data)
        return base64.b64encode(iv + token)

    def send(self, params):
        if not self.client.url.hasServer():
            if self.errback is not None:
                self.errback(*self.extraArgs)
            return

        self.channel = self.client.http.makeChannel(False)

        self.channel.sendExtraHeader('Accept', 'application/json')
        self.channel.sendExtraHeader('Content-Type', 'application/json')
        self.channel.sendExtraHeader('User-Agent', 'RPCClient (Toontown Infinite; src)')

        data = json.dumps({'jsonrpc': '2.0', 'method': self.name,
                           'params': params, 'id': id(self)})

        ds = DocumentSpec(self.client.url)

        self.channel.beginPostForm(ds, data)
        self.channel.downloadToStream(self.stream)

        self.client.channels[self.channel] = self

    def finish(self):
        if not self.channel.isValid():
            self.notify.warning('Failed to make HTTP request.')
            if self.errback is not None:
                self.errback(*self.extraArgs)
            return

        if not self.channel.isDownloadComplete():
            self.notify.warning('Received an incomplete response.')
            if self.errback is not None:
                self.errback(*self.extraArgs)
            return

        data = self.stream.getData()
        try:
            response = json.loads(data)
        except ValueError:
            self.notify.warning('Received an invalid response.')
            if self.errback is not None:
                self.errback(*self.extraArgs)
            return

        error = response.get('error')
        if error is not None:
            self.notify.warning('Resulted in error: ' + repr(error))
            if self.errback is not None:
                self.errback(*self.extraArgs)
            return

        if self.callback:
            self.callback(response['result'], *self.extraArgs)