aboutsummaryrefslogtreecommitdiff
path: root/models/openid-php-openid-782224d/Tests/Auth/Yadis/XRDS.php
blob: 3bb23e48d2721603b4e7ed4a4278c0ac451f285c (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
<?php

/**
 * XRDS-parsing tests for the Yadis library.
 */

require_once 'Auth/Yadis/XRDS.php';
require_once 'Auth/Yadis/XRIRes.php';
require_once 'Auth/Yadis/XRI.php';
require_once 'Tests/Auth/Yadis/TestUtil.php';

class Tests_Auth_Yadis_XRDS extends PHPUnit_Framework_TestCase {

    function test_good()
    {
        $files = array(
                       'brian.xrds' => 1,
                       'pip.xrds' => 2
                       );

        foreach ($files as $filename => $service_count) {
            $xml = Tests_Auth_Yadis_readdata($filename);
            $xrds = Auth_Yadis_XRDS::parseXRDS($xml);

            $this->assertTrue($xrds !== null);

            if ($xrds) {
                $this->assertEquals(count($xrds->services()), $service_count);
            } else {
                $this->fail("Could not test XRDS service list because the ".
                            "XRDS object is null");
            }
        }
    }

    function test_good_multi()
    {
        $xml = Tests_Auth_Yadis_readdata("brian.multi.xrds");
        $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
        $this->assertTrue($xrds !== null);
        $this->assertEquals(count($xrds->services()), 1);
        $s = $xrds->services();
        $s = $s[0];

        $types = $s->getTypes();

        $this->assertTrue(count($types) == 1);
        $this->assertEquals('http://openid.net/signon/1.0',
                            $types[0]);
    }

    function test_good_uri_multi()
    {
        $xml = Tests_Auth_Yadis_readdata("brian.multi_uri.xrds");
        $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
        $this->assertTrue($xrds !== null);
        $this->assertEquals(1, count($xrds->services()));
    }

    function test_uri_sorting()
    {
        $xml = Tests_Auth_Yadis_readdata("uri_priority.xrds");
        $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
        $services = $xrds->services();
        $uris = $services[0]->getURIs();

        $expected_uris = array(
                               "http://zero.priority/",
                               "http://one.priority/",
                               "http://no.priority/"
                               );

        $this->assertEquals($uris, $expected_uris);
    }

    function test_bad()
    {
        $this->assertTrue(Auth_Yadis_XRDS::parseXRDS(null) === null);
        $this->assertTrue(Auth_Yadis_XRDS::parseXRDS(5) === null);
        $this->assertTrue(Auth_Yadis_XRDS::parseXRDS('') === null);
        $this->assertTrue(Auth_Yadis_XRDS::parseXRDS('<html></html>') ===
                          null);
        $this->assertTrue(Auth_Yadis_XRDS::parseXRDS("\x00") === null);
    }

    function test_getCanonicalID()
    {
        $canonicalIDtests = array(
               array("@ootao*test1", "delegated-20060809.xrds",
                     "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
               array("@ootao*test1", "delegated-20060809-r1.xrds",
                     "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
               array("@ootao*test1", "delegated-20060809-r2.xrds",
                     "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
               array("@ootao*test1", "sometimesprefix.xrds",
                     "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
               array("@ootao*test1", "prefixsometimes.xrds",
                     "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
               array("=keturn*isDrummond", "spoof1.xrds", null),
               array("=keturn*isDrummond", "spoof2.xrds", null),
               array("@keturn*is*drummond", "spoof3.xrds", null),
               // Don't let IRI authorities be canonical for the GCS.
               array("phreak.example.com", "delegated-20060809-r2.xrds", null)
               // TODO: Refs
               // ("@ootao*test.ref", "ref.xrds", "@!BAE.A650.823B.2475")
               );

        foreach ($canonicalIDtests as $tupl) {
            list($iname, $filename, $expectedID) = $tupl;

            $xml = Tests_Auth_Yadis_readdata($filename);
            $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
            $this->_getCanonicalID($iname, $xrds, $expectedID);
        }
    }

    function _getCanonicalID($iname, $xrds, $expectedID)
    {
        if ($expectedID === null) {
            $result = Auth_Yadis_getCanonicalID($iname, $xrds);
            if ($result !== false) {
                $this->fail($iname.' (got '.$result.')');
            }
        } else {
            $cid = Auth_Yadis_getCanonicalID($iname, $xrds);
            $this->assertEquals(Auth_Yadis_XRI($expectedID), $cid);
        }
    }

    function test_services_filters()
    {
        // First, just be sure that service objects do the right
        // thing.
        $xml = Tests_Auth_Yadis_readdata("brian_priority.xrds");
        $xrds = Auth_Yadis_XRDS::parseXRDS($xml,
                                               array('openid' =>
                                                     'http://openid.net/xmlns/1.0'));
        $this->assertTrue($xrds !== null);

        // Get list of service objects.
        $services = $xrds->services();
        $this->assertEquals(count($services), 2, "first service count");

        // Query the two service objecs.
        $s1 = $services[0];
        $this->assertEquals($s1->getPriority(), 1, "first priority check");
        $types = $s1->getTypes();
        $this->assertEquals(count($types), 1, "first type check");

        $s2 = $services[1];
        $this->assertEquals($s2->getPriority(), 2, "second priority check");
        $types = $s2->getTypes();
        $this->assertEquals(count($types), 1, "second type check");

        function _DelegateFilter($service)
            {
                if ($service->getElements('openid:Delegate')) {
                    return true;
                }
                return false;
            }

        // Make sure that a filter which matches both DOES match both.
        $this->assertEquals(count(
                              $xrds->services(array("_DelegateFilter"))), 2,
                            "_DelegateFilter check");

        // This filter should match all services in the document.
        function _HasTypeAndURI($service)
            {
                if ($service->getTypes() &&
                    $service->getURIs()) {
                    return true;
                }
                return false;
            }

        // This filter should only match one.
        function _URIMatchesSchtuff($service)
            {
                $uris = $service->getURIs();

                foreach ($uris as $uri) {
                    if (preg_match("|schtuff|", $uri)) {
                        return true;
                    }
                }
                return false;
            }

        // This filter should only match one.
        function _URIMatchesMyOpenID($service)
            {
                $uris = $service->getURIs();

                foreach ($uris as $uri) {
                    if (preg_match("|myopenid|", $uri)) {
                        return true;
                    }
                }
                return false;
            }

        // Make sure a pair of filters in ALL mode only match one service.
        $this->assertEquals(count(
                              $xrds->services(array("_HasTypeAndURI",
                                                    "_URIMatchesSchtuff"),
                                              SERVICES_YADIS_MATCH_ALL)), 1,
                            "_HasTypeAndURI / _URIMatchesSchtuff check");

        // Make sure a pair of filters in ALL mode only match one service.
        $this->assertEquals(count(
                              $xrds->services(array("_HasTypeAndURI",
                                                    "_URIMatchesMyOpenID"),
                                              SERVICES_YADIS_MATCH_ALL)), 1,
                            "_HasTypeAndURI / _URIMatchesMyOpenID check");

        // Make sure a pair of filters in ANY mode matches both services.
        $this->assertEquals(count(
                              $xrds->services(array("_URIMatchesMyOpenID",
                                                    "_URIMatchesSchtuff"))), 2,
                            "_URIMatchesMyOpenID / _URIMatchesSchtuff check");

        // Make sure the order of the services returned (when using
        // filters) is correct.
        $s = $xrds->services(array("_URIMatchesMyOpenID",
                                   "_URIMatchesSchtuff"));

        $this->assertTrue($s[0]->getPriority() === 1, "s[0] priority check");
        $this->assertTrue($s[1]->getPriority() === 2, "s[1] priority check");

        // Make sure a bad filter mode gets us a null service list.
        $this->assertTrue($xrds->services(array("_URIMatchesMyOpenID",
                                                "_URIMatchesSchtuff"),
                                          "bogus") === null,
                          "bogus filter check");
    }

    function test_multisegment_xri()
    {
        $xml = Tests_Auth_Yadis_readdata('subsegments.xrds');
        $xmldoc = Auth_Yadis_XRDS::parseXRDS($xml);
        $result = Auth_Yadis_getCanonicalId('xri://=nishitani*masaki', $xmldoc);
        $this->assertEquals($result, "xri://=!E117.EF2F.454B.C707!0000.0000.3B9A.CA01");
    }
}