3 #load "netstring.cma";;
8 (**********************************************************************)
10 (**********************************************************************)
13 * "", "a", "ab", "abc", "abcd", "abcde",
14 * "abcdefghijklmnopqrstuvwxyz".
18 (* ENCODE. No line breaks. *)
19 Base64.encode "" = "" &
20 Base64.encode "a" = "YQ==" &
21 Base64.encode "ab" = "YWI=" &
22 Base64.encode "abc" = "YWJj" &
23 Base64.encode "abcd" = "YWJjZA==" &
24 Base64.encode "abcde" = "YWJjZGU=" &
25 Base64.encode "abcdefghijklmnopqrstuvwxyz" =
26 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo="
31 (* ENCODE. Lines with length of 4, separated by LF *)
32 let abc = "abcdefghijklmnopqrstuvwxyz" in
33 Base64.encode_substring abc 0 0 4 false = "" &
34 Base64.encode_substring abc 0 1 4 false = "YQ==\n" &
35 Base64.encode_substring abc 0 2 4 false = "YWI=\n" &
36 Base64.encode_substring abc 0 3 4 false = "YWJj\n" &
37 Base64.encode_substring abc 0 4 4 false = "YWJj\nZA==\n" &
38 Base64.encode_substring abc 0 5 4 false = "YWJj\nZGU=\n" &
39 Base64.encode_substring abc 0 26 4 false =
40 "YWJj\nZGVm\nZ2hp\namts\nbW5v\ncHFy\nc3R1\ndnd4\neXo=\n"
45 (* ENCODE. Lines with length of 5, separated by LF *)
46 let abc = "abcdefghijklmnopqrstuvwxyz" in
47 Base64.encode_substring abc 0 0 5 false = "" &
48 Base64.encode_substring abc 0 1 5 false = "YQ==\n" &
49 Base64.encode_substring abc 0 2 5 false = "YWI=\n" &
50 Base64.encode_substring abc 0 3 5 false = "YWJj\n" &
51 Base64.encode_substring abc 0 4 5 false = "YWJj\nZA==\n" &
52 Base64.encode_substring abc 0 5 5 false = "YWJj\nZGU=\n" &
53 Base64.encode_substring abc 0 26 5 false =
54 "YWJj\nZGVm\nZ2hp\namts\nbW5v\ncHFy\nc3R1\ndnd4\neXo=\n"
59 (* ENCODE. Lines with length of 7, separated by LF *)
60 let abc = "abcdefghijklmnopqrstuvwxyz" in
61 Base64.encode_substring abc 0 0 7 false = "" &
62 Base64.encode_substring abc 0 1 7 false = "YQ==\n" &
63 Base64.encode_substring abc 0 2 7 false = "YWI=\n" &
64 Base64.encode_substring abc 0 3 7 false = "YWJj\n" &
65 Base64.encode_substring abc 0 4 7 false = "YWJj\nZA==\n" &
66 Base64.encode_substring abc 0 5 7 false = "YWJj\nZGU=\n" &
67 Base64.encode_substring abc 0 26 7 false =
68 "YWJj\nZGVm\nZ2hp\namts\nbW5v\ncHFy\nc3R1\ndnd4\neXo=\n"
73 (* ENCODE. Lines with length of 8, separated by LF *)
74 let abc = "abcdefghijklmnopqrstuvwxyz" in
75 Base64.encode_substring abc 0 0 8 false = "" &
76 Base64.encode_substring abc 0 1 8 false = "YQ==\n" &
77 Base64.encode_substring abc 0 2 8 false = "YWI=\n" &
78 Base64.encode_substring abc 0 3 8 false = "YWJj\n" &
79 Base64.encode_substring abc 0 4 8 false = "YWJjZA==\n" &
80 Base64.encode_substring abc 0 5 8 false = "YWJjZGU=\n" &
81 Base64.encode_substring abc 0 26 8 false =
82 "YWJjZGVm\nZ2hpamts\nbW5vcHFy\nc3R1dnd4\neXo=\n"
87 (* ENCODE. Lines with length of 8, separated by CRLF *)
88 let abc = "abcdefghijklmnopqrstuvwxyz" in
89 Base64.encode_substring abc 0 0 8 true = "" &
90 Base64.encode_substring abc 0 1 8 true = "YQ==\r\n" &
91 Base64.encode_substring abc 0 2 8 true = "YWI=\r\n" &
92 Base64.encode_substring abc 0 3 8 true = "YWJj\r\n" &
93 Base64.encode_substring abc 0 4 8 true = "YWJjZA==\r\n" &
94 Base64.encode_substring abc 0 5 8 true = "YWJjZGU=\r\n" &
95 Base64.encode_substring abc 0 26 8 true =
96 "YWJjZGVm\r\nZ2hpamts\r\nbW5vcHFy\r\nc3R1dnd4\r\neXo=\r\n"
101 (* DECODE. First test without spaces *)
102 Base64.decode_substring "" 0 0 false false = "" &
103 Base64.decode_substring "YQ==" 0 4 false false = "a" &
104 Base64.decode_substring "YWI=" 0 4 false false = "ab" &
105 Base64.decode_substring "YWJj" 0 4 false false = "abc" &
106 Base64.decode_substring "YWJjZA==" 0 8 false false = "abcd" &
107 Base64.decode_substring "YWJjZGU=" 0 8 false false = "abcde" &
108 Base64.decode_substring
109 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=" 0 36 false false =
110 "abcdefghijklmnopqrstuvwxyz"
115 (* DECODE. With spaces *)
116 Base64.decode_substring " \r\n\t" 0 4 false true = "" &
117 Base64.decode_substring " Y W J j\n Z G U = " 0 18 false true = "abcde"
122 (* DECODE. With URL characters and spaces *)
123 Base64.decode_substring " Y W J j\n Z G U = " 0 18 true true = "abcde" &
124 Base64.decode_substring " Y W J j\n Z G U . " 0 18 true true = "abcde"
127 (**********************************************************************)
128 (* Quoted Printable *)
129 (**********************************************************************)
133 QuotedPrintable.encode "a %= 12345 &$[]\"" = "a %=3D 12345 &=24=5B=5D=22" &
134 QuotedPrintable.encode "\000\001\002" = "=00=01=02" &
135 QuotedPrintable.encode "abc\r\ndef\nghi" = "abc\r\ndef\nghi" &
136 QuotedPrintable.encode " abc\r\n def\n ghi" = " abc\r\n def\n ghi" &
137 QuotedPrintable.encode "abc \r\n def\nghi " = "abc=20\r\n def\nghi=20"
143 QuotedPrintable.decode "a %=3D 12345 &=24=5B=5D=22" = "a %= 12345 &$[]\"" &
144 QuotedPrintable.decode "=00=01=02" = "\000\001\002" &
145 QuotedPrintable.decode "abc\r\ndef\nghi" = "abc\r\ndef\nghi" &
146 QuotedPrintable.decode " abc\r\n def\n ghi" = " abc\r\n def\n ghi" &
147 QuotedPrintable.decode "abc=20\r\n def\nghi=20" = "abc \r\n def\nghi " &
148 QuotedPrintable.decode "abc=\r\n def\nghi=20" = "abc def\nghi "
151 (**********************************************************************)
153 (**********************************************************************)
157 Q.encode "a %= 12345 &$[]\"" = "a=20=25=3D=2012345=20=26=24=5B=5D=22" &
158 Q.encode "\000\001\002\r\n" = "=00=01=02=0D=0A"
164 Q.decode "a=20=25=3D=2012345=20=26=24=5B=5D=22" = "a %= 12345 &$[]\"" &
165 Q.decode "=00=01=02=0D=0A" = "\000\001\002\r\n" &
166 Q.decode "a=20=25=3d=2012345=20=26=24=5b=5d=22" = "a %= 12345 &$[]\""
169 (**********************************************************************)
171 (**********************************************************************)
173 (* Already tested for Cgi *)
175 (**********************************************************************)
177 (**********************************************************************)
180 Html.encode_from_latin1 "<>&\"abcdefäöÜ\160\025'" =
181 "<>&"abcdefäöÜ '"
186 Html.decode_to_latin1
187 "<>&"abcdefäöÜ " =
188 "<>&\"abcdefäöÜ\160\025" &
189 Html.decode_to_latin1 "'" = "'" &
190 Html.decode_to_latin1 "&nonsense;" = "&nonsense;" &
191 Html.decode_to_latin1 "Ā" = "Ā"
195 (**********************************************************************)
199 print_endline ("Test " ^ n ^ " ok")
201 print_endline ("Test " ^ n ^ " FAILED!!!!");