Back to Repositories

Testing Unknown Compression Handling in Apktool APK Processing

This test suite validates compression handling in APK files using Apktool, focusing on various file extensions and their expected compression levels. It ensures proper compression behavior during APK decoding and rebuilding processes.

Test Coverage Overview

The test suite provides comprehensive coverage of compression-related functionality in APK file handling:
  • Verification of PKM file compression levels
  • Testing of double extension file storage
  • JSON file compression validation
  • PNG file storage verification

Implementation Analysis

The testing approach utilizes JUnit framework with a structured setup using @BeforeClass for initial APK preparation. Tests compare compression levels between original and rebuilt APK files, implementing precise assertions for compression validation.

The implementation leverages ApkDecoder and ApkBuilder classes for APK manipulation, with specific focus on file compression attributes.

Technical Details

Key technical components include:
  • JUnit test framework integration
  • ExtFile utility for directory handling
  • Custom configuration for AAPT version 1
  • BrutException handling for error cases
  • Compression level comparison using getCompressionLevel()

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation and setup using @BeforeClass
  • Clear test method naming conventions
  • Specific assertion checks for both positive and negative cases
  • Organized test structure with separate test cases for different file types
  • Comprehensive logging for debugging purposes

ibotpeaches/apktool

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/UnknownCompressionTest.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.aapt1;

import brut.androlib.ApkBuilder;
import brut.androlib.ApkDecoder;
import brut.androlib.BaseTest;
import brut.androlib.TestUtils;
import brut.directory.ExtFile;
import brut.common.BrutException;

import java.io.File;

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

public class UnknownCompressionTest extends BaseTest {

    @BeforeClass
    public static void beforeClass() throws Exception {
        TestUtils.copyResourceDir(UnknownCompressionTest.class, "aapt1/unknown_compression", sTmpDir);

        sConfig.setFrameworkDirectory(sTmpDir.getAbsolutePath());
        sConfig.setAaptVersion(1);

        LOGGER.info("Building deflated_unknowns.apk...");
        sTestOrigDir = new ExtFile(sTmpDir, "deflated_unknowns.apk");
        ExtFile testDir = new ExtFile(sTestOrigDir + ".out");
        new ApkDecoder(sTestOrigDir, sConfig).decode(testDir);

        LOGGER.info("Decoding deflated_unknowns.apk...");
        new ApkBuilder(testDir, sConfig).build(null);
        sTestNewDir = new ExtFile(testDir, "dist/" + sTestOrigDir.getName());
    }

    @Test
    public void pkmExtensionDeflatedTest() throws BrutException {
        String fileName = "assets/bin/Data/test.pkm";
        Integer control = sTestOrigDir.getDirectory().getCompressionLevel(fileName);
        Integer rebuilt = sTestNewDir.getDirectory().getCompressionLevel(fileName);

        // Check that control = rebuilt (both deflated)
        // Add extra check for checking not equal to 0, just in case control gets broken
        assertEquals(control, rebuilt);
        assertNotSame(Integer.valueOf(0), rebuilt);
    }

    @Test
    public void doubleExtensionStoredTest() throws BrutException {
        String fileName = "assets/bin/Data/two.extension.file";
        Integer control = sTestOrigDir.getDirectory().getCompressionLevel(fileName);
        Integer rebuilt = sTestNewDir.getDirectory().getCompressionLevel(fileName);

        // Check that control = rebuilt (both stored)
        // Add extra check for checking = 0 to enforce check for stored just in case control breaks
        assertEquals(control, rebuilt);
        assertEquals(Integer.valueOf(0), rebuilt);
    }

    @Test
    public void confirmJsonFileIsDeflatedTest() throws BrutException {
        String fileName = "test.json";
        Integer control = sTestOrigDir.getDirectory().getCompressionLevel(fileName);
        Integer rebuilt = sTestNewDir.getDirectory().getCompressionLevel(fileName);

        assertEquals(control, rebuilt);
        assertEquals(Integer.valueOf(8), rebuilt);
    }

    @Test
    public void confirmPngFileIsStoredTest() throws BrutException {
        String fileName = "950x150.png";
        Integer control = sTestOrigDir.getDirectory().getCompressionLevel(fileName);
        Integer rebuilt = sTestNewDir.getDirectory().getCompressionLevel(fileName);

        assertNotSame(control, rebuilt);
        assertEquals(Integer.valueOf(0), rebuilt);
    }
}