1<?php
2
3namespace dokuwiki\plugin\passpolicy\test;
4
5use dokuwiki\Utf8\Clean;
6
7/**
8 * @group plugin_passpolicy
9 * @group plugins
10 */
11class HelperTest extends \DokuWikiTest
12{
13
14    protected $pluginsEnabled = array('passpolicy');
15
16    /**
17     * Quickly create a custom policy
18     *
19     * @param int $minl
20     * @param int $minp
21     * @param boolean $lower
22     * @param boolean $upper
23     * @param boolean $num
24     * @param boolean $special
25     * @param boolean $ucheck
26     * @param boolean $pron
27     * @param bool $nocom
28     * @param bool $noleak
29     * @return \helper_plugin_passpolicy
30     */
31    public function newPolicy(
32        $minl,
33        $minp,
34        $lower,
35        $upper,
36        $num,
37        $special,
38        $ucheck,
39        $pron = true,
40        $nocom = true,
41        $noleak = false
42    ) {
43        /** @var \helper_plugin_passpolicy $policy */
44        $policy = plugin_load('helper', 'passpolicy');
45        $policy->min_pools = $minp;
46        $policy->min_length = $minl;
47        $policy->usepools = array(
48            'lower' => $lower,
49            'upper' => $upper,
50            'numeric' => $num,
51            'special' => $special,
52        );
53        $policy->usernamecheck = $ucheck;
54        $policy->pronouncable = $pron;
55        $policy->nocommon = $nocom;
56        $policy->noleaked = $noleak;
57
58        return $policy;
59    }
60
61    public function test_policies()
62    {
63        $policy = $this->newPolicy(6, 1, true, true, true, true, 0);
64        $this->assertTrue($policy->checkPolicy('tested', 'tested'), '1 pool, no user check ' . $policy->error);
65        $this->assertFalse($policy->checkPolicy('test', 'tested'),
66            '1 pool, no user check, but too short ' . $policy->error);
67        $this->assertEquals(\helper_plugin_passpolicy::LENGTH_VIOLATION, $policy->error);
68        $this->assertTrue($policy->checkPolicy('tested99!', 'tested'), '1 pool, no user check ' . $policy->error);
69
70        $policy = $this->newPolicy(6, 3, true, true, true, true, 0);
71        $this->assertFalse($policy->checkPolicy('tested', 'tested'), '3 pools, no user check ' . $policy->error);
72        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
73        $this->assertTrue($policy->checkPolicy('tested99!', 'tested'), '3 pools, no user check ' . $policy->error);
74
75        $policy = $this->newPolicy(6, 1, true, true, true, true, 2);
76        $this->assertFalse($policy->checkPolicy('tested', 'tested'), '1 pool, user check ' . $policy->error);
77        $this->assertEquals(\helper_plugin_passpolicy::USERNAME_VIOLATION, $policy->error);
78        $this->assertFalse($policy->checkPolicy('tested99!', 'tested'), '1 pool, user check ' . $policy->error);
79        $this->assertEquals(\helper_plugin_passpolicy::USERNAME_VIOLATION, $policy->error);
80        $this->assertFalse($policy->checkPolicy('tested', 'untested'), '1 pool, user check ' . $policy->error);
81        $this->assertEquals(\helper_plugin_passpolicy::USERNAME_VIOLATION, $policy->error);
82        $this->assertFalse($policy->checkPolicy('tested99!', 'comptessa'), '1 pool1, user check ' . $policy->error);
83        $this->assertEquals(\helper_plugin_passpolicy::USERNAME_VIOLATION, $policy->error);
84    }
85
86    public function test_nocommon()
87    {
88        $policy = $this->newPolicy(6, 1, true, true, true, true, 0, true, true);
89        $this->assertTrue($policy->checkPolicy('bazzel', 'nope'));
90        $this->assertFalse($policy->checkPolicy('eyphed', 'nope'));
91        $this->assertEquals(\helper_plugin_passpolicy::COMMON_VIOLATION, $policy->error);
92
93        $policy->nocommon = false;
94        $this->assertTrue($policy->checkPolicy('password', 'nope'));
95    }
96
97    public function test_noleak()
98    {
99        $policy = $this->newPolicy(6, 1, true, true, true, true, 0, false, true, true);
100        $this->assertTrue($policy->checkPolicy('sadf asfd s as asf a afafaa fadfsa fas', 'nope'));
101        $this->assertFalse($policy->checkPolicy('qwertzuiop', 'nope'));
102        $this->assertEquals(\helper_plugin_passpolicy::LEAK_VIOLATION, $policy->error);
103
104        $policy->noleaked = false;
105        $this->assertTrue($policy->checkPolicy('qwertzuiop', 'nope'));
106    }
107
108    public function test_minpools()
109    {
110        $policy = $this->newPolicy(3, 0, true, true, true, true, 0);
111        $this->assertTrue($policy->checkPolicy('lowerUPPER123!"', 'tester'), '0 required, 4 given ' . $policy->error);
112        $this->assertTrue($policy->checkPolicy('lowerUPPER123', 'tester'), '0 required, 3 given ' . $policy->error);
113        $this->assertTrue($policy->checkPolicy('lowerUPPER', 'tester'), '0 required, 2 given ' . $policy->error);
114        $this->assertTrue($policy->checkPolicy('lower', 'tester'), '0 required, 1 given ' . $policy->error);
115
116        $policy = $this->newPolicy(3, 1, true, true, true, true, 0);
117        $this->assertTrue($policy->checkPolicy('lowerUPPER123!"', 'tester'), '1 required, 4 given ' . $policy->error);
118        $this->assertTrue($policy->checkPolicy('lowerUPPER123', 'tester'), '1 required, 3 given ' . $policy->error);
119        $this->assertTrue($policy->checkPolicy('lowerUPPER', 'tester'), '1 required, 2 given ' . $policy->error);
120        $this->assertTrue($policy->checkPolicy('lower', 'tester'), '1 required, 1 given ' . $policy->error);
121
122        $policy = $this->newPolicy(3, 2, true, true, true, true, 0);
123        $this->assertTrue($policy->checkPolicy('lowerUPPER123!"', 'tester'), '2 required, 4 given ' . $policy->error);
124        $this->assertTrue($policy->checkPolicy('lowerUPPER123', 'tester'), '2 required, 3 given ' . $policy->error);
125        $this->assertTrue($policy->checkPolicy('lowerUPPER', 'tester'), '2 required, 2 given ' . $policy->error);
126        $this->assertFalse($policy->checkPolicy('lower', 'tester'), '2 required, 1 given ' . $policy->error);
127        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
128
129        $policy = $this->newPolicy(3, 3, true, true, true, true, 0);
130        $this->assertTrue($policy->checkPolicy('lowerUPPER123!"', 'tester'), '3 required, 4 given ' . $policy->error);
131        $this->assertTrue($policy->checkPolicy('lowerUPPER123', 'tester'), '3 required, 3 given ' . $policy->error);
132        $this->assertFalse($policy->checkPolicy('lowerUPPER', 'tester'), '3 required, 2 given ' . $policy->error);
133        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
134        $this->assertFalse($policy->checkPolicy('lower', 'tester'), '3 required, 1 given ' . $policy->error);
135        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
136
137        $policy = $this->newPolicy(3, 4, true, true, true, true, 0);
138        $this->assertTrue($policy->checkPolicy('lowerUPPER123!"', 'tester'), '4 required, 4 given ' . $policy->error);
139        $this->assertFalse($policy->checkPolicy('lowerUPPER123', 'tester'), '4 required, 3 given ' . $policy->error);
140        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
141        $this->assertFalse($policy->checkPolicy('lowerUPPER', 'tester'), '4 required, 2 given ' . $policy->error);
142        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
143        $this->assertFalse($policy->checkPolicy('lower', 'tester'), '4 required, 1 given ' . $policy->error);
144        $this->assertEquals(\helper_plugin_passpolicy::POOL_VIOLATION, $policy->error);
145    }
146
147    public function test_selfcheck()
148    {
149        $policy = $this->newPolicy(6, 4, true, true, true, true, 0, true);
150        $pw1 = $policy->generatePassword('test');
151        $pw2 = $policy->generatePassword('test');
152        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
153        $this->assertTrue(strlen($pw1) >= 6, 'pw too short');
154        $this->assertTrue(strlen($pw2) >= 6, 'pw too short');
155        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
156        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
157
158        //echo "\n$pw1\n$pw2\n";
159
160        $policy = $this->newPolicy(18, 4, true, true, true, true, 0, true);
161        $pw1 = $policy->generatePassword('test');
162        $pw2 = $policy->generatePassword('test');
163        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
164        $this->assertTrue(strlen($pw1) >= 18, 'pw too short');
165        $this->assertTrue(strlen($pw2) >= 18, 'pw too short');
166        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
167        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
168
169        //echo "\n$pw1\n$pw2\n";
170
171        $policy = $this->newPolicy(6, 4, true, true, true, true, 0, false);
172        $pw1 = $policy->generatePassword('test');
173        $pw2 = $policy->generatePassword('test');
174        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
175        $this->assertTrue(strlen($pw1) >= 6, 'pw too short');
176        $this->assertTrue(strlen($pw2) >= 6, 'pw too short');
177        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
178        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
179
180        //echo "\n$pw1\n$pw2\n";
181
182        $policy = $this->newPolicy(18, 4, true, true, true, true, 0, false);
183        $pw1 = $policy->generatePassword('test');
184        $pw2 = $policy->generatePassword('test');
185        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
186        $this->assertTrue(strlen($pw1) >= 18, 'pw too short');
187        $this->assertTrue(strlen($pw2) >= 18, 'pw too short');
188        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
189        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
190
191        //echo "\n$pw1\n$pw2\n";
192
193        $policy = $this->newPolicy(18, 1, false, false, false, true, 0, false);
194        $pw1 = $policy->generatePassword('test');
195        $pw2 = $policy->generatePassword('test');
196        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
197        $this->assertTrue(strlen($pw1) >= 18, 'pw too short');
198        $this->assertTrue(strlen($pw2) >= 18, 'pw too short');
199        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
200        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
201
202        //echo "\n$pw1\n$pw2\n";
203
204        $policy = $this->newPolicy(18, 1, false, false, true, false, 0, false);
205        $pw1 = $policy->generatePassword('test');
206        $pw2 = $policy->generatePassword('test');
207        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
208        $this->assertTrue(strlen($pw1) >= 18, 'pw too short');
209        $this->assertTrue(strlen($pw2) >= 18, 'pw too short');
210        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
211        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
212
213        //echo "\n$pw1\n$pw2\n";
214
215        $policy = $this->newPolicy(18, 1, false, true, false, false, 0, false);
216        $pw1 = $policy->generatePassword('test');
217        $pw2 = $policy->generatePassword('test');
218        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
219        $this->assertTrue(strlen($pw1) >= 18, 'pw too short');
220        $this->assertTrue(strlen($pw2) >= 18, 'pw too short');
221        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
222        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
223
224        //echo "\n$pw1\n$pw2\n";
225
226        $policy = $this->newPolicy(18, 1, true, false, false, false, 0, false);
227        $pw1 = $policy->generatePassword('test');
228        $pw2 = $policy->generatePassword('test');
229        $this->assertNotEquals($pw1, $pw2, 'randomness broken');
230        $this->assertTrue(strlen($pw1) >= 18, 'pw too short');
231        $this->assertTrue(strlen($pw2) >= 18, 'pw too short');
232        $this->assertTrue(Clean::isASCII($pw1), 'pw contains non-ASCII, something went wrong');
233        $this->assertTrue(Clean::isASCII($pw2), 'pw contains non-ASCII, something went wrong');
234
235        //echo "\n$pw1\n$pw2\n";
236    }
237}
238
239