/ scripts / test-functionality.sh
test-functionality.sh
  1  #!/bin/bash
  2  
  3  # Exit on error
  4  set -e
  5  
  6  # Set colors for better output
  7  GREEN="\033[0;32m"
  8  BLUE="\033[0;34m"
  9  YELLOW="\033[0;33m"
 10  RED="\033[0;31m"
 11  RESET="\033[0m"
 12  
 13  echo -e "${BLUE}"
 14  echo -e "╔════════════════════════════════════════════════════════════════╗"
 15  echo -e "║                                                                ║"
 16  echo -e "║                KeepSync Functionality Test                     ║"
 17  echo -e "║                                                                ║"
 18  echo -e "╚════════════════════════════════════════════════════════════════╝"
 19  echo -e "${RESET}"
 20  
 21  # Set directories
 22  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 23  PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
 24  BUILD_DIR="${PROJECT_ROOT}/bin"
 25  TEST_DATA_DIR="${PROJECT_ROOT}/test-data"
 26  TEST_TEMP_DIR="${TEST_DATA_DIR}/temp"
 27  BINARY="${BUILD_DIR}/keepsync"
 28  
 29  # Create test directories
 30  mkdir -p "${TEST_TEMP_DIR}/source"
 31  mkdir -p "${TEST_TEMP_DIR}/destination"
 32  mkdir -p "${TEST_TEMP_DIR}/config"
 33  
 34  # Function to check if a command exists
 35  command_exists() {
 36    command -v "$1" >/dev/null 2>&1
 37  }
 38  
 39  # Function to check if a file exists
 40  file_exists() {
 41    [ -f "$1" ]
 42  }
 43  
 44  # Function to check if a directory exists
 45  directory_exists() {
 46    [ -d "$1" ]
 47  }
 48  
 49  # Function to check if a Go package compiles
 50  package_compiles() {
 51    local package=$1
 52    echo -e "${YELLOW}Testing if package ${package} compiles...${RESET}"
 53    if go build -o /dev/null "${package}" >/dev/null 2>&1; then
 54      echo -e "${GREEN}✓ Package ${package} compiles successfully${RESET}"
 55      return 0
 56    else
 57      echo -e "${RED}✗ Package ${package} fails to compile${RESET}"
 58      return 1
 59    fi
 60  }
 61  
 62  # Function to run a test and report result
 63  run_test() {
 64    local test_name=$1
 65    local test_command=$2
 66    local test_description=$3
 67    
 68    echo -e "${YELLOW}Testing: ${test_description}${RESET}"
 69    
 70    if eval "${test_command}"; then
 71      echo -e "${GREEN}✓ ${test_name}: PASS${RESET}"
 72      return 0
 73    else
 74      echo -e "${RED}✗ ${test_name}: FAIL${RESET}"
 75      return 1
 76    fi
 77  }
 78  
 79  # Create a test configuration file
 80  create_test_config() {
 81    cat > "${TEST_TEMP_DIR}/config/test-config.json" << EOF
 82  {
 83    "source": "${TEST_TEMP_DIR}/source",
 84    "destination": "${TEST_TEMP_DIR}/destination",
 85    "providers": [
 86      {
 87        "name": "local",
 88        "type": "local",
 89        "options": {
 90          "path": "${TEST_TEMP_DIR}/destination"
 91        }
 92      }
 93    ],
 94    "security": {
 95      "useTpm": true,
 96      "requireTpm": false,
 97      "allowSoftwareFallback": true,
 98      "useQuantumResistant": true,
 99      "enclaves": {
100        "preferSgx": true,
101        "preferSev": false,
102        "allowSoftwareFallback": true,
103        "requireUserConfirmation": false
104      }
105    },
106    "metadata": {
107      "encryptLocally": true,
108      "encryptionAlgorithm": "AES-GCM"
109    },
110    "sync": {
111      "useLockFree": true,
112      "watchInterval": 1000
113    },
114    "encryption": {
115      "enabled": true,
116      "useTpm": false,
117      "password": "test-password"
118    },
119    "chunking": {
120      "enabled": true,
121      "chunkSize": 1048576
122    },
123    "compression": {
124      "enabled": true,
125      "level": "adaptive"
126    }
127  }
128  EOF
129  }
130  
131  # Create test files
132  create_test_files() {
133    echo "This is a test file" > "${TEST_TEMP_DIR}/source/file1.txt"
134    dd if=/dev/urandom of="${TEST_TEMP_DIR}/source/file2.bin" bs=1M count=1 2>/dev/null
135    mkdir -p "${TEST_TEMP_DIR}/source/subdir"
136    echo "This is a test file in a subdirectory" > "${TEST_TEMP_DIR}/source/subdir/file3.txt"
137  }
138  
139  # Initialize test environment
140  initialize_test_environment() {
141    echo -e "${YELLOW}Initializing test environment...${RESET}"
142    
143    # Clean test directories
144    rm -rf "${TEST_TEMP_DIR}/source"
145    rm -rf "${TEST_TEMP_DIR}/destination"
146    rm -rf "${TEST_TEMP_DIR}/config"
147    
148    # Create test directories
149    mkdir -p "${TEST_TEMP_DIR}/source"
150    mkdir -p "${TEST_TEMP_DIR}/destination"
151    mkdir -p "${TEST_TEMP_DIR}/config"
152    
153    # Create test configuration
154    create_test_config
155    
156    # Create test files
157    create_test_files
158    
159    echo -e "${GREEN}Test environment initialized${RESET}"
160  }
161  
162  # Test if the Makefile exists and works
163  test_makefile() {
164    local makefile="${PROJECT_ROOT}/Makefile"
165    local result=0
166    
167    echo -e "${YELLOW}Testing Makefile...${RESET}"
168    
169    if file_exists "${makefile}"; then
170      echo -e "${GREEN}✓ Makefile exists${RESET}"
171    else
172      echo -e "${RED}✗ Makefile does not exist${RESET}"
173      return 1
174    fi
175    
176    # Test make help
177    if make -C "${PROJECT_ROOT}" help >/dev/null 2>&1; then
178      echo -e "${GREEN}✓ 'make help' works${RESET}"
179    else
180      echo -e "${RED}✗ 'make help' fails${RESET}"
181      result=1
182    fi
183    
184    # Test make clean
185    if make -C "${PROJECT_ROOT}" clean >/dev/null 2>&1; then
186      echo -e "${GREEN}✓ 'make clean' works${RESET}"
187    else
188      echo -e "${RED}✗ 'make clean' fails${RESET}"
189      result=1
190    fi
191    
192    return ${result}
193  }
194  
195  # Test if the build scripts exist and are executable
196  test_build_scripts() {
197    local result=0
198    
199    echo -e "${YELLOW}Testing build scripts...${RESET}"
200    
201    # Check if integrated-build.sh exists
202    if file_exists "${SCRIPT_DIR}/integrated-build.sh"; then
203      echo -e "${GREEN}✓ integrated-build.sh exists${RESET}"
204      
205      # Check if it's executable
206      if [ -x "${SCRIPT_DIR}/integrated-build.sh" ]; then
207        echo -e "${GREEN}✓ integrated-build.sh is executable${RESET}"
208      else
209        echo -e "${YELLOW}! integrated-build.sh is not executable, fixing...${RESET}"
210        chmod +x "${SCRIPT_DIR}/integrated-build.sh"
211      fi
212    else
213      echo -e "${RED}✗ integrated-build.sh does not exist${RESET}"
214      result=1
215    fi
216    
217    # Check if fix-import-paths.sh exists
218    if file_exists "${SCRIPT_DIR}/fix-import-paths.sh"; then
219      echo -e "${GREEN}✓ fix-import-paths.sh exists${RESET}"
220      
221      # Check if it's executable
222      if [ -x "${SCRIPT_DIR}/fix-import-paths.sh" ]; then
223        echo -e "${GREEN}✓ fix-import-paths.sh is executable${RESET}"
224      else
225        echo -e "${YELLOW}! fix-import-paths.sh is not executable, fixing...${RESET}"
226        chmod +x "${SCRIPT_DIR}/fix-import-paths.sh"
227      fi
228    else
229      echo -e "${RED}✗ fix-import-paths.sh does not exist${RESET}"
230      result=1
231    fi
232    
233    # Check if fix-package-structure.sh exists
234    if file_exists "${SCRIPT_DIR}/fix-package-structure.sh"; then
235      echo -e "${GREEN}✓ fix-package-structure.sh exists${RESET}"
236      
237      # Check if it's executable
238      if [ -x "${SCRIPT_DIR}/fix-package-structure.sh" ]; then
239        echo -e "${GREEN}✓ fix-package-structure.sh is executable${RESET}"
240      else
241        echo -e "${YELLOW}! fix-package-structure.sh is not executable, fixing...${RESET}"
242        chmod +x "${SCRIPT_DIR}/fix-package-structure.sh"
243      fi
244    else
245      echo -e "${RED}✗ fix-package-structure.sh does not exist${RESET}"
246      result=1
247    fi
248    
249    # Check if update-dependencies.sh exists
250    if file_exists "${SCRIPT_DIR}/update-dependencies.sh"; then
251      echo -e "${GREEN}✓ update-dependencies.sh exists${RESET}"
252      
253      # Check if it's executable
254      if [ -x "${SCRIPT_DIR}/update-dependencies.sh" ]; then
255        echo -e "${GREEN}✓ update-dependencies.sh is executable${RESET}"
256      else
257        echo -e "${YELLOW}! update-dependencies.sh is not executable, fixing...${RESET}"
258        chmod +x "${SCRIPT_DIR}/update-dependencies.sh"
259      fi
260    else
261      echo -e "${RED}✗ update-dependencies.sh does not exist${RESET}"
262      result=1
263    fi
264    
265    # Check if consolidated-clean.sh exists
266    if file_exists "${SCRIPT_DIR}/consolidated-clean.sh"; then
267      echo -e "${GREEN}✓ consolidated-clean.sh exists${RESET}"
268      
269      # Check if it's executable
270      if [ -x "${SCRIPT_DIR}/consolidated-clean.sh" ]; then
271        echo -e "${GREEN}✓ consolidated-clean.sh is executable${RESET}"
272      else
273        echo -e "${YELLOW}! consolidated-clean.sh is not executable, fixing...${RESET}"
274        chmod +x "${SCRIPT_DIR}/consolidated-clean.sh"
275      fi
276    else
277      echo -e "${RED}✗ consolidated-clean.sh does not exist${RESET}"
278      result=1
279    fi
280    
281    # Check if make-scripts-executable.sh exists
282    if file_exists "${SCRIPT_DIR}/make-scripts-executable.sh"; then
283      echo -e "${GREEN}✓ make-scripts-executable.sh exists${RESET}"
284      
285      # Check if it's executable
286      if [ -x "${SCRIPT_DIR}/make-scripts-executable.sh" ]; then
287        echo -e "${GREEN}✓ make-scripts-executable.sh is executable${RESET}"
288      else
289        echo -e "${YELLOW}! make-scripts-executable.sh is not executable, fixing...${RESET}"
290        chmod +x "${SCRIPT_DIR}/make-scripts-executable.sh"
291      fi
292    else
293      echo -e "${RED}✗ make-scripts-executable.sh does not exist${RESET}"
294      result=1
295    fi
296    
297    return ${result}
298  }
299  
300  # Test if the key packages compile
301  test_key_packages() {
302    local result=0
303    
304    echo -e "${YELLOW}Testing key packages...${RESET}"
305    
306    # Test internal/security/tpm/core package
307    if directory_exists "${PROJECT_ROOT}/internal/security/tpm/core"; then
308      echo -e "${GREEN}✓ internal/security/tpm/core directory exists${RESET}"
309      if package_compiles "${PROJECT_ROOT}/internal/security/tpm/core"; then
310        echo -e "${GREEN}✓ internal/security/tpm/core package compiles${RESET}"
311      else
312        echo -e "${RED}✗ internal/security/tpm/core package fails to compile${RESET}"
313        result=1
314      fi
315    else
316      echo -e "${RED}✗ internal/security/tpm/core directory does not exist${RESET}"
317      result=1
318    fi
319    
320    # Test internal/security/tpm/quantum package
321    if directory_exists "${PROJECT_ROOT}/internal/security/tpm/quantum"; then
322      echo -e "${GREEN}✓ internal/security/tpm/quantum directory exists${RESET}"
323      if package_compiles "${PROJECT_ROOT}/internal/security/tpm/quantum"; then
324        echo -e "${GREEN}✓ internal/security/tpm/quantum package compiles${RESET}"
325      else
326        echo -e "${RED}✗ internal/security/tpm/quantum package fails to compile${RESET}"
327        result=1
328      fi
329    else
330      echo -e "${RED}✗ internal/security/tpm/quantum directory does not exist${RESET}"
331      result=1
332    fi
333    
334    # Test internal/security/tpm/master package
335    if directory_exists "${PROJECT_ROOT}/internal/security/tpm/master"; then
336      echo -e "${GREEN}✓ internal/security/tpm/master directory exists${RESET}"
337      if package_compiles "${PROJECT_ROOT}/internal/security/tpm/master"; then
338        echo -e "${GREEN}✓ internal/security/tpm/master package compiles${RESET}"
339      else
340        echo -e "${RED}✗ internal/security/tpm/master package fails to compile${RESET}"
341        result=1
342      fi
343    else
344      echo -e "${RED}✗ internal/security/tpm/master directory does not exist${RESET}"
345      result=1
346    fi
347    
348    # Test internal/security/enclave package
349    if directory_exists "${PROJECT_ROOT}/internal/security/enclave"; then
350      echo -e "${GREEN}✓ internal/security/enclave directory exists${RESET}"
351      if package_compiles "${PROJECT_ROOT}/internal/security/enclave"; then
352        echo -e "${GREEN}✓ internal/security/enclave package compiles${RESET}"
353      else
354        echo -e "${RED}✗ internal/security/enclave package fails to compile${RESET}"
355        result=1
356      fi
357    else
358      echo -e "${RED}✗ internal/security/enclave directory does not exist${RESET}"
359      result=1
360    fi
361    
362    # Test internal/security/memory package
363    if directory_exists "${PROJECT_ROOT}/internal/security/memory"; then
364      echo -e "${GREEN}✓ internal/security/memory directory exists${RESET}"
365      if package_compiles "${PROJECT_ROOT}/internal/security/memory"; then
366        echo -e "${GREEN}✓ internal/security/memory package compiles${RESET}"
367      else
368        echo -e "${RED}✗ internal/security/memory package fails to compile${RESET}"
369        result=1
370      fi
371    else
372      echo -e "${RED}✗ internal/security/memory directory does not exist${RESET}"
373      result=1
374    fi
375    
376    return ${result}
377  }
378  
379  # Test if the chunking and metadata packages compile
380  test_chunking_packages() {
381    local result=0
382    
383    echo -e "${YELLOW}Testing chunking and metadata packages...${RESET}"
384    
385    # Test internal/cloud/provider/v2/chunking package
386    if directory_exists "${PROJECT_ROOT}/internal/cloud/provider/v2/chunking"; then
387      echo -e "${GREEN}✓ internal/cloud/provider/v2/chunking directory exists${RESET}"
388      if package_compiles "${PROJECT_ROOT}/internal/cloud/provider/v2/chunking"; then
389        echo -e "${GREEN}✓ internal/cloud/provider/v2/chunking package compiles${RESET}"
390      else
391        echo -e "${RED}✗ internal/cloud/provider/v2/chunking package fails to compile${RESET}"
392        result=1
393      fi
394    else
395      echo -e "${RED}✗ internal/cloud/provider/v2/chunking directory does not exist${RESET}"
396      result=1
397    fi
398    
399    # Test internal/cloud/provider/v2/retention package
400    if directory_exists "${PROJECT_ROOT}/internal/cloud/provider/v2/retention"; then
401      echo -e "${GREEN}✓ internal/cloud/provider/v2/retention directory exists${RESET}"
402      if package_compiles "${PROJECT_ROOT}/internal/cloud/provider/v2/retention"; then
403        echo -e "${GREEN}✓ internal/cloud/provider/v2/retention package compiles${RESET}"
404      else
405        echo -e "${RED}✗ internal/cloud/provider/v2/retention package fails to compile${RESET}"
406        result=1
407      fi
408    else
409      echo -e "${RED}✗ internal/cloud/provider/v2/retention directory does not exist${RESET}"
410      result=1
411    fi
412    
413    return ${result}
414  }
415  
416  # Test if the sync packages compile
417  test_sync_packages() {
418    local result=0
419    
420    echo -e "${YELLOW}Testing sync packages...${RESET}"
421    
422    # Test cmd/keepsync-cli-standalone/sync package
423    if directory_exists "${PROJECT_ROOT}/cmd/keepsync-cli-standalone/sync"; then
424      echo -e "${GREEN}✓ cmd/keepsync-cli-standalone/sync directory exists${RESET}"
425      if package_compiles "${PROJECT_ROOT}/cmd/keepsync-cli-standalone/sync"; then
426        echo -e "${GREEN}✓ cmd/keepsync-cli-standalone/sync package compiles${RESET}"
427      else
428        echo -e "${RED}✗ cmd/keepsync-cli-standalone/sync package fails to compile${RESET}"
429        result=1
430      fi
431    else
432      echo -e "${RED}✗ cmd/keepsync-cli-standalone/sync directory does not exist${RESET}"
433      result=1
434    fi
435    
436    return ${result}
437  }
438  
439  # Test if the main binary builds
440  test_binary_build() {
441    local result=0
442    
443    echo -e "${YELLOW}Testing binary build...${RESET}"
444    
445    # Build the binary directly
446    if go build -o "${BINARY}" "${PROJECT_ROOT}/main.go" >/dev/null 2>&1; then
447      echo -e "${GREEN}✓ Binary builds successfully${RESET}"
448      
449      # Check if the binary exists
450      if file_exists "${BINARY}"; then
451        echo -e "${GREEN}✓ Binary exists at ${BINARY}${RESET}"
452      else
453        echo -e "${RED}✗ Binary does not exist at ${BINARY}${RESET}"
454        result=1
455      fi
456    else
457      echo -e "${RED}✗ Binary build fails${RESET}"
458      result=1
459    fi
460    
461    return ${result}
462  }
463  
464  # Test if the binary runs
465  test_binary_run() {
466    local result=0
467    
468    echo -e "${YELLOW}Testing binary execution...${RESET}"
469    
470    # Check if the binary exists
471    if ! file_exists "${BINARY}"; then
472      echo -e "${RED}✗ Binary does not exist at ${BINARY}${RESET}"
473      return 1
474    fi
475    
476    # Try to run the binary with --help
477    if "${BINARY}" --help >/dev/null 2>&1; then
478      echo -e "${GREEN}✓ Binary runs with --help${RESET}"
479    else
480      echo -e "${RED}✗ Binary fails to run with --help${RESET}"
481      result=1
482    fi
483    
484    # Try to run the binary with --version
485    if "${BINARY}" --version >/dev/null 2>&1; then
486      echo -e "${GREEN}✓ Binary runs with --version${RESET}"
487    else
488      echo -e "${RED}✗ Binary fails to run with --version${RESET}"
489      result=1
490    fi
491    
492    return ${result}
493  }
494  
495  # Run all tests
496  run_all_tests() {
497    local result=0
498    
499    echo -e "${BLUE}Running all tests...${RESET}"
500    
501    # Initialize test environment
502    initialize_test_environment
503    
504    # Test Makefile
505    if test_makefile; then
506      echo -e "${GREEN}✓ Makefile tests passed${RESET}"
507    else
508      echo -e "${RED}✗ Makefile tests failed${RESET}"
509      result=1
510    fi
511    
512    # Test build scripts
513    if test_build_scripts; then
514      echo -e "${GREEN}✓ Build scripts tests passed${RESET}"
515    else
516      echo -e "${RED}✗ Build scripts tests failed${RESET}"
517      result=1
518    fi
519    
520    # Test key packages
521    if test_key_packages; then
522      echo -e "${GREEN}✓ Key packages tests passed${RESET}"
523    else
524      echo -e "${RED}✗ Key packages tests failed${RESET}"
525      result=1
526    fi
527    
528    # Test chunking packages
529    if test_chunking_packages; then
530      echo -e "${GREEN}✓ Chunking packages tests passed${RESET}"
531    else
532      echo -e "${RED}✗ Chunking packages tests failed${RESET}"
533      result=1
534    fi
535    
536    # Test sync packages
537    if test_sync_packages; then
538      echo -e "${GREEN}✓ Sync packages tests passed${RESET}"
539    else
540      echo -e "${RED}✗ Sync packages tests failed${RESET}"
541      result=1
542    fi
543    
544    # Test binary build
545    if test_binary_build; then
546      echo -e "${GREEN}✓ Binary build tests passed${RESET}"
547    else
548      echo -e "${RED}✗ Binary build tests failed${RESET}"
549      result=1
550    fi
551    
552    # Test binary run
553    if test_binary_run; then
554      echo -e "${GREEN}✓ Binary run tests passed${RESET}"
555    else
556      echo -e "${RED}✗ Binary run tests failed${RESET}"
557      result=1
558    fi
559    
560    return ${result}
561  }
562  
563  # Main function
564  main() {
565    run_all_tests
566    local result=$?
567    
568    if [ ${result} -eq 0 ]; then
569      echo -e "${GREEN}All tests passed!${RESET}"
570    else
571      echo -e "${RED}Some tests failed!${RESET}"
572    fi
573    
574    exit ${result}
575  }
576  
577  # Run main function
578  main