Exemplo n.º 1
0
    def check_sapcar_archive(self, filename, version):
        """Test SAP CAR archive file"""

        with open(data_filename(filename), "rb") as fd:
            sapcar_archive = SAPCARArchive(fd, mode="r")

            self.assertEqual(filename, basename(sapcar_archive.filename))
            self.assertEqual(version, sapcar_archive.version)
            self.assertEqual(1, len(sapcar_archive.files))
            self.assertEqual(1, len(sapcar_archive.files_names))
            self.assertListEqual([self.test_filename], sapcar_archive.files_names)
            self.assertListEqual([self.test_filename], sapcar_archive.files.keys())

            af = sapcar_archive.open(self.test_filename)
            self.assertEqual(self.test_string, af.read())
            af.close()

            ff = sapcar_archive.files[self.test_filename]
            self.assertEqual(len(self.test_string), ff.size)
            self.assertEqual(self.test_filename, ff.filename)
            self.assertEqual(self.test_timestamp, ff.timestamp)
            self.assertEqual(self.test_timestamp_raw, ff.timestamp_raw)
            self.assertEqual(self.test_permissions, ff.permissions)
            self.assertEqual(self.test_perm_mode, ff.perm_mode)
            self.assertEqual(version, ff.version)
            self.assertTrue(ff.is_file())
            self.assertFalse(ff.is_directory())

            self.assertTrue(ff.check_checksum())
            self.assertEqual(ff.calculate_checksum(self.test_string), ff.checksum)

            af = ff.open()
            self.assertEqual(self.test_string, af.read())
            af.close()
Exemplo n.º 2
0
    def check_sapcar_archive(self, filename, version):
        """Test SAP CAR archive file version 201"""

        with open(data_filename(filename), "rb") as fd:
            sapcar_archive = SAPCARArchive(fd, mode="r")

            self.assertEqual(filename, basename(sapcar_archive.filename))
            self.assertEqual(version, sapcar_archive.version)
            self.assertEqual(1, len(sapcar_archive.files))
            self.assertEqual(1, len(sapcar_archive.files_names))
            self.assertListEqual([self.test_filename], sapcar_archive.files_names)
            self.assertListEqual([self.test_filename], sapcar_archive.files.keys())

            af = sapcar_archive.open(self.test_filename)
            self.assertEqual(self.test_string, af.read())
            af.close()

            ff = sapcar_archive.files[self.test_filename]
            self.assertEqual(len(self.test_string), ff.size)
            self.assertEqual(self.test_filename, ff.filename)
            self.assertEqual(self.test_timestamp, ff.timestamp)
            self.assertEqual(self.test_permissions, ff.permissions)

            self.assertTrue(ff.check_checksum())
            self.assertEqual(ff.calculate_checksum(self.test_string), ff.checksum)

            af = ff.open()
            self.assertEqual(self.test_string, af.read())
            af.close()
Exemplo n.º 3
0
    def test_sapcar_archive_add_file(self):
        """Test some basic construction of a SAP CAR archive adding from an existent file"""

        ar = SAPCARArchive(self.test_archive_file, "w")
        ar.add_file(self.test_filename)
        ar.add_file(self.test_filename, archive_filename=self.test_filename+"two")

        self.assertEqual("2.01", ar.version)
        self.assertEqual(2, len(ar.files))
        self.assertEqual(2, len(ar.files_names))
        self.assertListEqual([self.test_filename, self.test_filename+"two"], ar.files_names)
        self.assertListEqual([self.test_filename, self.test_filename+"two"], ar.files.keys())

        for filename in [self.test_filename, self.test_filename+"two"]:
            af = ar.open(filename)
            self.assertEqual(self.test_string, af.read())
            af.close()

            ff = ar.files[filename]
            self.assertEqual(len(self.test_string), ff.size)
            self.assertEqual(filename, ff.filename)

            self.assertTrue(ff.check_checksum())
            self.assertEqual(ff.calculate_checksum(self.test_string), ff.checksum)

            af = ff.open()
            self.assertEqual(self.test_string, af.read())
            af.close()

        ar.write()
        ar.close()
