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