예제 #1
0
    def test_deploy_raise_no_package_above_revision3(self):
        """
        if package doesn't contain package.json, raise exception(no package.json) above revision 3
        """
        zip_list = ['nodir_nopackage.zip', 'normal_nopackage.zip']

        for zip_item in zip_list:
            address: 'Address' = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample',
                                             zip_item)
            tx_hash1 = create_tx_hash()
            score_deploy_path: str = get_score_deploy_path(
                self.score_root_path, address, tx_hash1)

            with self.assertRaises(BaseException) as e:
                IconScoreDeployer.deploy(
                    score_deploy_path,
                    self.read_zipfile_as_byte(self.archive_path),
                    Revision.THREE.value)
            self.assertEqual(e.exception.code, ExceptionCode.INVALID_PACKAGE)
            self.assertEqual(e.exception.message, "package.json not found")
            self.assertTrue(os.path.exists(score_deploy_path))

            score_path: str = get_score_path(self.score_root_path, address)
            remove_path(score_path)
예제 #2
0
    def test_deploy_when_score_depth_is_different_above_revision3(self):
        """
        Reads all files from the depth lower than where the file 'package.json' is
        and test deploying successfully.
        """
        zip_list = [('score_registry.zip', [
            'interfaces/__init__.py', 'interfaces/abc_owned.py',
            'interfaces/abc_score_registry.py', 'package.json',
            'score_registry/__init__.py', 'score_registry/score_registry.py',
            'utility/__init__.py', 'utility/owned.py', 'utility/utils.py'
        ]), ('fakedir.zip', ['__init__.py', 'call_class1.py', 'package.json']),
                    ('nodir.zip',
                     ['__init__.py', 'package.json', 'sample_token.py'])]

        for zip_file, expected_list in zip_list:
            address: 'Address' = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample',
                                             zip_file)
            tx_hash1 = create_tx_hash()
            score_deploy_path: str = get_score_deploy_path(
                self.score_root_path, address, tx_hash1)

            IconScoreDeployer.deploy(
                score_deploy_path,
                self.read_zipfile_as_byte(self.archive_path),
                Revision.THREE.value)
            self.assertEqual(True, os.path.exists(score_deploy_path))

            installed_files = self.get_installed_files(score_deploy_path)
            installed_files.sort()
            self.assertEqual(installed_files, expected_list)

            score_path: str = get_score_path(self.score_root_path, address)
            remove_path(score_path)
예제 #3
0
    def test_deploy_when_score_depth_is_different(self):
        """
        Reads all files from the depth lower than where the file 'package.json' is
        and test deploying successfully.
        """
        zip_list = ['score_registry.zip', 'fakedir.zip', 'nodir.zip']

        for zip_item in zip_list:
            address: 'Address' = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample',
                                             zip_item)
            tx_hash1 = create_tx_hash()
            score_deploy_path: str = get_score_deploy_path(
                self.score_root_path, address, tx_hash1)

            IconScoreDeployer.deploy(
                score_deploy_path,
                self.read_zipfile_as_byte(self.archive_path))
            self.assertEqual(True, os.path.exists(score_deploy_path))

            zip_file_info_gen = IconScoreDeployer._extract_files_gen(
                self.read_zipfile_as_byte(self.archive_path))
            file_path_list = [
                name for name, info, parent_dir in zip_file_info_gen
            ]

            installed_contents = self.get_installed_files(score_deploy_path)
            self.assertTrue(
                self.check_package_json_validity(installed_contents))
            installed_contents.sort()
            file_path_list.sort()
            self.assertEqual(installed_contents, file_path_list)

            score_path: str = get_score_path(self.score_root_path, address)
            remove_path(score_path)
예제 #4
0
    def test_remove_existing_score(self):
        tx_hash: bytes = create_tx_hash()
        score_deploy_path: str = get_score_deploy_path(self.score_root_path,
                                                       self.address, tx_hash)

        self.normal_score_path = os.path.join(DIRECTORY_PATH, 'sample',
                                              'normal_score.zip')
        IconScoreDeployer.deploy(
            score_deploy_path,
            self.read_zipfile_as_byte(self.normal_score_path))
        remove_path(score_deploy_path)
        self.assertFalse(os.path.exists(score_deploy_path))
