/ scripts / test-differential-chunking.sh
test-differential-chunking.sh
  1  #!/bin/bash
  2  
  3  # Script to test the differential chunking functionality
  4  # This script will:
  5  # 1. Create test files with controlled differences
  6  # 2. Run differential chunking operations
  7  # 3. Verify the results
  8  
  9  set -e
 10  
 11  echo "Differential Chunking Test Script"
 12  echo "================================="
 13  
 14  # Create test directory
 15  TEST_DIR="test-differential-chunking/$(date +%Y%m%d-%H%M%S)"
 16  mkdir -p "$TEST_DIR"
 17  mkdir -p "$TEST_DIR/original"
 18  mkdir -p "$TEST_DIR/modified"
 19  mkdir -p "$TEST_DIR/result"
 20  
 21  # Create test files with controlled differences
 22  create_test_files() {
 23      echo "Creating test files..."
 24      
 25      # Create original files
 26      dd if=/dev/urandom bs=1M count=1 of="$TEST_DIR/original/small.dat" 2>/dev/null
 27      dd if=/dev/urandom bs=1M count=10 of="$TEST_DIR/original/medium.dat" 2>/dev/null
 28      dd if=/dev/urandom bs=1M count=100 of="$TEST_DIR/original/large.dat" 2>/dev/null
 29      
 30      # Create modified files with controlled differences
 31      
 32      # Small file - modify 10% at the beginning
 33      cp "$TEST_DIR/original/small.dat" "$TEST_DIR/modified/small.dat"
 34      dd if=/dev/urandom bs=102K count=1 of="$TEST_DIR/modified/small.dat" conv=notrunc 2>/dev/null
 35      
 36      # Medium file - modify 10% in the middle
 37      cp "$TEST_DIR/original/medium.dat" "$TEST_DIR/modified/medium.dat"
 38      dd if=/dev/urandom bs=1M count=1 of="$TEST_DIR/modified/medium.dat" seek=5 conv=notrunc 2>/dev/null
 39      
 40      # Large file - modify 10% at the end
 41      cp "$TEST_DIR/original/large.dat" "$TEST_DIR/modified/large.dat"
 42      dd if=/dev/urandom bs=10M count=1 of="$TEST_DIR/modified/large.dat" seek=90 conv=notrunc 2>/dev/null
 43      
 44      # Create a file with many small changes throughout
 45      cp "$TEST_DIR/original/medium.dat" "$TEST_DIR/modified/medium_scattered.dat"
 46      for i in {0..9}; do
 47          offset=$((i * 1048576))
 48          dd if=/dev/urandom bs=10K count=1 of="$TEST_DIR/modified/medium_scattered.dat" seek=$((offset / 1024)) conv=notrunc 2>/dev/null
 49      done
 50      
 51      # Create a completely different file of the same size
 52      dd if=/dev/urandom bs=1M count=10 of="$TEST_DIR/modified/medium_different.dat" 2>/dev/null
 53      
 54      echo "Test files created successfully."
 55  }
 56  
 57  # Build the differential chunking test tool
 58  build_test_tool() {
 59      echo "Building differential chunking test tool..."
 60      go build -o "$TEST_DIR/diff-chunking-tool" ./cmd/keepsync-cli/diff-chunking-tool/diff-chunking-test.go
 61      echo "Test tool built successfully."
 62  }
 63  
 64  # Run differential chunking tests
 65  run_tests() {
 66      echo "Running differential chunking tests..."
 67      
 68      # Test small file
 69      echo "Testing small file..."
 70      "$TEST_DIR/diff-chunking-tool" \
 71          --original "$TEST_DIR/original/small.dat" \
 72          --modified "$TEST_DIR/modified/small.dat" \
 73          --output "$TEST_DIR/result/small.dat" \
 74          --stats "$TEST_DIR/result/small_stats.json"
 75      
 76      # Test medium file
 77      echo "Testing medium file..."
 78      "$TEST_DIR/diff-chunking-tool" \
 79          --original "$TEST_DIR/original/medium.dat" \
 80          --modified "$TEST_DIR/modified/medium.dat" \
 81          --output "$TEST_DIR/result/medium.dat" \
 82          --stats "$TEST_DIR/result/medium_stats.json"
 83      
 84      # Test large file
 85      echo "Testing large file..."
 86      "$TEST_DIR/diff-chunking-tool" \
 87          --original "$TEST_DIR/original/large.dat" \
 88          --modified "$TEST_DIR/modified/large.dat" \
 89          --output "$TEST_DIR/result/large.dat" \
 90          --stats "$TEST_DIR/result/large_stats.json"
 91      
 92      # Test scattered changes
 93      echo "Testing scattered changes..."
 94      "$TEST_DIR/diff-chunking-tool" \
 95          --original "$TEST_DIR/original/medium.dat" \
 96          --modified "$TEST_DIR/modified/medium_scattered.dat" \
 97          --output "$TEST_DIR/result/medium_scattered.dat" \
 98          --stats "$TEST_DIR/result/medium_scattered_stats.json"
 99      
100      # Test completely different file
101      echo "Testing completely different file..."
102      "$TEST_DIR/diff-chunking-tool" \
103          --original "$TEST_DIR/original/medium.dat" \
104          --modified "$TEST_DIR/modified/medium_different.dat" \
105          --output "$TEST_DIR/result/medium_different.dat" \
106          --stats "$TEST_DIR/result/medium_different_stats.json"
107      
108      echo "Tests completed successfully."
109  }
110  
111  # Verify results
112  verify_results() {
113      echo "Verifying results..."
114      
115      # Verify that the result files match the modified files
116      verify_file() {
117          local file=$1
118          echo "Verifying $file..."
119          if cmp -s "$TEST_DIR/modified/$file" "$TEST_DIR/result/$file"; then
120              echo "  ✓ $file verification successful"
121          else
122              echo "  ✗ $file verification failed"
123              exit 1
124          fi
125      }
126      
127      verify_file "small.dat"
128      verify_file "medium.dat"
129      verify_file "large.dat"
130      verify_file "medium_scattered.dat"
131      verify_file "medium_different.dat"
132      
133      echo "All verifications passed."
134  }
135  
136  # Print summary
137  print_summary() {
138      echo "Printing summary..."
139      
140      # Parse and display statistics
141      for stats_file in "$TEST_DIR/result/"*_stats.json; do
142          echo "Statistics for $(basename "$stats_file" _stats.json):"
143          cat "$stats_file" | jq '.'
144          echo
145      done
146      
147      echo "Summary completed."
148  }
149  
150  # Main function
151  main() {
152      create_test_files
153      build_test_tool
154      run_tests
155      verify_results
156      print_summary
157      
158      echo "Differential chunking test completed successfully."
159      echo "Results available in: $TEST_DIR"
160  }
161  
162  # Run the main function
163  main