r10883: update policy
[puri.git] / tests.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;; copyright (c) 1999-2001 Franz Inc, Berkeley, CA  - All rights reserved.
3 ;; copyright (c) 2003 Kevin Rosenberg (significant fixes for using
4 ;; tester package)
5 ;;
6 ;; The software, data and information contained herein are proprietary
7 ;; to, and comprise valuable trade secrets of, Franz, Inc.  They are
8 ;; given in confidence by Franz, Inc. pursuant to a written license
9 ;; agreement, and may be stored and used only in accordance with the terms
10 ;; of such license.
11 ;;
12 ;; Restricted Rights Legend
13 ;; ------------------------
14 ;; Use, duplication, and disclosure of the software, data and information
15 ;; contained herein by any agency, department or entity of the U.S.
16 ;; Government are subject to restrictions of Restricted Rights for
17 ;; Commercial Software developed at private expense as specified in
18 ;; DOD FAR Supplement 52.227-7013 (c) (1) (ii), as applicable.
19 ;;
20 ;; Original version from ACL 6.1:
21 ;; t-uri.cl,v 1.3.6.3.2.1 2001/08/09 17:42:43 layer
22 ;;
23 ;; $Id$
24
25
26 (defpackage #:puri-tests (:use #:puri #:cl #:ptester))
27 (in-package #:puri-tests)
28
29 (unintern-uri t)
30
31 (defmacro gen-test-forms ()
32   (let ((res '())
33         (base-uri "http://a/b/c/d;p?q"))
34
35     (dolist (x `(;; (relative-uri result base-uri compare-function)
36 ;;;; RFC Appendix C.1 (normal examples)
37                  ("g:h" "g:h" ,base-uri)
38                  ("g" "http://a/b/c/g" ,base-uri)
39                  ("./g" "http://a/b/c/g" ,base-uri)
40                  ("g/" "http://a/b/c/g/" ,base-uri)
41                  ("/g" "http://a/g" ,base-uri) 
42                  ("//g" "http://g" ,base-uri) 
43                  ;; Following was changed from appendix C of RFC 2396
44                  ;; http://www.apache.org/~fielding/uri/rev-2002/issues.html#003-relative-query
45                  #-ignore ("?y" "http://a/b/c/d;p?y" ,base-uri) 
46                  #+ignore ("?y" "http://a/b/c/?y" ,base-uri) 
47                  ("g?y" "http://a/b/c/g?y" ,base-uri)
48                  ("#s" "http://a/b/c/d;p?q#s" ,base-uri) 
49                  ("g#s" "http://a/b/c/g#s" ,base-uri) 
50                  ("g?y#s" "http://a/b/c/g?y#s" ,base-uri)
51                  (";x" "http://a/b/c/;x" ,base-uri) 
52                  ("g;x" "http://a/b/c/g;x" ,base-uri) 
53                  ("g;x?y#s" "http://a/b/c/g;x?y#s" ,base-uri)
54                  ("." "http://a/b/c/" ,base-uri) 
55                  ("./" "http://a/b/c/" ,base-uri) 
56                  (".." "http://a/b/" ,base-uri) 
57                  ("../" "http://a/b/" ,base-uri)
58                  ("../g" "http://a/b/g" ,base-uri) 
59                  ("../.." "http://a/" ,base-uri) 
60                  ("../../" "http://a/" ,base-uri)
61                  ("../../g" "http://a/g" ,base-uri)
62 ;;;; RFC Appendix C.2 (abnormal examples)
63                  ("" "http://a/b/c/d;p?q" ,base-uri) 
64                  ("../../../g" "http://a/../g" ,base-uri)
65                  ("../../../../g" "http://a/../../g" ,base-uri) 
66                  ("/./g" "http://a/./g" ,base-uri) 
67                  ("/../g" "http://a/../g" ,base-uri)
68                  ("g." "http://a/b/c/g." ,base-uri) 
69                  (".g" "http://a/b/c/.g" ,base-uri) 
70                  ("g.." "http://a/b/c/g.." ,base-uri)
71                  ("..g" "http://a/b/c/..g" ,base-uri) 
72                  ("./../g" "http://a/b/g" ,base-uri) 
73                  ("./g/." "http://a/b/c/g/" ,base-uri)
74                  ("g/./h" "http://a/b/c/g/h" ,base-uri) 
75                  ("g/../h" "http://a/b/c/h" ,base-uri) 
76                  ("g;x=1/./y" "http://a/b/c/g;x=1/y" ,base-uri)
77                  ("g;x=1/../y" "http://a/b/c/y" ,base-uri) 
78                  ("g?y/./x" "http://a/b/c/g?y/./x" ,base-uri)
79                  ("g?y/../x" "http://a/b/c/g?y/../x" ,base-uri) 
80                  ("g#s/./x" "http://a/b/c/g#s/./x" ,base-uri)
81                  ("g#s/../x" "http://a/b/c/g#s/../x" ,base-uri) 
82                  ("http:g" "http:g" ,base-uri)
83
84                  ("foo/bar/baz.htm#foo"
85                   "http://a/b/foo/bar/baz.htm#foo"
86                   "http://a/b/c.htm")
87                  ("foo/bar/baz.htm#foo"
88                   "http://a/b/foo/bar/baz.htm#foo"
89                   "http://a/b/")
90                  ("foo/bar/baz.htm#foo"
91                   "http://a/foo/bar/baz.htm#foo"
92                   "http://a/b")
93                  ("foo/bar;x;y/bam.htm"
94                   "http://a/b/c/foo/bar;x;y/bam.htm"
95                   "http://a/b/c/")))
96       (push `(test (intern-uri ,(second x))
97                              (intern-uri (merge-uris (intern-uri ,(first x))
98                                                      (intern-uri ,(third x))))
99                              :test 'uri=)
100             res))
101
102 ;;;; intern tests
103     (dolist (x '(;; default port and specifying the default port are
104                  ;; supposed to compare the same:
105                  ("http://www.franz.com:80" "http://www.franz.com")
106                  ("http://www.franz.com:80" "http://www.franz.com" eq)
107                  ;; make sure they're `eq':
108                  ("http://www.franz.com:80" "http://www.franz.com" eq)
109                  ("http://www.franz.com" "http://www.franz.com" eq)
110                  ("http://www.franz.com/foo" "http://www.franz.com/foo" eq)
111                  ("http://www.franz.com/foo?bar"
112                   "http://www.franz.com/foo?bar" eq)
113                  ("http://www.franz.com/foo?bar#baz"
114                   "http://www.franz.com/foo?bar#baz" eq)
115                  ("http://WWW.FRANZ.COM" "http://www.franz.com" eq)
116                  ("http://www.FRANZ.com" "http://www.franz.com" eq)
117                  ("http://www.franz.com" "http://www.franz.com/" eq)
118                  (;; %72 is "r", %2f is "/", %3b is ";"
119                   "http://www.franz.com/ba%72%2f%3b;x;y;z/baz/"
120                   "http://www.franz.com/bar%2f%3b;x;y;z/baz/" eq)))
121       (push `(test (intern-uri ,(second x))
122                              (intern-uri ,(first x))
123               :test ',(if (third x)
124                           (third x)
125                           'uri=))
126             res))
127
128 ;;;; parsing and equivalence tests
129     (push `(test
130             (parse-uri "http://foo+bar?baz=b%26lob+bof")
131             (parse-uri (parse-uri "http://foo+bar?baz=b%26lob+bof"))
132             :test 'uri=)
133           res)
134     (push '(test
135             (parse-uri "http://www.foo.com")
136             (parse-uri (parse-uri "http://www.foo.com?")) ; allow ? at end
137             :test 'uri=)
138           res)
139     (push `(test
140             "baz=b%26lob+bof"
141             (uri-query (parse-uri "http://foo+bar?baz=b%26lob+bof"))
142             :test 'string=)
143           res)
144     (push `(test
145             "baz=b%26lob+bof%3d"
146             (uri-query (parse-uri "http://foo+bar?baz=b%26lob+bof%3d"))
147             :test 'string=)
148           res)
149     (push
150      `(test (parse-uri "xxx?%41") (parse-uri "xxx?A") :test 'uri=)
151      res)
152     (push
153      `(test "A" (uri-query (parse-uri "xxx?%41")) :test 'string=)
154      res)
155
156     (push `(test-error (parse-uri " ")
157                                  :condition-type 'uri-parse-error)
158           res)
159     (push `(test-error (parse-uri "foo ")
160                                  :condition-type 'uri-parse-error)
161           res)
162     (push `(test-error (parse-uri " foo ")
163                                  :condition-type 'uri-parse-error)
164           res)
165     (push `(test-error (parse-uri "<foo")
166                                  :condition-type 'uri-parse-error)
167           res)
168     (push `(test-error (parse-uri "foo>")
169                                  :condition-type 'uri-parse-error)
170           res)
171     (push `(test-error (parse-uri "<foo>")
172                                  :condition-type 'uri-parse-error)
173           res)
174     (push `(test-error (parse-uri "%")
175                                  :condition-type 'uri-parse-error)
176           res)
177     (push `(test-error (parse-uri "foo%xyr")
178                                  :condition-type 'uri-parse-error)
179           res)
180     (push `(test-error (parse-uri "\"foo\"")
181                                  :condition-type 'uri-parse-error)
182           res)
183     (push `(test "%20" (format nil "~a" (parse-uri "%20"))
184                            :test 'string=)
185           res)
186      (push `(test "%FF" (format nil "~a" (parse-uri "%FF"))
187                           :test 'string=)
188          res) ;Value 255 outside reserved-chars vector (128 bits)
189     (push `(test "&" (format nil "~a" (parse-uri "%26"))
190                            :test 'string=)
191           res)
192     (push
193      `(test "foo%23bar" (format nil "~a" (parse-uri "foo%23bar"))
194                       :test 'string=)
195      res)
196     (push
197      `(test "foo%23bar#foobar"
198                       (format nil "~a" (parse-uri "foo%23bar#foobar"))
199                       :test 'string=)
200      res)
201     (push
202      `(test "foo%23bar#foobar#baz"
203                       (format nil "~a" (parse-uri "foo%23bar#foobar#baz"))
204                       :test 'string=)
205      res)
206     (push
207      `(test "foo%23bar#foobar#baz"
208                       (format nil "~a" (parse-uri "foo%23bar#foobar%23baz"))
209                       :test 'string=)
210      res)
211     (push
212      `(test "foo%23bar#foobar/baz"
213                       (format nil "~a" (parse-uri "foo%23bar#foobar%2fbaz"))
214                       :test 'string=)
215      res)
216     (push `(test-error (parse-uri "foobar??")
217                                  :condition-type 'uri-parse-error)
218           res)
219     (push `(test-error (parse-uri "foobar?foo?")
220                                  :condition-type 'uri-parse-error)
221           res)
222     (push `(test "foobar?%3f"
223                            (format nil "~a" (parse-uri "foobar?%3f"))
224                            :test 'string=)
225           res)
226     (push `(test
227             "http://foo/bAr;3/baz?baf=3"
228             (format nil "~a" (parse-uri "http://foo/b%41r;3/baz?baf=3"))
229             :test 'string=)
230           res)
231     (push `(test
232             '(:absolute ("/bAr" "3") "baz")
233             (uri-parsed-path (parse-uri "http://foo/%2fb%41r;3/baz?baf=3"))
234             :test 'equal)
235           res)
236     (push `(test
237             "/%2fbAr;3/baz"
238             (let ((u (parse-uri "http://foo/%2fb%41r;3/baz?baf=3")))
239               (setf (uri-parsed-path u) '(:absolute ("/bAr" "3") "baz"))
240               (uri-path u))
241             :test 'string=)
242           res)
243     (push `(test
244             "http://www.verada.com:8010/kapow?name=foo%3Dbar%25"
245             (format nil "~a"
246                     (parse-uri
247                      "http://www.verada.com:8010/kapow?name=foo%3Dbar%25"))
248             :test 'string=)
249           res)
250     (push `(test
251             "ftp://parcftp.xerox.com/pub/pcl/mop/"
252             (format nil "~a"
253                     (parse-uri "ftp://parcftp.xerox.com:/pub/pcl/mop/"))
254             :test 'string=)
255           res)
256
257 ;;;; enough-uri tests
258     (dolist (x `(("http://www.franz.com/foo/bar/baz.htm"
259                   "http://www.franz.com/foo/bar/"
260                   "baz.htm")
261                  ("http://www.franz.com/foo/bar/baz.htm"
262                   "http://www.franz.com/foo/bar"
263                   "baz.htm")
264                  ("http://www.franz.com:80/foo/bar/baz.htm"
265                   "http://www.franz.com:80/foo/bar"
266                   "baz.htm")
267                  ("http:/foo/bar/baz.htm" "http:/foo/bar"  "baz.htm")
268                  ("http:/foo/bar/baz.htm" "http:/foo/bar/" "baz.htm")
269                  ("/foo/bar/baz.htm" "/foo/bar"  "baz.htm")
270                  ("/foo/bar/baz.htm" "/foo/bar/" "baz.htm")
271                  ("/foo/bar/baz.htm#foo" "/foo/bar/" "baz.htm#foo")
272                  ("/foo/bar/baz.htm?bar#foo" "/foo/bar/" "baz.htm?bar#foo")
273                  
274                  ("http://www.dnai.com/~layer/foo.htm"
275                   "http://www.known.net"
276                   "http://www.dnai.com/~layer/foo.htm")
277                  ("http://www.dnai.com/~layer/foo.htm"
278                   "http://www.dnai.com:8000/~layer/"
279                   "http://www.dnai.com/~layer/foo.htm")
280                  ("http://www.dnai.com:8000/~layer/foo.htm"
281                   "http://www.dnai.com/~layer/"
282                   "http://www.dnai.com:8000/~layer/foo.htm")
283                  ("http://www.franz.com"
284                   "http://www.franz.com"
285                   "/")))
286       (push `(test (parse-uri ,(third x))
287                              (enough-uri (parse-uri ,(first x))
288                                          (parse-uri ,(second x)))
289                              :test 'uri=)
290             res))
291     
292 ;;;; urn tests, ideas of which are from rfc2141
293     (let ((urn "urn:com:foo-the-bar"))
294       (push `(test "com" (urn-nid (parse-uri ,urn))
295                              :test #'string=)
296             res)
297       (push `(test "foo-the-bar" (urn-nss (parse-uri ,urn))
298                              :test #'string=)
299             res))
300     (push `(test-error (parse-uri "urn:")
301                                  :condition-type 'uri-parse-error)
302           res)
303     (push `(test-error (parse-uri "urn:foo")
304                                  :condition-type 'uri-parse-error)
305           res)
306     (push `(test-error (parse-uri "urn:foo$")
307                                  :condition-type 'uri-parse-error)
308           res)
309     (push `(test-error (parse-uri "urn:foo_")
310                                  :condition-type 'uri-parse-error)
311           res)
312     (push `(test-error (parse-uri "urn:foo:foo&bar")
313                                  :condition-type 'uri-parse-error)
314           res)
315     (push `(test (parse-uri "URN:foo:a123,456")
316                            (parse-uri "urn:foo:a123,456")
317                            :test #'uri=)
318           res)
319     (push `(test (parse-uri "URN:foo:a123,456")
320                            (parse-uri "urn:FOO:a123,456")
321                            :test #'uri=)
322           res)
323     (push `(test (parse-uri "urn:foo:a123,456")
324                            (parse-uri "urn:FOO:a123,456")
325                            :test #'uri=)
326           res)
327     (push `(test (parse-uri "URN:FOO:a123%2c456")
328                            (parse-uri "urn:foo:a123%2C456")
329                            :test #'uri=)
330           res)
331     (push `(test
332             nil
333             (uri= (parse-uri "urn:foo:A123,456")
334                   (parse-uri "urn:FOO:a123,456")))
335           res)
336     (push `(test
337             nil
338             (uri= (parse-uri "urn:foo:A123,456")
339                   (parse-uri "urn:foo:a123,456")))
340           res)
341     (push `(test
342             nil
343             (uri= (parse-uri "urn:foo:A123,456")
344                   (parse-uri "URN:foo:a123,456")))
345           res)
346     (push `(test
347             nil
348             (uri= (parse-uri "urn:foo:a123%2C456")
349                   (parse-uri "urn:FOO:a123,456")))
350           res)
351     (push `(test
352             nil
353             (uri= (parse-uri "urn:foo:a123%2C456")
354                   (parse-uri "urn:foo:a123,456")))
355           res)
356     (push `(test
357             nil
358             (uri= (parse-uri "URN:FOO:a123%2c456")
359                   (parse-uri "urn:foo:a123,456")))
360           res)
361     (push `(test
362             nil
363             (uri= (parse-uri "urn:FOO:a123%2c456")
364                   (parse-uri "urn:foo:a123,456")))
365           res)
366     (push `(test
367             nil
368             (uri= (parse-uri "urn:foo:a123%2c456")
369                   (parse-uri "urn:foo:a123,456")))
370           res)
371     
372     (push `(test t
373                            (uri= (parse-uri "foo") (parse-uri "foo#")))
374           res)
375     
376     (push
377      '(let ((puri::*strict-parse* nil))
378        (test-no-error
379         (puri:parse-uri
380          "http://foo.com/bar?a=zip|zop")))
381      res)
382     (push
383      '(test-error
384        (puri:parse-uri "http://foo.com/bar?a=zip|zop")
385        :condition-type 'uri-parse-error)
386      res)
387     
388     (push
389      '(let ((puri::*strict-parse* nil))
390        (test-no-error
391         (puri:parse-uri
392          "http://arc3.msn.com/ADSAdClient31.dll?GetAd?PG=NBCSBU?SC=D2?AN=1.0586041")))
393      res)
394     (push
395      '(test-error
396        (puri:parse-uri
397         "http://arc3.msn.com/ADSAdClient31.dll?GetAd?PG=NBCSBU?SC=D2?AN=1.0586041")
398        :condition-type 'uri-parse-error)
399      res)
400     
401     (push
402      '(let ((puri::*strict-parse* nil))
403        (test-no-error
404         (puri:parse-uri
405          "http://scbc.booksonline.com/cgi-bin/ndCGI.exe/Develop/pagClubHome.hrfTIOLI_onWebEvent(hrfTIOLI)?selGetClubOffer.TB_OFFER_ID_OFFER=344879%2e0&selGetClubOffer.TB_OFFER_ID_ITEM=34487%2e0&selGetClubOffer.TB_OFFER_ID_OFFER=344879%2e0&^CSpCommand.currRowNumber=5&hrfTIOLI=The+Visual+Basic+6+Programmer%27s+Toolkit&SPIDERSESSION=%3f%3f%3f%3f%3f%5f%3f%3f%3f%40%5b%3f%3f%3f%3fBOs%5cH%3f%3f%3f%3f%3f%3f%3f%3f%3fMMpXO%5f%40JG%7d%40%5c%5f%3f%3f%3fECK%5dt%3fLDT%3fTBD%3fDDTxPEToBS%40%5f%5dBDgXVoKBSDs%7cDT%3fK%3fd%3fTIb%7ceHbkeMfh%60LRpO%5cact%5eUC%7bMu%5fyWUGzLUhP%5ebpdWRka%5dFO%3f%5dBopW%3f%40HMrxbMRd%60LOpuMVga%3fv%3fTS%3fpODT%40O&%5euniqueValue=977933764843")))
406      res)
407     (push
408      '(test-error
409        (puri:parse-uri
410         "http://scbc.booksonline.com/cgi-bin/ndCGI.exe/Develop/pagClubHome.hrfTIOLI_onWebEvent(hrfTIOLI)?selGetClubOffer.TB_OFFER_ID_OFFER=344879%2e0&selGetClubOffer.TB_OFFER_ID_ITEM=34487%2e0&selGetClubOffer.TB_OFFER_ID_OFFER=344879%2e0&^CSpCommand.currRowNumber=5&hrfTIOLI=The+Visual+Basic+6+Programmer%27s+Toolkit&SPIDERSESSION=%3f%3f%3f%3f%3f%5f%3f%3f%3f%40%5b%3f%3f%3f%3fBOs%5cH%3f%3f%3f%3f%3f%3f%3f%3f%3fMMpXO%5f%40JG%7d%40%5c%5f%3f%3f%3fECK%5dt%3fLDT%3fTBD%3fDDTxPEToBS%40%5f%5dBDgXVoKBSDs%7cDT%3fK%3fd%3fTIb%7ceHbkeMfh%60LRpO%5cact%5eUC%7bMu%5fyWUGzLUhP%5ebpdWRka%5dFO%3f%5dBopW%3f%40HMrxbMRd%60LOpuMVga%3fv%3fTS%3fpODT%40O&%5euniqueValue=977933764843")
411        :condition-type 'uri-parse-error)
412      res)
413     
414     `(progn ,@(nreverse res))))
415
416 (defun do-tests ()
417   (let ((*break-on-test-failures* t))
418     (with-tests (:name "puri")
419       (gen-test-forms)))
420   t)
421
422