コード例 #1
0
ファイル: test_ogg.py プロジェクト: douglaskastle/mutagen
    def test_replace_continued(self):
        # take a partial packet and replace it with a new page
        # replace() should make it spanning again
        fileobj = BytesIO()
        pages = [OggPage(), OggPage()]
        pages[0].serial = 1
        pages[0].sequence = 0
        pages[0].complete = False
        pages[0].packets = [b"foo"]
        pages[1].serial = 1
        pages[1].sequence = 1
        pages[1].continued = True
        pages[1].packets = [b"bar"]
        fileobj = BytesIO()
        for page in pages:
            fileobj.write(page.write())

        fileobj.seek(0, 0)
        pages_from_file = [OggPage(fileobj), OggPage(fileobj)]
        self.assertEqual(OggPage.to_packets(pages_from_file), [b"foobar"])
        packets_part = OggPage.to_packets([pages_from_file[0]])
        self.assertEqual(packets_part, [b"foo"])
        new_pages = OggPage.from_packets([b"quuux"])
        OggPage.replace(fileobj, [pages_from_file[0]], new_pages)

        fileobj.seek(0, 0)
        written = OggPage.to_packets([OggPage(fileobj), OggPage(fileobj)])
        self.assertEquals(written, [b"quuuxbar"])
コード例 #2
0
    def test_replace_continued(self):
        # take a partial packet and replace it with a new page
        # replace() should make it spanning again
        fileobj = BytesIO()
        pages = [OggPage(), OggPage()]
        pages[0].serial = 1
        pages[0].sequence = 0
        pages[0].complete = False
        pages[0].packets = [b"foo"]
        pages[1].serial = 1
        pages[1].sequence = 1
        pages[1].continued = True
        pages[1].packets = [b"bar"]
        fileobj = BytesIO()
        for page in pages:
            fileobj.write(page.write())

        fileobj.seek(0, 0)
        pages_from_file = [OggPage(fileobj), OggPage(fileobj)]
        self.assertEqual(OggPage.to_packets(pages_from_file), [b"foobar"])
        packets_part = OggPage.to_packets([pages_from_file[0]])
        self.assertEqual(packets_part, [b"foo"])
        new_pages = OggPage.from_packets([b"quuux"])
        OggPage.replace(fileobj, [pages_from_file[0]], new_pages)

        fileobj.seek(0, 0)
        written = OggPage.to_packets([OggPage(fileobj), OggPage(fileobj)])
        self.assertEquals(written, [b"quuuxbar"])
コード例 #3
0
    def test_replace_fast_path(self):
        # create interleaved pages
        fileobj = BytesIO()
        pages = [OggPage(), OggPage(), OggPage()]
        pages[0].serial = 42
        pages[0].sequence = 0
        pages[0].packets = [b"foo"]
        pages[1].serial = 24
        pages[1].sequence = 0
        pages[1].packets = [b"bar"]
        pages[2].serial = 42
        pages[2].sequence = 1
        pages[2].packets = [b"baz"]
        for page in pages:
            fileobj.write(page.write())

        fileobj.seek(0, 0)
        pages_from_file = [
            OggPage(fileobj),
            OggPage(fileobj),
            OggPage(fileobj)
        ]

        old_pages = [pages_from_file[0], pages_from_file[2]]
        packets = OggPage.to_packets(old_pages, strict=True)
        self.assertEqual(packets, [b"foo", b"baz"])
        new_packets = [b"111", b"222"]
        new_pages = OggPage._from_packets_try_preserve(new_packets, old_pages)
        self.assertEqual(len(new_pages), 2)

        # remove insert_bytes, so we can be sure the fast path was taken
        old_insert_bytes = _util.insert_bytes
        _util.insert_bytes = None
        try:
            OggPage.replace(fileobj, old_pages, new_pages)
        finally:
            _util.insert_bytes = old_insert_bytes

        # validate that the new data was written and the other pages
        # are untouched
        fileobj.seek(0, 0)
        pages_from_file = [
            OggPage(fileobj),
            OggPage(fileobj),
            OggPage(fileobj)
        ]
        packets = OggPage.to_packets([pages_from_file[0], pages_from_file[2]],
                                     strict=True)
        self.assertEqual(packets, [b"111", b"222"])
        packets = OggPage.to_packets([pages_from_file[1]], strict=True)
        self.assertEqual(packets, [b"bar"])
コード例 #4
0
 def test_renumber_extradata(self):
     fileobj = BytesIO()
     for page in self.pages:
         fileobj.write(page.write())
     fileobj.write(b"left over data")
     fileobj.seek(0)
     # Trying to rewrite should raise an error...
     self.failUnlessRaises(Exception, OggPage.renumber, fileobj, 1, 10)
     fileobj.seek(0)
     # But the already written data should remain valid,
     pages = [OggPage(fileobj) for i in range(3)]
     self.failUnlessEqual([page.sequence for page in pages], [10, 11, 12])
     # And the garbage that caused the error should be okay too.
     self.failUnlessEqual(fileobj.read(), b"left over data")
