]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/pxp/netstring/tests/test_netencoding.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / DEVEL / pxp / netstring / tests / test_netencoding.ml
1 #require "str";;
2 #directory "..";;
3 #load "netstring.cma";;
4
5
6 open Netencoding;;
7
8 (**********************************************************************)
9 (* Base64                                                             *)
10 (**********************************************************************)
11
12 (* Test strings:
13  * "", "a", "ab", "abc", "abcd", "abcde",
14  * "abcdefghijklmnopqrstuvwxyz".
15  *)
16
17 let t001() =
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="
27 ;;
28
29
30 let t002() =
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"
41 ;;
42
43
44 let t003() =
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"
55 ;;
56
57
58 let t004() =
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"
69 ;;
70
71
72 let t005() =
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"
83 ;;
84
85
86 let t006() =
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"
97 ;;
98
99
100 let t020() =
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"
111 ;;
112
113
114 let t021() =
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"
118 ;;
119  
120
121 let t022() =
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"
125 ;;
126
127 (**********************************************************************)
128 (* Quoted Printable                                                   *)
129 (**********************************************************************)
130
131 let t100() =
132   (* ENCODE. *)
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"
138 ;;
139
140
141 let t120() =
142   (* DECODE. *)
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 "
149 ;;
150
151 (**********************************************************************)
152 (* Q                                                                  *)
153 (**********************************************************************)
154
155 let t200() =
156   (* ENCODE. *)
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"
159 ;;
160
161
162 let t220() =
163   (* DECODE. *)
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 &$[]\"" 
167 ;;
168
169 (**********************************************************************)
170 (* Url                                                                *)
171 (**********************************************************************)
172
173 (* Already tested for Cgi *)
174
175 (**********************************************************************)
176 (* Html                                                               *)
177 (**********************************************************************)
178
179 let t300() =
180   Html.encode_from_latin1 "<>&\"abcdefäöÜ\160\025'" = 
181     "&lt;&gt;&amp;&quot;abcdef&auml;&ouml;&Uuml;&nbsp;&#25;'"
182 ;;
183
184
185 let t320() =
186   Html.decode_to_latin1 
187     "&lt;&gt;&amp;&quot;abcdef&auml;&ouml;&Uuml;&nbsp;&#25;" =
188     "<>&\"abcdefäöÜ\160\025" &
189   Html.decode_to_latin1 "&apos;" = "'" &
190   Html.decode_to_latin1 "&nonsense;" = "&nonsense;" &
191   Html.decode_to_latin1 "&#256;" = "&#256;"
192 ;;
193
194
195 (**********************************************************************)
196
197 let test f n =
198   if f() then
199     print_endline ("Test " ^ n ^ " ok")
200   else 
201     print_endline ("Test " ^ n ^ " FAILED!!!!");
202   flush stdout
203 ;;
204
205 test t001 "001";
206 test t002 "002";
207 test t003 "003";
208 test t004 "004";
209 test t005 "005";
210 test t006 "006";
211
212 test t020 "020";
213 test t021 "021";
214 test t022 "022";
215
216 test t100 "100";
217 test t120 "120";
218
219 test t200 "200";
220 test t220 "220";
221
222 test t300 "300";
223 test t320 "320";