1/* ***** BEGIN LICENSE BLOCK ***** 2* Version: NPL 1.1/GPL 2.0/LGPL 2.1 3* 4* The contents of this file are subject to the Netscape Public License 5* Version 1.1 (the "License"); you may not use this file except in 6* compliance with the License. You may obtain a copy of the License at 7* http://www.mozilla.org/NPL/ 8* 9* Software distributed under the License is distributed on an "AS IS" basis, 10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 11* for the specific language governing rights and limitations under the 12* License. 13* 14* The Original Code is JavaScript Engine testing utilities. 15* 16* The Initial Developer of the Original Code is Netscape Communications Corp. 17* Portions created by the Initial Developer are Copyright (C) 2002 18* the Initial Developer. All Rights Reserved. 19* 20* Contributor(s): pschwartau@netscape.com 21* 22* Alternatively, the contents of this file may be used under the terms of 23* either the GNU General Public License Version 2 or later (the "GPL"), or 24* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 25* in which case the provisions of the GPL or the LGPL are applicable instead 26* of those above. If you wish to allow use of your version of this file only 27* under the terms of either the GPL or the LGPL, and not to allow others to 28* use your version of this file under the terms of the NPL, indicate your 29* decision by deleting the provisions above and replace them with the notice 30* and other provisions required by the GPL or the LGPL. If you do not delete 31* the provisions above, a recipient may use your version of this file under 32* the terms of any one of the NPL, the GPL or the LGPL. 33* 34* ***** END LICENSE BLOCK ***** 35* 36* 37* Date: 11 Nov 2002 38* SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc. 39* See http://bugzilla.mozilla.org/show_bug.cgi?id=179524 40* 41* Note that when testing str.replace(), we have to be careful if the first 42* argument provided to str.replace() is not a regexp object. ECMA-262 says 43* it is NOT converted to one, unlike the case for str.match(), str.search(). 44* 45* See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means 46* we have to be careful how we test meta-characters in the first argument 47* to str.replace(), if that argument is a string - 48*/ 49//----------------------------------------------------------------------------- 50var UBound = 0; 51var bug = 179524; 52var summary = "Don't crash on extraneous arguments to str.match(), etc."; 53var status = ''; 54var statusitems = []; 55var actual = ''; 56var actualvalues = []; 57var expect= ''; 58var expectedvalues = []; 59 60 61str = 'ABC abc'; 62var re = /z/ig; 63 64status = inSection(1); 65actual = str.match(re); 66expect = null; 67addThis(); 68 69status = inSection(2); 70actual = str.match(re, 'i'); 71expect = null; 72addThis(); 73 74status = inSection(3); 75actual = str.match(re, 'g', ''); 76expect = null; 77addThis(); 78 79status = inSection(4); 80actual = str.match(re, 'z', new Object(), new Date()); 81expect = null; 82addThis(); 83 84 85/* 86 * Now try the same thing with str.search() 87 */ 88status = inSection(5); 89actual = str.search(re); 90expect = -1; 91addThis(); 92 93status = inSection(6); 94actual = str.search(re, 'i'); 95expect = -1; 96addThis(); 97 98status = inSection(7); 99actual = str.search(re, 'g', ''); 100expect = -1; 101addThis(); 102 103status = inSection(8); 104actual = str.search(re, 'z', new Object(), new Date()); 105expect = -1; 106addThis(); 107 108 109/* 110 * Now try the same thing with str.replace() 111 */ 112status = inSection(9); 113actual = str.replace(re, 'Z'); 114expect = str; 115addThis(); 116 117status = inSection(10); 118actual = str.replace(re, 'Z', 'i'); 119expect = str; 120addThis(); 121 122status = inSection(11); 123actual = str.replace(re, 'Z', 'g', ''); 124expect = str; 125addThis(); 126 127status = inSection(12); 128actual = str.replace(re, 'Z', 'z', new Object(), new Date()); 129expect = str; 130addThis(); 131 132 133 134/* 135 * Now test the case where str.match()'s first argument is not a regexp object. 136 * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a 137 * regexp object using the argument as a regexp pattern, but then extends ECMA 138 * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig'). 139 * 140 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10 141 */ 142status = inSection(13); 143actual = str.match('a').toString(); 144expect = str.match(/a/).toString(); 145addThis(); 146 147status = inSection(14); 148actual = str.match('a', 'i').toString(); 149expect = str.match(/a/i).toString(); 150addThis(); 151 152status = inSection(15); 153actual = str.match('a', 'ig').toString(); 154expect = str.match(/a/ig).toString(); 155addThis(); 156 157status = inSection(16); 158actual = str.match('\\s', 'm').toString(); 159expect = str.match(/\s/m).toString(); 160addThis(); 161 162 163/* 164 * Now try the previous three cases with extraneous parameters 165 */ 166status = inSection(17); 167actual = str.match('a', 'i', 'g').toString(); 168expect = str.match(/a/i).toString(); 169addThis(); 170 171status = inSection(18); 172actual = str.match('a', 'ig', new Object()).toString(); 173expect = str.match(/a/ig).toString(); 174addThis(); 175 176status = inSection(19); 177actual = str.match('\\s', 'm', 999).toString(); 178expect = str.match(/\s/m).toString(); 179addThis(); 180 181 182/* 183 * Try an invalid second parameter (i.e. an invalid regexp flag) 184 */ 185status = inSection(20); 186try 187{ 188 actual = str.match('a', 'z').toString(); 189 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; 190 addThis(); 191} 192catch (e) 193{ 194 actual = e instanceof SyntaxError; 195 expect = true; 196 addThis(); 197} 198 199 200 201/* 202 * Now test str.search() where the first argument is not a regexp object. 203 * The same considerations as above apply - 204 * 205 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16 206 */ 207status = inSection(21); 208actual = str.search('a'); 209expect = str.search(/a/); 210addThis(); 211 212status = inSection(22); 213actual = str.search('a', 'i'); 214expect = str.search(/a/i); 215addThis(); 216 217status = inSection(23); 218actual = str.search('a', 'ig'); 219expect = str.search(/a/ig); 220addThis(); 221 222status = inSection(24); 223actual = str.search('\\s', 'm'); 224expect = str.search(/\s/m); 225addThis(); 226 227 228/* 229 * Now try the previous three cases with extraneous parameters 230 */ 231status = inSection(25); 232actual = str.search('a', 'i', 'g'); 233expect = str.search(/a/i); 234addThis(); 235 236status = inSection(26); 237actual = str.search('a', 'ig', new Object()); 238expect = str.search(/a/ig); 239addThis(); 240 241status = inSection(27); 242actual = str.search('\\s', 'm', 999); 243expect = str.search(/\s/m); 244addThis(); 245 246 247/* 248 * Try an invalid second parameter (i.e. an invalid regexp flag) 249 */ 250status = inSection(28); 251try 252{ 253 actual = str.search('a', 'z'); 254 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; 255 addThis(); 256} 257catch (e) 258{ 259 actual = e instanceof SyntaxError; 260 expect = true; 261 addThis(); 262} 263 264 265 266/* 267 * Now test str.replace() where the first argument is not a regexp object. 268 * The same considerations as above apply, EXCEPT for meta-characters. 269 * See introduction to testcase above. References: 270 * 271 * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16 272 * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21 273 */ 274status = inSection(29); 275actual = str.replace('a', 'Z'); 276expect = str.replace(/a/, 'Z'); 277addThis(); 278 279status = inSection(30); 280actual = str.replace('a', 'Z', 'i'); 281expect = str.replace(/a/i, 'Z'); 282addThis(); 283 284status = inSection(31); 285actual = str.replace('a', 'Z', 'ig'); 286expect = str.replace(/a/ig, 'Z'); 287addThis(); 288 289status = inSection(32); 290actual = str.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg! 291actual = str.replace(' ', 'Z', 'm'); //<--- Have to do this instead 292expect = str.replace(/\s/m, 'Z'); 293addThis(); 294 295 296/* 297 * Now try the previous three cases with extraneous parameters 298 */ 299status = inSection(33); 300actual = str.replace('a', 'Z', 'i', 'g'); 301expect = str.replace(/a/i, 'Z'); 302addThis(); 303 304status = inSection(34); 305actual = str.replace('a', 'Z', 'ig', new Object()); 306expect = str.replace(/a/ig, 'Z'); 307addThis(); 308 309status = inSection(35); 310actual = str.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg! 311actual = str.replace(' ', 'Z', 'm', 999); //<--- Have to do this instead 312expect = str.replace(/\s/m, 'Z'); 313addThis(); 314 315 316/* 317 * Try an invalid third parameter (i.e. an invalid regexp flag) 318 */ 319status = inSection(36); 320try 321{ 322 actual = str.replace('a', 'Z', 'z'); 323 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; 324 addThis(); 325} 326catch (e) 327{ 328 actual = e instanceof SyntaxError; 329 expect = true; 330 addThis(); 331} 332 333 334 335 336//----------------------------------------------------------------------------- 337test(); 338//----------------------------------------------------------------------------- 339 340 341 342function addThis() 343{ 344 statusitems[UBound] = status; 345 actualvalues[UBound] = actual; 346 expectedvalues[UBound] = expect; 347 UBound++; 348} 349 350 351function test() 352{ 353 enterFunc('test'); 354 printBugNumber(bug); 355 printStatus(summary); 356 357 for (var i=0; i<UBound; i++) 358 { 359 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 360 } 361 362 exitFunc ('test'); 363} 364