Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +------------------------------------------------------------------------+
3
// | PEAR :: Text_Password                                                  |
4
// +------------------------------------------------------------------------+
5
// | Copyright (c) 2004 Martin Jansen                                       |
6
// +------------------------------------------------------------------------+
7
// | This source file is subject to version 3.00 of the PHP License,        |
8
// | that is available at http://www.php.net/license/3_0.txt.               |
9
// | If you did not receive a copy of the PHP license and are unable to     |
10
// | obtain it through the world-wide-web, please send a note to            |
11
// | license@php.net so we can mail you a copy immediately.                 |
12
// +------------------------------------------------------------------------+
13
//
14
// $Id: Text_Password_Test.php,v 1.2 2004/10/31 22:40:50 mj Exp $
15
//
16
 
17
require_once "PHPUnit.php";
18
require_once "PHPUnit/TestCase.php";
19
require_once "PHPUnit/TestSuite.php";
20
require_once "Text/Password.php";
21
 
22
/**
23
 * Unit test suite for the Text_Password package
24
 *
25
 * @author  Martin Jansen <mj@php.net>
26
 * @extends PHPUnit_TestCase
27
 * @version $Id: Text_Password_Test.php,v 1.2 2004/10/31 22:40:50 mj Exp $
28
 */
29
class Text_Password_Test extends PHPUnit_TestCase {
30
 
31
    function testCreate()
32
    {
33
        $password = Text_Password::create();
34
        $this->assertTrue(strlen($password) == 10);
35
    }
36
 
37
    function testCreateWithLength()
38
    {
39
        $password = Text_Password::create(15);
40
        $this->assertTrue(strlen($password) == 15);
41
    }
42
 
43
    function testCreateMultiple()
44
    {
45
        $passwords = Text_Password::createMultiple(3);
46
        $this->_testCreateMultiple($passwords, 3, 10);
47
    }
48
 
49
    function testCreateMultipleWithLength()
50
    {
51
        $passwords = Text_Password::createMultiple(3, 15);
52
        $this->_testCreateMultiple($passwords, 3, 15);
53
    }
54
 
55
    function testCreateNumericWithLength()
56
    {
57
        $password = Text_Password::create(8, 'unpronounceable', 'numeric');
58
 
59
        $this->assertRegExp("/^[0-9]{8}$/", $password);
60
    }
61
 
62
    function testCreateFromABCWithLength()
63
    {
64
        $password = Text_Password::create(8, 'unpronounceable', 'a,b,c');
65
        $this->assertRegExp("/^[abc]{8}$/i", $password);
66
    }
67
 
68
    function testCreateAlphabeticWithLength()
69
    {
70
        $password = Text_Password::create(8, 'unpronounceable', 'alphabetic');
71
 
72
        $this->assertRegExp("/^[a-z]{8}$/i", $password);
73
    }
74
 
75
    function testTimeToBruteForce()
76
    {
77
        $password = Text_Password::create(5, 'unpronounceable');
78
        $result = $this->_TimeToBruteForce($pass);
79
 
80
        $this->assertTrue($result['combination'] == 1);
81
        $this->assertTrue($result['max'] == 0.00025);
82
        $this->assertTrue($result['min'] == 0.000125);
83
    }
84
 
85
    // {{{ Test cases for creating passwords based on a given login string
86
 
87
    function testCreateFromLoginReverse()
88
    {
89
        $this->assertEquals("eoj", Text_Password::createFromLogin("joe", "reverse"));
90
    }
91
 
92
    function testCreateFromLoginShuffle()
93
    {
94
        $this->assertTrue(strlen(Text_Password::createFromLogin("hello world", "shuffle")) == strlen("hello world"));
95
    }
96
 
97
    function testCreateFromLoginRotX()
98
    {
99
        $this->assertEquals("tyo", Text_Password::createFromLogin("joe", "rotx", 10));
100
    }
101
 
102
    function testCreateFromLoginRot13()
103
    {
104
        $this->assertEquals("wbr", Text_Password::createFromLogin("joe", "rot13"));
105
    }
106
 
107
    function testCreateFromLoginRotXplusplus()
108
    {
109
        $this->assertEquals("syp", Text_Password::createFromLogin("joe", "rotx++", 9));
110
    }
111
 
112
    function testCreateFromLoginRotXminusminus()
113
    {
114
        $this->assertEquals("swl", Text_Password::createFromLogin("joe", "rotx--", 9));
115
    }
116
 
117
    function testCreateFromLoginXOR()
118
    {
119
        $this->assertEquals("oj`", Text_Password::createFromLogin("joe", "xor", 5));
120
    }
121
 
122
    function testCreateFromLoginASCIIRotX()
123
    {
124
        $this->assertEquals("otj", Text_Password::createFromLogin("joe", "ascii_rotx", 5));
125
    }
126
 
127
    function testCreateFromLoginASCIIRotXplusplus()
128
    {
129
        $this->assertEquals("oul", Text_Password::createFromLogin("joe", "ascii_rotx++", 5));
130
    }
131
 
132
    function testCreateFromLoginASCIIRotXminusminus()
133
    {
134
        $this->assertEquals("uyn", Text_Password::createFromLogin("joe", "ascii_rotx--", 11));
135
    }
136
 
137
    /**
138
     * Unit test for bug #2605
139
     *
140
     * Actually this method does not implement a real unit test, but
141
     * instead it is there to make sure that no warning is produced
142
     * by PHP.
143
     *
144
     * @link http://pear.php.net/bugs/bug.php?id=2605
145
     */
146
    function testBugReport2605()
147
    {
148
        $password = Text_Password::create(7, 'unpronounceable', '1,3,a,Q,~,[,f');
149
        $this->assertTrue(strlen($password) == 7);
150
    }
151
 
152
    // }}}
153
    // {{{ private helper methods
154
 
155
    function _testCreateMultiple($passwords, $count, $length)
156
    {
157
        $this->assertType("array", $passwords);
158
        $this->assertTrue(count($passwords) == $count);
159
 
160
        foreach ($passwords as $password) {
161
            $this->assertTrue(strlen($password) == $length);
162
        }
163
    }
164
 
165
    function _timeToBruteForce($password, $nbr = 0, $cmbPerSeconde = 4000)
166
    {
167
        global $_Text_Password_NumberOfPossibleCharacters;
168
 
169
        $nbr = ($nbr == 0) ? $_Text_Password_NumberOfPossibleCharacters : $nbr;
170
        $cmb = pow($nbr, strlen($password));
171
        $time_max = $cmb / $cmbPerSeconde;
172
        $time_min = ($cmb / $cmbPerSeconde) / 2;
173
 
174
        return array("combination" => $cmb,
175
                     "max"         => $time_max,
176
                     "min"         => $time_min);
177
    }
178
 
179
    // }}}
180
}
181
 
182
$suite = new PHPUnit_TestSuite('Text_Password_Test');
183
$result = PHPUnit::run($suite);
184
echo $result->toString();