|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import unittest |
| 4 | + |
| 5 | +from mpos import AppManager |
| 6 | + |
| 7 | +class TestAppManagerInstallMpk(unittest.TestCase): |
| 8 | + APP_FULLNAME = "com.micropythonos.ziptest" |
| 9 | + APP_ROOT = f"apps/{APP_FULLNAME}" |
| 10 | + TEMP_MPK = "data/tmp_ziptest_Xr0.mpk" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + self.dest_folder = self.APP_ROOT |
| 14 | + self.temp_mpk = self.TEMP_MPK |
| 15 | + self._remove_path(self.dest_folder) |
| 16 | + self._remove_path(self.temp_mpk) |
| 17 | + try: |
| 18 | + os.stat("data") |
| 19 | + except OSError: |
| 20 | + os.mkdir("data") |
| 21 | + |
| 22 | + def tearDown(self): |
| 23 | + self._remove_path(self.dest_folder) |
| 24 | + self._remove_path(self.temp_mpk) |
| 25 | + |
| 26 | + def _remove_path(self, path): |
| 27 | + try: |
| 28 | + st = os.stat(path) |
| 29 | + except OSError: |
| 30 | + return |
| 31 | + if st[0] & 0x4000: |
| 32 | + shutil.rmtree(path) |
| 33 | + else: |
| 34 | + os.remove(path) |
| 35 | + |
| 36 | + def _copy_file(self, source, dest): |
| 37 | + with open(source, "rb") as source_file: |
| 38 | + with open(dest, "wb") as dest_file: |
| 39 | + while True: |
| 40 | + chunk = source_file.read(1024) |
| 41 | + if not chunk: |
| 42 | + break |
| 43 | + dest_file.write(chunk) |
| 44 | + |
| 45 | + def _exists(self, path): |
| 46 | + try: |
| 47 | + os.stat(path) |
| 48 | + return True |
| 49 | + except OSError: |
| 50 | + return False |
| 51 | + |
| 52 | + def _assert_dir(self, path): |
| 53 | + st = os.stat(path) |
| 54 | + self.assertTrue(st[0] & 0x4000) |
| 55 | + |
| 56 | + def _assert_file_size(self, path, expected_size): |
| 57 | + st = os.stat(path) |
| 58 | + self.assertTrue(st[0] & 0x8000) |
| 59 | + self.assertEqual(st[6], expected_size) |
| 60 | + |
| 61 | + def _assert_app_tree(self, root): |
| 62 | + self._assert_dir(root) |
| 63 | + self._assert_dir(f"{root}/assets") |
| 64 | + self._assert_dir(f"{root}/META-INF") |
| 65 | + self._assert_dir(f"{root}/res") |
| 66 | + self._assert_dir(f"{root}/res/mipmap-mdpi") |
| 67 | + |
| 68 | + self._assert_file_size( |
| 69 | + f"{root}/assets/hello.py", |
| 70 | + 232, |
| 71 | + ) |
| 72 | + self._assert_file_size( |
| 73 | + f"{root}/META-INF/MANIFEST.JSON", |
| 74 | + 406, |
| 75 | + ) |
| 76 | + self._assert_file_size( |
| 77 | + f"{root}/res/mipmap-mdpi/icon_64x64.png", |
| 78 | + 5499, |
| 79 | + ) |
| 80 | + |
| 81 | + def test_install_mpk_extracts_files(self): |
| 82 | + # Uncompressed and without extended attributes: |
| 83 | + source_mpk = "../tests/com.micropythonos.ziptest_Xr0.mpk" |
| 84 | + self._copy_file(source_mpk, self.temp_mpk) |
| 85 | + |
| 86 | + AppManager.install_mpk(self.temp_mpk, self.dest_folder) |
| 87 | + |
| 88 | + self.assertFalse(self._exists(self.temp_mpk)) |
| 89 | + self._assert_app_tree(self.APP_ROOT) |
| 90 | + |
| 91 | + def test_install_mpk_extracts_files_xr(self): |
| 92 | + # Default zip (deflate.RAW) |
| 93 | + source_mpk = "../tests/com.micropythonos.ziptest_r.mpk" |
| 94 | + self._copy_file(source_mpk, self.temp_mpk) |
| 95 | + |
| 96 | + AppManager.install_mpk(self.temp_mpk, self.dest_folder) |
| 97 | + |
| 98 | + self.assertFalse(self._exists(self.temp_mpk)) |
| 99 | + self._assert_app_tree(self.APP_ROOT) |
| 100 | + |
| 101 | + |
| 102 | + def test_install_mpk_extracts_files_topdir(self): |
| 103 | + # Zip contains top dir |
| 104 | + source_mpk = "../tests/com.micropythonos.ziptest_topdir.mpk" |
| 105 | + self._copy_file(source_mpk, self.temp_mpk) |
| 106 | + |
| 107 | + self.dest_folder = "apps/com.micropythonos.ziptest" |
| 108 | + AppManager.install_mpk(self.temp_mpk, self.dest_folder) |
| 109 | + |
| 110 | + self.assertFalse(self._exists(self.temp_mpk)) |
| 111 | + self._assert_app_tree(self.dest_folder) |
| 112 | + |
| 113 | + def test_install_mpk_rejects_invalid_topdir(self): |
| 114 | + # Zip contains top dir that does not match destination name |
| 115 | + source_mpk = "../tests/com.micropythonos.ziptest_invalid_topdir.mpk" |
| 116 | + self._copy_file(source_mpk, self.temp_mpk) |
| 117 | + |
| 118 | + self.dest_folder = "apps/com.micropythonos.ziptest_invalid" |
| 119 | + AppManager.install_mpk(self.temp_mpk, self.dest_folder) |
| 120 | + |
| 121 | + self.assertFalse(self._exists(self.temp_mpk)) |
| 122 | + self.assertFalse(self._exists(self.dest_folder)) |
0 commit comments