Back to Repositories

Validating Baksmali Variable Processing in Apktool AAPT1 Implementation

This test suite validates the baksmali variable handling in Apktool’s AAPT1 implementation, focusing on BuildConfig.smali file generation and comparison. It ensures consistent decompilation of Android application configuration parameters and static field declarations.

Test Coverage Overview

The test suite provides comprehensive coverage of baksmali variable handling, specifically targeting the BuildConfig class decompilation process. Key areas include:

  • Static field declarations verification
  • Application ID and version information parsing
  • Debug flag initialization testing
  • BuildConfig constructor validation

Implementation Analysis

The testing approach utilizes JUnit framework to perform file-based comparison testing. It implements a before-class setup pattern to prepare test environments and employs direct byte comparison for smali output validation.

The test leverages ApkBuilder and ApkDecoder classes to process test resources and verify decompilation accuracy.

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • ExtFile for file handling
  • Custom TestUtils for resource management
  • AAPT1 configuration settings
  • File-based assertion mechanisms

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper test setup isolation using @BeforeClass
  • Resource cleanup management
  • Detailed logging of test stages
  • Explicit expected vs. actual output comparison
  • Comprehensive static field validation

ibotpeaches/apktool

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/DefaultBaksmaliVariableTest.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.common.BrutException;
import brut.directory.ExtFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

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

public class DefaultBaksmaliVariableTest extends BaseTest {

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

        LOGGER.info("Unpacking testjar...");
        TestUtils.copyResourceDir(DefaultBaksmaliVariableTest.class, "aapt1/issue1481", sTestOrigDir);

        sConfig.setAaptVersion(1);

        LOGGER.info("Building issue1481.jar...");
        ExtFile testJar = new ExtFile(sTmpDir, "issue1481.jar");
        new ApkBuilder(sTestOrigDir, sConfig).build(testJar);

        LOGGER.info("Decoding issue1481.jar...");
        new ApkDecoder(testJar, sConfig).decode(sTestNewDir);
    }

    @Test
    public void confirmBaksmaliParamsAreTheSame() throws IOException {
        String expected = TestUtils.replaceNewlines(".class public final Lcom/ibotpeaches/issue1481/BuildConfig;\n" +
                ".super Ljava/lang/Object;\n" +
                ".source \"BuildConfig.java\"\n" +
                "\n" +
                "\n" +
                "# static fields\n" +
                ".field public static final APPLICATION_ID:Ljava/lang/String; = \"com.ibotpeaches.issue1481\"\n" +
                "\n" +
                ".field public static final BUILD_TYPE:Ljava/lang/String; = \"debug\"\n" +
                "\n" +
                ".field public static final DEBUG:Z\n" +
                "\n" +
                ".field public static final FLAVOR:Ljava/lang/String; = \"\"\n" +
                "\n" +
                ".field public static final VERSION_CODE:I = 0x1\n" +
                "\n" +
                ".field public static final VERSION_NAME:Ljava/lang/String; = \"1.0\"\n" +
                "\n" +
                "\n" +
                "# direct methods\n" +
                ".method static constructor <clinit>()V\n" +
                "    .locals 1\n" +
                "\n" +
                "    .prologue\n" +
                "    .line 7\n" +
                "    const-string v0, \"true\"\n" +
                "\n" +
                "    invoke-static {v0}, Ljava/lang/Boolean;->parseBoolean(Ljava/lang/String;)Z\n" +
                "\n" +
                "    move-result v0\n" +
                "\n" +
                "    sput-boolean v0, Lcom/ibotpeaches/issue1481/BuildConfig;->DEBUG:Z\n" +
                "\n" +
                "    return-void\n" +
                ".end method\n" +
                "\n" +
                ".method public constructor <init>()V\n" +
                "    .locals 0\n" +
                "\n" +
                "    .prologue\n" +
                "    .line 6\n" +
                "    invoke-direct {p0}, Ljava/lang/Object;-><init>()V\n" +
                "\n" +
                "    return-void\n" +
                ".end method");

        byte[] encoded = Files.readAllBytes(new File(sTestNewDir, "smali/com/ibotpeaches/issue1481/BuildConfig.smali").toPath());
        String obtained = TestUtils.replaceNewlines(new String(encoded));

        assertEquals(expected, obtained);
    }
}