gopher-server.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # Copyright (C) 2017–2019 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package OddMuse;
  16. use strict;
  17. use 5.10.0;
  18. use Test::More;
  19. use IO::Socket::IP;
  20. use utf8; # tests contain UTF-8 characters and it matters
  21. require './t/test.pl';
  22. add_module('tags.pl');
  23. # enable uploads
  24. our($ConfigFile);
  25. AppendStringToFile($ConfigFile, "\$UploadAllowed = 1;\n");
  26. my $port = random_port();
  27. my $pid = fork();
  28. END {
  29. # kill server
  30. if ($pid) {
  31. kill 'KILL', $pid or warn "Could not kill server $pid";
  32. }
  33. }
  34. our ($DataDir);
  35. if (!defined $pid) {
  36. die "Cannot fork: $!";
  37. } elsif ($pid == 0) {
  38. use Config;
  39. my $secure_perl_path = $Config{perlpath};
  40. exec($secure_perl_path,
  41. "stuff/gopher-server.pl",
  42. "--host=127.0.0.1",
  43. "--port=$port",
  44. "--log_level=0", # set to 4 for verbose logging
  45. "--wiki=./wiki.pl",
  46. "--wiki_dir=$DataDir",
  47. "--wiki_pages=Alex",
  48. "--wiki_pages=Berta",
  49. "--wiki_pages=Chris")
  50. or die "Cannot exec: $!";
  51. }
  52. update_page('Alex', "My best friend is [[Berta]].\n\nTags: [[tag:Friends]]\n");
  53. update_page('Berta', "This is me.\n\nTags: [[tag:Friends]]\n");
  54. update_page('Chris', "I'm Chris.\n\nTags: [[tag:Friends]]\n");
  55. update_page('Friends', "Some friends.\n");
  56. update_page('2017-12-25', 'It was a Monday.\n\nTags: [[tag:Day]]');
  57. update_page('2017-12-26', 'It was a Tuesday.\n\nTags: [[tag:Day]]');
  58. update_page('2017-12-27', 'It was a Wednesday.\n\nTags: [[tag:Day]]');
  59. update_page('Friends', "News about friends.\n", 'rewrite', 1); # minor change
  60. update_page('Friends', "News about friends:\n\n<journal search tag:friends>\n",
  61. 'add journal tag', 1); # minor change
  62. # file created using convert NULL: test.png && base64 test.png
  63. update_page('Picture',
  64. "#FILE image/png\niVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bv"
  65. . "kkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg==");
  66. sub query_gopher {
  67. my $query = shift;
  68. my $text = shift;
  69. # create client
  70. my $socket = IO::Socket::IP->new(
  71. PeerHost => "localhost",
  72. PeerPort => $port,
  73. Type => SOCK_STREAM, )
  74. or die "Cannot construct client socket: $@";
  75. $socket->print("$query\r\n");
  76. $socket->print($text);
  77. undef $/; # slurp
  78. return <$socket>;
  79. }
  80. # main menu
  81. my $page = query_gopher("");
  82. for my $item(qw(Alex Berta Chris 2017-12-25 2017-12-26 2017-12-27)) {
  83. like($page, qr/^1$item\t$item\/menu\t/m, "main menu contains $item");
  84. }
  85. # page menu
  86. $page = query_gopher("Alex/menu");
  87. like($page, qr/^0Alex\tAlex\t/m,
  88. "Alex menu links to plain text");
  89. like($page, qr/^hAlex\tAlex\/html\t/m,
  90. "Alex menu links to HTML");
  91. like($page, qr/^1Page History\tAlex\/history\t/m,
  92. "Alex menu links to page history");
  93. like($page, qr/^1Berta\tBerta\/menu\t/m,
  94. "Alex menu links to Berta menu");
  95. like($page, qr/^1Friends\tFriends\/tag\t/m,
  96. "Alex menu links to Friends tag");
  97. # plain text
  98. $page = query_gopher("Alex");
  99. like($page, qr/^My best friend is \[\[Berta\]\]/, "Alex plain text");
  100. # HTML
  101. $page = query_gopher("Alex/html");
  102. like($page, qr/<p>My best friend is <a.*?>Berta<\/a>/, "Alex HTML");
  103. # tags
  104. $page = query_gopher("Friends/tag");
  105. like($page, qr/iThis page is about the tag Friends/, "tag menu intro");
  106. for my $item(qw(Friends Alex Berta Chris)) {
  107. like($page, qr/^1$item\t$item\/menu\t/m, "tag menu contains $item");
  108. }
  109. # tags
  110. $page = query_gopher("Day/tag");
  111. like($page, qr/2017-12-27.*2017-12-26.*2017-12-25/s,
  112. "tag menu sorted newest first");
  113. # match
  114. $page = query_gopher("do/match\t2017");
  115. for my $item(qw(2017-12-25 2017-12-26 2017-12-27)) {
  116. like($page, qr/^1$item\t$item\/menu\t/m, "match menu contains $item");
  117. }
  118. like($page, qr/2017-12-27.*2017-12-26.*2017-12-25/s,
  119. "match menu sorted newest first");
  120. # search
  121. $page = query_gopher("do/search\ttag:day");
  122. for my $item(qw(2017-12-25 2017-12-26 2017-12-27)) {
  123. like($page, qr/^1$item\t$item\/menu\t/m, "serch menu contains $item");
  124. }
  125. like($page, qr/2017-12-27.*2017-12-26.*2017-12-25/s,
  126. "search menu sorted newest first");
  127. # rc
  128. $page = query_gopher("do/rc");
  129. my $re = join(".*", "Picture", "2017-12-27", "2017-12-26", "2017-12-25",
  130. "Friends", "Chris", "Berta", "Alex");
  131. like($page, qr/$re/s, "rc in the right order");
  132. $page = query_gopher("do/rc/showedits");
  133. $re = join(".*", "Friends", "2017-12-27", "2017-12-26", "2017-12-25");
  134. like($page, qr/$re/s, "rc in the right order");
  135. # history
  136. $page = query_gopher("Friends/history");
  137. like($page, qr/^1Friends \(1\)\tFriends\/1\/menu\t/m,
  138. "Friends (1)");
  139. like($page, qr/^1Friends \(2\)\tFriends\/2\/menu\t/m,
  140. "Friends (2)");
  141. like($page, qr/^1Friends \(current\)\tFriends\/menu\t/m,
  142. "Friends (current)");
  143. like($page, qr/Friends\/menu.*Friends\/2\/menu.*Friends\/1\/menu/s,
  144. "history in the right order");
  145. # revision menu
  146. $page = query_gopher("Friends/1/menu");
  147. like($page, qr/^0Friends\tFriends\/1\t/m,
  148. "Friends/1 menu links to plain text");
  149. like($page, qr/^hFriends\tFriends\/1\/html\t/m,
  150. "Friends/1 menu links to HTML");
  151. unlike($page, qr/Search result for tag/,
  152. "Friends/1 has no journal and thus no tag search");
  153. # revision plain text
  154. $page = query_gopher("Friends/1");
  155. like($page, qr/^Some friends/m, "Friends/1 plain text");
  156. # revision html
  157. $page = query_gopher("Friends/1/html");
  158. like($page, qr/<p>Some friends/m, "Friends/1 html");
  159. # upload text
  160. my $haiku = <<EOT;
  161. Quiet disk ratling
  162. Keyboard clicking, then it stops.
  163. Rain falls and I think
  164. .
  165. EOT
  166. $page = query_gopher("Haiku/write/text", "$haiku");
  167. like($page, qr/^iPage was saved./m, "Write Haiku");
  168. like($page, qr/^1Haiku\tHaiku\/menu/m, "Link back to Haiku");
  169. my $haiku_re = quotemeta(substr($haiku, 0, -2)); # strip period and \n
  170. $page = query_gopher("Haiku");
  171. like($page, qr/^$haiku_re/, "Haiku saved");
  172. $haiku = <<"EOT";
  173. ```
  174. username: Alex
  175. minor: 1
  176. summary: typos
  177. ```
  178. Quiet disk rattling
  179. Keyboard clicking, then it stops.
  180. Rain falls and I think.
  181. .
  182. EOT
  183. $page = query_gopher("Haiku/write/text", "$haiku");
  184. like($page, qr/^iPage was saved./m, "Write haiku");
  185. $haiku_re = quotemeta(<<"EOT");
  186. Quiet disk rattling
  187. Keyboard clicking, then it stops.
  188. Rain falls and I think.
  189. EOT
  190. $page = query_gopher("Haiku");
  191. like($page, qr/^$haiku_re/, "Haiku updated");
  192. $page = query_gopher("Haiku/history");
  193. like($page, qr/^1Haiku \(current\)\tHaiku\/menu\t/m, "Haiku (current)");
  194. like($page, qr/^i\d\d:\d\d UTC by Alex: typos \(minor\)/m,
  195. "Metadata recorded");
  196. like($page, qr/^1Haiku \(1\)\tHaiku\/1\/menu\t/m, "Haiku (1)");
  197. # new page
  198. $page = query_gopher("do/new", <<"EOT");
  199. ```
  200. username: Alex
  201. summary: copy
  202. title: Haiku_Copy
  203. ```
  204. Quiet disk rattling
  205. Keyboard clicking, then it stops.
  206. Rain falls and I think.
  207. .
  208. EOT
  209. like($page, qr/^iPage was saved./m, "Write copy of haiku");
  210. $page = query_gopher("Haiku_Copy");
  211. like($page, qr/^$haiku_re/, "New copy of haiku created");
  212. # append
  213. $page = query_gopher("Haiku_Copy/append/text", "This is a comment by me!\n.\n");
  214. like($page, qr/^iPage was saved./m, "Append to copy of haiku");
  215. $page = query_gopher("Haiku_Copy");
  216. like($page, qr/^$haiku_re/, "Copy of haiku still there");
  217. like($page, qr/\n\n----\n\nThis is a comment by me!\n\n-- Anonymous/,
  218. "Comment is also there");
  219. # Image download
  220. my $image = query_gopher("Picture");
  221. like($image, qr/\211PNG\r\n/, "Image download");
  222. # Image upload
  223. $page = query_gopher("PictureCopy/write/file\t" . length($image), "$image");
  224. like($page, qr/Files of type application\/octet-stream are not allowed/m,
  225. "MIME type check");
  226. $page = query_gopher("PictureCopy/image/png/write/file\t" . length($image), "$image");
  227. like($page, qr/^iPage was saved./m, "Image upload");
  228. unlike($page, qr/^3Page was not saved/, "Messages are correct");
  229. my $copy = query_gopher("PictureCopy");
  230. like($copy, qr/\211PNG\r\n/, "Image copy download");
  231. is($copy, $image, "Image and copy are identical");
  232. # image:link
  233. $page = query_gopher("Test/write/text", "[[image:Picture]]\n.\n");
  234. like($page, qr/^iPage was saved./m, "Saved test page containing image link");
  235. $page = query_gopher("Test/menu");
  236. like($page, qr/^1Picture\tPicture\/menu/m, "Link to image page looks good");
  237. $page = query_gopher("Picture/menu");
  238. like($page, qr/^IPicture\tPicture/, "Link to image file looks good");
  239. # Test upload of large page (but note $MaxPost: 1024 * 210 > (10 * 8 + 1) * 2600)
  240. my $garbage = (("0123456789" x 8) . "\n") x 2600 . "Last Line\n";
  241. $page = query_gopher("Large/write/text", "$garbage.\n");
  242. like($page, qr/^iPage was saved./m, "Write page with "
  243. . length($garbage) . " bytes");
  244. $page = query_gopher("Large");
  245. like(substr($page, -20), qr/Last Line/, "All of large page was saved");
  246. # Test of Umlauts in the selector
  247. test_page(update_page('Zürich♥', '[[Üetliberg♥]]'), 'Zürich♥', 'Üetliberg♥');
  248. $page = query_gopher("Z%c3%bcrich%e2%99%a5");
  249. utf8::decode($page);
  250. like($page, qr/Üetliberg♥/, "UTF-8 encoded page names");
  251. $page = query_gopher("Z%c3%bcrich%e2%99%a5/menu");
  252. utf8::decode($page);
  253. like($page, qr/^0Zürich♥\tZ%c3%bcrich%e2%99%a5\t/m, "UTF-8 encoded text link");
  254. like($page, qr/^1Üetliberg♥\t%c3%9cetliberg%e2%99%a5\/menu\t/m,
  255. "UTF-8 encoded links");
  256. # Space normalization
  257. test_page(update_page('my_page', '[[my page]]'));
  258. $page = query_gopher("my_page"); # all pages are normalized
  259. like($page, qr/\[\[my page\]\]/, "Page name with space");
  260. $page = query_gopher("my_page/menu");
  261. like($page, qr/^0my page\tmy_page\t/m, "Space translates to underscore in links");
  262. $page = <<EOF;
  263. Floodgap link, and the typical Gopher link:
  264. [http://gopher.floodgap.com/gopher/gw?a=gopher%3A%2F%2Fsdf.org%3A70%2F0%2Fusers%2Fsolderpunk%2Fphlog%2Fintroducing-vf1.txt VF-1], [gopher://sdf.org:70/1/phlogs/ Phlogs]
  265. Solderpunk was writing about Gopher and the Web again.
  266. [gopher://zaibatsu.circumlunar.space:70/0/~solderpunk/phlog/protocol-pondering-intensifies.txt]
  267. [gopher://zaibatsu.circumlunar.space:70/0/~solderpunk/phlog/protocol-pondering-intensifies-ii.txt]
  268. [gopher://zaibatsu.circumlunar.space:70/0/~solderpunk/phlog/protocol-pondering-intensifies-iii.txt]
  269. So that's what I did. I wrote a little server that serves text files.
  270. Requests are simple selectors. Like Gopher. Like Finger. Remember,
  271. [[2019-01-09 Finger is Gopher|finger is gopher]]!
  272. I called it *Nimi Mute*, "many words."
  273. * https://alexschroeder.ch/cgit/nimi-mute/about/
  274. * [https://github.com/kensanata/nimi-mute Nimi Mute]
  275. As you can see in the README, you can even use `finger` or `lynx` to
  276. get text files from it! It's all the same. `telnet` and `nc` also
  277. work, of course. :)
  278. Tags: [[tag:Gopher]] [[tag:Finger]] [[tag:Perl 5]]
  279. EOF
  280. # gopher links
  281. update_page('Gopher', $page);
  282. $page = query_gopher("Gopher/menu");
  283. like($page, qr/^1Phlogs\t\/phlogs\/\tsdf\.org\t70/m, "Direct Gopher link");
  284. like($page, qr/^0VF-1\t\/users\/solderpunk\/phlog\/introducing-vf1.txt\tsdf\.org\t70/m, "Floodgap proxy link");
  285. my $re = "^0"
  286. . join("\t",
  287. quotemeta("zaibatsu.circumlunar.space:70/0/~solderpunk/phlog/protocol-pondering-intensifies.txt"),
  288. quotemeta("/~solderpunk/phlog/protocol-pondering-intensifies.txt"),
  289. quotemeta("zaibatsu.circumlunar.space"),
  290. "70");
  291. like($page, qr/$re/m, "Gopher link 1");
  292. my $re = "^0"
  293. . join("\t",
  294. quotemeta("zaibatsu.circumlunar.space:70/0/~solderpunk/phlog/protocol-pondering-intensifies-ii.txt"),
  295. quotemeta("/~solderpunk/phlog/protocol-pondering-intensifies-ii.txt"),
  296. quotemeta("zaibatsu.circumlunar.space"),
  297. "70");
  298. like($page, qr/$re/m, "Gopher link 2");
  299. my $re = "^0"
  300. . join("\t",
  301. quotemeta("zaibatsu.circumlunar.space:70/0/~solderpunk/phlog/protocol-pondering-intensifies-iii.txt"),
  302. quotemeta("/~solderpunk/phlog/protocol-pondering-intensifies-iii.txt"),
  303. quotemeta("zaibatsu.circumlunar.space"),
  304. "70");
  305. like($page, qr/$re/m, "Gopher link 3");
  306. my $re = "^1"
  307. . join("\t",
  308. quotemeta("finger is gopher"),
  309. quotemeta("2019-01-09_Finger_is_Gopher/menu"),
  310. "127\.0\.0\.1",
  311. $port);
  312. like($page, qr/$re/m, "Internal link");
  313. my $re = "^h"
  314. . join("\t",
  315. quotemeta("Nimi Mute"),
  316. quotemeta("URL:https://github.com/kensanata/nimi-mute"),
  317. "127\.0\.0\.1",
  318. $port);
  319. like($page, qr/$re/m, "HTML Link");
  320. my $re = "^h"
  321. . join("\t",
  322. quotemeta("https://alexschroeder.ch/cgit/nimi-mute/about/"),
  323. quotemeta("URL:https://alexschroeder.ch/cgit/nimi-mute/about/"),
  324. "127\.0\.0\.1",
  325. $port);
  326. like($page, qr/$re/m, "Bare HTML Link");
  327. # and on the page itself, tags are rendered differently
  328. $page = query_gopher("Gopher");
  329. like($page, qr/#Gopher/m, "Gopher tag");
  330. like($page, qr/#Perl_5/m, "Gopher multi-word tag");
  331. done_testing();