1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eu
4set -o pipefail
5
6SOURCE_ROOT="${1:?}"
7BUILD_ROOT="${2:?}"
8
9command -v gawk &>/dev/null || exit 77
10
11function generate_directives() {
12    gawk -v sec_rx="${2:-""}" -v unit_type="${3:-""}" '
13    match($0, /^([^ \t\.]+)\.([^ \t\.,]+)/, m) {
14        # res[section][directive] = 1
15        res[m[1]][m[2]] = 1;
16    }
17    END {
18        if (unit_type)
19            print unit_type
20
21        for (section in res) {
22            if (sec_rx && section !~ sec_rx)
23                continue
24
25            print "[" section "]";
26            for (directive in res[section]) {
27                print directive "=";
28            }
29        }
30    }
31    ' "$1"
32}
33
34ret=0
35if ! diff \
36     <(generate_directives "$SOURCE_ROOT"/src/network/networkd-network-gperf.gperf | sort) \
37     <(sort "$SOURCE_ROOT"/test/fuzz/fuzz-network-parser/directives); then
38    echo "Looks like test/fuzz/fuzz-network-parser/directives hasn't been updated"
39    ret=1
40fi
41
42if ! diff \
43     <(generate_directives "$SOURCE_ROOT"/src/network/netdev/netdev-gperf.gperf | sort) \
44     <(sort "$SOURCE_ROOT"/test/fuzz/fuzz-netdev-parser/directives.netdev); then
45    echo "Looks like test/fuzz/fuzz-netdev-parser/directives.netdev hasn't been updated"
46    ret=1
47fi
48
49if ! diff \
50     <(generate_directives "$SOURCE_ROOT"/src/udev/net/link-config-gperf.gperf | sort) \
51     <(sort "$SOURCE_ROOT"/test/fuzz/fuzz-link-parser/directives.link) ; then
52    echo "Looks like test/fuzz/fuzz-link-parser/directives.link hasn't been updated"
53    ret=1
54fi
55
56for section in Automount Mount Path Scope Slice Socket Swap Timer; do
57    if ! diff \
58         <(generate_directives "$BUILD_ROOT"/src/core/load-fragment-gperf.gperf "$section" "${section,,}" | sort) \
59         <(sort "$SOURCE_ROOT/test/fuzz/fuzz-unit-file/directives.${section,,}") ; then
60        echo "Looks like test/fuzz/fuzz-unit-file/directives.${section,,} hasn't been updated"
61        ret=1
62    fi
63done
64
65if ! diff \
66     <(generate_directives "$BUILD_ROOT"/src/core/load-fragment-gperf.gperf "(Service|Unit|Install)" "service" | sort) \
67     <(sort "$SOURCE_ROOT/test/fuzz/fuzz-unit-file/directives.service") ; then
68    echo "Looks like test/fuzz/fuzz-unit-file/directives.service hasn't been updated"
69    ret=1
70fi
71
72exit $ret
73