~ubuntu-branches/ubuntu/trusty/ufw/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/check-locales

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2012-04-04 12:12:25 UTC
  • mfrom: (30.1.13)
  • mto: This revision was merged to the branch mainline in revision 65.
  • Revision ID: package-import@ubuntu.com-20120404121225-pjt6j7hm3ua0q580
Tags: 0.31.1-1
* New upstream release (Closes: 663677, Closes: 625681)
* debian/control: update to standards 3.9.3
* convert to source format 3.0 (quilt)
* 0001-optimize-boot.patch: only read in /etc/ufw/ufw.conf when disabled
* debian/rules: adjust to only install the application profiles when not
  Ubuntu
* debian/po/nl.po: add Dutch translation of debconf templates. Thanks to
  Jeroen Schot (Closes: 658495)
* debian/po/da.po: add Danish translation of debconf templates. Thanks to
  Joe Dalton (Closes: 666557)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# check-locales: verify ufw with the existing locales. This is Ubuntu/Debian
 
3
# specific
 
4
#
 
5
# Copyright 2012 Canonical Ltd.
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License version 3,
 
9
#    as published by the Free Software Foundation.
 
10
#
 
11
#    This program is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the GNU General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
set -e
 
20
 
 
21
usage() {
 
22
    cat <<EOM
 
23
`basename $0` [OPTIONS] [<directory with ufw mo files>]
 
24
 -f   force installation of locales
 
25
 -d   directory with ufw mo files
 
26
 -p   directory with ufw po files
 
27
 -D   directory with to place mo files after converting from po
 
28
 -l   locale to test
 
29
 -v   verbose output
 
30
 
 
31
Eg:
 
32
# Run the C tests
 
33
$ sudo `basename $0` -l C
 
34
 
 
35
# Run the en tests
 
36
$ sudo `basename $0` -l en
 
37
 
 
38
# Run the sk tests, installing language-pack-sk* if needed
 
39
$ sudo `basename $0` -l sk -i
 
40
 
 
41
# Run the C tests
 
42
$ sudo `basename $0` -d /tmp/ufw-translations
 
43
 
 
44
# Run the all tests where we have translations, installing language-packs as
 
45
# needed.
 
46
$ sudo `basename $0` -l sk -i
 
47
EOM
 
48
}
 
49
 
 
50
is_root() {
 
51
    if id | egrep -q '^uid=0' ; then
 
52
        return 0
 
53
    fi
 
54
    return 1
 
55
}
 
56
 
 
57
install_langpack() {
 
58
    local myloc=`echo "$1" | sed 's#_.*$##'`
 
59
    langpack="language-pack-$myloc*"
 
60
    if ! dpkg-query -l "$langpack" 2>&1 | egrep -q '^ii' ; then
 
61
        echo "Installing '$langpack'"
 
62
        apt-get install -y --force-yes "$langpack"
 
63
    fi
 
64
}
 
65
 
 
66
has_locale() {
 
67
    local myloc="$1"
 
68
    if locale -a | egrep -q "^${myloc}.utf8$" ; then
 
69
        return 0
 
70
    elif locale -a | egrep -q "^${myloc}_.*.utf8$" ; then
 
71
        return 0
 
72
    fi
 
73
    return 1
 
74
}
 
75
 
 
76
gen_locale() {
 
77
    local myloc="${1}".UTF-8
 
78
    echo "Running 'locale-gen $myloc'"
 
79
    locale-gen "$myloc"
 
80
    echo ""
 
81
}
 
82
 
 
83
runcmd() {
 
84
    local myloc="$1"
 
85
    shift
 
86
    myargs=$*
 
87
 
 
88
    echo "Test ($myloc): $myargs"
 
89
    count=$((count+1))
 
90
    if [ "$verbose" = "yes" ]; then
 
91
        LC_ALL="$myloc" ufw $myargs || {
 
92
            err_count=$((err_count+1))
 
93
            return 1
 
94
        }
 
95
    else
 
96
        LC_ALL="$myloc" ufw $myargs >/dev/null || {
 
97
            err_count=$((err_count+1))
 
98
            return 1
 
99
        }
 
100
    fi
 
101
    echo ""
 
102
}
 
