#!/bin/bash

FILE_PATH="/usr/sbin/mx-audio-ctl"

function test_pass_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[32mPASS\e[0m] $2"
        else
                echo -e "[\e[31mFAIL\e[0m] $2"
        fi
}

function test_fail_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[31mFAIL\e[0m] $2"
        else
                echo -e "[\e[32mPASS\e[0m] $2"
        fi
}

# Test 1: Check if tool is installed
test -e $FILE_PATH
test_pass_result $? "Test 1: Check if tool is installed"

# Test 2: No arguments
bash $FILE_PATH >/dev/null 2>&1
test_fail_result $? "Test 2: No arguments"

# Test 3: Invalid option
bash $FILE_PATH -z >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid option"

# Test 4: Invalid speaker status
bash $FILE_PATH -s foo >/dev/null 2>&1
test_fail_result $? "Test 4: Invalid speaker status"

# Test 5: Get speaker status
result=$(bash $FILE_PATH -s)
test_pass_result $(
        [[ "$result" == "external" ]] ||
                [[ "$result" == "embedded" ]] ||
                [[ "$result" == *"(DIP SW forced)"* ]]
        echo $?
) "Test 5: Get speaker status"

# Test 6: Set speaker status to external
bash $FILE_PATH -s external >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
        echo -e "[\e[33mSKIP\e[0m] Test 6: Set speaker status to external (DIP SW forced)"
else
        result=$(bash $FILE_PATH -s)
        test_pass_result $(
                [[ "$result" == "external" ]]
                echo $?
        ) "Test 6: Set speaker status to external"
fi

# Test 7: Set speaker status to embedded
bash $FILE_PATH -s embedded >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
        echo -e "[\e[33mSKIP\e[0m] Test 7: Set speaker status to embedded (DIP SW forced)"
else
        result=$(bash $FILE_PATH -s)
        test_pass_result $(
                [[ "$result" == "embedded" ]]
                echo $?
        ) "Test 7: Set speaker status to embedded"
fi

# ─── Invalid Format ───────────────────────────────────────────────────────────

# Test 8: Numeric status
bash $FILE_PATH -s 0 >/dev/null 2>&1
test_fail_result $? "Test 8: Numeric status"

# Test 9: Uppercase status
bash $FILE_PATH -s EXTERNAL >/dev/null 2>&1
test_fail_result $? "Test 9: Uppercase status"

# Test 10: Empty status string
bash $FILE_PATH -s "" >/dev/null 2>&1
test_fail_result $? "Test 10: Empty status string"

# ─── Length Constraint ────────────────────────────────────────────────────────

# Test 11: Extremely long status string
bash $FILE_PATH -s "$(printf 'a%.0s' {1..1000})" >/dev/null 2>&1
test_fail_result $? "Test 11: Extremely long status string (1000 chars)"

# ─── Boundary Value ───────────────────────────────────────────────────────────

# Test 12: Status with leading space
bash $FILE_PATH -s " external" >/dev/null 2>&1
test_fail_result $? "Test 12: Status with leading space"

# Test 13: Status with trailing space
bash $FILE_PATH -s "external " >/dev/null 2>&1
test_fail_result $? "Test 13: Status with trailing space"

# Test 14: Status with extra argument after valid status
bash $FILE_PATH -s external extra >/dev/null 2>&1
test_fail_result $? "Test 14: Extra argument after valid status"

# ─── Security/Injection ───────────────────────────────────────────────────────

# Test 15: Command injection via semicolon
bash $FILE_PATH -s "external; id" >/dev/null 2>&1
test_fail_result $? "Test 15: Command injection via semicolon"

# Test 16: Command substitution injection
bash $FILE_PATH -s '$(id)' >/dev/null 2>&1
test_fail_result $? "Test 16: Command substitution injection"

# Test 17: Backtick injection
bash $FILE_PATH -s '`id`' >/dev/null 2>&1
test_fail_result $? "Test 17: Backtick injection"

# Test 18: Path traversal
bash $FILE_PATH -s "../etc/passwd" >/dev/null 2>&1
test_fail_result $? "Test 18: Path traversal"
