#!/bin/bash


function call_bash_replace_or_append {
    {{{ bash_replace_or_append("$1", "$2", "$3") | indent(4) }}}
}

function call_bash_replace_or_append_w_format {
    {{{ bash_replace_or_append("$1", "$2", "$3", "$4") | indent(4) }}}
}

@test "bash_replace_or_append - Basic value remediation" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.randomize_va_space = 5" > "$tmp_file"
    expected_output="kernel.randomize_va_space = 2\n"

    call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - No remediation happened" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.randomize_va_space = 2" > "$tmp_file"
    expected_output="kernel.randomize_va_space = 2\n"

    call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - Append key to empty file" {
    tmp_file="$(mktemp)"
    printf "" > "$tmp_file"
    expected_output="kernel.randomize_va_space = 2\n"

    call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - Append key to non-empty file" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.randomize_va_fake = 5" > "$tmp_file"
    expected_output="kernel.randomize_va_fake = 5\nkernel.randomize_va_space = 2\n"

    call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - Remediation with format" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.randomize_va_space=5" > "$tmp_file"
    expected_output="kernel.randomize_va_space=2\n"

    call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - No remediation with format" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.randomize_va_space=2" > "$tmp_file"
    expected_output="kernel.randomize_va_space=2\n"

    call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - Append key with format to empty file" {
    tmp_file="$(mktemp)"
    printf "" > "$tmp_file"
    expected_output="kernel.randomize_va_space=2\n"

    call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - Append key with format to non-empty file" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.randomize_va_fake=5" > "$tmp_file"
    expected_output="kernel.randomize_va_fake=5\nkernel.randomize_va_space=2\n"

    call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "bash_replace_or_append - Remediate value containing forward slash" {
    tmp_file="$(mktemp)"
    printf "%s\n" "kernel.core_pattern=|/bin/true" > "$tmp_file"
    expected_output="kernel.core_pattern=|/bin/false\n"

    call_bash_replace_or_append_w_format "$tmp_file" '^kernel.core_pattern' '|/bin/false' '%s=%s'

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}
