44f08daaed345316ecc0b3907e43e0c8c567d10e
[debian/amanda] / installcheck / Amanda_Util.pl
1 # Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 28;
20
21 use lib "@amperldir@";
22 use Data::Dumper;
23 use Amanda::Util;
24
25 # Data::Dumper is used to output strings with control characters
26 # in them, below
27 $Data::Dumper::Useqq = 1;  # quote strings
28 $Data::Dumper::Terse = 1;  # no $VAR1 = ..
29 $Data::Dumper::Indent = 0; # no newlines
30
31 # most of Amanda::Util is tested via running applications that use it
32
33 # Tests for quote_string and unquote string.  First, some fuzzing of the
34 # quote + unquote round-trip.
35 my @fuzzstrs = (
36     '',
37     'abcd',
38     '"',
39     '""',
40     '\\',
41     "\t", "\r", "\n", "\f",
42     '\\\\\\\\', # memory overflow?
43     'backslash\nletter',
44     'backslash\tletter',
45     '"quoted"',
46     "line\nanother", # real newline
47     "ends with slash\\",
48     '"starts with quote',
49     'ends with quote"',
50     "single'quote",
51 );
52
53 for my $fuzzstr (@fuzzstrs) {
54     is(Amanda::Util::unquote_string(Amanda::Util::quote_string($fuzzstr)), $fuzzstr,
55         "fuzz " . Dumper($fuzzstr));
56 }
57
58 # since users often provide quoted strings (e.g., in config files), test that chosen
59 # quoted strings are correctly unquoted.  The need to quote the quoted strings for perl
60 # makes this a little hard to read..
61 my %unquote_checks = (
62     '""' => '',
63     'abcd' => 'abcd',
64     '"abcd"' => 'abcd',
65     '"\t"' => "\t",
66     '"\r"' => "\r",
67     '"\n"' => "\n",
68     '"\f"' => "\f",
69     '"\t"' => "\t",
70     '"\\\\n"' => '\n', # literal \
71     '"\\\\"' => "\\",
72     '"\""' => "\"",
73 );
74
75 while (my ($qstr, $uqstr) = each %unquote_checks) {
76     is(Amanda::Util::unquote_string($qstr), $uqstr,
77         "unquote " . Dumper($qstr));
78 }