-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_extractor.py
133 lines (113 loc) · 4.22 KB
/
test_extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import io
import zipfile
from unittest.mock import Mock, patch
from extractor.extractor import (
extract_bundle,
extract_bundle_from_apk,
extract_bundle_from_device,
pull_apk_from_device,
)
def test_extract_bundle_from_apk(tmp_path):
bundle_in_path = "assets/index.android.bundle"
bundle_out_path = tmp_path / "index.android.bundle"
zip_bytes = io.BytesIO()
with zipfile.ZipFile(zip_bytes, mode="w") as zf:
zf.writestr("AndroidManifest.xml", b"")
zf.writestr("classes.dex", b"")
zf.writestr(bundle_in_path, b"const a=42 ;")
apk_path = tmp_path / "test_extract.apk"
apk_path.write_bytes(zip_bytes.getvalue())
extract_bundle_from_apk(apk_path, bundle_in_path, bundle_out_path)
assert bundle_out_path.exists()
assert bundle_out_path.read_text() == "const a = 42;"
@patch("extractor.extractor.check_adb")
@patch("extractor.extractor.get_packages")
@patch("extractor.extractor.verify_package_exists")
@patch("extractor.extractor.find_package_path")
@patch("extractor.extractor.pull_path")
def test_pull_apk_from_device(
mock_pull_path,
mock_find_package_path,
mock_verify_package_exists,
mock_get_packages,
mock_check_adb,
):
package = "com.example.app"
out_path = "/some/path/app.apk"
mock_check_adb.return_value = None
mock_get_packages.return_value = [package]
mock_verify_package_exists.return_value = None
mock_find_package_path.return_value = (
f"/data/app/{package}-RGW6eku5yrzZ" f"062ftW4_7Q==/base.apk"
)
pull_apk_from_device(package, out_path)
mock_pull_path.assert_called_with(
mock_find_package_path.return_value,
out_path,
)
@patch("tempfile.NamedTemporaryFile")
@patch("extractor.extractor.pull_apk_from_device")
@patch("extractor.extractor.extract_bundle_from_apk")
def test_extract_bundle_from_device(
mock_extract_bundle_from_apk,
mock_pull_apk_from_device,
mock_tmp,
):
package = "com.example.app"
bundle_in_path = "index.android.bundle.in"
bundle_out_path = "index.android.bundle.out"
tmp_filename = "temporary_file"
mock_tmp.return_value.__enter__.return_value.name = tmp_filename
extract_bundle_from_device(package, bundle_in_path, bundle_out_path)
mock_pull_apk_from_device.assert_called_with(package, tmp_filename)
mock_extract_bundle_from_apk.assert_called_with(
tmp_filename,
bundle_in_path,
bundle_out_path,
)
@patch("sys.exit")
@patch("extractor.extractor.is_apk", Mock())
@patch("extractor.extractor.extract_bundle_from_apk", Mock())
@patch("extractor.extractor.extract_bundle_from_device", Mock())
def test_extract_bundle_prints_usage_when_run_without_arguments(mock_exit, capsys):
extract_bundle([])
captured = capsys.readouterr()
assert captured.err.startswith("usage:")
mock_exit.assert_called_with(0)
@patch("sys.exit", Mock())
@patch("extractor.extractor.is_apk", Mock(return_value=True))
@patch("extractor.extractor.extract_bundle_from_apk")
def test_extract_bundle_with_existing_apk(mock_extract_bundle_from_apk):
apk_path = "app.apk"
extract_bundle([apk_path])
mock_extract_bundle_from_apk.assert_called_with(
apk_path,
"assets/index.android.bundle",
"index.android.bundle",
)
@patch("sys.exit", Mock())
@patch("extractor.extractor.extract_bundle_from_device")
def test_extract_bundle_with_package(mock_extract_bundle_from_device):
package = "com.example.app"
extract_bundle([package])
mock_extract_bundle_from_device.assert_called_with(
package,
"assets/index.android.bundle",
"index.android.bundle",
)
@patch("sys.exit")
def test_extract_bundle_with_invalid_package(mock_exit):
extract_bundle(["test"])
mock_exit.assert_called_with(1)
@patch("sys.exit")
@patch("extractor.extractor.parse_args")
def test_extract_bundle_with_keyboard_interrupt(mock_parse_args, mock_exit):
mock_parse_args.side_effect = KeyboardInterrupt
extract_bundle([])
mock_exit.assert_called_with(1)
@patch("sys.exit")
@patch("extractor.extractor.parse_args")
def test_extract_bundle_exits_with_runtime_error(mock_parse_args, mock_exit):
mock_parse_args.side_effect = RuntimeError
extract_bundle([])
mock_exit.assert_called_with(1)