1#!/usr/bin/perl -w
2#
3# Copyright (C) 2001-2005 Blair Zajac.  All rights reserved.
4
5$| = 1;
6
7use strict;
8use Test::More tests => 1162;
9use File::Spec;
10
11BEGIN { use_ok('Apache::ConfigParser::Directive'); }
12
13# Cd into t if it exists.
14chdir 't' if -d 't';
15
16package EmptySubclass;
17use Apache::ConfigParser::Directive;
18use vars qw(@ISA);
19@ISA = qw(Apache::ConfigParser);
20package main;
21
22my $d = Apache::ConfigParser::Directive->new;
23ok($d, 'Apache::ConfigParser::Directive created');
24
25# Check the initial values of the object.
26is($d->name,        '', 'initial name is empty');
27is($d->value,       '', 'initial value is empty');
28is($d->orig_value,  '', 'initial `original\' value is empty');
29
30my @value = $d->get_value_array;
31ok(eq_array(\@value, []), 'initial value array is empty');
32
33@value = $d->get_orig_value_array;
34ok(eq_array(\@value, []), 'initial `original\' value array is empty');
35
36is($d->filename,    '', 'initial filename is empty');
37is($d->line_number, -1, 'initial line number is -1');
38
39is($d->filename('file.txt'), '',         'filename is empty and set it');
40is($d->filename,             'file.txt', 'filename is now file.txt');
41
42is($d->line_number(123),  -1, 'line number is -1 and set it to 123');
43is($d->line_number,      123, 'line number is now 123');
44
45# Test setting and getting parameters.
46is($d->name('SomeDirective'), '',              'name is empty and set it');
47is($d->name,                  'somedirective', 'name is no somedirective');
48
49is($d->value('SomeValue1 Value2'), '', 'initial value is empty and set it');
50is($d->value, 'SomeValue1 Value2', 'value is now SomeValue1 Value2');
51
52@value = $d->get_value_array;
53ok(eq_array(\@value, [qw(SomeValue1 Value2)]), 'value array has two elements');
54ok(eq_array(\@value, $d->value_array_ref),     'value array ref matches');
55
56# Check that the `original' value has not changed.
57is($d->orig_value, '', '`original\' value has not changed');
58@value = $d->get_orig_value_array;
59ok(eq_array(\@value, []), '`original\' value array has not changed');
60ok(eq_array([], $d->orig_value_array_ref),
61   '`original\' value array ref has not changed');
62
63# Try a more complicates value string.
64my $str1 = '"%h \"%r\" %>s \"%{Referer}i\" \"%{User-Agent}i\"" \foo  bar';
65is($d->value($str1), 'SomeValue1 Value2', 'setting a new complicated value');
66is($d->value,
67   '"%h \"%r\" %>s \"%{Referer}i\" \"%{User-Agent}i\"" \foo  bar',
68   'complicated string value matched');
69@value = $d->get_value_array;
70ok(eq_array(\@value,
71            ['%h "%r" %>s "%{Referer}i" "%{User-Agent}i"', '\foo', 'bar']),
72   'complicated value array matches');
73ok(eq_array($d->value_array_ref,
74            ['%h "%r" %>s "%{Referer}i" "%{User-Agent}i"', '\foo', 'bar']),
75   'complicated value array ref matches');
76
77# Set the value using the array interface.
78$d->set_orig_value_array;
79is($d->orig_value, '', 'set empty array results in empty string value');
80@value = $d->get_orig_value_array;
81ok(eq_array(\@value, []), 'set empty array results in empty array value');
82ok(eq_array([], $d->orig_value_array_ref),
83   'set empty array results in empty array value ref');
84
85@value = ('this', 'value', 'has whitespace and quotes in it ""\ \ ');
86$d->set_orig_value_array(@value);
87my @v = $d->get_orig_value_array;
88ok(eq_array(\@v, \@value), 'complicated set value array matches array');
89ok(eq_array(\@v, $d->orig_value_array_ref),
90   'complicates set value array matches array ref');
91is($d->orig_value,
92   'this value "has whitespace and quotes in it \"\"\\\\ \\\\ "',
93   'complicated set value array string matches');
94
95# Test setting and getting undefined values.
96is($d->value(undef),    $str1, 'value matches and set to undef');
97is($d->value_array_ref, undef, 'value array ref is undef');
98is($d->value(''),       undef, 'value is now undef');
99ok(eq_array([], $d->value_array_ref), 'value array ref to empty array');
100ok(eq_array([], $d->value_array_ref(undef)), 'value array ref to empty array');
101is($d->value,           undef, 'value is not undef again');
102is($d->value_array_ref, undef, 'value array ref is again undef');
103is(scalar $d->get_value_array, undef, 'getting value array returns undef');
104@value = $d->get_value_array;
105ok(eq_array(\@value, []), 'value array is empty');
106
107# Test value_is_path, value_is_rel_path and value_is_abs_path.
108my $one_pipe     = '| some_program_to_pipe_to';
109my @pipe         = ($one_pipe, $one_pipe);
110my $pipe         = "'$one_pipe' '$one_pipe'";
111my $one_syslog   = 'syslog:local7';
112my @syslog       = ($one_syslog, $one_syslog);
113my $syslog       = "'$one_syslog' '$one_syslog'";
114my $one_abs_path = File::Spec->rel2abs('.');
115my @abs_path     = ($one_abs_path, $one_abs_path);
116my $abs_path     = "$one_abs_path $one_abs_path";
117my $one_rel_path = File::Spec->catfile('some', 'relative', 'path');
118my @rel_path     = ($one_rel_path, $one_rel_path);
119my $rel_path     = "@rel_path";
120my $one_dev_null = File::Spec->devnull;
121my @dev_null     = ($one_dev_null, $one_dev_null);
122my $dev_null     = "@dev_null";
123
124# This array is grouped into sets of 13 elements.  The elements are:
125#  1) Directive name
126#  2) value_is_path($pipe)
127#  3) value_is_abs_path($pipe)
128#  4) value_is_rel_path($pipe)
129#  5) value_is_path($syslog)
130#  6) value_is_abs_path($syslog)
131#  7) value_is_rel_path($syslog)
132#  8) value_is_path($abs_path)
133#  9) value_is_abs_path($abs_path)
134# 10) value_is_rel_path($abs_path)
135# 11) value_is_path($rel_path)
136# 12) value_is_abs_path($rel_path)
137# 13) value_is_rel_path($rel_path);
138my @tests = qw(aaa               0 0 0 0 0 0 0 0 0 0 0 0
139               AccessConfig      1 0 1 1 0 1 1 1 0 1 0 1
140               AgentLog          0 0 0 1 0 0 1 1 0 1 0 0
141               AuthDBGroupFile   1 0 0 1 0 0 1 1 0 1 0 0
142               AuthDBMGroupFile  1 0 0 1 0 0 1 1 0 1 0 0
143               AuthDBMUserFile   1 0 0 1 0 0 1 1 0 1 0 0
144               AuthDBUserFile    1 0 0 1 0 0 1 1 0 1 0 0
145               AuthDigestFile    1 0 0 1 0 0 1 1 0 1 0 0
146               AuthGroupFile     1 0 1 1 0 1 1 1 0 1 0 1
147               AuthUserFile      1 0 1 1 0 1 1 1 0 1 0 1
148               CacheRoot         1 0 0 1 0 0 1 1 0 1 0 0
149               CookieLog         1 0 1 1 0 1 1 1 0 1 0 1
150               CoreDumpDirectory 1 0 0 1 0 0 1 1 0 1 0 0
151               CustomLog         0 0 0 1 0 1 1 1 0 1 0 1
152               Directory         1 0 0 1 0 0 1 1 0 1 0 0
153               DocumentRoot      1 0 0 1 0 0 1 1 0 1 0 0
154               ErrorLog          0 0 0 0 0 0 1 1 0 1 0 1
155               Include           1 0 1 1 0 1 1 1 0 1 0 1
156               LoadFile          1 0 1 1 0 1 1 1 0 1 0 1
157               LoadModule        1 0 1 1 0 1 1 1 0 1 0 1
158               LockFile          1 0 1 1 0 1 1 1 0 1 0 1
159               MimeMagicFile     1 0 1 1 0 1 1 1 0 1 0 1
160               MMapFile          1 0 0 1 0 0 1 1 0 1 0 0
161               PidFile           1 0 1 1 0 1 1 1 0 1 0 1
162               RefererLog        0 0 0 1 0 1 1 1 0 1 0 1
163               ResourceConfig    1 0 1 1 0 1 1 1 0 1 0 1
164               RewriteLock       1 0 0 1 0 0 1 1 0 1 0 0
165               ScoreBoardFile    1 0 1 1 0 1 1 1 0 1 0 1
166               ScriptLog         1 0 1 1 0 1 1 1 0 1 0 1
167               ServerRoot        1 0 0 1 0 0 1 1 0 1 0 0
168               TransferLog       0 0 0 1 0 1 1 1 0 1 0 1
169               TypesConfig       1 0 1 1 0 1 1 1 0 1 0 1);
170is(@tests % 13, 0, 'number of elements in @tests is a multiple of 13');
171while (@tests > 6) {
172  my ($dn, @a) = splice(@tests, 0, 13);
173
174  $d->name($dn);
175
176  # Check that a pipe is treated properly.
177  $d->set_value_array(@pipe);
178  $d->set_orig_value_array(@pipe);
179  is($d->value_is_path,          $a[0], "$dn $pipe value path");
180  is($d->value_is_abs_path,      $a[1], "$dn $pipe value abs path");
181  is($d->value_is_rel_path,      $a[2], "$dn $pipe value rel path");
182
183  is($d->orig_value_is_path,     $a[0], "$dn $pipe value path");
184  is($d->orig_value_is_abs_path, $a[1], "$dn $pipe value abs path");
185  is($d->orig_value_is_rel_path, $a[2], "$dn $pipe value rel path");
186
187  # Check that a syslog is treated properly.
188  ok(eq_array(\@pipe, [$d->set_value_array(@syslog)]), "old value is @pipe");
189  ok(eq_array(\@pipe, [$d->set_orig_value_array(@syslog)]), "old orig value is @pipe");
190
191  is($d->value_is_path,          $a[3], "$dn $syslog value path");
192  is($d->value_is_abs_path,      $a[4], "$dn $syslog value abs path");
193  is($d->value_is_rel_path,      $a[5], "$dn $syslog value rel path");
194
195  is($d->orig_value_is_path,     $a[3], "$dn $syslog value path");
196  is($d->orig_value_is_abs_path, $a[4], "$dn $syslog value abs path");
197  is($d->orig_value_is_rel_path, $a[5], "$dn $syslog value rel path");
198
199  # Test setting to the /dev/null equivalent on this operating system.
200  is($d->value($dev_null),      "@syslog", "old value is $syslog");
201  is($d->orig_value($dev_null), "@syslog", "old orig value is $syslog");
202  is($d->value_is_path,     0, "$dn $dev_null is not a path");
203  is($d->value_is_abs_path, 0, "$dn $dev_null is not a abs path");
204  is($d->value_is_rel_path, 0, "$dn $dev_null is not a rel path");
205
206  is($d->value($abs_path),      $dev_null, "old value is $dev_null");
207  is($d->orig_value($abs_path), $dev_null, "old orig value is $dev_null");
208
209  is($d->value_is_path,          $a[6], "$dn $abs_path path value");
210  is($d->value_is_abs_path,      $a[7], "$dn $abs_path abs path value");
211  is($d->value_is_rel_path,      $a[8], "$dn $abs_path rel path value");
212
213  is($d->orig_value_is_path,     $a[6], "$dn $abs_path path orig value");
214  is($d->orig_value_is_abs_path, $a[7], "$dn $abs_path abs path orig value");
215  is($d->orig_value_is_rel_path, $a[8], "$dn $abs_path rel path orig value");
216
217  is($d->value($rel_path),       $abs_path, "old value is $abs_path");
218  is($d->orig_value($rel_path),  $abs_path, "old orig value is $abs_path");
219
220  is($d->value_is_path,          $a[9], " $dn $rel_path path value");
221  is($d->value_is_abs_path,      $a[10], "$dn $rel_path abs path value");
222  is($d->value_is_rel_path,      $a[11], "$dn $rel_path rel path value");
223
224  is($d->orig_value_is_path,     $a[9],  "$dn $rel_path path orig value");
225  is($d->orig_value_is_abs_path, $a[10], "$dn $rel_path abs path orig value");
226  is($d->orig_value_is_rel_path, $a[11], "$dn $rel_path rel path orig value");
227}
228