~ubuntu-branches/ubuntu/lucid/libebox/lucid

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Description:
use strict;
use warnings;

use Test::More tests => 115;
use Test::Exception;
use Fatal qw(mkdir);

use lib '../..';

BEGIN { use_ok('EBox::Validate') }; 

checkFilePathTest();
checkAbsoluteFilePathTest();
checkIsPrivateDir();
checkHostTest();
checkEmailAddressTest();
checkIP6Test();

sub checkFilePathTest
{
    my $checkFilePath_r = \&EBox::Validate::checkFilePath;
    _checkPathSubsTest($checkFilePath_r, 1);
}

sub checkAbsoluteFilePathTest
{
    my $checkAbsoluteFilePath_r = \&EBox::Validate::checkAbsoluteFilePath;
    _checkPathSubsTest($checkAbsoluteFilePath_r, 0);
}

sub checkIsPrivateDir
{
  ok !EBox::Validate::isPrivateDir('/macaco'), 'Testing isPrivateDir in a inaccesible dir';
  dies_ok  { EBox::Validate::isPrivateDir('/macaco', 1) } 'The same  with exceptions';


  ok !EBox::Validate::isPrivateDir('/'), 'Testing isPrivateDir in a no-owned and public accesible dir';
  dies_ok  { EBox::Validate::isPrivateDir('/', 1) } 'The same  with exceptions';


  my $dir = '/tmp/ebox.test.validate';
  system "rm -rf $dir" ;
  die $! if ($? != 0);

  mkdir($dir, 0777);		# public access
  ok !EBox::Validate::isPrivateDir($dir), 'Testing isPrivateDir in a owned but public accesible dir';
  dies_ok  { EBox::Validate::isPrivateDir($dir, 1) } 'The same  with exceptions';

  system "rm -rf $dir";
  die $! if ($? != 0);

  mkdir ($dir, 0700);
  ok EBox::Validate::isPrivateDir($dir), 'Testing isPrivateDir in a owned and private dir';
  lives_ok  { EBox::Validate::isPrivateDir($dir, 1) } 'The same  with exceptions';

  
}



sub _checkPathSubsTest
{
    my ($checkPathSub_r, $relativeIsValid) = @_;

    my @straightCases = qw(
			   /
			   /home/javier/.emacs
			   /home/javier/.emacs.d
			   /home/javier/.emacs.d/auto-save-list
			   /home/javier/.emacs.d/auto-save-list/.saves-12049-localhost.localdomain~
			   /home/javier/src/ebox-platform/trunk/extra/esofttool/debian/rules
			   /home/javier/src/ebox-platform/trunk/doc/.svn/format
			   /var/log/ntpstats/peerstats.20060524
			   /usr/share/doc/libtext-wrapi18n-perl/changelog.gz
			   /usr/share/man/man1/lexgrog.1.gz
			   /usr/share/man/man3/Apache::Resource.3pm.gz
			   /usr/share/locale/ro/LC_MESSAGES/libgnomecanvas-2.0.mo
			 );

    my @relativePathsCases = qw(
			  .
			  ..
			  ../..
			  .emacs
			  .ea.txt.gz
			  Config/t/cover_db/-home-javier-src-ebox-platform-trunk-common-libebox-src-EBox-pm.html
			  home/javier/.emacs
			  home/javier/.emacs.d
			  home/javier/.emacs.d/auto-save-list
			  home/javier/.emacs.d/auto-save-list/.saves-12049-localhost.localdomain~
			  home/javier/src/ebox-platform/trunk/extra/esofttool/debian/rules
			  ./home/javier/src/ebox-platform/trunk/doc/.svn/format
			  ./var/log/ntpstats/peerstats.20060524
			  ../share/doc/libtext-wrapi18n-perl/changelog.gz
			  ../share/man/man1/lexgrog.1.gz
			  ../share/man/man3/Apache::Resource.3pm.gz
			  ../../share/locale/ro/LC_MESSAGES/libgnomecanvas-2.0.mo
			   /home/../.emacs.d
			   /home/./.emacs.d/../auto-save-list
			   /home/./.emacs.d/auto-save-list/.saves-12049-localhost.localdomain~
			  );

    my @deviantCases = qw(
		       	);

    if ($relativeIsValid) {
	push @straightCases, @relativePathsCases;
    }
    else {
	push @deviantCases, @relativePathsCases;
    }

    foreach my $case (@straightCases) {
	my $name = "checking validation for straight case: $case";
	ok $checkPathSub_r->($case), $name;
    }

    foreach my $case (@deviantCases) {
	my $name = "checking validation error for deviant case: $case";
	ok !$checkPathSub_r->($case), $name;
	dies_ok { $checkPathSub_r->($case, $name) } "$name (with name parameter)";
    }

}


sub checkHostTest
{
  my @straightCases  = (
                          'macaco.monos.org',  # valid hostname
                          'isolatedMonkey',    # valid stand-alone hostname
                          '192.168.45.21',     # valid ip address
		       );
  my @deviantCases  = (
		       '198.23.423.12',  # invalid ip address
		       'badhost_.a.com', # invalid hostname

		      );

    foreach my $case (@straightCases) {
	my $name = "checking validation for straight case: $case";
	ok EBox::Validate::checkHost($case), $name;
    }

    foreach my $case (@deviantCases) {
	my $name = "checking validation error for deviant case: $case";
	ok ! EBox::Validate::checkHost($case), $name;
	dies_ok {  EBox::Validate::checkHost($case, $name) } "$name (with name parameter)";
    }
}


sub checkEmailAddressTest
{
  my @straightCases = qw(
       macaco@monos.org
       homo.sapiens@primates.com
       mandrill+colorful@monos.org
   );


  my @deviantCases = qw(
      macaco
   );

    foreach my $case (@straightCases) {
	my $name = "checking validation for straight case: $case";
	ok EBox::Validate::checkEmailAddress($case), $name;
    }

    foreach my $case (@deviantCases) {
	my $name = "checking validation error for deviant case: $case";
	ok ! EBox::Validate::checkEmailAddress($case), $name;
	dies_ok {  EBox::Validate::checkEmailAddress($case, $name) } "$name (with name parameter)";
    }
}


sub checkIP6Test
{
  my @valid = (
	       '2001:0db8:0000:0000:0000:0000:1428:57ab',
	       '2001:0db8:0000:0000:0000::1428:57ab',
	       '2001:0db8:0:0:0:0:1428:57ab',
	       '2001:0db8:0:0::1428:57ab',
	       '2001:0db8::1428:57ab',
	       '2001:db8::1428:57ab',
	      );

  my @invalid = (
		 'macaco',
		 '192.168.45.3',
		);


  foreach my $ip (@valid) {
    ok EBox::Validate::checkIP6($ip), 'checking wether checkIP6 recognizes valid addresses';
  }

  
  foreach my $ip (@invalid) {
    my $errorReturnValue = not EBox::Validate::checkIP6($ip);
    ok $errorReturnValue, 'checking wether checkIP6 signals invalid values wit its return value';;
    dies_ok {
      EBox::Validate::checkIP6($ip, 'error');
    } 'checking wether checkIP6 signals a invalid value raising exception';
  }


}

1;