1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4test_write_result() {
5	file=$1
6	content=$2
7	orig_content=$3
8	expect_reason=$4
9	expected=$5
10
11	echo "$content" > "$file"
12	if [ $? -ne "$expected" ]
13	then
14		echo "writing $content to $file doesn't return $expected"
15		echo "expected because: $expect_reason"
16		echo "$orig_content" > "$file"
17		exit 1
18	fi
19}
20
21test_write_succ() {
22	test_write_result "$1" "$2" "$3" "$4" 0
23}
24
25test_write_fail() {
26	test_write_result "$1" "$2" "$3" "$4" 1
27}
28
29test_content() {
30	file=$1
31	orig_content=$2
32	expected=$3
33	expect_reason=$4
34
35	content=$(cat "$file")
36	if [ "$content" != "$expected" ]
37	then
38		echo "reading $file expected $expected but $content"
39		echo "expected because: $expect_reason"
40		echo "$orig_content" > "$file"
41		exit 1
42	fi
43}
44
45source ./_chk_dependency.sh
46
47# Test attrs file
48# ===============
49
50file="$DBGFS/attrs"
51orig_content=$(cat "$file")
52
53test_write_succ "$file" "1 2 3 4 5" "$orig_content" "valid input"
54test_write_fail "$file" "1 2 3 4" "$orig_content" "no enough fields"
55test_write_fail "$file" "1 2 3 5 4" "$orig_content" \
56	"min_nr_regions > max_nr_regions"
57test_content "$file" "$orig_content" "1 2 3 4 5" "successfully written"
58echo "$orig_content" > "$file"
59
60# Test schemes file
61# =================
62
63file="$DBGFS/schemes"
64orig_content=$(cat "$file")
65
66test_write_succ "$file" "1 2 3 4 5 6 4 0 0 0 1 2 3 1 100 3 2 1" \
67	"$orig_content" "valid input"
68test_write_fail "$file" "1 2
693 4 5 6 3 0 0 0 1 2 3 1 100 3 2 1" "$orig_content" "multi lines"
70test_write_succ "$file" "" "$orig_content" "disabling"
71echo "$orig_content" > "$file"
72
73# Test target_ids file
74# ====================
75
76file="$DBGFS/target_ids"
77orig_content=$(cat "$file")
78
79test_write_succ "$file" "1 2 3 4" "$orig_content" "valid input"
80test_write_succ "$file" "1 2 abc 4" "$orig_content" "still valid input"
81test_content "$file" "$orig_content" "1 2" "non-integer was there"
82test_write_succ "$file" "abc 2 3" "$orig_content" "the file allows wrong input"
83test_content "$file" "$orig_content" "" "wrong input written"
84test_write_succ "$file" "" "$orig_content" "empty input"
85test_content "$file" "$orig_content" "" "empty input written"
86echo "$orig_content" > "$file"
87
88echo "PASS"
89