File: test.pl

package info (click to toggle)
libapache-htpasswd-perl 1.8-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 64 kB
  • ctags: 21
  • sloc: perl: 432; makefile: 2
file content (130 lines) | stat: -rwxr-xr-x 3,591 bytes parent folder | download | duplicates (4)
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
# $Id: test.pl,v 1.1.1.1 2002/07/26 18:58:46 root Exp $
# $Log: test.pl,v $
# Revision 1.1.1.1  2002/07/26 18:58:46  root
# initial
#
# Revision 0.3  1998/10/22 02:49:53  meltzek
# Added verbose begining and ending of test.
#
# Revision 0.2  1998/10/22 02:46:56  meltzek
# Added new checks.
#

BEGIN { $| = 1; print "Tests 1..20 begining\n"; }
END {print "not ok 1\n" unless $loaded;}
use Apache::Htpasswd;

$loaded = 1;
print "ok 1\n";

######################### End of black magic.

sub report_result {
	my $ok = shift;
	$TEST_NUM ||= 2;
	print "not " unless $ok;
	print "ok $TEST_NUM\n";
	print "@_\n" if (not $ok and $ENV{TEST_VERBOSE});
	$TEST_NUM++;
}

sub report_skip {
        my $why = shift;
        print "not ok $TEST_NUM # SKIP $why\n";
	$TEST_NUM++;
}

# Create a test password file
my $File = "testpasswords.test";
open(TEST,">$File") || die "Can't run tests because I can't create $File [$!]";
print TEST "kevin:kjDqW.pgNIz3Ufoo:suvPq./X7Q8nk\n";
close TEST;



{
	
	# 2: Get file
	&report_result($pwdFile = new Apache::Htpasswd ($File), $! );

	# 3: store a value
	&report_result($pwdFile->htpasswd("foo","foobar") , $! );

	# 4: change value 
	&report_result(!$pwdFile->htpasswd("fooo", "goo","foobar" ) , $! );
	&report_result($pwdFile->htpasswd("foo", "goo","foobar" ) , $! );
	
	# 5: force change value
	&report_result($pwdFile->htpasswd("foo","ummm",{'overwrite' => 1}), $! );

	# 6: check the stored value
	&report_result($pwdFile->fetchPass("foo") , $!);

	# 7: check whether the empty key exists()
	&report_result($pwdFile->htCheckPassword("foo","ummm"),$!);

	# 8: add extra info
	&report_result($pwdFile->writeInfo("kevin", "Test info"),$!);
	
	# 9: fetch extra info
	&report_result($pwdFile->fetchInfo("kevin"),$!);
	
	# 10: Delete user
	&report_result($pwdFile->htDelete("kevin"),$!);
	
        # 11: get list
        my @list = $pwdFile->fetchUsers();
        &report_result($list[0] eq 'foo', $!);

	# 12: get number of users
        my $num  = $pwdFile->fetchUsers();
        &report_result($num == 1, $!);

	undef $pwdFile;

	# 13: Create in read-only mode
        &report_result($pwdFile = new Apache::Htpasswd({passwdFile => $File, ReadOnly => 1}), $! );

  # 14: store a value (should fail)
	# Should carp, but don't want to display it
	sub Apache::Htpasswd::carp {};
        &report_result(!$pwdFile->htpasswd("kevin","zog") , $! );

}

open(TEST,">>$File");
print TEST "cryptuser:Iao36C/TVmCRc\n";
print TEST "MD5user:\$apr1\$Yy.pS/..\$4bwpMUiVq/95BDr4kZ2lK.\n";
print TEST "SHA1user:{SHA}QL0AFWMIX8NRZTKeof9cXsvbvu8=\n";
print TEST "plainuser:123\n";
close TEST;

{
	# 16: Create in read-only mode with UsePlain
        &report_result($pwdFile = new Apache::Htpasswd({passwdFile => $File, ReadOnly => 1, UsePlain => 1}), $! );

	# 17: check whether crypt passwords work
	&report_result($pwdFile->htCheckPassword("cryptuser","123"),$!);

	# 18: check whether MD5 passwords work
        eval { require Crypt::PasswdMD5 };
        if ($@) {
            &report_skip('Crypt::PasswdMD5 required for this test');
        } else {
            &report_result($pwdFile->htCheckPassword("MD5user","123"),$!);
        }

	# 19: check whether SHA1 passwords work
        eval { require Digest::SHA; require MIME::Base64 };
        if ($@) {
            &report_skip('Digest::SHA and MIME::Base64 required for this test');
        } else {
            &report_result($pwdFile->htCheckPassword("SHA1user","123"),$!);
        }

	# 20: check whether plain passwords work
	&report_result($pwdFile->htCheckPassword("plainuser","123"),$!);

}

print "Test complete.\n";