diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..1bf2559 --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,23 @@ +name: Python application + +on: + push: + branches: ["*"] + pull_request: + branches: ["*"] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Run tests + run: | + pytest diff --git a/main.py b/main.py index fc45fe8..c3e6fc5 100644 --- a/main.py +++ b/main.py @@ -104,23 +104,30 @@ def __init__(self, framework, directories): input_path = 'input/' + +def detect_frameworks(app_path): + """Return a list of detected frameworks for the given APK path.""" + detected_frameworks = [] + with zipfile.ZipFile(app_path, 'r') as zip_object: + file_names = zip_object.namelist() + + for tech in tech_list: + if any( + any(directory in file_name for file_name in file_names) + for directory in tech.directories + ): + detected_frameworks.append(tech.framework) + + if not detected_frameworks: + detected_frameworks.append(FrameWork.NATIVE) + + return detected_frameworks + def main(): app_name = get_app_name() - detected_frameworks = [] try: - with zipfile.ZipFile(app_name, 'r') as zipObject: - file_names = zipObject.namelist() - # Uncomment the line below to extract the list of files in the apk to the output directory - # zipObject.extractall('output') - - for tech in tech_list: - if any(any(file_name.find(directory) != -1 for file_name in file_names) - for directory in tech.directories): - detected_frameworks.append(tech.framework) - - if not detected_frameworks: - detected_frameworks.append(FrameWork.NATIVE) + detected_frameworks = detect_frameworks(app_name) except FileNotFoundError: print(f"File {app_name} not found.") return diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/tests/test_detector.py b/tests/test_detector.py new file mode 100644 index 0000000..fc8d9b0 --- /dev/null +++ b/tests/test_detector.py @@ -0,0 +1,54 @@ +import os +import sys +import tempfile +import zipfile + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) +from main import detect_frameworks, FrameWork + + +def create_test_apk(entries): + tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.apk') + with zipfile.ZipFile(tmp.name, 'w') as zf: + for entry in entries: + zf.writestr(entry, b'') + tmp.close() + return tmp.name + + +def test_flutter_detection(): + apk = create_test_apk(['libflutter.so']) + try: + result = detect_frameworks(apk) + assert result == [FrameWork.FLUTTER] + finally: + os.remove(apk) + + +def test_react_native_detection(): + apk = create_test_apk(['libreactnativejni.so']) + try: + result = detect_frameworks(apk) + assert result == [FrameWork.REACT_NATIVE] + finally: + os.remove(apk) + + +def test_multiple_detection(): + apk = create_test_apk(['libflutter.so', 'libreactnativejni.so']) + try: + result = detect_frameworks(apk) + assert FrameWork.FLUTTER in result + assert FrameWork.REACT_NATIVE in result + assert len(result) == 2 + finally: + os.remove(apk) + + +def test_native_fallback(): + apk = create_test_apk(['somefile.txt']) + try: + result = detect_frameworks(apk) + assert result == [FrameWork.NATIVE] + finally: + os.remove(apk)