File: helpers

package info (click to toggle)
tang 15-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 468 kB
  • sloc: ansic: 1,079; sh: 439; makefile: 13
file content (135 lines) | stat: -rwxr-xr-x 3,273 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh -ex
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
#
# Copyright (c) 2016 Red Hat, Inc.
# Author: Nathaniel McCallum <npmccallum@redhat.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

fetch() {
    curl -sfg "http://127.0.0.1:${PORT}${1}"
}

ver() {
    jose jws ver -i- -k "${1}"
}

random_port() {
    if [ -n "${TANG_BSD}" ]; then
        jot -r 1 1024 65536
    else
        if test -f /dev/urandom;
        then
            shuf -i 1024-65535 -n 1 --random-file=/dev/urandom
        else
            shuf -i 1024-65535 -n 1
        fi
    fi
}

check_if_port_listening() {
    if [ -n "${TANG_BSD}" ]; then
        sockstat -l|grep "[\:\.]${1}" >/dev/null 2>&1
    else

        ss -anl|grep "[\:\.]${1}"|grep LISTEN >/dev/null 2>&1
    fi
}

wait_for_port()
{
    local port="${1}"
    sleep 1

    local i=0
    while [ "${i}" -lt 90 ]; do
        check_if_port_listening "${port}" && return 0
        i=$((i + 1))
        echo "try ${i}: waiting for port" >&2
        sleep 1
    done
    return 1
}

start_server() {
    "${SOCAT}" TCP-LISTEN:"${1}",bind=127.0.0.1,fork SYSTEM:"${VALGRIND} tangd ${TMP}/db" &
}

start_server_endpoint() {
    "${SOCAT}" TCP-LISTEN:"${1}",bind=127.0.0.1,fork SYSTEM:"${VALGRIND} tangd ${TMP}/db -e ${ENDPOINT}" &
}

start_standalone_server() {
    ${VALGRIND} tangd -p ${1} -l ${TMP}/db &
}

start_standalone_server_endpoint() {
    ${VALGRIND} tangd -p ${1} -l ${TMP}/db -e ${2} &
}

on_exit() {
    if [ "${PID}" ]; then
        kill "${PID}" || true
        wait "${PID}" || true
    fi
    [ -d "${TMP}" ] && rm -rf "${TMP}"
}

validate() {
    if ! _jwks="$(jose fmt --json="${1}" -Og payload -SyOg keys \
                 -AUo- 2>/dev/null)"; then
        echo "Advertisement is malformed" >&2
        exit 1
    fi
    _ver="$(printf '%s' "${_jwks}" | jose jwk use -i- -r -u verify -o-)"
    if ! printf '%s' "${_ver}" | jose jws ver -i "${1}" -k- -a; then
        echo "Advertisement is missing signatures" >&2
        exit 1
    fi
}

validate_sig() {
    jose fmt --json "${1}" --output=- | jose jwk use --input=- --required \
        --use verify 2>/dev/null
}

validate_exc() {
    jose fmt --json "${1}" --output=- | jose jwk use --input=- --required \
        --use deriveKey 2>/dev/null
}

sanity_check() {
    # Skip test if socat is not available.
    [ -n "${SOCAT}" ] || exit 77
}

die() {
    echo "${1}" >&2
    exit 1
}

valid_key_perm() {
    if [ -n "${TANG_BSD}" ]; then
        _perm="$(stat -f %Lp "${1}")"
    else
        _perm="$(stat -c %a "${1}")"
    fi
    [ "${_perm}" = "440" ]
}

expected_fail () {
    echo "Test was expected to fail" >&2
    exit 1
}