~ubuntu-x-swat/xorg-server/xsmoke

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
DELAY=3           # Time to wait for X to start up (in sec)
DEFAULT_XORG_CONF="/etc/X11/xorg.conf"
DISPLAY=:0

# Internal variables
test_count=0
pass_count=0
fail_count=0

is_superuser() {
    test $(id -u) = 0
}

print_test_summary() {
    echo "Tests: $test_count;  Passed: $pass_count;  Failed: $fail_count"
}

die() {
    msg=$1

    echo "Error:  $msg"
    print_test_summary
    exit 1
}

pass() {
    msg=$1
    echo "[PASS]  $msg"
    pass_count=$(( $pass_count + 1 ))
    return 0
}

fail() {
    msg=$1
    echo "[FAIL]  $msg"
    fail_count=$(( $fail_count + 1 ))
    return 1
}

is_running() {
    proc=$1
    $(pidof $proc > /dev/null)
}

stop_service() {
    svc=$1
    pidof $svc > /dev/null && /etc/init.d/$svc stop
    pidof $svc > /dev/null && die "Could not stop service $svc"
    echo "Service $svc stopped"
}

stop_command() {
    cmd=$1
    pidof $cmd > /dev/null && pkill -9 $cmd
    pidof $cmd > /dev/null && die "Could not stop command $cmd"
    echo "Command $cmd stopped"
}

get_xorg_drivers() {
    pid=`pidof X`
    if [ -z "$pid" ]; then
        echo "Error:  X is not running"
        return 1
    fi
    cat /proc/$pid/smaps | \
        egrep '_drv.so|_dri.so' | \
        cut -d '/' -f 7 | \
        cut -d "_" -f 1 | \
        uniq
}

get_xorg_driver_version_loaded() {
    driver=$1
    xlogparse /var/log/Xorg.0.log --modules | egrep "^$driver " | sed -e 's/ \+/ /g' | cut -d ' ' -f2
}

get_debian_version() {
    pkg=$1
    dpkg -l $pkg  | cat \
        | egrep '^ii' \
        | sed -e "s/ \+/ /g" \
        | cut -d' ' -f 3
}

get_installed_version() {
    get_debian_version $1 \
        | sed -e "s/^.*+//" \
        | cut -d- -f1 \
        | sed -e "s/^[0-9]://" \
        | sed -e "s/ubuntu.*$//"
}

test_syntax_bash() {
    file=$1
    test_count=$(( $test_count + 1))

    errors=`bash -n $file 2>&1`
    if [ $? = 0 ]; then
        pass "$file is a valid bash program"
    else
        fail "$file has invalid bash syntax:  $errors"
    fi
}

test_syntax_perl() {
    file=$1
    test_count=$(( $test_count + 1))

    errors=`perl -c $file 2>&1`
    if [ $? = 0 ]; then
        pass "$file is a valid perl program"
    else
        fail "$file has invalid perl syntax:  $errors"
    fi
}

test_process_running() {
    proc=$1
    test_count=$(( $test_count + 1 ))

    pid=`pidof $proc`
    if [ ! -z $pid ]; then
        pass "$proc is running as process $pid"
    else
        fail "$proc is not running"
    fi
}

test_driver_loaded() {
    test_count=$(( $test_count + 1 ))

    driver=`get_xorg_drivers | grep $1`

    if [ ! -z $driver ]; then
        pass "driver $driver is loaded"
    else
        fail "driver $driver is not loaded"
    fi
}

test_driver_version() {
    driver=$1
    expected=$2

    test_count=$(( $test_count + 1 ))

    version=`get_xorg_driver_version_loaded $driver`
    if [ $version = $expected ]; then
        pass "$driver driver version $expected is loaded"
    else
        fail "$driver driver version is $version, not $expected as expected"
    fi
}

test_xorg_restart() {
    # Shut down X, gdm, etc
    echo "Stopping X"
    stop_command gdm
    stop_command startx
    stop_command X

    # Remove any stray X lock files
    rm -f /tmp/.X*-lock

    echo "Starting X"
    startx &

    sleep $DELAY

    # Verify X is running
    test_process_running X
}

test_xorg_conf_creation() {
    xorg_conf_file=$1

    test_count=$(( $test_count + 1 ))

    if [ -e $DEFAULT_XORG_CONF ]; then
        mv -f $DEFAULT_XORG_CONF $DEFAULT_XORG_CONF.backup
    fi

    output=`dexconf 2>&1`
    if [ $? = 0 ]; then
        pass "dexconf ran without error"
    else
        fail "dexconf returned error code.  $output"
    fi
}