103
 
 
104
run_tests() {
 
105
    local myloc="$1"
 
106
 
 
107
    local locales="$myloc"
 
108
    if [ "$myloc" != "C" ] && echo "$myloc" | grep -qv "_" ; then
 
109
        locales="${myloc}.utf8 `locale -a | egrep "^${myloc}_.*.utf8$"`"
 
110
    fi
 
111
 
 
112
    local loc=
 
113
    myerr=""
 
114
    for loc in $locales ; do
 
115
        runcmd "$loc" status || myerr="yes"
 
116
        runcmd "$loc" allow 22/tcp || myerr="yes"
 
117
        runcmd "$loc" --force enable || myerr="yes"
 
118
        runcmd "$loc" allow 80 || myerr="yes"
 
119
        runcmd "$loc" allow from 192.168.254.1 || myerr="yes"
 
120
 
 
121
        runcmd "$loc" status || myerr="yes"
 
122
        runcmd "$loc" status verbose || myerr="yes"
 
123
        runcmd "$loc" status numbered || myerr="yes"
 
124
 
 
125
        runcmd "$loc" delete allow from 192.168.254.1 || myerr="yes"
 
126
        runcmd "$loc" delete allow 80 || myerr="yes"
 
127
        runcmd "$loc" delete allow 22/tcp || myerr="yes"
 
128
 
 
129
        # nonexistent rules
 
130
        runcmd "$loc" delete allow 23 || myerr="yes"
 
131
 
 
132
        # disable
 
133
        runcmd "$loc" disable || myerr="yes"
 
134
    done
 
135
    if [ "$myerr" = "yes" ]; then
 
136
        return 1
 
137
    fi
 
138
    return 0
 
139
}
 
140
 
 
141
#
 
142
# Main
 
143
#
 
144
 
 
145
if ! is_root ; then
 
146
    echo "Must be root to run this script" >&2
 
147
    exit 2
 
148
fi
 
149
 
 
150
force=
 
151
ufw_locales="/usr/share/ufw/messages"
 
152
ufw_locale="*"
 
153
verbose=
 
154
install=
 
155
while getopts "hfid:l:v" opt
 
156
do
 
157
    case "$opt" in
 
158
        f) force="yes";;
 
159
        d) ufw_locales=`echo "$OPTARG" | sed 's#/\+$##'`;;
 
160
        l) ufw_locale="$OPTARG";;
 
161
        i) install="yes";;
 
162
        v) verbose="yes";;
 
163
        h) usage ; exit 0;;
 
164
        ?) usage;;
 
165
    esac
 
166
done
 
167
shift $(($OPTIND - 1))
 
168
 
 
169
if [ "$ufw_locale" != "*" ]; then
 
170
    if [ "$ufw_locale" = "C" ]; then
 
171
        ufw_locale="C"
 
172
    elif ls -1 "$ufw_locales/$ufw_locale"*.mo >/dev/null 2>&1 ; then
 
173
        ufw_locale="${ufw_locale}*"
 
174
    fi
 
175
fi
 
176
if [ ! -d "$ufw_locales" ]; then
 
177
    echo "'$ufw_locales' is not a directory" >&2
 
178
    usage
 
179
    exit 1
 
180
fi
 
181
 
 
182
if [ "$force" != "yes" ]; then
 
183
    echo "This script will alter your system by generating locales and running"
 
184
    echo "various ufw commands. It will not clean up generated locales and may"
 
185
    echo "alter your system in other ways. This should not be used on a"
 
186
    echo -n "production system. Proceed (y|N)? "
 
187
    read ans
 
188
    if [ "$ans" != "y" ] && [ "$ans" != "Y" ]; then
 
189
        echo "Aborting" >&2
 
190
        exit 2
 
191
    fi
 
192
fi
 
193
 
 
194
if [ "$install" = "yes" ] && [ "$ufw_locale" != "C" ]; then
 
195
    echo "Configuring locales..."
 
196
    if ls -1 "$ufw_locales"/$ufw_locale.mo >/dev/null 2>&1 ; then
 
197
        for i in `ls "$ufw_locales"/$ufw_locale.mo` ; do
 
198
            loc=`basename $i | sed 's#\.mo$##'`
 
199
            if ! install_langpack "$loc" ; then
 
200
                echo "Could not install langpack for '$loc'. Skipping"
 
201
                continue
 
202
            fi
 
203
            if ! has_locale "$loc" ; then
 
204
                gen_locale "$loc"
 
205
            fi
 
206
        done
 
207
    else
 
208
        if ! install_langpack "$ufw_locale" ; then
 
209
            echo "Could not install langpack for '$ufw_locale'. Skipping"
 
210
            continue
 
211
        fi
 
212
        if ! has_locale "$ufw_locale" ; then
 
213
            gen_locale "$ufw_locale"
 
214
        fi
 
215
    fi
 
216
    echo "Done configuring locales"
 
217
fi
 
218
 
 
219
count=0
 
220
err_count=0
 
221
if [ "$ufw_locale" = "C" ] || ! ls -1 "$ufw_locales"/$ufw_locale.mo >/dev/null 2>&1 ; then
 
222
    run_tests "$ufw_locale" || echo FAIL
 
223
else
 
224
    echo "Testing locales in '$ufw_locales'"
 
225
    for i in `ls "$ufw_locales"/$ufw_locale.mo` ; do
 
226
        loc=`basename $i | sed 's#\.mo$##'`
 
227
        run_tests "$loc" || echo FAIL
 
228
    done
 
229
fi
 
230
 
 
231
echo "--"
 
232
echo "Test summary: $count tests, $err_count failures"
 
233
 
 
234
if [ $err_count -ne 0 ]; then
 
235
    exit 1
 
236
fi