all repos — dot @ master

Shell, editor and terminal configuration, and the tools that go with them

secrets/test/edge-cases.sh (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
#!/bin/bash
# Edge case tests

this_dir="$(cd "$(dirname "$0")" && pwd)"
source "${this_dir}/common.sh"

test_special_characters_in_value() {
    local secret_value='!@#$%^&*()_+-=[]{}|;:",.<>?/`~'"'"
    echo "${secret_value}" | szecret put "special-chars"

    local retrieved
    retrieved="$(szecret get "special-chars")"
    assert_equals "${secret_value}" "${retrieved}" "special characters"
    pass "handles special characters in value"
}

test_whitespace_in_value() {
    local secret_value=$'  leading and trailing spaces  '
    echo "${secret_value}" | szecret put "whitespace"

    local retrieved
    retrieved="$(szecret get "whitespace")"
    assert_equals "${secret_value}" "${retrieved}" "whitespace"
    pass "preserves whitespace in value"
}

test_empty_value() {
    echo -n "" | szecret put "empty-secret"

    local retrieved
    retrieved="$(szecret get "empty-secret")"
    assert_equals "" "${retrieved}" "empty value"
    pass "handles empty value"
}

test_multiline_value() {
    # Note: trailing newlines are stripped by bash $() substitution
    # This is bash behavior, not a szecret limitation
    local secret_value=$'line1\nline2\n\nline4 with spaces'
    echo "${secret_value}" | szecret put "multiline"

    local retrieved
    retrieved="$(szecret get "multiline")"
    assert_equals "${secret_value}" "${retrieved}" "multiline value"
    pass "handles multiline value with blank lines"
}

test_unicode_in_value() {
    local secret_value='Hello 世界 🔐 émoji café'
    echo "${secret_value}" | szecret put "unicode"

    local retrieved
    retrieved="$(szecret get "unicode")"
    assert_equals "${secret_value}" "${retrieved}" "unicode value"
    pass "handles unicode in value"
}

test_secret_name_with_dashes() {
    echo "value" | szecret put "my-secret-with-dashes"
    assert_success szecret get "my-secret-with-dashes" >/dev/null
    pass "handles dashes in secret name"
}

test_secret_name_with_underscores() {
    echo "value" | szecret put "my_secret_with_underscores"
    assert_success szecret get "my_secret_with_underscores" >/dev/null
    pass "handles underscores in secret name"
}

test_secret_name_with_dots() {
    echo "value" | szecret put "my.secret.with.dots"
    assert_success szecret get "my.secret.with.dots" >/dev/null
    pass "handles dots in secret name"
}

test_long_value() {
    # Generate a long value (10KB)
    local secret_value
    secret_value="$(head -c 10240 /dev/urandom | base64)"
    echo "${secret_value}" | szecret put "long-secret"

    local retrieved
    retrieved="$(szecret get "long-secret")"
    assert_equals "${secret_value}" "${retrieved}" "long value"
    pass "handles long value (10KB)"
}

run_tests() {
    test_special_characters_in_value
    test_whitespace_in_value
    test_empty_value
    test_multiline_value
    test_unicode_in_value
    test_secret_name_with_dashes
    test_secret_name_with_underscores
    test_secret_name_with_dots
    test_long_value
}

[[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests