Back to Repositories

Testing Duplicate DEX File Handling in Apktool

This test suite validates the handling of duplicate DEX files in Android APK processing using Apktool. It focuses on testing the decoder’s behavior when encountering APKs with multiple DEX files and ensures proper error handling and processing modes.

Test Coverage Overview

The test suite provides comprehensive coverage for duplicate DEX file scenarios in APK processing.

Key areas tested include:
  • Exception handling for duplicate DEX files during full source decoding
  • Successful decoding using main classes only mode
  • APK rebuilding verification after decoding

Implementation Analysis

The testing approach employs JUnit framework to validate two distinct decoding scenarios. It utilizes a structured setup with @BeforeClass initialization and specific test cases that verify both error conditions and successful processing paths.

Technical implementation includes:
  • Resource directory copying for test setup
  • Configuration management for different decode modes
  • Exception verification using expected annotations

Technical Details

Testing infrastructure includes:
  • JUnit test framework
  • ApkDecoder and ApkBuilder utilities
  • ExtFile for file handling
  • Custom configuration settings via Config class
  • TestUtils for resource management
  • Logging implementation for test execution tracking

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation and setup using @BeforeClass
  • Clear test case naming that describes functionality
  • Explicit exception testing using @Test(expected)
  • Comprehensive logging for debugging
  • Resource cleanup and management

ibotpeaches/apktool

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

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

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

public class DuplicateDexTest extends BaseTest {
    private static final String TEST_APK = "duplicatedex.apk";

    @BeforeClass
    public static void beforeClass() throws Exception {
        LOGGER.info("Unpacking " + TEST_APK + "...");
        TestUtils.copyResourceDir(DuplicateDexTest.class, "decode/duplicatedex", sTmpDir);
    }

    @Test(expected = AndrolibException.class)
    public void decodeAllSourcesShouldThrowException() throws BrutException {
        LOGGER.info("Decoding " + TEST_APK + "...");
        ExtFile testApk = new ExtFile(sTmpDir, TEST_APK);
        ExtFile testDir = new ExtFile(testApk + ".out");
        new ApkDecoder(testApk, sConfig).decode(testDir);

        LOGGER.info("Building " + TEST_APK + "...");
        new ApkBuilder(testDir, sConfig).build(null);
    }

    @Test
    public void decodeUsingOnlyMainClassesMode() throws BrutException {
        sConfig.setForceDelete(true);
        sConfig.setDecodeSources(Config.DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES);

        LOGGER.info("Decoding " + TEST_APK + "...");
        ExtFile testApk = new ExtFile(sTmpDir, TEST_APK);
        ExtFile testDir = new ExtFile(testApk + ".out.main");
        new ApkDecoder(testApk, sConfig).decode(testDir);

        LOGGER.info("Building " + TEST_APK + "...");
        new ApkBuilder(testDir, sConfig).build(null);
    }

}