| 1 | /****************************************************************************** |
| 2 | * Copyright (c) 2000-2014 Ericsson Telecom AB |
| 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 uni |
| 9 | { |
| 10 | |
| 11 | import from ReadXml all; |
| 12 | import from rec { type Neg; function check_match; }; |
| 13 | |
| 14 | type union U { |
| 15 | integer i, |
| 16 | charstring s, |
| 17 | record { |
| 18 | boolean b, |
| 19 | float f |
| 20 | } r |
| 21 | }; |
| 22 | |
| 23 | external function encU(in U u) return octetstring |
| 24 | with { extension "prototype(convert) encode(XER:XER_EXTENDED)" }; |
| 25 | |
| 26 | const U c_plain := { |
| 27 | s := "Howdy" |
| 28 | } |
| 29 | |
| 30 | template Nodes t_plain := { |
| 31 | // node type , depth, name , value |
| 32 | { XML_READER_TYPE_ELEMENT , 0, "U", "", "" }, |
| 33 | { XML_READER_TYPE_ELEMENT , 1, "s", "", "" }, |
| 34 | { XML_READER_TYPE_TEXT , 2, "#text", "Howdy", "" }, |
| 35 | { XML_READER_TYPE_END_ELEMENT, 1, "s", "", "" }, |
| 36 | { XML_READER_TYPE_END_ELEMENT, 0, "U", "", "" } |
| 37 | } |
| 38 | |
| 39 | type octetstring myoctetstring; |
| 40 | |
| 41 | const U c_replace_other := { |
| 42 | s := "Howdy" |
| 43 | } |
| 44 | with { |
| 45 | // Replacing i when s is selected: NOP |
| 46 | erroneous (i) "value := 'BABEFACE'O " |
| 47 | } |
| 48 | |
| 49 | const U c_plain_r := { |
| 50 | r := { true, 3.141592 } |
| 51 | } |
| 52 | |
| 53 | template Nodes t_plain_r := { |
| 54 | // node type , depth, name , value |
| 55 | { XML_READER_TYPE_ELEMENT , 0, "U", "", "" }, |
| 56 | { XML_READER_TYPE_ELEMENT , 1, "r", "", "" }, |
| 57 | |
| 58 | { XML_READER_TYPE_ELEMENT , 2, "b", "", "" }, |
| 59 | { XML_READER_TYPE_TEXT , 3, "#text", "true", "" }, |
| 60 | { XML_READER_TYPE_END_ELEMENT, 2, "b", "", "" }, |
| 61 | |
| 62 | { XML_READER_TYPE_ELEMENT , 2, "f", "", "" }, |
| 63 | { XML_READER_TYPE_TEXT , 3, "#text", "3.141592", "" }, |
| 64 | { XML_READER_TYPE_END_ELEMENT, 2, "f", "", "" }, |
| 65 | |
| 66 | { XML_READER_TYPE_END_ELEMENT, 1, "r", "", "" }, |
| 67 | { XML_READER_TYPE_END_ELEMENT, 0, "U", "", "" } |
| 68 | } |
| 69 | |
| 70 | testcase u_plain() runs on Neg |
| 71 | { |
| 72 | var octetstring o; |
| 73 | var Nodes nodes; |
| 74 | |
| 75 | o := encU(c_plain); |
| 76 | nodes := gather(o, ignore_ws); |
| 77 | check_match(nodes, t_plain); |
| 78 | |
| 79 | o := encU(c_replace_other); |
| 80 | nodes := gather(o, ignore_ws); |
| 81 | // Result unaffected by modifying a non-selected alternative |
| 82 | check_match(nodes, t_plain); |
| 83 | |
| 84 | o := encU(c_plain_r); |
| 85 | nodes := gather(o, ignore_ws); |
| 86 | check_match(nodes, t_plain_r); |
| 87 | } |
| 88 | |
| 89 | /*-------------------- REPLACE --------------------*/ |
| 90 | |
| 91 | const U c_replace := { |
| 92 | s := "Howdy" |
| 93 | } |
| 94 | with { |
| 95 | erroneous (s) "value := 'BABEFACE'O " |
| 96 | } |
| 97 | |
| 98 | const U c_replace2 := { // "named" |
| 99 | s := "Howdy" |
| 100 | } |
| 101 | with { |
| 102 | erroneous (s) "value := myoctetstring : 'BABEFACE'O " |
| 103 | } |
| 104 | |
| 105 | template Nodes t_replace := { |
| 106 | // node type , depth, name , value |
| 107 | { XML_READER_TYPE_ELEMENT , 0, "U", "", "" }, |
| 108 | { XML_READER_TYPE_ELEMENT , 1, "OCTET_STRING", "", "" }, |
| 109 | { XML_READER_TYPE_TEXT , 2, "#text", "BABEFACE", "" }, |
| 110 | { XML_READER_TYPE_END_ELEMENT, 1, "OCTET_STRING", "", "" }, |
| 111 | { XML_READER_TYPE_END_ELEMENT, 0, "U", "", "" } |
| 112 | } |
| 113 | |
| 114 | template Nodes t_replace2 := { |
| 115 | // node type , depth, name , value |
| 116 | { XML_READER_TYPE_ELEMENT , 0, "U", "", "" }, |
| 117 | { XML_READER_TYPE_ELEMENT , 1, "myoctetstring", "", "" }, |
| 118 | { XML_READER_TYPE_TEXT , 2, "#text", "BABEFACE", "" }, |
| 119 | { XML_READER_TYPE_END_ELEMENT, 1, "myoctetstring", "", "" }, |
| 120 | { XML_READER_TYPE_END_ELEMENT, 0, "U", "", "" } |
| 121 | } |
| 122 | |
| 123 | const U c_replace_raw := { |
| 124 | s := "Howdy" |
| 125 | } |
| 126 | with { |
| 127 | //before no-no with union ): erroneous (i) "before := 'BABEFACE'O " |
| 128 | erroneous (s) "value(raw) := 'E29795E280BFE280BFE29795'O " |
| 129 | } |
| 130 | |
| 131 | //const U c_replace_raw := { |
| 132 | // s := "Howdy" |
| 133 | //} |
| 134 | //with { |
| 135 | // erroneous (s) "value(raw) := '111000101001011110010101111000101000000010111111111000101000000010111111111000101001011110010101'B " |
| 136 | //} |
| 137 | // Alas, this is not accepted; bitstring can only be used as an erroneous raw value for RAW or PER, not XER |
| 138 | |
| 139 | template Nodes t_replace_raw := { |
| 140 | // node type , depth, name , value |
| 141 | { XML_READER_TYPE_ELEMENT , 0, "U", "", "" }, |
| 142 | |
| 143 | { XML_READER_TYPE_TEXT , 1, "#text", "\n" & "\xE2\x97\x95" & "\xE2\x80\xBF" & "\xE2\x80\xBF" & "\xE2\x97\x95" /* & "\t\t"*/, "" }, |
| 144 | |
| 145 | { XML_READER_TYPE_END_ELEMENT, 0, "U", "", "" } |
| 146 | } |
| 147 | |
| 148 | testcase u_replace() runs on Neg |
| 149 | { |
| 150 | var octetstring o; |
| 151 | var Nodes nodes; |
| 152 | |
| 153 | o := encU(c_replace); |
| 154 | nodes := gather(o, ignore_ws); |
| 155 | check_match(nodes, t_replace); |
| 156 | |
| 157 | o := encU(c_replace2); |
| 158 | nodes := gather(o, ignore_ws); |
| 159 | check_match(nodes, t_replace2); |
| 160 | |
| 161 | o := encU(c_replace_raw); |
| 162 | nodes := gather(o, ignore_ws); |
| 163 | check_match(nodes, t_replace_raw); |
| 164 | } |
| 165 | |
| 166 | // -- -- -- -- -- -- -- -- before embedded field |
| 167 | |
| 168 | const U c_before_r_f := c_plain_r |
| 169 | with { |
| 170 | erroneous (r.f) "before := myoctetstring : 'BABEFACE'O" |
| 171 | } |
| 172 | |
| 173 | template Nodes t_before_r_f modifies t_plain_r := |
| 174 | { |
| 175 | -,-,-,-,-, // <U>, <r>, <b>, #text, </b> |
| 176 | // perturbation: an extra element |
| 177 | { XML_READER_TYPE_ELEMENT , 2, "myoctetstring", "", "" }, //5 |
| 178 | { XML_READER_TYPE_TEXT , 3, "#text", "BABEFACE", "" }, |
| 179 | { XML_READER_TYPE_END_ELEMENT, 2, "myoctetstring", "", "" }, |
| 180 | t_plain_r[5], |
| 181 | t_plain_r[6], |
| 182 | t_plain_r[7], |
| 183 | t_plain_r[8], |
| 184 | t_plain_r[9] |
| 185 | } |
| 186 | |
| 187 | testcase u_before_embedded() runs on Neg |
| 188 | { |
| 189 | var octetstring o; |
| 190 | var Nodes nodes; |
| 191 | |
| 192 | o := encU(c_before_r_f); |
| 193 | nodes := gather(o, ignore_ws); |
| 194 | check_match(nodes, t_before_r_f); |
| 195 | } |
| 196 | |
| 197 | // Do not forget to add the testcase to the one and only control part in rec_of.ttcn |
| 198 | } |
| 199 | with { |
| 200 | encode "XML" |
| 201 | } |