Exemplo n.º 4
0
    def test_sapcar_archive_add_file(self):
        """Test some basic construction of a SAP CAR archive adding from an existent file"""

        ar = SAPCARArchive(self.test_archive_file, "w")
        ar.add_file(self.test_filename)
        ar.add_file(self.test_filename,
                    archive_filename=self.test_filename + "two")

        self.assertEqual("2.01", ar.version)
        self.assertEqual(2, len(ar.files))
        self.assertEqual(2, len(ar.files_names))
        self.assertListEqual([self.test_filename, self.test_filename + "two"],
                             ar.files_names)
        self.assertListEqual([self.test_filename, self.test_filename + "two"],
                             ar.files.keys())

        for filename in [self.test_filename, self.test_filename + "two"]:
            af = ar.open(filename)
            self.assertEqual(self.test_string, af.read())
            af.close()

            ff = ar.files[filename]
            self.assertEqual(len(self.test_string), ff.size)
            self.assertEqual(filename, ff.filename)

            self.assertTrue(ff.check_checksum())
            self.assertEqual(ff.calculate_checksum(self.test_string),
                             ff.checksum)

            af = ff.open()
            self.assertEqual(self.test_string, af.read())
            af.close()
Exemplo n.º 5
0
def infect_sar_file(sar_filename, inject_files):
    """ Receives a SAR file and infects it by adding new files

    :type sar_filename: string
    :param sar_filename: name of the SAR file to infect

    :type inject_files: list of strings
    :param inject_files: list of files to inject into the SAR file
    """

    sar = SAPCARArchive(sar_filename, "r+")

    # Add each of the files specified as inject files
    for filename, archive_filename in inject_files:
        sar.add_file(filename, archive_filename=archive_filename)

    # Writes the modified file
    sar.write()
Exemplo n.º 6
0
    def test_sapcar_archive_201_to_200(self):
        """Test SAP CAR archive object conversion from 2.01 to 2.00"""

        with open(data_filename("car201_test_string.sar"), "rb") as fd:
            ar = SAPCARArchive(fd, mode="r")
            ff201 = ar.files[self.test_filename]

            ar.version = SAPCAR_VERSION_200
            ff200 = ar.files[self.test_filename]

            self.assertEqual(ff200.size, ff201.size)
            self.assertEqual(ff200.filename, ff201.filename)
            self.assertEqual(ff200.timestamp, ff201.timestamp)
            self.assertEqual(ff200.permissions, ff201.permissions)
            self.assertEqual(ff200.checksum, ff201.checksum)

            af = ff200.open()
            self.assertEqual(self.test_string, af.read())
            af.close()
Exemplo n.º 7
0
    def test_sapcar_archive_201_to_200(self):
        """Test SAP CAR archive object conversion from 2.01 to 2.00"""

        with open(data_filename("car201_test_string.sar"), "rb") as fd:
            ar = SAPCARArchive(fd, mode="r")
            ff201 = ar.files[self.test_filename]

            ar.version = SAPCAR_VERSION_200
            ff200 = ar.files[self.test_filename]

            self.assertEqual(ff200.size, ff201.size)
            self.assertEqual(ff200.filename, ff201.filename)
            self.assertEqual(ff200.timestamp, ff201.timestamp)
            self.assertEqual(ff200.permissions, ff201.permissions)
            self.assertEqual(ff200.checksum, ff201.checksum)

            af = ff200.open()
            self.assertEqual(self.test_string, af.read())
            af.close()
Exemplo n.º 8
0
    def test_sapcar_archive_file_200_to_201(self):
        """Test SAP CAR archive file object conversion from 2.00 to 2.01"""

        with open(data_filename("car200_test_string.sar"), "rb") as fd200:
            ar200 = SAPCARArchive(fd200, mode="r")
            ff200 = ar200.files[self.test_filename]
            ff201 = SAPCARArchiveFile.from_archive_file(ff200, SAPCAR_VERSION_201)

            self.assertEqual(ff200.size, ff201.size)
            self.assertEqual(ff200.filename, ff201.filename)
            self.assertEqual(ff200.timestamp, ff201.timestamp)
            self.assertEqual(ff200.timestamp_raw, ff201.timestamp_raw)
            self.assertEqual(ff200.permissions, ff201.permissions)
            self.assertEqual(ff200.perm_mode, ff201.perm_mode)
            self.assertEqual(ff200.checksum, ff201.checksum)
            self.assertIs(ff200.is_file(), ff201.is_file())
            self.assertIs(ff200.is_directory(), ff201.is_directory())

            af = ff201.open()
            self.assertEqual(self.test_string, af.read())
            af.close()
Exemplo n.º 9
0
def infect_sar_file(sar_filename, inject_files):
    """ Receives a SAR file and infects it by adding new files

    :type sar_filename: string
    :param sar_filename: name of the SAR file to infect

    :type inject_files: list of strings
    :param inject_files: list of files to inject into the SAR file
    """

    sar = SAPCARArchive(sar_filename, "r+")

    # Add each of the files specified as inject files
    for filename, archive_filename in inject_files:
        sar.add_file(filename, archive_filename=archive_filename)

    # Writes the modified file
    sar.write()