Commit | Line | Data |
---|---|---|
970ed795 | 1 | /****************************************************************************** |
3abe9331 | 2 | * Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | ******************************************************************************/ | |
8 | module Attribute | |
9 | { | |
10 | modulepar boolean Attribute_verbose := false; | |
11 | #define verbose Attribute_verbose | |
12 | #include "../macros.ttcnin" | |
13 | ||
14 | type component ATR {} | |
15 | ||
16 | type charstring Color | |
17 | with { | |
18 | variant "attribute" | |
19 | } | |
20 | ||
21 | type record ProductA { | |
22 | Color color, | |
23 | charstring material, | |
24 | charstring size, | |
25 | universal charstring size2, | |
26 | charstring name, | |
27 | integer price | |
28 | } | |
29 | with { | |
30 | variant (color) "attribute" // it is not enough that Color has "attribute" | |
31 | variant (material) "attribute" | |
32 | variant (size) "attribute" | |
33 | variant (size2) "attribute" | |
34 | } | |
35 | ||
36 | DECLARE_XER_ENCODERS (ProductA, prodA); | |
37 | DECLARE_EXER_ENCODERS(ProductA, prodA); | |
38 | ||
39 | // Decoder function which treats ET_INVAL_MSG as a warning. | |
40 | // Everything else is still a dynamic testcase error. | |
41 | external function exer_dec_prodA_expect_inval(in octetstring o, out ProductA pa) return integer | |
3abe9331 | 42 | with { extension "prototype(backtrack) decode(XER:XER_EXTENDED) errorbehavior(INVAL_MSG:WARNING,INCOMPL_MSG:WARNING)" } |
970ed795 EL |
43 | |
44 | ||
45 | const ProductA shoes := { | |
46 | color := "blue", | |
47 | material := "suede", | |
48 | size := "1'2\"", | |
49 | size2:= "1'2\"", | |
50 | name := "Shoes", | |
51 | price:= 25 | |
52 | } | |
53 | ||
54 | const universal charstring str_shoes_e := | |
55 | "<ProductA color=\'blue\' material=\'suede\' size='1'2"' size2='1'2"'>\n" & | |
56 | "\t<name>Shoes</name>\n" & | |
57 | "\t<price>25</price>\n" & | |
58 | "</ProductA>\n" & | |
59 | "\n"; | |
60 | ||
61 | const universal charstring str_shoes_nocolor := | |
62 | "<ProductA material=\'suede\' size='1'2\"' size2='1'2\"'>\n" & | |
63 | "\t<name>Shoes</name>\n" & | |
64 | "\t<price>25</price>\n" & | |
65 | "</ProductA>\n" & | |
66 | "\n"; | |
67 | ||
68 | const universal charstring str_shoes_nosize := | |
69 | "<ProductA color=\'blue\' material=\'suede\' size2='1'2\"'>\n" & | |
70 | "\t<name>Shoes</name>\n" & | |
71 | "\t<price>25</price>\n" & | |
72 | "</ProductA>\n" & | |
73 | "\n"; | |
74 | ||
75 | const universal charstring str_shoes_nosize2 := | |
76 | "<ProductA color=\'blue\' material=\'suede\' size='1'2\"'>\n" & | |
77 | "\t<name>Shoes</name>\n" & | |
78 | "\t<price>25</price>\n" & | |
79 | "</ProductA>\n" & | |
80 | "\n"; | |
81 | ||
82 | const universal charstring str_shoes_extra := | |
83 | "<ProductA color=\'blue\' speed=\'high\' material=\'suede\' size='1'2\"' size2='1'2\"'>\n" & | |
84 | "\t<name>Shoes</name>\n" & | |
85 | "\t<price>25</price>\n" & | |
86 | "</ProductA>\n" & | |
87 | "\n"; | |
88 | ||
89 | const universal charstring str_shoes_extra2 := | |
90 | "<ProductA color=\'blue\' super:speed=\'high\' material=\'suede\' size='1'2\"' size2='1'2\"' " & | |
91 | "xmlns:super='duper'>\n" & | |
92 | "\t<name>Shoes</name>\n" & | |
93 | "\t<price>25</price>\n" & | |
94 | "</ProductA>\n" & | |
95 | "\n"; | |
96 | ||
97 | const universal charstring str_shoes_e_jumbled := | |
98 | "<ProductA material=\'suede\' color=\'blue\' size='1'2\"' size2='1'2\"'>\n" & | |
99 | "\t<name>Shoes</name>\n" & | |
100 | "\t<price>25</price>\n" & | |
101 | "</ProductA>\n" & | |
102 | "\n"; | |
103 | ||
104 | const universal charstring str_shoes_b := | |
105 | "<ProductA>\n" & | |
106 | "\t<color>blue</color>\n" & | |
107 | "\t<material>suede</material>\n" & | |
108 | "\t<size>1'2"</size>\n" & | |
109 | "\t<size2>1'2"</size2>\n" & | |
110 | "\t<name>Shoes</name>\n" & | |
111 | "\t<price>25</price>\n" & | |
112 | "</ProductA>\n" & | |
113 | "\n"; | |
114 | ||
115 | // Test the escaping of TAB, CR, LF in attributes: X.693 (11/2008), clause 20.3.15 | |
116 | const ProductA nutrimatic := { | |
117 | color := "black", | |
118 | material := "unknown", | |
119 | size := | |
120 | "When the 'Drink' button is pressed it makes an instant but highly detailed" & | |
121 | "\texamination of the subject's taste buds,\r\n" & | |
122 | "a spectroscopic analysis of the subject's metabolism,\r\n" & | |
123 | "and then sends tiny experimental signals down the neural pathways" & | |
124 | "\tto the taste centres of the subject's brain to see what is likely to be well received.", | |
125 | size2 := | |
126 | "\tHowever, no-one knows quite why it does this because it then invariably delivers\r\n" & | |
127 | "a cupful of liquid that is almost, but not quite, entirely unlike tea.\r\n", | |
128 | name := "Nutrimatic Drinks Dispenser", | |
129 | price := 42 | |
130 | } | |
131 | ||
132 | const universal charstring str_nutrimatic_e := | |
133 | "<ProductA color='black' material='unknown' " & | |
134 | ||
135 | "size='When the 'Drink' button is pressed it makes an instant but highly detailed" & | |
136 | "	examination of the subject's taste buds,
" & | |
137 | "a spectroscopic analysis of the subject's metabolism,
" & | |
138 | "and then sends tiny experimental signals down the neural pathways" & | |
139 | "	to the taste centres of the subject's brain to see what is likely to be well received.' " & | |
140 | ||
141 | "size2='	However, no-one knows quite why it does this because it then invariably delivers
" & | |
142 | "a cupful of liquid that is almost, but not quite, entirely unlike tea.
'>\n" & | |
143 | ||
144 | "\t<name>Nutrimatic Drinks Dispenser</name>\n" & | |
145 | "\t<price>42</price>\n" & | |
146 | "</ProductA>\n\n"; | |
147 | ||
148 | const universal charstring str_nutrimatic_b := | |
149 | "<ProductA>\n" & | |
150 | "\t<color>black</color>\n" & | |
151 | "\t<material>unknown</material>\n" & | |
152 | ||
153 | "\t<size>When the 'Drink' button is pressed it makes an instant but highly detailed" & | |
154 | "\texamination of the subject's taste buds,\r\n" & | |
155 | "a spectroscopic analysis of the subject's metabolism,\r\n" & | |
156 | "and then sends tiny experimental signals down the neural pathways" & | |
157 | "\tto the taste centres of the subject's brain to see what is likely to be well received.</size>\n" & | |
158 | ||
159 | "\t<size2>\tHowever, no-one knows quite why it does this because it then invariably delivers\r\n" & | |
160 | "a cupful of liquid that is almost, but not quite, entirely unlike tea.\r\n</size2>\n" & | |
161 | ||
162 | "\t<name>Nutrimatic Drinks Dispenser</name>\n" & | |
163 | "\t<price>42</price>\n" & | |
164 | "</ProductA>\n\n"; | |
165 | ||
166 | // The result from BXER decoding. | |
167 | // All combinations of \r, \n, \r\n are reduced to \n by libxml | |
168 | const ProductA nutrimatic_bxer := { | |
169 | color := "black", | |
170 | material := "unknown", | |
171 | size := | |
172 | "When the 'Drink' button is pressed it makes an instant but highly detailed" & | |
173 | "\texamination of the subject's taste buds,\n" & | |
174 | "a spectroscopic analysis of the subject's metabolism,\n" & | |
175 | "and then sends tiny experimental signals down the neural pathways" & | |
176 | "\tto the taste centres of the subject's brain to see what is likely to be well received.", | |
177 | size2 := | |
178 | "\tHowever, no-one knows quite why it does this because it then invariably delivers\n" & | |
179 | "a cupful of liquid that is almost, but not quite, entirely unlike tea.\n", | |
180 | name := "Nutrimatic Drinks Dispenser", | |
181 | price := 42 | |
182 | } | |
183 | ||
184 | const ProductA empty := { | |
185 | color := "", | |
186 | material := "antimatter", | |
187 | size := "", | |
188 | size2 := "", | |
189 | name := "", | |
190 | price := 0 | |
191 | } | |
192 | ||
193 | const universal charstring str_empty_e := | |
194 | "<ProductA color=\'\' material=\'antimatter\' size='' size2=''>\n" & | |
195 | "\t<name/>\n" & | |
196 | "\t<price>0</price>\n" & | |
197 | "</ProductA>\n" & | |
198 | "\n"; | |
199 | ||
200 | const universal charstring str_empty_e_jumbled := | |
201 | "<ProductA material=\'antimatter\' color=\'\' size='' size2=''>\n" & | |
202 | "\t<name/>\n" & | |
203 | "\t<price>0</price>\n" & | |
204 | "</ProductA>\n" & | |
205 | "\n"; | |
206 | ||
207 | const universal charstring str_empty_b := | |
208 | "<ProductA>\n" & | |
209 | "\t<color/>\n" & | |
210 | "\t<material>antimatter</material>\n" & | |
211 | "\t<size/>\n" & | |
212 | "\t<size2/>\n" & | |
213 | "\t<name/>\n" & | |
214 | "\t<price>0</price>\n" & | |
215 | "</ProductA>\n" & | |
216 | "\n"; | |
217 | ||
218 | testcase enc_atr() runs on ATR | |
219 | { | |
220 | CHECK_METHOD(bxer_enc_prodA, shoes, str_shoes_b); | |
221 | CHECK_METHOD(exer_enc_prodA, shoes, str_shoes_e); | |
222 | ||
223 | CHECK_METHOD(bxer_enc_prodA, empty, str_empty_b); | |
224 | CHECK_METHOD(exer_enc_prodA, empty, str_empty_e); | |
225 | ||
226 | CHECK_METHOD(bxer_enc_prodA, nutrimatic, str_nutrimatic_b); | |
227 | CHECK_METHOD(exer_enc_prodA, nutrimatic, str_nutrimatic_e); | |
228 | } | |
229 | ||
230 | testcase dec_atr() runs on ATR | |
231 | { | |
232 | CHECK_DECODE(bxer_dec_prodA, str_shoes_b, ProductA, shoes); | |
233 | CHECK_DECODE(exer_dec_prodA, str_shoes_e, ProductA, shoes); | |
234 | // check that attributes are accepted in any order | |
235 | CHECK_DECODE(exer_dec_prodA, str_shoes_e_jumbled, ProductA, shoes); | |
236 | ||
237 | // Decoding bogus attribute. Should be DTE instead of segfault. | |
238 | // CHECK_DECODE(exer_dec_prodA, str_shoes_extra, ProductA, shoes); | |
239 | CHECK_DECODE_FAIL_AND_COMPARE(exer_dec_prodA_expect_inval, str_shoes_extra, ProductA, shoes); | |
240 | CHECK_DECODE_FAIL_AND_COMPARE(exer_dec_prodA_expect_inval, str_shoes_extra2, ProductA, shoes); | |
241 | ||
242 | // Decoding not enough attributes | |
243 | CHECK_DECODE_FAIL(exer_dec_prodA_expect_inval, str_shoes_nocolor, ProductA); | |
244 | CHECK_DECODE_FAIL(exer_dec_prodA_expect_inval, str_shoes_nosize , ProductA); | |
245 | CHECK_DECODE_FAIL(exer_dec_prodA_expect_inval, str_shoes_nosize2, ProductA); | |
246 | ||
247 | CHECK_DECODE(bxer_dec_prodA, str_empty_b, ProductA, empty); | |
248 | CHECK_DECODE(exer_dec_prodA, str_empty_e, ProductA, empty); | |
249 | // check that attributes are accepted in any order | |
250 | CHECK_DECODE(exer_dec_prodA, str_empty_e_jumbled, ProductA, empty); | |
251 | ||
252 | CHECK_DECODE(bxer_dec_prodA, str_nutrimatic_b, ProductA, nutrimatic_bxer); | |
253 | CHECK_DECODE(exer_dec_prodA, str_nutrimatic_e, ProductA, nutrimatic); | |
254 | } | |
255 | ||
256 | /* | |
257 | Attributes must be at front; mixture is not allowed | |
258 | ||
259 | type record mix { | |
260 | integer yes, | |
261 | integer no, | |
262 | integer maybe | |
263 | } | |
264 | with { | |
265 | variant (yes) "attribute"; | |
266 | variant (maybe) "attribute"; | |
267 | } | |
268 | */ | |
269 | ||
270 | type record NSProduct { | |
271 | Color color, | |
272 | charstring material, | |
273 | charstring size, | |
274 | universal charstring size2, | |
275 | charstring name, | |
276 | integer price | |
277 | } | |
278 | with { | |
279 | variant (color) "attribute" // it is not enough that Color has "attribute" | |
280 | variant (material) "attribute" | |
281 | variant (material) "namespace as 'foo:bar:baz' prefix 'raw' " | |
282 | variant (material) "form as qualified"; | |
283 | variant (size) "attribute" | |
284 | variant (size2) "attribute" | |
285 | } | |
286 | ||
287 | /* | |
288 | type ProductA NSProductWTF | |
289 | with { | |
290 | variant (materisl) "namespace as 'foo:bar:baz' prefix 'raw' " | |
291 | // Uh-oh, this should not compile | |
292 | } | |
293 | */ | |
294 | ||
295 | DECLARE_XER_ENCODERS (NSProduct, nsprod); | |
296 | DECLARE_EXER_ENCODERS(NSProduct, nsprod); | |
297 | ||
298 | const NSProduct nsshoes := { | |
299 | color := "black", | |
300 | material := "lac", | |
301 | size := "1'7\"", | |
302 | size2:= "1'9\"", | |
303 | name := "Shoes", | |
304 | price:= 200 | |
305 | } | |
306 | ||
307 | const universal charstring str_nsshoes_e := | |
308 | "<NSProduct xmlns:raw='foo:bar:baz' color=\'black\' raw:material=\'lac\' size='1'7"' size2='1'9"'>\n" & | |
309 | "\t<name>Shoes</name>\n" & | |
310 | "\t<price>200</price>\n" & | |
311 | "</NSProduct>\n" & | |
312 | "\n"; | |
313 | ||
314 | const universal charstring str_nsshoes_e_jumbled := | |
315 | "<NSProduct xmlns:raw='foo:bar:baz' raw:material=\'lac\' color=\'black\' size='1'7"' size2='1'9"'>\n" & | |
316 | "\t<name>Shoes</name>\n" & | |
317 | "\t<price>200</price>\n" & | |
318 | "</NSProduct>\n" & | |
319 | "\n"; | |
320 | ||
321 | const universal charstring str_nsshoes_b := | |
322 | "<NSProduct>\n" & | |
323 | "\t<color>black</color>\n" & | |
324 | "\t<material>lac</material>\n" & | |
325 | "\t<size>1'7"</size>\n" & | |
326 | "\t<size2>1'9"</size2>\n" & | |
327 | "\t<name>Shoes</name>\n" & | |
328 | "\t<price>200</price>\n" & | |
329 | "</NSProduct>\n" & | |
330 | "\n"; | |
331 | ||
332 | ||
333 | testcase enc_atrns() runs on ATR | |
334 | { | |
335 | CHECK_METHOD(bxer_enc_nsprod, nsshoes, str_nsshoes_b); | |
336 | CHECK_METHOD(exer_enc_nsprod, nsshoes, str_nsshoes_e); | |
337 | } | |
338 | ||
339 | testcase dec_atrns() runs on ATR | |
340 | { | |
341 | CHECK_DECODE(bxer_dec_nsprod, str_nsshoes_b, NSProduct, nsshoes); | |
342 | CHECK_DECODE(exer_dec_nsprod, str_nsshoes_e, NSProduct, nsshoes); | |
343 | // check that attributes are accepted in any order | |
344 | CHECK_DECODE(exer_dec_nsprod, str_nsshoes_e_jumbled, NSProduct, nsshoes); | |
345 | } | |
346 | ||
347 | group HL19919 { | |
348 | ||
349 | type record Window { | |
350 | float height optional, | |
351 | record of union { | |
352 | boolean truthiness, | |
353 | integer length_ | |
354 | } choice_list | |
355 | } | |
356 | with { | |
357 | variant /* Window */ "element"; // otherwise form=unqualified | |
358 | variant (height) "attribute"; | |
359 | variant (choice_list) "untagged"; | |
360 | variant (choice_list[-]) "untagged"; | |
361 | } | |
362 | ||
363 | external function dec_w(in octetstring o) return Window | |
364 | with { extension "prototype(convert) decode(XER:XER_EXTENDED) errorbehavior(ALL:ERROR)" } | |
365 | ||
366 | /* Regression test for HL19919. XML decoding could get stuck in one place, | |
367 | (endless loop) failing to advance when decoding attributes. */ | |
368 | testcase hl19919() runs on ATR | |
369 | { | |
370 | var octetstring input := char2oct( | |
371 | "<xul:Window xmlns:xul='there.is.only.xul' height='3.1415' >\n" & | |
372 | "</xul:Window>\n\n" | |
373 | ); | |
374 | ||
375 | var Window w := dec_w(input); | |
376 | if (isvalue(w) and w.height == 3.1415 and 0 == lengthof(w.choice_list)) | |
377 | { setverdict(pass); } | |
378 | else { setverdict(fail, w, "unbound"); } | |
379 | } | |
380 | ||
381 | } | |
382 | with { | |
383 | variant "namespace as 'there.is.only.xul' prefix 'xul' "; | |
384 | } | |
385 | ||
386 | group SchemaLocation { | |
387 | // The decoder should ignore xsi:schemaLocation attributes | |
388 | // (instead of throwing an "Unexpected attribute ..." error) | |
389 | ||
390 | type record Something { | |
391 | bitstring bs optional, | |
392 | integer num, | |
393 | charstring cs | |
394 | } with { | |
395 | variant "name as uncapitalized"; | |
396 | variant(bs) "attribute"; | |
397 | } | |
398 | ||
399 | const universal charstring c_schema_loc_str := | |
400 | "<something xmlns=\"http://www.somewhere.com\" " & | |
401 | "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " & | |
402 | "xsi:schemaLocation=\"www.thereareschemashere.com\">\n"& | |
403 | "\t<num>19</num>\n" & | |
404 | "\t<cs>nothing</cs>\n" & | |
405 | "</something>"; | |
406 | ||
407 | const Something c_schema_loc := { bs := omit, num := 19, cs := "nothing" }; | |
408 | ||
409 | DECLARE_EXER_ENCODERS(Something, smthn); | |
410 | ||
411 | testcase dec_atr_schemaloc() runs on ATR | |
412 | { | |
413 | CHECK_DECODE(exer_dec_smthn, c_schema_loc_str, Something, c_schema_loc); | |
414 | } | |
415 | ||
416 | } with { | |
417 | variant "namespace as 'http://www.somewhere.com' prefix 'smw'"; | |
418 | variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'"; | |
419 | variant "elementFormQualified"; | |
420 | } | |
421 | ||
422 | ||
423 | control { | |
424 | execute(enc_atr()); | |
425 | execute(dec_atr()); | |
426 | ||
427 | execute(enc_atrns()); | |
428 | execute(dec_atrns()); | |
429 | ||
430 | execute(hl19919(), 5.0); // too bad the timer can't save the testcase from its endless loop | |
431 | ||
432 | execute(dec_atr_schemaloc()); | |
433 | } | |
434 | ||
435 | } | |
436 | with { | |
437 | encode "XML" | |
438 | } |