summaryrefslogtreecommitdiffstatshomepage
path: root/wsl/.local/bin/gpg-test.sh
blob: f23b83d2fa0add4d1920c999d194b111ea3bae77 (plain)
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
101
102
#!/usr/bin/env bash

TEMP=$(getopt -o hvdsea: --long help,verbose,debug,signature,encryption,authentication \
    -n 'javawrap' -- "$@")

if [ $? != 0 ]; then
    echo "Terminating..." >&2
    exit 1
fi

eval set -- "$TEMP"

usage() {
    cat <<EOF
usage: $0 [OPTIONS]

    -h,--help               Show this message
    -v,--verbose            Show test output
    -d,--debug              NOT IMPLEMENTED
    -s,--signature          Test signature key
    -e,--encryption         Test encryption key
    -a,--authentication     Test authentication key
EOF
}

VERBOSE=false
DEBUG=false
SIGNATURE=false
ENCRYPTION=false
AUTHENTICATION=false
ALL=true

while true; do
    case "$1" in
    -h | --help)
        usage
        exit 0
        ;;
    -v | --verbose)
        VERBOSE=true
        shift
        ;;
    -d | --debug)
        DEBUG=true
        shift
        ;;
    -s | --signature)
        ALL=false
        SIGNATURE=true
        shift
        ;;
    -e | --encryption)
        ALL=false
        ENCRYPTION=true
        shift
        ;;
    -a | --authentication)
        ALL=false
        AUTHENTICATION=true
        shift
        ;;
    --)
        shift
        break
        ;;
    *) break ;;
    esac
done

function print_result() {
    if [ $? -ne 0 ]; then
        echo "Failed: $?"
    else
        echo "Succeeded"
    fi

    if [ $VERBOSE -eq "true" ]; then
        echo "\nOutput:\n$result"
    fi
}

if [[ $ALL -eq "true" || $SIGNITURE -eq "true" ]]; then
    echo "Testing signiture key..."
    result="$(echo '' | gpg --clearsign &>/dev/null 2>&1)"
    print_result result
fi

if [[ $ALL -eq "true" || $ENCRYPTION -eq "true" ]]; then
    echo "Testing encryption key..."
    temp="$(mktemp -d)"
    echo "secret file contents: 42" >$temp/test.txt

    result="$(gpg --output $temp/test.gpg -e $temp/test.txt 2>&1 && gpg --output $temp/test.out -d $temp/test.txt.gpg 2>&1)"
    grep '42' $temp/test.out 2>&1
    print_result result
fi

if [[ $ALL -eq "true" || $AUTHENTICATION -eq "true" ]]; then
    echo "Testing authentication key..."
    result="$(ssh-add -l)"
    print_result result
fi