summaryrefslogtreecommitdiffstats
path: root/Tests/Net/OpenID/CryptUtil.php
blob: d042c371e43dda79072fea78f0f5193f31e33a2c (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php

/**
 * Tests for the CryptUtil functions.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: See the COPYING file included in this distribution.
 *
 * @package OpenID
 * @author JanRain, Inc. <openid@janrain.com>
 * @copyright 2005 Janrain, Inc.
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 */

require_once('PHPUnit.php');
require_once('Net/OpenID/CryptUtil.php');

class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestCase {
    function test_length()
    {
        $cases = array(1, 10, 255);
        foreach ($cases as $length) {
            $data = Net_OpenID_CryptUtil::getBytes($length);
            $this->assertEquals(strlen($data), $length);
        }
    }

    function test_different()
    {
        $num_iterations = 100;
        $data_length = 20;

        $data = Net_OpenID_CryptUtil::getBytes($num_iterations);
        for ($i = 0; $i < $num_iterations; $i++) {
            $last = $data;
            $data = Net_OpenID_CryptUtil::getBytes($num_iterations);
            $this->assertFalse($data == $last);
        }
    }

    function test_cryptrand()
    {

        $lib =& Net_OpenID_MathLibrary::getLibWrapper();

        // It's possible, but HIGHLY unlikely that a correct
        // implementation will fail by returning the same number twice

        $s = Net_OpenID_CryptUtil::getBytes(32);
        $t = Net_OpenID_CryptUtil::getBytes(32);
        $this->assertEquals(strlen($s), 32);
        $this->assertEquals(strlen($t), 32);
        $this->assertFalse($s == $t);

        $a = Net_OpenID_CryptUtil::randrange($lib->pow(2, 128));
        $b = Net_OpenID_CryptUtil::randrange($lib->pow(2, 128));

        // If $a is a float, it's because we're using fallback number
        // storage (PHP stores overflowed ints as floats).
        $this->assertFalse($lib->cmp($b, $a) == 0, "Same: $a $b");

        $n = $lib->init(Net_OpenID_CryptUtil::maxint());
        $one = $lib->init(1);
        $n = $lib->add($n, 1);

        // Make sure that we can generate random numbers that are
        // larger than platform int size
        $result = Net_OpenID_CryptUtil::randrange($n);

        // What can we say about the result?
    }

    function test_strxor()
    {
        $NUL = "\x00";

        $cases = array(
                       array($NUL, $NUL, $NUL),
                       array("\x01", $NUL, "\x01"),
                       array("a", "a", $NUL),
                       array("a", $NUL, "a"),
                       array("abc", str_repeat($NUL, 3), "abc"),
                       array(str_repeat("x", 10),
                             str_repeat($NUL, 10),
                             str_repeat("x", 10)),
                       array("\x01", "\x02", "\x03"),
                       array("\xf0", "\x0f", "\xff"),
                       array("\xff", "\x0f", "\xf0"),
                       );

        while (list($index, $values) = each($cases)) {
            list($aa, $bb, $expected) = $values;
            $actual = Net_OpenID_CryptUtil::strxor($aa, $bb);
            $this->assertEquals($actual, $expected);
        }

        $exc_cases = array(
                           array('', 'a'),
                           array('foo', 'ba'),
                           array(str_repeat($NUL, 3),
                                 str_repeat($NUL, 4)),
                           array(implode('', array_map('chr',
                                                       range(0, 255))),
                                 implode('', array_map('chr',
                                                       range(0, 127))))
                           );

        while(list($index, $values) = each($exc_cases)) {
            list($aa, $bb) = $values;
            $unexpected = Net_OpenID_CryptUtil::strxor($aa, $bb);
            $this->assertNull($unexpected);
        }
    }

    function test_reversed()
    {
        $cases = array(
                       array('', ''),
                       array('a', 'a'),
                       array('ab', 'ba'),
                       array('abc', 'cba'),
                       array('abcdefg', 'gfedcba'),
                       array(array(), array()),
                       array(array(1), array(1)),
                       array(array(1,2), array(2,1)),
                       array(array(1,2,3), array(3,2,1)),
                       array(range(0, 999), array_reverse(range(0, 999)))
                       );

        while (list($index, $values) = each($cases)) {
            list($case, $expected) = $values;
            $actual = Net_OpenID_CryptUtil::reversed($case);
            $this->assertEquals($actual, $expected);
            $twice = Net_OpenID_CryptUtil::reversed($actual);
            $this->assertEquals($twice, $case);
        }
    }

    function test_binaryLongConvert()
    {
        
        $lib =& Net_OpenID_MathLibrary::getLibWrapper();

        $MAX = Net_OpenID_CryptUtil::maxint();
        
        foreach (range(0, 499) as $iteration) {
            $n = $lib->init(0);
            foreach (range(0, 9) as $i) {
                $rnd = Net_OpenID_CryptUtil::randrange($MAX);
                $n = $lib->add($n, $rnd);
            }

            $s = Net_OpenID_CryptUtil::longToBinary($n);
            $this->assertTrue(is_string($s));
            $n_prime = Net_OpenID_CryptUtil::binaryToLong($s);
            $this->assertEquals($lib->cmp($n, $n_prime), 0);
        }

        $cases = array(
                       array("\x00", 0),
                       array("\x01", 1),
                       array("\x00\xFF", 255),
                       array("\x00\x80", 128),
                       array("\x00\x81", 129),
                       array("\x00\x80\x00", 32768),
                       array("OpenID is cool",
                             "1611215304203901150134421257416556")
                       );

        foreach ($cases as $case) {
            list($s, $n) = $case;
            $n_prime = Net_OpenID_CryptUtil::binaryToLong($s);
            $s_prime = Net_OpenID_CryptUtil::longToBinary($n);
            $this->assertEquals($lib->cmp($n, $n_prime), 0);
            $this->assertTrue($s == $s_prime);
        }
    }

    function _parseBase64Data()
    {
        $lib =& Net_OpenID_MathLibrary::getLibWrapper();

        $lines = file_get_contents('Tests/n2b64', true);
        $this->assertTrue(is_string($lines));

        $lines = explode("\n", $lines);

        $data = array();
        foreach ($lines as $line) {
            if (!$line) {
                continue;
            }
            list($b64, $ascii) = explode(' ', $line);
            $num = @$lib->init($ascii);
            if ($num === false) {
                $this->assertEquals($lib->type, 'dumb');
            } else {
                $data[$b64] = $num;
            }
        }
        return $data;
    }

    function test_longToBase64()
    {
        $data = $this->_parseBase64Data();
        foreach ($data as $expected => $num) {
            $actual = Net_OpenID_CryptUtil::longToBase64($num);
            $this->assertEquals($expected, $actual);
        }
    }

    function test_base64ToLong()
    {
        $lib =& Net_OpenID_MathLibrary::getLibWrapper();
        $data = $this->_parseBase64Data();
        foreach ($data as $b64 => $expected) {
            $actual = Net_OpenID_CryptUtil::base64ToLong($b64);
            $this->assertTrue($lib->cmp($expected, $actual) == 0);
        }
    }
}

?>