#!/bin/bash # Tester for process_termination exercise # Tests process monitoring and termination with kill() RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color EXERCISE="process_termination" EXE_PATH="" PASSED=0 FAILED=0 # Determine the project root directory, which is one level up from the 'testers' directory. PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) echo "========================================" echo "Testing: $EXERCISE" echo "========================================" # --- Compilation and Executable Finding Logic --- EXE_PATH="$PROJECT_ROOT/$EXERCISE" RENDU_EXE_PATH="$PROJECT_ROOT/rendu/$EXERCISE" SOURCE_FILE="$PROJECT_ROOT/${EXERCISE}.c" RENDU_SOURCE_FILE="$PROJECT_ROOT/rendu/${EXERCISE}.c" # Prefer executable in 'rendu' directory if [ -f "$RENDU_EXE_PATH" ]; then EXE_PATH="$RENDU_EXE_PATH" # Fallback to root directory executable elif [ -f "$EXE_PATH" ]; then : # EXE_PATH is already set correctly # If no executable, try to compile from source else COMPILE_CANDIDATE="" if [ -f "$RENDU_SOURCE_FILE" ]; then COMPILE_CANDIDATE="$RENDU_SOURCE_FILE" elif [ -f "$SOURCE_FILE" ]; then COMPILE_CANDIDATE="$SOURCE_FILE" fi if [ -n "$COMPILE_CANDIDATE" ]; then echo -e "${YELLOW}Executable not found, attempting to compile from $COMPILE_CANDIDATE...${NC}" gcc -Wall -Wextra -Werror -pthread "$COMPILE_CANDIDATE" -o "$PROJECT_ROOT/$EXERCISE" if [ $? -eq 0 ]; then echo -e "${GREEN}✓ Compilation successful.${NC}" EXE_PATH="$PROJECT_ROOT/$EXERCISE" # Use the newly compiled executable else echo -e "${RED}✗ Compilation failed.${NC}" exit 1 fi else echo -e "${RED}✗ Executable and source file for '$EXERCISE' not found.${NC}" exit 1 fi fi echo -e "${YELLOW}Using executable at: $EXE_PATH${NC}" # --- End of Logic --- # Test 1: Program executes and terminates properly echo -n "Test 1: Basic execution... " OUTPUT=$(timeout 10 "$EXE_PATH" 2>&1) EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Program failed to execute (exit code: $EXIT_CODE)" ((FAILED++)) fi # Test 2: Creates 5 child processes echo -n "Test 2: Creates 5 child processes... " CHILD_COUNT=$(echo "$OUTPUT" | grep -oE "Child [0-9]+" | sort -u | wc -l) if [ "$CHILD_COUNT" -eq 5 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected 5 unique children, found: $CHILD_COUNT" ((FAILED++)) fi # Test 3: Detects when a child dies echo -n "Test 3: Detects child death... " if echo "$OUTPUT" | grep -q "has died"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Parent didn't detect child death" ((FAILED++)) fi # Test 4: Parent terminates remaining children echo -n "Test 4: Terminates remaining children... " TERMINATED_COUNT=$(echo "$OUTPUT" | grep -c "terminated") if [ "$TERMINATED_COUNT" -ge 4 ]; then # At least 4 children should be terminated echo -e "${GREEN}✓ PASSED${NC} ($TERMINATED_COUNT children terminated)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected at least 4 children to be terminated, got: $TERMINATED_COUNT" ((FAILED++)) fi # Test 5: Proper cleanup message echo -n "Test 5: Cleanup message... " if echo "$OUTPUT" | grep -q "All processes cleaned up"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Missing cleanup confirmation message" ((FAILED++)) fi # Test 6: Program duration (should terminate after first child dies, 2-5 seconds) echo -n "Test 6: Terminates after first death... " START_TIME=$(date +%s) timeout 10 "$EXE_PATH" > /dev/null 2>&1 END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) if [ $DURATION -ge 2 ] && [ $DURATION -le 7 ]; then echo -e "${GREEN}✓ PASSED${NC} (ran for ${DURATION}s)" ((PASSED++)) else echo -e "${YELLOW}⚠ PARTIAL${NC} (ran for ${DURATION}s, expected 2-5s)" echo "This may be acceptable depending on random death times" ((PASSED++)) fi # Test 7: No zombie processes remain after execution echo -n "Test 7: No zombie processes... " "$EXE_PATH" > /dev/null 2>&1 & PARENT_PID=$! sleep 2 ZOMBIE_COUNT=$(ps aux | grep "$PARENT_PID" | grep -c "defunct" || echo "0") wait $PARENT_PID 2>/dev/null sleep 1 ZOMBIE_COUNT_AFTER=$(ps aux | grep "defunct" | grep -c "$EXERCISE" || echo "0") if [ "$ZOMBIE_COUNT" -eq 0 ] && [ "$ZOMBIE_COUNT_AFTER" -eq 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Found zombie processes (during: $ZOMBIE_COUNT, after: $ZOMBIE_COUNT_AFTER)" ((FAILED++)) fi # Test 8: No orphaned processes echo -n "Test 8: No orphaned processes... " "$EXE_PATH" > /dev/null 2>&1 sleep 1 # Check if there are any processes from this exercise still running ORPHAN_COUNT=$(ps aux | grep "$EXERCISE" | grep -v "grep" | grep -v "test_" | wc -l) if [ "$ORPHAN_COUNT" -eq 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Found $ORPHAN_COUNT orphaned processes" # Clean up orphans pkill -f "$EXERCISE" 2>/dev/null ((FAILED++)) fi # Test 9: Check for memory leaks with valgrind if command -v valgrind &> /dev/null; then echo -n "Test 9: Memory leak check... " VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 --trace-children=yes "$EXE_PATH" 2>&1) if [ $? -ne 42 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Memory leaks detected!" echo "$VALGRIND_OUTPUT" | grep -A 5 "LEAK SUMMARY" ((FAILED++)) fi else echo -e "${YELLOW}⊘ Test 9: Valgrind not installed, skipping memory test${NC}" fi # Summary echo "========================================" echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}" echo "========================================" if [ $FAILED -eq 0 ]; then exit 0 else exit 1 fi