Back to Repositories

Testing Variable-Length Integer Serialization in JADX

This test suite validates the DataAdapterHelper utility class for handling variable-length integer encoding and decoding operations in the JADX GUI codebase. It ensures reliable data serialization and deserialization for cache management.

Test Coverage Overview

The test suite provides comprehensive coverage of unsigned variable-length integer (UVInt) operations.

Key areas tested include:
  • Zero value handling
  • Small positive integers (7, 0x7f)
  • Boundary values (0x80, 0x256)
  • System constant limits (Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE)

Implementation Analysis

The testing approach employs a systematic verification of integer encoding/decoding using ByteArrayStream for data manipulation. The implementation utilizes JUnit 5 framework with AssertJ assertions for precise value comparisons.

Testing patterns include:
  • Helper method pattern for repeated test scenarios
  • Stream-based I/O testing
  • Boundary value analysis

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • AssertJ assertion library
  • Java I/O streams (ByteArrayInputStream, ByteArrayOutputStream)
  • DataInput/DataOutputStream for binary data handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Single responsibility principle in test methods
  • Comprehensive boundary testing
  • Clean setup and teardown with proper resource handling
  • Reusable test utilities through helper methods
  • Clear and meaningful test method naming

skylot/jadx

jadx-gui/src/test/java/jadx/gui/utils/cache/code/disk/adapters/DataAdapterHelperTest.java

            
package jadx.gui.utils.cache.code.disk.adapters;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.junit.jupiter.api.Test;

import jadx.gui.cache.code.disk.adapters.DataAdapterHelper;

import static org.assertj.core.api.Assertions.assertThat;

class DataAdapterHelperTest {

	@Test
	void uvInt() throws IOException {
		checkUVIntFor(0);
		checkUVIntFor(7);
		checkUVIntFor(0x7f);
		checkUVIntFor(0x80);
		checkUVIntFor(0x256);
		checkUVIntFor(Byte.MAX_VALUE);
		checkUVIntFor(Short.MAX_VALUE);
		checkUVIntFor(Integer.MAX_VALUE);
	}

	private void checkUVIntFor(int val) throws IOException {
		assertThat(writeReadUVInt(val)).isEqualTo(val);
	}

	private int writeReadUVInt(int val) throws IOException {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		DataOutputStream out = new DataOutputStream(byteOut);
		DataAdapterHelper.writeUVInt(out, val);

		DataInput in = new DataInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
		return DataAdapterHelper.readUVInt(in);
	}
}