File: 03count.t

package info (click to toggle)
libstring-mkpasswd-perl 0.05-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 140 kB
  • ctags: 22
  • sloc: perl: 272; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download | duplicates (6)
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
use Test::More tests => 41;
BEGIN { use_ok('String::MkPasswd') };

String::MkPasswd->import("mkpasswd");

my $passwd = mkpasswd();

ok(length $passwd == 9, "length = 9");
ok(&count_digits($passwd) == 2, "digits = 2");
ok(&count_lower($passwd) == 4, "lower = 4");
ok(&count_upper($passwd) == 2, "upper = 2");
ok(&count_special($passwd) == 1, "special = 1");

$passwd = mkpasswd(
	-length		=> 20,
	-minnum		=> 4,
	-minlower	=> 7,
	-minupper	=> 6,
	-minspecial	=> 3,
);

ok(length $passwd == 20, "length = 20");
ok(&count_digits($passwd) == 4, "digits = 4");
ok(&count_lower($passwd) == 7, "lower = 7");
ok(&count_upper($passwd) == 6, "upper = 6");
ok(&count_special($passwd) == 3, "special = 3");

$passwd = mkpasswd(
	-minnum	=> 0,
);

ok(length $passwd == 9, "length = 9");
ok(&count_digits($passwd) == 0, "digits = 0");
ok(&count_lower($passwd) == 6, "lower = 6");
ok(&count_upper($passwd) == 2, "upper = 2");
ok(&count_special($passwd) == 1, "special = 1");

$passwd = mkpasswd(
	-minlower	=> 0,
);

ok(length $passwd == 9, "length = 9");
ok(&count_digits($passwd) == 2, "digits = 2");
ok(&count_lower($passwd) == 4, "lower = 4");
ok(&count_upper($passwd) == 2, "upper = 2");
ok(&count_special($passwd) == 1, "special = 1");

$passwd = mkpasswd(
	-minupper	=> 0,
);

ok(length $passwd == 9, "length = 9");
ok(&count_digits($passwd) == 2, "digits = 2");
ok(&count_lower($passwd) == 6, "lower = 6");
ok(&count_upper($passwd) == 0, "upper = 0");
ok(&count_special($passwd) == 1, "special = 1");

$passwd = mkpasswd(
	-minspecial	=> 0,
);

ok(length $passwd == 9, "length = 9");
ok(&count_digits($passwd) == 2, "digits = 2");
ok(&count_lower($passwd) == 5, "lower = 5");
ok(&count_upper($passwd) == 2, "upper = 2");
ok(&count_special($passwd) == 0, "special = 0");

$passwd = mkpasswd(
	-minnum		=> 0,
	-minlower	=> 0,
	-minupper	=> 0,
	-minspecial	=> 0,
);

ok(length $passwd == 9, "length = 9");
ok(&count_digits($passwd) == 0, "digits = 0");
ok(&count_lower($passwd) == 9, "lower = 9");
ok(&count_upper($passwd) == 0, "upper = 0");
ok(&count_special($passwd) == 0, "special = 0");

$passwd = mkpasswd(
	-length		=> 3,
	-minnum		=> 3,
	-minlower	=> 0,
	-minupper	=> 0,
	-minspecial	=> 0,
);

ok(length $passwd == 3, "length = 3");
ok(&count_digits($passwd) == 3, "digits = 3");
ok(&count_lower($passwd) == 0, "lower = 0");
ok(&count_upper($passwd) == 0, "upper = 0");
ok(&count_special($passwd) == 0, "special = 0");

sub count_digits {
	my $re = qr/([0-9])/;
	my $count = 0;
	$count++ while $_[0] =~ /$re/g;
	return $count;
}

sub count_lower {
	my $re = qr/([a-z])/;
	my $count = 0;
	$count++ while $_[0] =~ /$re/g;
	return $count;
}

sub count_special {
	my $re = qr/([!@#\$%~^&*()=_+\[\]{}\\|;:'"<>,.?\/-])/;
	my $count = 0;
	$count++ while $_[0] =~ /$re/g;
	return $count;
}

sub count_upper {
	my $re = qr/([A-Z])/;
	my $count = 0;
	$count++ while $_[0] =~ /$re/g;
	return $count;
}