Back to Repositories

Validating Non-Standard Package ID Processing in Apktool

This test suite validates non-standard package ID handling in Android APK resources using AAPT2, focusing on package ID 0x80 instead of the standard 0x7f. The tests ensure proper decoding and building of APKs with custom package IDs while maintaining resource integrity.

Test Coverage Overview

The test suite provides comprehensive coverage of non-standard package ID handling in Android APK resources.

  • Validates package ID 0x80 across resource specifications
  • Tests APK building and decoding processes
  • Verifies values/strings.xml file integrity
  • Confirms AndroidManifest.xml structure preservation

Implementation Analysis

The testing approach uses JUnit framework with a structured setup-execute-verify pattern. The implementation leverages BeforeClass and AfterClass annotations for efficient test resource management.

Key patterns include:
  • Resource directory copying and manipulation
  • APK building and decoding verification
  • Resource table ID validation
  • XML file comparison for integrity checks

Technical Details

Testing tools and configuration:

  • JUnit testing framework
  • AAPT2 for resource processing
  • ExtFile for file system operations
  • ApkBuilder for APK manipulation
  • ResourcesDecoder for resource extraction
  • Custom configuration with verbose logging

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through systematic organization and thorough validation.

  • Proper test lifecycle management
  • Comprehensive resource cleanup
  • Granular test methods for specific functionality
  • Clear assertion messages and validation points
  • Effective use of setup and teardown methods

ibotpeaches/apktool

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/NonStandardPkgIdTest.java

            
/*
 *  Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
 *  Copyright (C) 2010 Connor Tumbleson <[email protected]>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package brut.androlib.aapt2;

import brut.androlib.ApkBuilder;
import brut.androlib.BaseTest;
import brut.androlib.TestUtils;
import brut.androlib.apk.ApkInfo;
import brut.androlib.res.ResourcesDecoder;
import brut.androlib.res.data.ResTable;
import brut.common.BrutException;
import brut.directory.ExtFile;
import brut.util.OS;

import org.junit.*;
import static org.junit.Assert.*;

public class NonStandardPkgIdTest extends BaseTest {
    private static ResTable sResTable;

    @BeforeClass
    public static void beforeClass() throws Exception {
        sTestOrigDir = new ExtFile(sTmpDir, "pkgid8-orig");
        sTestNewDir = new ExtFile(sTmpDir, "pkgid8-new");

        LOGGER.info("Unpacking pkgid8...");
        TestUtils.copyResourceDir(BuildAndDecodeTest.class, "aapt2/pkgid8", sTestOrigDir);

        sConfig.setVerbose(true);

        LOGGER.info("Building pkgid8.apk...");
        ExtFile testApk = new ExtFile(sTmpDir, "pkgid8.apk");
        new ApkBuilder(sTestOrigDir, sConfig).build(testApk);

        LOGGER.info("Decoding pkgid8.apk...");
        ApkInfo testInfo = new ApkInfo(testApk);
        ResourcesDecoder resDecoder = new ResourcesDecoder(testInfo, sConfig);
        OS.mkdir(sTestNewDir);
        resDecoder.decodeResources(sTestNewDir);
        resDecoder.decodeManifest(sTestNewDir);
        sResTable = resDecoder.getResTable();
    }

    @AfterClass
    public static void afterClass() throws Exception {
        sResTable.getApkInfo().getApkFile().close();
    }

    @Test
    public void buildAndDecodeTest() {
        assertTrue(sTestNewDir.isDirectory());
    }

    @Test
    public void valuesStringsTest() throws BrutException {
        compareValuesFiles("values/strings.xml");
    }

    @Test
    public void confirmManifestStructureTest() throws BrutException {
        compareXmlFiles("AndroidManifest.xml");
    }

    @Test
    public void confirmResourcesAreFromPkgId8() throws BrutException {
        assertEquals(0x80, sResTable.getPackageId());

        assertEquals(0x80, sResTable.getResSpec(0x80020000).getPackage().getId());
        assertEquals(0x80, sResTable.getResSpec(0x80020001).getPackage().getId());
        assertEquals(0x80, sResTable.getResSpec(0x80030000).getPackage().getId());
    }
}