summaryrefslogtreecommitdiffstats
path: root/Tests/Net/OpenID/StoreTest.php
blob: ed4d88263182fa01967009763ce9af98ada61181 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php

/**
 * A test script for the OpenIDStore classes.
 *
 * 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('Net/OpenID/Association.php');
require_once('Net/OpenID/CryptUtil.php');

function Net_OpenID_rmtree($dir)
{
    if ($dir[strlen($dir) - 1] != DIRECTORY_SEPARATOR) {
        $dir .= DIRECTORY_SEPARATOR;
    }

    if ($handle = opendir($dir)) {
        while ($item = readdir($handle)) {
            if (!in_array($item, array('.', '..'))) {
                if (is_dir($dir . $item)) {
                    if (!Net_OpenID_rmtree($dir . $item)) {
                        return false;
                    }
                } else if (is_file($dir . $item)) {
                    if (!unlink($dir . $item)) {
                        return false;
                    }
                }
            }
        }

        closedir($handle);

        if (!@rmdir($dir)) {
            return false;
        }

        return true;
    } else {
        // Couldn't open directory.
        return false;
    }
}

class Tests_Net_OpenID_StoreTest extends PHPUnit_TestCase {

    function setUp()
    {
        $this->letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $this->digits = "0123456789";
        $this->punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
        $this->allowed_nonce = $this->letters . $this->digits;
        $this->allowed_handle = $this->letters . $this->digits . $this->punct;
    }

    function generateNonce()
    {
        return Net_OpenID_CryptUtil::randomString(8, $this->allowed_nonce);
    }

    function genAssoc($now, $issued = 0, $lifetime = 600)
    {
        $sec = call_user_func(array('Net_OpenID_CryptUtil', 'randomString'),
                              20);
        $hdl = Net_OpenID_CryptUtil::randomString(128, $this->allowed_handle);
        return new Net_OpenID_Association($hdl, $sec, $now + $issued, $lifetime,
                                          'HMAC-SHA1');
     }

    function _checkRetrieve(&$store, $url, $handle, $expected, $name=null)
    {
        $retrieved_assoc = $store->getAssociation($url, $handle);
        if (($expected === null) || ($store->isDumb())) {
            $this->assertNull($retrieved_assoc);
        } else {
            if ($retrieved_assoc === null) {
                $this->fail("$name: Got null when expecting " .
                            $expected->serialize());
            } else {
                $this->assertEquals($retrieved_assoc->serialize(),
                                    $expected->serialize(), $name);
            }
        }
    }

    function _checkRemove(&$store, $url, $handle, $expected)
    {
        $present = $store->removeAssociation($url, $handle);
        $expectedPresent = (!$store->isDumb() && $expected);
        $this->assertTrue((!$expectedPresent && !$present) ||
                          ($expectedPresent && $present));
    }

    /**
     * Make sure a given store has a minimum of API compliance. Call
     * this function with an empty store.
     *
     * Raises AssertionError if the store does not work as expected.
     *
     * OpenIDStore -> NoneType
     */
    function _testStore($store)
    {

        // Association functions
        $now = time();

        $server_url = 'http://www.myopenid.com/openid';

        $assoc = $this->genAssoc($now);

        $this->_checkRetrieve($store, $server_url, null, null,
            'Make sure that a missing association returns no result');

        $store->storeAssociation($server_url, $assoc);
        $this->_checkRetrieve($store, $server_url, null, $assoc,
            'Check that after storage, getting returns the same result');

        $this->_checkRetrieve($store, $server_url, null, $assoc,
            'more than once');

        $store->storeAssociation($server_url, $assoc);
        $this->_checkRetrieve($store, $server_url, null, $assoc,
            'Storing more than once has no ill effect');

        // Removing an association that does not exist returns not present
        $this->_checkRemove($store, $server_url, $assoc->handle . 'x', false);

        // Removing an association that does not exist returns not present
        $this->_checkRemove($store, $server_url . 'x', $assoc->handle, false);

        // Removing an association that is present returns present
        $this->_checkRemove($store, $server_url, $assoc->handle, true);

        // but not present on subsequent calls
        $this->_checkRemove($store, $server_url, $assoc->handle, false);

        // Put assoc back in the store
        $store->storeAssociation($server_url, $assoc);

        // More recent and expires after assoc
        $assoc2 = $this->genAssoc($now, $issued = 1);
        $store->storeAssociation($server_url, $assoc2);

        $this->_checkRetrieve($store, $server_url, null, $assoc2,
            'After storing an association with a different handle, but the
same $server_url, the handle with the later expiration is
returned.');

        $this->_checkRetrieve($store, $server_url, $assoc->handle, $assoc,
            'We can still retrieve the older association');

        $this->_checkRetrieve($store, $server_url, $assoc2->handle, $assoc2,
            'Plus we can retrieve the association with the later expiration
explicitly');

        // More recent, but expires earlier than assoc2 or assoc
        $assoc3 = $this->genAssoc($now, $issued = 2, $lifetime = 100);
        $store->storeAssociation($server_url, $assoc3);

        $this->_checkRetrieve($store, $server_url, null, $assoc3);
        $this->_checkRetrieve($store, $server_url, $assoc->handle, $assoc);
        $this->_checkRetrieve($store, $server_url, $assoc2->handle, $assoc2);
        $this->_checkRetrieve($store, $server_url, $assoc3->handle, $assoc3);

        $this->_checkRemove($store, $server_url, $assoc2->handle, true);

        $this->_checkRetrieve($store, $server_url, null, $assoc3);
        $this->_checkRetrieve($store, $server_url, $assoc->handle, $assoc);
        $this->_checkRetrieve($store, $server_url, $assoc2->handle, null);
        $this->_checkRetrieve($store, $server_url, $assoc3->handle, $assoc3);

        $this->_checkRemove($store, $server_url, $assoc2->handle, false);
        $this->_checkRemove($store, $server_url, $assoc3->handle, true);

        $this->_checkRetrieve($store, $server_url, null, $assoc);
        $this->_checkRetrieve($store, $server_url, $assoc->handle, $assoc);
        $this->_checkRetrieve($store, $server_url, $assoc2->handle, null);
        $this->_checkRetrieve($store, $server_url, $assoc3->handle, null);

        $this->_checkRemove($store, $server_url, $assoc2->handle, false);
        $this->_checkRemove($store, $server_url, $assoc->handle, true);
        $this->_checkRemove($store, $server_url, $assoc3->handle, false);
        $this->_checkRetrieve($store, $server_url, null, null);
        $this->_checkRetrieve($store, $server_url, $assoc->handle, null);
        $this->_checkRetrieve($store, $server_url, $assoc2->handle, null);
        $this->_checkRetrieve($store, $server_url,$assoc3->handle, null);

        $this->_checkRemove($store, $server_url, $assoc2->handle, false);
        $this->_checkRemove($store, $server_url, $assoc->handle, false);
        $this->_checkRemove($store, $server_url, $assoc3->handle, false);
    }

    function _checkUseNonce(&$store, $nonce, $expected)
    {
        $actual = $store->useNonce($nonce);
        $expected = $store->isDumb() || $expected;
        $this->assertTrue(($actual && $expected) || (!$actual && !$expected));
    }

    function _testNonce(&$store)
    {
        // Nonce functions

        // Random nonce (not in store)
        $nonce1 = $this->generateNonce();

        // A nonce is not present by default
        $this->_checkUseNonce($store, $nonce1, false);

        // Storing once causes useNonce to return true the first, and only
        // the first, time it is called after the $store->
        $store->storeNonce($nonce1);
        $this->_checkUseNonce($store, $nonce1, true);
        $this->_checkUseNonce($store, $nonce1, false);

        // Storing twice has the same effect as storing once.
        $store->storeNonce($nonce1);
        $store->storeNonce($nonce1);
        $this->_checkUseNonce($store, $nonce1, true);
        $this->_checkUseNonce($store, $nonce1, false);

        // Auth key functions

        // There is no key to start with, so generate a new key and return
        // it.
        $key = $store->getAuthKey();

        // The second time around should return the same as last time.
        $key2 = $store->getAuthKey();
        $this->assertEquals($key, $key2);
        $this->assertEquals(strlen($key), $store->AUTH_KEY_LEN);
    }

    function test_filestore()
    {
        require_once('Net/OpenID/Store/FileStore.php');

        $temp_dir = Net_OpenID_mkdtemp('/tmp');

        if (!$temp_dir) {
            trigger_error('Could not create temporary directory ' .
                          'with Net_OpenID_mkdtemp', E_USER_WARNING);
            return null;
        }

        $store = new Net_OpenID_FileStore($temp_dir);
        $this->_testStore($store);
        $this->_testNonce($store);
        Net_OpenID_rmtree($temp_dir);
    }
}

?>