コード例 #5
0
ファイル: test_ogg.py プロジェクト: svetlio2/hackbg
 def test_renumber_extradata(self):
     fileobj = BytesIO()
     for page in self.pages:
         fileobj.write(page.write())
     fileobj.write(b"left over data")
     fileobj.seek(0)
     # Trying to rewrite should raise an error...
     self.failUnlessRaises(Exception, OggPage.renumber, fileobj, 1, 10)
     fileobj.seek(0)
     # But the already written data should remain valid,
     pages = [OggPage(fileobj) for i in range(3)]
     self.failUnlessEqual([page.sequence for page in pages], [10, 11, 12])
     # And the garbage that caused the error should be okay too.
     self.failUnlessEqual(fileobj.read(), b"left over data")
コード例 #6
0
ファイル: test_ogg.py プロジェクト: dmoon4117/mutagen
 def test_renumber_muxed(self):
     pages = [OggPage() for i in range(10)]
     for seq, page in enumerate(pages[0:1] + pages[2:]):
         page.serial = 0
         page.sequence = seq
     pages[1].serial = 2
     pages[1].sequence = 100
     data = BytesIO(b"".join([page.write() for page in pages]))
     OggPage.renumber(data, 0, 20)
     data.seek(0)
     pages = [OggPage(data) for i in range(10)]
     self.failUnlessEqual(pages[1].serial, 2)
     self.failUnlessEqual(pages[1].sequence, 100)
     pages.pop(1)
     self.failUnlessEqual([page.sequence for page in pages], list(range(20, 29)))
コード例 #7
0
 def test_renumber_muxed(self):
     pages = [OggPage() for i in range(10)]
     for seq, page in enumerate(pages[0:1] + pages[2:]):
         page.serial = 0
         page.sequence = seq
     pages[1].serial = 2
     pages[1].sequence = 100
     data = BytesIO(b"".join([page.write() for page in pages]))
     OggPage.renumber(data, 0, 20)
     data.seek(0)
     pages = [OggPage(data) for i in range(10)]
     self.failUnlessEqual(pages[1].serial, 2)
     self.failUnlessEqual(pages[1].sequence, 100)
     pages.pop(1)
     self.failUnlessEqual([page.sequence for page in pages],
                          list(range(20, 29)))
コード例 #8
0
    def test_resize_dev_full(self):
        def raise_no_space(*args):
            raise IOError(errno.ENOSPC, os.strerror(errno.ENOSPC))

        # fail on first write
        h = BytesIO(b"abc")
        h.write = raise_no_space
        self.assertRaises(IOError, resize_file, h, 1)
        h.seek(0, 2)
        self.assertEqual(h.tell(), 3)

        # fail on flush
        h = BytesIO(b"abc")
        h.flush = raise_no_space
        self.assertRaises(IOError, resize_file, h, 1)
        h.seek(0, 2)
        self.assertEqual(h.tell(), 3)
コード例 #9
0
ファイル: test_ogg.py プロジェクト: douglaskastle/mutagen
    def test_replace_fast_path(self):
        # create interleaved pages
        fileobj = BytesIO()
        pages = [OggPage(), OggPage(), OggPage()]
        pages[0].serial = 42
        pages[0].sequence = 0
        pages[0].packets = [b"foo"]
        pages[1].serial = 24
        pages[1].sequence = 0
        pages[1].packets = [b"bar"]
        pages[2].serial = 42
        pages[2].sequence = 1
        pages[2].packets = [b"baz"]
        for page in pages:
            fileobj.write(page.write())

        fileobj.seek(0, 0)
        pages_from_file = [OggPage(fileobj), OggPage(fileobj),
                           OggPage(fileobj)]

        old_pages = [pages_from_file[0], pages_from_file[2]]
        packets = OggPage.to_packets(old_pages, strict=True)
        self.assertEqual(packets, [b"foo", b"baz"])
        new_packets = [b"111", b"222"]
        new_pages = OggPage._from_packets_try_preserve(new_packets, old_pages)
        self.assertEqual(len(new_pages), 2)

        # remove insert_bytes, so we can be sure the fast path was taken
        old_insert_bytes = _util.insert_bytes
        _util.insert_bytes = None
        try:
            OggPage.replace(fileobj, old_pages, new_pages)
        finally:
            _util.insert_bytes = old_insert_bytes

        # validate that the new data was written and the other pages
        # are untouched
        fileobj.seek(0, 0)
        pages_from_file = [OggPage(fileobj), OggPage(fileobj),
                           OggPage(fileobj)]
        packets = OggPage.to_packets(
            [pages_from_file[0], pages_from_file[2]], strict=True)
        self.assertEqual(packets, [b"111", b"222"])
        packets = OggPage.to_packets([pages_from_file[1]], strict=True)
        self.assertEqual(packets, [b"bar"])
