summaryrefslogtreecommitdiff
path: root/third-party/s-xml-rpc/src/validator1-client.lisp
blob: 8800671c9bebd373ed70f55f007bbfe69d17e485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: validator1-client.lisp,v 1.1 2004-06-14 20:11:55 scaekenberghe Exp $
;;;;
;;;; This is a Common Lisp implementation of the XML-RPC 'validator1'
;;;; server test suite, as live testable from the website
;;;; http://validator.xmlrpc.com and documented on the web page
;;;; http://www.xmlrpc.com/validator1Docs
;;;;
;;;; Copyright (C) 2002, 2004 Sven Van Caekenberghe, Beta Nine BVBA.
;;;;
;;;; You are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser General Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.

(in-package :s-xml-rpc)

(defun random-string (&optional (length 8))
  (with-output-to-string (stream)
    (dotimes (i (random length))
      (write-char (code-char (+ 32 (random 95)))
		  stream))))

(defun echo-struct-test ()
  (let* ((struct (xml-rpc-struct :|foo| (random 1000000)
				 :|bar| (random-string)
				 :|fooBar| (list (random 100) (random 100))))
	 (result (xml-rpc-call (encode-xml-rpc-call :|validator1.echoStructTest|
						    struct))))
    (format t "validator1.echoStructTest(~s)=~s~%" struct result)
    (assert (xml-rpc-struct-equal struct result))))
	     
(defun easy-struct-test ()
  (let* ((moe (random 1000))
	 (larry (random 1000))
	 (curry (random 1000))
	 (struct (xml-rpc-struct :|moe| moe
				 :|larry| larry
				 :|curly| curry))
	 (result (xml-rpc-call (encode-xml-rpc-call :|validator1.easyStructTest|
						    struct))))
    (format t "validator1.easyStructTest(~s)=~s~%" struct result) 
    (assert (= (+ moe larry curry) result))))

(defun count-the-entities ()
  (let* ((string (random-string 512))
	 (left-angle-brackets (count #\< string))
	 (right-angle-brackets (count #\> string))
	 (apostrophes (count #\' string))
	 (quotes (count #\" string))
	 (ampersands (count #\& string))
	 (result (xml-rpc-call (encode-xml-rpc-call :|validator1.countTheEntities|
						    string))))
    (format t "validator1.countTheEntitities(~s)=~s~%" string result)
    (assert
     (and (xml-rpc-struct-p result)
	  (= left-angle-brackets
	     (get-xml-rpc-struct-member result :|ctLeftAngleBrackets|))
	  (= right-angle-brackets
	     (get-xml-rpc-struct-member result :|ctRightAngleBrackets|))
	  (= apostrophes
	     (get-xml-rpc-struct-member result :|ctApostrophes|))
	  (= quotes
	     (get-xml-rpc-struct-member result :|ctQuotes|))
	  (= ampersands
	     (get-xml-rpc-struct-member result :|ctAmpersands|))))))

(defun array-of-structs-test ()
  (let ((array (make-array (random 32)))
	(sum 0))
    (dotimes (i (length array))
      (setf (aref array i)
	    (xml-rpc-struct :|moe| (random 1000)
			    :|larry| (random 1000)
			    :|curly| (random 1000)))
      (incf sum (get-xml-rpc-struct-member (aref array i)
					   :|curly|)))
    (let ((result (xml-rpc-call (encode-xml-rpc-call :|validator1.arrayOfStructsTest|
						     array))))
      (format t "validator1.arrayOfStructsTest(~s)=~s~%" array result)
      (assert (= result sum)))))

(defun random-bytes (&optional (length 16))
  (let ((bytes (make-array (random length) :element-type '(unsigned-byte 8))))
    (dotimes (i (length bytes) bytes)
      (setf (aref bytes i) (random 256)))))

(defun many-types-test ()
  (let* ((integer (random 10000))
	 (boolean (if (zerop (random 2)) t nil))
	 (string (random-string))
	 (double (random 10000.0))
	 (dateTime (xml-rpc-time))
	 (base64 (random-bytes))
	 (result (xml-rpc-call (encode-xml-rpc-call :|validator1.manyTypesTest|
						    integer
						    boolean
						    string
						    double
						    dateTime
						    base64))))
    (format t
	    "validator1.manyTypesTest(~s,~s,~s,~s,~s,~s)=~s~%"
	    integer
	    boolean
	    string
	    double
	    dateTime
	    base64
	    result)
    (assert (equal integer (elt result 0)))
    (assert (equal boolean (elt result 1)))
    (assert (equal string (elt result 2)))
    (assert (equal double (elt result 3)))
    (assert (equal (xml-rpc-time-universal-time dateTime)
		   (xml-rpc-time-universal-time (elt result 4))))
    (assert (reduce #'(lambda (x y) (and x y))
		    (map 'list #'= base64 (elt result 5))
		    :initial-value t))))

(defun simple-struct-return-test ()
  (let* ((number (random 1000))
	 (result (xml-rpc-call (encode-xml-rpc-call :|validator1.simpleStructReturnTest| number))))
    (format t "validator1.simpleStructReturnTest(~s)=~s~%" number result)
    (assert
     (and (= (* number 10)
	     (get-xml-rpc-struct-member result :|times10|))
	  (= (* number 100)
	     (get-xml-rpc-struct-member result :|times100|))
	  (= (* number 1000)
	     (get-xml-rpc-struct-member result :|times1000|))))))

(defun moderate-size-array-check ()
  (let ((array (make-array (+ 100 (random 100))
			   :element-type 'string)))
    (dotimes (i (length array))
      (setf (aref array i) (random-string)))
    (let ((result (xml-rpc-call (encode-xml-rpc-call :|validator1.moderateSizeArrayCheck|
						     array))))
      (format t "validator1.moderateSizeArrayCheck(~s)=~s~%" array result)
      (assert
       (equal (concatenate 'string (elt array 0) (elt array (1- (length array))))
	      result)))))

(defun nested-struct-test ()
  (let* ((moe (random 1000))
	 (larry (random 1000))
	 (curry (random 1000))
	 (struct (xml-rpc-struct :|moe| moe
				 :|larry| larry
				 :|curly| curry))
	 (first (xml-rpc-struct :\01 struct))
	 (april (xml-rpc-struct :\04 first))
	 (year (xml-rpc-struct :\2000 april))
	 (result (xml-rpc-call (encode-xml-rpc-call :|validator1.nestedStructTest|
						    year))))
    (format t "validator1.nestedStructTest(~s)=~s~%" year result) 
    (assert (= (+ moe larry curry) result))))

(defun test-run (&optional (runs 1))
  (dotimes (i runs t)
    (echo-struct-test)
    (easy-struct-test)
    (count-the-entities)
    (array-of-structs-test)
    (many-types-test)
    (simple-struct-return-test)
    (moderate-size-array-check)
    (nested-struct-test)))

(defun timed-test-run (&optional (runs 1))
  (dotimes (i runs t)
    (time (echo-struct-test))
    (time (easy-struct-test))
    (time (count-the-entities))
    (time (array-of-structs-test))
    (time (many-types-test))
    (time (simple-struct-return-test))
    (time (moderate-size-array-check))
    (time (nested-struct-test))))

;;;; eof