예제 #5
0
    def test_install(self):
        self.normal_score_path = os.path.join(DIRECTORY_PATH, 'sample',
                                              'normal_score.zip')
        self.bad_zip_file_path = os.path.join(DIRECTORY_PATH, 'sample',
                                              'badzipfile.zip')
        self.inner_dir_path = os.path.join(DIRECTORY_PATH, 'sample',
                                           'innerdir.zip')

        # Case when the user install SCORE first time.
        tx_hash1 = create_tx_hash()
        score_deploy_path: str = get_score_deploy_path(self.score_root_path,
                                                       self.address, tx_hash1)

        IconScoreDeployer.deploy(
            score_deploy_path,
            self.read_zipfile_as_byte(self.normal_score_path))
        self.assertEqual(True, os.path.exists(score_deploy_path))

        zip_file_info_gen = IconScoreDeployer._extract_files_gen(
            self.read_zipfile_as_byte(self.normal_score_path))
        file_path_list = [name for name, info, parent_dir in zip_file_info_gen]

        installed_contents = self.get_installed_files(score_deploy_path)
        self.assertTrue(self.check_package_json_validity(installed_contents))
        installed_contents.sort()
        file_path_list.sort()
        self.assertEqual(installed_contents, file_path_list)

        # Case when installing SCORE with bad-zip-file Data.
        tx_hash2 = create_tx_hash()
        score_deploy_path: str = get_score_deploy_path(self.score_root_path,
                                                       self.address, tx_hash2)

        with self.assertRaises(BaseException) as e:
            IconScoreDeployer.deploy(
                score_deploy_path,
                self.read_zipfile_as_byte(self.bad_zip_file_path))
        self.assertEqual(e.exception.code, ExceptionCode.INVALID_PACKAGE)
        self.assertTrue(os.path.exists(score_deploy_path))

        # Case when the user specifies an installation path that does not have permission.
        score_deploy_path: str = get_score_deploy_path('/', self.address,
                                                       tx_hash1)
        with self.assertRaises(BaseException) as e:
            IconScoreDeployer.deploy(
                score_deploy_path,
                self.read_zipfile_as_byte(self.normal_score_path))
        # On MacOS OSError is raised which is different from Linux
        # self.assertIsInstance(e.exception, PermissionError)

        # Case when the user try to install scores inner directories.
        tx_hash3 = create_tx_hash()
        score_deploy_path: str = get_score_deploy_path(self.score_root_path,
                                                       self.address, tx_hash3)
        IconScoreDeployer.deploy(
            score_deploy_path, self.read_zipfile_as_byte(self.inner_dir_path))
        self.assertEqual(True, os.path.exists(score_deploy_path))
