1--TEST--
2Temporary test of mbstring's UUEncode 'encoding'
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7srand(1000); // Make results consistent
8
9/* Using mbstring to convert strings from UUEncode has already been deprecated
10 * So this test should be removed when the UUEncode 'encoding' is */
11error_reporting(E_ALL & ~E_DEPRECATED);
12
13function testConversion($uuencode, $raw) {
14  $converted = mb_convert_encoding($uuencode, '8bit', 'UUENCODE');
15  if ($converted !== $raw)
16    die('Expected "' . $uuencode . '" to convert to ' . bin2hex($raw) . '; actually got ' . bin2hex($converted));
17  $converted = mb_convert_encoding($raw, 'UUENCODE', '8bit');
18  if ($converted !== $uuencode)
19    die('Expected ' . bin2hex($raw) . ' to convert to "' . $uuencode . '"; actually got "' . $converted . '"');
20}
21
22testConversion('', '');
23
24/* mbstring's uuencode requires the user to strip off the terminating "`\nend\n" */
25
26testConversion("begin 0644 filename\n#0V%T\n", 'Cat');
27testConversion("begin 0644 filename\n::'1T<#HO+W=W=RYW:6MI<&5D:6\$N;W)G#0H`\n", "http://www.wikipedia.org\r\n");
28testConversion("begin 0644 filename\n#`0(#\n", "\x01\x02\x03");
29testConversion("begin 0644 filename\n$`0(#\"@``\n", "\x01\x02\x03\n");
30
31function testRoundTrip($data) {
32  $encoded = mb_convert_encoding($data, 'UUENCODE', '8bit');
33  $decoded = mb_convert_encoding($encoded, '8bit', 'UUENCODE');
34  if ($data !== $decoded)
35    die("Round-trip failed! Expected " . bin2hex($data) . " to round-trip; actually got " . bin2hex($decoded));
36}
37
38for ($iterations = 0; $iterations < 500; $iterations++) {
39  $strlen = rand(1, 300);
40  $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
41  $randstring = '';
42  for ($i = 0; $i < $strlen; $i++) {
43      $randstring .= $characters[rand(0, strlen($characters) - 1)];
44  }
45  testRoundTrip($randstring);
46}
47
48echo "Done!\n";
49?>
50--EXPECTF--
51Done!
52