]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/tests/slap.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / OStatus / tests / slap.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2010, StatusNet, Inc.
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 published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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 <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 $longoptions = array('verify', 'slap=', 'notice=');
24
25 $helptext = <<<END_OF_HELP
26 slap.php [options]
27
28 Test generation and sending of magic envelopes for Salmon slaps.
29
30      --notice=N   generate entry for this notice number
31      --verify     send signed magic envelope to Tuomas Koski's test service
32      --slap=<url> send signed Salmon slap to the destination endpoint
33
34
35 END_OF_HELP;
36
37 require_once INSTALLDIR.'/scripts/commandline.inc.php';
38
39 if (!have_option('--notice')) {
40     print "$helptext";
41     exit(1);
42 }
43
44 $notice_id = get_option_value('--notice');
45
46 $notice = Notice::getKV('id', $notice_id);
47 $profile = $notice->getProfile();
48 $entry = $notice->asAtomEntry(true);
49
50 echo "== Original entry ==\n\n";
51 print $entry;
52 print "\n\n";
53
54 $magic_env = MagicEnvelope::signAsUser($entry, $profile->getUser());
55 $envxml = $magic_env->toXML();
56
57 echo "== Signed envelope ==\n\n";
58 print $envxml;
59 print "\n\n";
60
61 echo "== Testing local verification ==\n\n";
62 $magic_env = new MagicEnvelope($envxml);
63 $activity = new Activity($magic_env->getPayload()->documentElement);
64 $actprofile = Profile::fromUri($activity->actor->id);
65 $ok = $magic_env->verify($actprofile);
66 if ($ok) {
67     print "OK\n\n";
68 } else {
69     print "FAIL\n\n";
70 }
71
72 if (have_option('--verify')) {
73     $url = 'http://www.madebymonsieur.com/ostatus_discovery/magic_env/validate/';
74     echo "== Testing remote verification ==\n\n";
75     print "Sending for verification to $url ...\n";
76
77     $client = new HTTPClient();
78     $response = $client->post($url, array(), array('magic_env' => $envxml));
79
80     print $response->getStatus() . "\n\n";
81     print $response->getBody() . "\n\n";
82 }
83
84 if (have_option('--slap')) {
85     $url = get_option_value('--slap');
86     echo "== Remote salmon slap ==\n\n";
87     print "Sending signed Salmon slap to $url ...\n";
88
89     $ok = Salmon::post($url, $entry, $profile->getUser());
90     if ($ok) {
91         print "OK\n\n";
92     } else {
93         print "FAIL\n\n";
94     }
95 }