コード例 #10
0
ファイル: test__util.py プロジェクト: quodlibet/mutagen
    def test_resize_dev_full(self):

        def raise_no_space(*args):
            raise IOError(errno.ENOSPC, os.strerror(errno.ENOSPC))

        # fail on first write
        h = BytesIO(b"abc")
        h.write = raise_no_space
        self.assertRaises(IOError, resize_file, h, 1)
        h.seek(0, 2)
        self.assertEqual(h.tell(), 3)

        # fail on flush
        h = BytesIO(b"abc")
        h.flush = raise_no_space
        self.assertRaises(IOError, resize_file, h, 1)
        h.seek(0, 2)
        self.assertEqual(h.tell(), 3)
コード例 #11
0
    def test_replace(self):
        # create interleaved pages
        fileobj = BytesIO()
        pages = [OggPage(), OggPage(), OggPage()]
        pages[0].serial = 42
        pages[0].sequence = 0
        pages[0].packets = [b"foo"]
        pages[1].serial = 24
        pages[1].sequence = 0
        pages[1].packets = [b"bar"]
        pages[2].serial = 42
        pages[2].sequence = 1
        pages[2].packets = [b"baz"]
        for page in pages:
            fileobj.write(page.write())

        fileobj.seek(0, 0)
        pages_from_file = [
            OggPage(fileobj),
            OggPage(fileobj),
            OggPage(fileobj)
        ]

        old_pages = [pages_from_file[0], pages_from_file[2]]
        packets = OggPage.to_packets(old_pages, strict=True)
        self.assertEqual(packets, [b"foo", b"baz"])
        new_packets = [b"1111", b"2222"]
        new_pages = OggPage.from_packets(new_packets,
                                         sequence=old_pages[0].sequence)
        self.assertEqual(len(new_pages), 1)
        OggPage.replace(fileobj, old_pages, new_pages)

        fileobj.seek(0, 0)
        first = OggPage(fileobj)
        self.assertEqual(first.serial, 42)
        self.assertEqual(OggPage.to_packets([first], strict=True),
                         [b"1111", b"2222"])
        second = OggPage(fileobj)
        self.assertEqual(second.serial, 24)
        self.assertEqual(OggPage.to_packets([second], strict=True), [b"bar"])
コード例 #12
0
    def test_renumber(self):
        self.failUnlessEqual([page.sequence for page in self.pages], [0, 1, 2])
        fileobj = BytesIO()
        for page in self.pages:
            fileobj.write(page.write())
        fileobj.seek(0)
        OggPage.renumber(fileobj, 1, 10)
        fileobj.seek(0)
        pages = [OggPage(fileobj) for i in range(3)]
        self.failUnlessEqual([page.sequence for page in pages], [10, 11, 12])

        fileobj.seek(0)
        OggPage.renumber(fileobj, 1, 20)
        fileobj.seek(0)
        pages = [OggPage(fileobj) for i in range(3)]
        self.failUnlessEqual([page.sequence for page in pages], [20, 21, 22])
コード例 #13
0
ファイル: test_ogg.py プロジェクト: dmoon4117/mutagen
    def test_renumber(self):
        self.failUnlessEqual([page.sequence for page in self.pages], [0, 1, 2])
        fileobj = BytesIO()
        for page in self.pages:
            fileobj.write(page.write())
        fileobj.seek(0)
        OggPage.renumber(fileobj, 1, 10)
        fileobj.seek(0)
        pages = [OggPage(fileobj) for i in range(3)]
        self.failUnlessEqual([page.sequence for page in pages], [10, 11, 12])

        fileobj.seek(0)
        OggPage.renumber(fileobj, 1, 20)
        fileobj.seek(0)
        pages = [OggPage(fileobj) for i in range(3)]
        self.failUnlessEqual([page.sequence for page in pages], [20, 21, 22])
コード例 #14
0
ファイル: test_ogg.py プロジェクト: douglaskastle/mutagen
    def test_replace(self):
        # create interleaved pages
        fileobj = BytesIO()
        pages = [OggPage(), OggPage(), OggPage()]
        pages[0].serial = 42
        pages[0].sequence = 0
        pages[0].packets = [b"foo"]
        pages[1].serial = 24
        pages[1].sequence = 0
        pages[1].packets = [b"bar"]
        pages[2].serial = 42
        pages[2].sequence = 1
        pages[2].packets = [b"baz"]
        for page in pages:
            fileobj.write(page.write())

        fileobj.seek(0, 0)
        pages_from_file = [OggPage(fileobj), OggPage(fileobj),
                           OggPage(fileobj)]

        old_pages = [pages_from_file[0], pages_from_file[2]]
        packets = OggPage.to_packets(old_pages, strict=True)
        self.assertEqual(packets, [b"foo", b"baz"])
        new_packets = [b"1111", b"2222"]
        new_pages = OggPage.from_packets(new_packets,
                                         sequence=old_pages[0].sequence)
        self.assertEqual(len(new_pages), 1)
        OggPage.replace(fileobj, old_pages, new_pages)

        fileobj.seek(0, 0)
        first = OggPage(fileobj)
        self.assertEqual(first.serial, 42)
        self.assertEqual(OggPage.to_packets([first], strict=True),
                         [b"1111", b"2222"])
        second = OggPage(fileobj)
        self.assertEqual(second.serial, 24)
        self.assertEqual(OggPage.to_packets([second], strict=True), [b"bar"])