1+ #
2+ # Copyright (c) nexB Inc. and others. All rights reserved.
3+ # ScanCode is a trademark of nexB Inc.
4+ # SPDX-License-Identifier: Apache-2.0
5+ # See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+ # See https://github.com/nexB/extractcode for support or download.
7+ # See https://aboutcode.org for more information about nexB OSS projects.
8+ #
9+
10+ import os
11+ #from pathlib import Path
12+
13+ import pytest
14+ import io
15+ from contextlib import redirect_stdout
16+
17+ from extractcode_assert_utils import BaseArchiveTestCase
18+ from extractcode_assert_utils import check_files
19+
20+ from extractcode import androidappbundle as aab_extractor
21+
22+
23+ class TestExtractAAB (BaseArchiveTestCase ):
24+ test_data_dir = os .path .join (os .path .dirname (__file__ ), 'data' )
25+
26+ def test_can_extract_aab_file (self ):
27+ # Path to the test .aab file
28+ test_file = self .get_test_loc ('androidappbundle/app-release.aab' )
29+ target_dir = self .get_temp_dir ('aab_extraction' )
30+
31+ # Extract the .aab file and get the file map
32+ file_map = aab_extractor .extract (test_file , target_dir )
33+
34+ # Check if expected files are extracted
35+ expected_files = [
36+ 'BUNDLE-METADATA/com.android.tools.build.libraries/dependencies.pb' ,
37+ 'base/manifest/AndroidManifest.xml' ,
38+ 'base/resources.pb' ,
39+ 'BundleConfig.pb' ,
40+ ]
41+ # Verify that all expected files are in the file map
42+ for expected_file in expected_files :
43+ assert expected_file in file_map , f"Expected file { expected_file } not found in the file map"
44+
45+ # Verify that the directories and files are physically created
46+ for expected_file in expected_files :
47+ # Construct the full path to the expected file
48+ full_path = os .path .join (target_dir , expected_file )
49+ # Check if the file exists
50+ assert os .path .exists (full_path ), f"Expected file { full_path } does not exist"
51+ # Check if it is a file (not a directory)
52+ assert os .path .isfile (full_path ), f"Expected file { full_path } is not a file"
53+
54+ # Verify that the directories are created
55+ expected_directories = [
56+ 'BUNDLE-METADATA' ,
57+ 'BUNDLE-METADATA/com.android.tools.build.libraries' ,
58+ 'base' ,
59+ 'base/manifest' ,
60+ ]
61+
62+ for expected_dir in expected_directories :
63+ # Construct the full path to the expected directory
64+ full_path = os .path .join (target_dir , expected_dir )
65+ # Check if the directory exists
66+ assert os .path .exists (full_path ), f"Expected directory { full_path } does not exist"
67+ # Check if it is a directory
68+ assert os .path .isdir (full_path ), f"Expected directory { full_path } is not a directory"
69+
70+ def test_can_identify_aab_file (self ):
71+ # Path to the test .aab file
72+ test_file = self .get_test_loc ('androidappbundle/app-release.aab' )
73+
74+ # Check if the file is identified as an .aab file
75+ assert aab_extractor .is_aab (test_file ) == True
76+
77+ def test_extract_aab_invalid_file (self ):
78+ # Create an invalid .aab file (not a zip file)
79+ invalid_file = os .path .join (self .get_temp_dir (), 'invalid.aab' )
80+ with open (invalid_file , 'w' ) as f :
81+ f .write ('This is not a valid .aab file' )
82+
83+ target_dir = self .get_temp_dir ('aab_extraction_invalid' )
84+
85+ # Attempt to extract the invalid .aab file
86+ with pytest .raises (Exception ):
87+ aab_extractor .extract (invalid_file , target_dir )
88+
89+ def test_extract_aab_nonexistent_file (self ):
90+ # Define a non-existent .aab file
91+ nonexistent_file = "nonexistent.aab"
92+ target_dir = self .get_temp_dir ('aab_extraction_nonexistent' )
93+
94+ # Attempt to extract the non-existent .aab file
95+ with pytest .raises (aab_extractor .ExtractErrorFailedToExtract ):
96+ aab_extractor .extract (nonexistent_file , target_dir )
97+
98+ def test_show_file_map (self ):
99+ # Path to the test .aab file
100+ test_file = self .get_test_loc ('androidappbundle/app-release.aab' )
101+ target_dir = self .get_temp_dir ('aab_extraction' )
102+
103+ # Create an AndroidAppBundle instance and extract the .aab file
104+ aab = aab_extractor .AndroidAppBundle .from_file (test_file )
105+ file_map = aab .extract (target_dir )
106+
107+ # Verify that the file map is not empty
108+ assert file_map , "File map should not be empty after extraction"
109+
110+ # Call show_file_map() and capture the output
111+ output = io .StringIO ()
112+ with redirect_stdout (output ):
113+ aab .show_file_map ()
114+
115+ # Verify that the output contains expected files
116+ output_str = output .getvalue ()
117+ expected_files = [
118+ 'BUNDLE-METADATA/com.android.tools.build.libraries/dependencies.pb' ,
119+ 'base/manifest/AndroidManifest.xml' ,
120+ 'base/resources.pb' ,
121+ 'BundleConfig.pb' ,
122+ ]
123+ for expected_file in expected_files :
124+ assert expected_file in output_str , f"Expected file { expected_file } not found in the output"
0 commit comments