#!/bin/bash # Test script for apt-layer deep dpkg integration # Validates the Phase 2.1 implementation: Deep dpkg Integration set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Test counters TOTAL_TESTS=0 PASSED_TESTS=0 FAILED_TESTS=0 # Test logging functions log_test() { echo -e "${BLUE}[TEST]${NC} $1" } log_pass() { echo -e "${GREEN}[PASS]${NC} $1" ((PASSED_TESTS++)) } log_fail() { echo -e "${RED}[FAIL]${NC} $1" ((FAILED_TESTS++)) } log_info() { echo -e "${YELLOW}[INFO]${NC} $1" } # Test summary print_summary() { echo "" echo "==========================================" echo "DPKG INTEGRATION TEST SUMMARY" echo "==========================================" echo "Total Tests: $TOTAL_TESTS" echo "Passed: $PASSED_TESTS" echo "Failed: $FAILED_TESTS" echo "Success Rate: $((PASSED_TESTS * 100 / TOTAL_TESTS))%" echo "==========================================" if [[ $FAILED_TESTS -eq 0 ]]; then echo -e "${GREEN}All tests passed! DPKG integration is working correctly.${NC}" exit 0 else echo -e "${RED}Some tests failed. Please review the output above.${NC}" exit 1 fi } # Cleanup function cleanup() { log_info "Cleaning up test artifacts..." rm -rf /tmp/apt-layer-test-* } # Setup test environment setup_test_env() { log_info "Setting up test environment..." # Create test directories mkdir -p /tmp/apt-layer-test-extract mkdir -p /tmp/apt-layer-test-analyze mkdir -p /tmp/apt-layer-test-install # Download a test package if not available if [[ ! -f /tmp/apt-layer-test-curl.deb ]]; then log_info "Downloading test package (curl)..." apt-get download curl -o Dir::Cache=/tmp cp /var/cache/apt/archives/curl_*.deb /tmp/apt-layer-test-curl.deb fi # Download another test package if [[ ! -f /tmp/apt-layer-test-wget.deb ]]; then log_info "Downloading test package (wget)..." apt-get download wget -o Dir::Cache=/tmp cp /var/cache/apt/archives/wget_*.deb /tmp/apt-layer-test-wget.deb fi } # Test 1: Basic dpkg metadata extraction test_dpkg_metadata_extraction() { ((TOTAL_TESTS++)) log_test "Testing dpkg metadata extraction..." local test_dir="/tmp/apt-layer-test-extract" local deb_file="/tmp/apt-layer-test-curl.deb" if ! apt-layer dpkg-analyze extract "$deb_file" "$test_dir"; then log_fail "dpkg metadata extraction failed" return 1 fi # Check if control directory exists if [[ ! -d "$test_dir/control" ]]; then log_fail "Control directory not created" return 1 fi # Check if control file exists if [[ ! -f "$test_dir/control/control" ]]; then log_fail "Control file not extracted" return 1 fi # Check if data directory exists if [[ ! -d "$test_dir/data" ]]; then log_fail "Data directory not created" return 1 fi # Check if file list exists if [[ ! -f "$test_dir/file-list" ]]; then log_fail "File list not created" return 1 fi log_pass "dpkg metadata extraction test passed" return 0 } # Test 2: Package analysis test_package_analysis() { ((TOTAL_TESTS++)) log_test "Testing comprehensive package analysis..." local test_dir="/tmp/apt-layer-test-analyze" local deb_file="/tmp/apt-layer-test-curl.deb" if ! apt-layer dpkg-analyze analyze "$deb_file" "$test_dir"; then log_fail "Package analysis failed" return 1 fi # Check if analysis report exists if [[ ! -f "$test_dir/analysis-report.json" ]]; then log_fail "Analysis report not created" return 1 fi # Check if JSON is valid if ! jq . "$test_dir/analysis-report.json" >/dev/null 2>&1; then log_fail "Analysis report is not valid JSON" return 1 fi # Check for required fields in analysis report local package_name package_name=$(jq -r '.package_analysis.package_info.control.Package // empty' "$test_dir/analysis-report.json") if [[ -z "$package_name" ]]; then log_fail "Package name not found in analysis report" return 1 fi log_pass "Package analysis test passed (package: $package_name)" return 0 } # Test 3: Package validation test_package_validation() { ((TOTAL_TESTS++)) log_test "Testing package validation..." local deb_file="/tmp/apt-layer-test-curl.deb" if ! apt-layer dpkg-analyze validate "$deb_file" "warn"; then log_fail "Package validation failed" return 1 fi log_pass "Package validation test passed" return 0 } # Test 4: Package installation with metadata test_package_installation() { ((TOTAL_TESTS++)) log_test "Testing package installation with metadata preservation..." local test_dir="/tmp/apt-layer-test-install" local deb_file="/tmp/apt-layer-test-wget.deb" # Clean test directory rm -rf "$test_dir" mkdir -p "$test_dir" if ! apt-layer dpkg-analyze install "$deb_file" "$test_dir" "true"; then log_fail "Package installation failed" return 1 fi # Check if files were installed if [[ ! -d "$test_dir/usr" ]]; then log_fail "Package files not installed" return 1 fi # Check if metadata was preserved if [[ ! -f "$test_dir/.apt-layer-metadata.json" ]]; then log_fail "Metadata not preserved" return 1 fi # Check if control information was preserved if [[ ! -d "$test_dir/.apt-layer-control" ]]; then log_fail "Control information not preserved" return 1 fi # Check if file list was preserved if [[ ! -f "$test_dir/.apt-layer-file-list" ]]; then log_fail "File list not preserved" return 1 fi log_pass "Package installation with metadata test passed" return 0 } # Test 5: Control file parsing test_control_file_parsing() { ((TOTAL_TESTS++)) log_test "Testing control file parsing..." local test_dir="/tmp/apt-layer-test-extract" local control_file="$test_dir/control/control" if [[ ! -f "$control_file" ]]; then log_fail "Control file not found for parsing test" return 1 fi # Test basic parsing by checking for required fields local package_name package_name=$(grep "^Package:" "$control_file" | cut -d: -f2 | xargs) if [[ -z "$package_name" ]]; then log_fail "Package name not found in control file" return 1 fi local version version=$(grep "^Version:" "$control_file" | cut -d: -f2 | xargs) if [[ -z "$version" ]]; then log_fail "Version not found in control file" return 1 fi log_pass "Control file parsing test passed (package: $package_name, version: $version)" return 0 } # Test 6: File list parsing test_file_list_parsing() { ((TOTAL_TESTS++)) log_test "Testing file list parsing..." local test_dir="/tmp/apt-layer-test-extract" local file_list="$test_dir/file-list" if [[ ! -f "$file_list" ]]; then log_fail "File list not found for parsing test" return 1 fi # Check if file list contains entries local file_count file_count=$(wc -l < "$file_list") if [[ $file_count -eq 0 ]]; then log_fail "File list is empty" return 1 fi # Check if file list has correct format (permissions, owner/group, size, date, path) local valid_entries valid_entries=$(grep -c "^[d-][rwx-]\{9\}[[:space:]]\+[^/]\+/[^[:space:]]\+[[:space:]]\+[0-9]\+[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+[[:space:]]\+" "$file_list" || echo "0") if [[ $valid_entries -eq 0 ]]; then log_fail "No valid file entries found in file list" return 1 fi log_pass "File list parsing test passed ($file_count total entries, $valid_entries valid entries)" return 0 } # Test 7: Maintainer script analysis test_maintainer_script_analysis() { ((TOTAL_TESTS++)) log_test "Testing maintainer script analysis..." local test_dir="/tmp/apt-layer-test-extract" local control_dir="$test_dir/control" if [[ ! -d "$control_dir" ]]; then log_fail "Control directory not found for script analysis test" return 1 fi # Check for maintainer scripts local script_count=0 for script in preinst postinst prerm postrm config; do if [[ -f "$control_dir/$script" ]]; then ((script_count++)) fi done log_info "Found $script_count maintainer scripts" # Test script analysis by checking for problematic patterns local problematic_scripts=0 for script in preinst postinst prerm postrm config; do if [[ -f "$control_dir/$script" ]]; then if grep -q "systemctl\|debconf\|/proc\|/sys" "$control_dir/$script"; then ((problematic_scripts++)) fi fi done log_pass "Maintainer script analysis test passed ($script_count scripts, $problematic_scripts with potential issues)" return 0 } # Test 8: Architecture compatibility test_architecture_compatibility() { ((TOTAL_TESTS++)) log_test "Testing architecture compatibility..." local test_dir="/tmp/apt-layer-test-extract" local control_file="$test_dir/control/control" if [[ ! -f "$control_file" ]]; then log_fail "Control file not found for architecture test" return 1 fi # Get package architecture local package_arch package_arch=$(grep "^Architecture:" "$control_file" | cut -d: -f2 | xargs) if [[ -z "$package_arch" ]]; then log_fail "Architecture not found in control file" return 1 fi # Get system architecture local system_arch system_arch=$(dpkg --print-architecture) # Check compatibility if [[ "$package_arch" != "all" ]] && [[ "$package_arch" != "$system_arch" ]]; then log_fail "Architecture mismatch: package=$package_arch, system=$system_arch" return 1 fi log_pass "Architecture compatibility test passed (package: $package_arch, system: $system_arch)" return 0 } # Test 9: Dependency analysis test_dependency_analysis() { ((TOTAL_TESTS++)) log_test "Testing dependency analysis..." local test_dir="/tmp/apt-layer-test-extract" local control_file="$test_dir/control/control" if [[ ! -f "$control_file" ]]; then log_fail "Control file not found for dependency test" return 1 fi # Check for dependency fields local dependency_fields=("Depends" "Pre-Depends" "Recommends" "Suggests" "Conflicts" "Breaks" "Provides" "Replaces" "Enhances") local found_dependencies=0 for field in "${dependency_fields[@]}"; do if grep -q "^$field:" "$control_file"; then ((found_dependencies++)) fi done log_info "Found $found_dependencies dependency fields" log_pass "Dependency analysis test passed" return 0 } # Test 10: Multi-arch support detection test_multiarch_detection() { ((TOTAL_TESTS++)) log_test "Testing multi-arch support detection..." local test_dir="/tmp/apt-layer-test-extract" local control_file="$test_dir/control/control" if [[ ! -f "$control_file" ]]; then log_fail "Control file not found for multi-arch test" return 1 fi # Check for Multi-Arch field local multi_arch multi_arch=$(grep "^Multi-Arch:" "$control_file" | cut -d: -f2 | xargs || echo "none") log_info "Multi-Arch support: $multi_arch" log_pass "Multi-arch detection test passed" return 0 } # Main test execution main() { echo "==========================================" echo "apt-layer DPKG INTEGRATION TEST SUITE" echo "==========================================" echo "Testing Phase 2.1: Deep dpkg Integration" echo "==========================================" echo "" # Setup test environment setup_test_env # Run tests test_dpkg_metadata_extraction test_package_analysis test_package_validation test_package_installation test_control_file_parsing test_file_list_parsing test_maintainer_script_analysis test_architecture_compatibility test_dependency_analysis test_multiarch_detection # Print summary print_summary } # Cleanup on exit trap cleanup EXIT # Run main function main "$@"