]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/ProbeTest.php
Merge pull request #12789 from nupplaphil/bug/flasky_test
[friendica.git] / tests / src / Network / ProbeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Network;
23
24 use Friendica\Network\Probe;
25 use Friendica\Test\DiceHttpMockHandlerTrait;
26 use Friendica\Test\MockedTest;
27 use GuzzleHttp\Middleware;
28
29 class ProbeTest extends MockedTest
30 {
31         use DiceHttpMockHandlerTrait;
32
33         protected function setUp(): void
34         {
35                 parent::setUp();
36
37                 $this->setupHttpMockHandler();
38         }
39
40         protected function tearDown(): void
41         {
42                 $this->tearDownHandler();
43
44                 parent::tearDown();
45         }
46
47         const TEMPLATENOBASE = '
48 <!DOCTYPE html>
49 <html lang="en-us">
50 <head>
51     <title>Example Blog</title>
52     <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
53         <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
54 </head>
55 <body>
56     <p>Hello World!</p>
57 </body>
58 </html>';
59
60         const TEMPLATEBASE = '
61 <!DOCTYPE html>
62 <html lang="en-us">
63 <head>
64     <title>Example Blog</title>
65     <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
66         <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
67     <base href="{{$url}}">
68 </head>
69 <body>
70     <p>Hello World!</p>
71 </body>
72 </html>';
73
74         const EXPECTED = [
75                 'https://example.org/path/to/blog/index.php' => [
76                         'index.xml'               => 'https://example.org/path/to/blog/index.xml',
77                         './index.xml'             => 'https://example.org/path/to/blog/index.xml',
78                         '../index.xml'            => 'https://example.org/path/to/index.xml',
79                         '/index.xml'              => 'https://example.org/index.xml',
80                         '//example.com/index.xml' => 'https://example.com/index.xml',
81                 ],
82                 'https://example.org/path/to/blog/' => [
83                         'index.xml'               => 'https://example.org/path/to/blog/index.xml',
84                         './index.xml'             => 'https://example.org/path/to/blog/index.xml',
85                         '../index.xml'            => 'https://example.org/path/to/index.xml',
86                         '/index.xml'              => 'https://example.org/index.xml',
87                         '//example.com/index.xml' => 'https://example.com/index.xml',
88                 ],
89                 'https://example.org/blog/' => [
90                         'index.xml'               => 'https://example.org/blog/index.xml',
91                         './index.xml'             => 'https://example.org/blog/index.xml',
92                         '../index.xml'            => 'https://example.org/index.xml',
93                         '/index.xml'              => 'https://example.org/index.xml',
94                         '//example.com/index.xml' => 'https://example.com/index.xml',
95                 ],
96                 'https://example.org' => [
97                         'index.xml'               => 'https://example.org/index.xml',
98                         './index.xml'             => 'https://example.org/index.xml',
99                         '../index.xml'            => 'https://example.org/index.xml',
100                         '/index.xml'              => 'https://example.org/index.xml',
101                         '//example.com/index.xml' => 'https://example.com/index.xml',
102                 ],
103         ];
104
105         private function replaceMacros($template, $vars)
106         {
107                 foreach ($vars as $var => $value) {
108                         $template = str_replace('{{' . $var . '}}', $value, $template);
109                 }
110
111                 return $template;
112         }
113
114         /**
115          * @small
116          */
117         public function testGetFeedLinkNoBase()
118         {
119                 foreach (self::EXPECTED as $url => $hrefs) {
120                         foreach ($hrefs as $href => $expected) {
121                                 $body = $this->replaceMacros(self::TEMPLATENOBASE, ['$link' => $href]);
122
123                                 $feedLink = Probe::getFeedLink($url, $body);
124
125                                 self::assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
126                         }
127                 }
128         }
129
130         /**
131          * @small
132          */
133         public function testGetFeedLinkBase()
134         {
135                 foreach (self::EXPECTED as $url => $hrefs) {
136                         foreach ($hrefs as $href => $expected) {
137                                 $body = $this->replaceMacros(self::TEMPLATEBASE, ['$url' => $url, '$link' => $href]);
138
139                                 $feedLink = Probe::getFeedLink('http://example.com', $body);
140
141                                 self::assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
142                         }
143                 }
144         }
145
146         public function dataCleanUri(): array
147         {
148                 return [
149                         '@-first' => [
150                                 'expected' => 'Artists4Future_Muenchen@climatejustice.global',
151                                 'uri'      => '@Artists4Future_Muenchen@climatejustice.global',
152                         ],
153                         'no-scheme-no-fragment' => [
154                                 'expected' => 'example.com/path?arg=value',
155                                 'uri'      => 'example.com/path?arg=value',
156                         ],
157                         /* This case makes little sense, both in our expectation of receiving it in any context and in the way we
158                          * do not change it in Probe::cleanUri, but it doesn't seem to be the source of any terrible security hole.
159                          */
160                         'no-scheme-fragment' => [
161                                 'expected' => 'example.com/path?arg=value#fragment',
162                                 'uri'      => 'example.com/path?arg=value#fragment',
163                         ],
164                         'scheme-no-fragment' => [
165                                 'expected' => 'https://example.com/path?arg=value',
166                                 'uri'      => 'https://example.com/path?arg=value#fragment',
167                         ],
168                         'scheme-fragment' => [
169                                 'expected' => 'https://example.com/path?arg=value',
170                                 'uri'      => 'https://example.com/path?arg=value#fragment',
171                         ],
172                 ];
173         }
174
175         /**
176          * @dataProvider dataCleanUri
177          */
178         public function testCleanUri(string $expected, string $uri)
179         {
180                 self::assertEquals($expected, Probe::cleanURI($uri));
181         }
182
183         public function dataUri(): array
184         {
185                 return [
186                         'Artists4Future_Muenchen@climatejustice.global' => [
187                                 'uri'         => 'Artists4Future_Muenchen@climatejustice.global',
188                                 'assertInfos' => [
189                                         'name'         => 'Artists4Future München',
190                                         'nick'         => 'Artists4Future_Muenchen',
191                                         'url'          => 'https://climatejustice.global/users/Artists4Future_Muenchen',
192                                         'alias'        => 'https://climatejustice.global/@Artists4Future_Muenchen',
193                                         'photo'        => 'https://cdn.masto.host/climatejusticeglobal/accounts/avatars/000/021/220/original/05ee9e827a5b47fc.jpg',
194                                         'header'       => 'https://cdn.masto.host/climatejusticeglobal/accounts/headers/000/021/220/original/9b98b75cf696cd11.jpg',
195                                         'account-type' => 0,
196                                         'about'        => 'Wir sind Künstler oder einfach gerne kreativ tätig und setzen uns unabhängig von politischen Parteien für den Klimaschutz ein. Die Bedingungen zu schaffen, die die [url=https://climatejustice.global/tags/Klimakrise]#Klimakrise[/url] verhindern/eindämmen (gemäß den Forderungen der [url=https://climatejustice.global/tags/Fridays4Future]#Fridays4Future[/url]) ist Aufgabe der Politik, muss aber gesamtgesellschaftlich getragen werden. Mit unseren künstlerischen Aktionen wollen wir einen anderen Zugang anbieten für wissenschaftlich rationale Argumente, speziell zur Erderwärmung und ihre Konsequenzen.',
197                                         'hide'         => 0,
198                                         'batch'        => 'https://climatejustice.global/inbox',
199                                         'notify'       => 'https://climatejustice.global/users/Artists4Future_Muenchen/inbox',
200                                         'poll'         => 'https://climatejustice.global/users/Artists4Future_Muenchen/outbox',
201                                         'subscribe'    => 'https://climatejustice.global/authorize_interaction?uri={uri}',
202                                         'following'    => 'https://climatejustice.global/users/Artists4Future_Muenchen/following',
203                                         'followers'    => 'https://climatejustice.global/users/Artists4Future_Muenchen/followers',
204                                         'inbox'        => 'https://climatejustice.global/users/Artists4Future_Muenchen/inbox',
205                                         'outbox'       => 'https://climatejustice.global/users/Artists4Future_Muenchen/outbox',
206                                         'sharedinbox'  => 'https://climatejustice.global/inbox',
207                                         'priority'     => 0,
208                                         'network'      => 'apub',
209                                         'pubkey'       => '-----BEGIN PUBLIC KEY-----
210 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6pYKPuDKb+rmBB869uPV
211 uLYFPosGxMUfenWqfWmFKzEqJ87rAft0IQDAL6dCoYE55ov/lEDNROhasTZLirZf
212 M5b7/1JmwMrAfEiaciuYqDWT3/yDpnekOIdzP5iSClg4zt7e6HRFuClqo4+b6hIE
213 DTMV4ksItvq/92MIu62pZ2SZr5ADPPZ/914lJ86hIH5BanbE8ZFzDS9vJA7V74rt
214 Vvkr5c/OiUyuODNYApSl87Ez8cuj8Edt89YWkDCajQn3EkmXGeJY/VRjEDfcyk6r
215 AvdUa0ArjXud3y3NkakVFZ0d7tmB20Vn9s/CfYHU8FXzbI1kFkov2BX899VVP5Ay
216 xQIDAQAB
217 -----END PUBLIC KEY-----',
218                                         'manually-approve' => 0,
219                                         'baseurl'          => 'https://climatejustice.global',
220                                 ]
221                         ]
222                 ];
223         }
224
225         /**
226          * @dataProvider dataUri
227          */
228         public function testProbeUri(string $uri, array $assertInfos)
229         {
230                 self::markTestIncomplete('hard work due mocking 19 different http-requests');
231
232                 /**
233                  * Requests:
234                  *
235                  * GET : https://climatejustice.global/.well-known/webfinger?resource=acct:Artists4Future_Muenchen%40climatejustice.global
236                  * 200
237                  * GET : http://localhost/.well-known/nodeinfo
238                  * 200
239                  * GET : http://localhost/statistics.json
240                  * 404
241                  * GET : http://localhost
242                  * 200
243                  * GET : http://localhost/friendica/json
244                  * 404
245                  * GET : http://localhost/friendika/json
246                  * 404
247                  * GET : http://localhost/poco
248                  * 403
249                  * GET : http://localhost/api/v1/directory?limit=1
250                  * 200
251                  * GET : http://localhost/.well-known/x-social-relay
252                  * 200
253                  * GET : http://localhost/friendica
254                  * 404
255                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen
256                  * 200
257                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen/following
258                  * 200
259                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen/followers
260                  * 200
261                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen/outbox
262                  * 200
263                  * GET : https://climatejustice.global/.well-known/nodeinfo
264                  * 200
265                  * GET : https://climatejustice.global/nodeinfo/2.0
266                  * 200
267                  * GET : https://climatejustice.global/poco
268                  * 404
269                  * GET : https://climatejustice.global/api/v1/directory?limit=1
270                  * 200
271                  * GET : https://climatejustice.global/.well-known/webfinger?resource=acct%3AArtists4Future_Muenchen%40climatejustice.global
272                  * 200
273                  *
274                  */
275
276                 $container = [];
277                 $history   = Middleware::history($container);
278
279                 $this->httpRequestHandler->push($history);
280
281                 self::assertArraySubset($assertInfos, Probe::uri($uri, '', 0));
282
283                 // Iterate over the requests and responses
284                 foreach ($container as $transaction) {
285                         echo $transaction['request']->getMethod() . " : " . $transaction['request']->getUri() . PHP_EOL;
286                         //> GET, HEAD
287                         if ($transaction['response']) {
288                                 echo $transaction['response']->getStatusCode() . PHP_EOL;
289                         //> 200, 200
290                         } elseif ($transaction['error']) {
291                                 echo $transaction['error'];
292                                 //> exception
293                         }
294                 }
295         }
296 }