class TestIconScoreDeployer(unittest.TestCase):
    def setUp(self):
        self.deployer = IconScoreDeployer('./')
        self.address = create_address(AddressPrefix.CONTRACT)
        self.archive_path = os.path.join(DIRECTORY_PATH, 'sample', 'valid.zip')
        self.archive_path2 = os.path.join(DIRECTORY_PATH, 'sample',
                                          'invalid.zip')
        self.archive_path3 = os.path.join(DIRECTORY_PATH, 'sample',
                                          'valid.zip')
        self.score_root_path = os.path.join(self.deployer.score_root_path,
                                            str(self.address.to_bytes().hex()))
        self.deployer2 = IconScoreDeployer('/')

    @staticmethod
    def read_zipfile_as_byte(archive_path: str) -> bytes:
        with open(archive_path, 'rb') as f:
            byte_data = f.read()
            return byte_data

    def test_install(self):
        # Case when the user install SCORE first time.
        tx_hash1 = create_tx_hash()
        self.deployer.deploy(self.address,
                             self.read_zipfile_as_byte(self.archive_path),
                             tx_hash1)
        converted_tx_hash = f'0x{bytes.hex(tx_hash1)}'
        install_path = os.path.join(self.score_root_path, converted_tx_hash)
        zip_file_info_gen = self.deployer._extract_files_gen(
            self.read_zipfile_as_byte(self.archive_path))
        file_path_list = [name for name, info, parent_dir in zip_file_info_gen]

        installed_contents = []
        for directory, dirs, filename in os.walk(install_path):
            parent_directory_index = directory.rfind('/')
            parent_dir_name = directory[parent_directory_index + 1:]
            for file in filename:
                if parent_dir_name == f'0x{bytes.hex(tx_hash1)}':
                    installed_contents.append(file)
                else:
                    installed_contents.append(f'{parent_dir_name}/{file}')
        self.assertEqual(True, os.path.exists(install_path))
        self.assertTrue(installed_contents.sort() == file_path_list.sort())

        # Case when the user install SCORE second time.
        with self.assertRaises(BaseException) as e:
            self.deployer.deploy(self.address,
                                 self.read_zipfile_as_byte(self.archive_path),
                                 tx_hash1)
        self.assertEqual(e.exception.code, ExceptionCode.INVALID_PARAMS)

        # Case when installing SCORE with badzipfile Data.
        tx_hash2 = create_tx_hash()
        with self.assertRaises(BaseException) as e:
            self.deployer.deploy(self.address,
                                 self.read_zipfile_as_byte(self.archive_path2),
                                 tx_hash2)
        self.assertEqual(e.exception.code, ExceptionCode.INVALID_PARAMS)
        converted_tx_hash = f'0x{bytes.hex(tx_hash2)}'
        install_path2 = os.path.join(self.score_root_path, converted_tx_hash)
        self.assertFalse(os.path.exists(install_path2))

        # Case when The user specifies an installation path that does not have permission.
        with self.assertRaises(BaseException) as e:
            self.deployer2.deploy(self.address,
                                  self.read_zipfile_as_byte(self.archive_path),
                                  tx_hash1)
        self.assertIsInstance(e.exception, PermissionError)

        # Case when the user try to install scores without directories.
        tx_hash3 = create_tx_hash()
        converted_tx_hash = f'0x{bytes.hex(tx_hash3)}'
        self.deployer.deploy(self.address,
                             self.read_zipfile_as_byte(self.archive_path3),
                             tx_hash3)
        install_path3 = os.path.join(self.score_root_path, converted_tx_hash)
        self.assertEqual(True, os.path.exists(install_path3))

    def test_remove_existing_score(self):
        tx_hash = create_tx_hash()
        converted_tx_hash = f'0x{bytes.hex(tx_hash)}'
        install_path = os.path.join(self.score_root_path, converted_tx_hash)
        self.deployer.deploy(self.address,
                             self.read_zipfile_as_byte(self.archive_path),
                             tx_hash)
        self.deployer.remove_existing_score(install_path)
        self.assertFalse(os.path.exists(install_path))

    def test_deploy_when_score_depth_is_different(self):
        """
        Reads all files from the depth lower than where the file 'package.json' is
        and test deploying successfully.
        """
        zip_list = [
            "valid.zip", "sample_token.zip", "sample_token01.zip",
            'test_score01.zip', 'test_score02.zip', 'test_score02_2.zip'
        ]
        for zip in zip_list:
            self.deployer = IconScoreDeployer('./')
            self.address = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample', zip)
            self.score_root_path = os.path.join(
                self.deployer.score_root_path,
                str(self.address.to_bytes().hex()))
            tx_hash1 = create_tx_hash()
            self.deployer.deploy(self.address,
                                 self.read_zipfile_as_byte(self.archive_path),
                                 tx_hash1)
            converted_tx_hash = f'0x{bytes.hex(tx_hash1)}'
            install_path = os.path.join(self.score_root_path,
                                        converted_tx_hash)
            zip_file_info_gen = self.deployer._extract_files_gen(
                self.read_zipfile_as_byte(self.archive_path))
            file_path_list = [
                name for name, info, parent_dir in zip_file_info_gen
            ]

            installed_contents = []
            for directory, dirs, filename in os.walk(install_path):
                parent_directory_index = directory.rfind('/')
                parent_dir_name = directory[parent_directory_index + 1:]
                for file in filename:
                    if parent_dir_name == f'0x{bytes.hex(tx_hash1)}':
                        installed_contents.append(file)
                    else:
                        installed_contents.append(f'{parent_dir_name}/{file}')
            self.assertEqual(True, os.path.exists(install_path))
            self.assertTrue(installed_contents.sort() == file_path_list.sort())
            IconScoreDeployer.remove_existing_score(self.score_root_path)

    def tearDown(self):
        IconScoreDeployer.remove_existing_score(self.score_root_path)