js/src/tests/js1_5/Regress/regress-179524.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_5/Regress/regress-179524.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,334 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + *
    1.11 + * Date:    11 Nov 2002
    1.12 + * SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc.
    1.13 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=179524
    1.14 + *
    1.15 + * Note that when testing str.replace(), we have to be careful if the first
    1.16 + * argument provided to str.replace() is not a regexp object. ECMA-262 says
    1.17 + * it is NOT converted to one, unlike the case for str.match(), str.search().
    1.18 + *
    1.19 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means
    1.20 + * we have to be careful how we test meta-characters in the first argument
    1.21 + * to str.replace(), if that argument is a string -
    1.22 + */
    1.23 +//-----------------------------------------------------------------------------
    1.24 +var UBound = 0;
    1.25 +var BUGNUMBER = 179524;
    1.26 +var summary = "Don't crash on extraneous arguments to str.match(), etc.";
    1.27 +var status = '';
    1.28 +var statusitems = [];
    1.29 +var actual = '';
    1.30 +var actualvalues = [];
    1.31 +var expect= '';
    1.32 +var expectedvalues = [];
    1.33 +
    1.34 +
    1.35 +str = 'ABC abc';
    1.36 +var re = /z/ig;
    1.37 +
    1.38 +status = inSection(1);
    1.39 +actual = str.match(re);
    1.40 +expect = null;
    1.41 +addThis();
    1.42 +
    1.43 +status = inSection(2);
    1.44 +actual = str.match(re, 'i');
    1.45 +expect = null;
    1.46 +addThis();
    1.47 +
    1.48 +status = inSection(3);
    1.49 +actual = str.match(re, 'g', '');
    1.50 +expect = null;
    1.51 +addThis();
    1.52 +
    1.53 +status = inSection(4);
    1.54 +actual = str.match(re, 'z', new Object(), new Date());
    1.55 +expect = null;
    1.56 +addThis();
    1.57 +
    1.58 +
    1.59 +/*
    1.60 + * Now try the same thing with str.search()
    1.61 + */
    1.62 +status = inSection(5);
    1.63 +actual = str.search(re);
    1.64 +expect = -1;
    1.65 +addThis();
    1.66 +
    1.67 +status = inSection(6);
    1.68 +actual = str.search(re, 'i');
    1.69 +expect = -1;
    1.70 +addThis();
    1.71 +
    1.72 +status = inSection(7);
    1.73 +actual = str.search(re, 'g', '');
    1.74 +expect = -1;
    1.75 +addThis();
    1.76 +
    1.77 +status = inSection(8);
    1.78 +actual = str.search(re, 'z', new Object(), new Date());
    1.79 +expect = -1;
    1.80 +addThis();
    1.81 +
    1.82 +
    1.83 +/*
    1.84 + * Now try the same thing with str.replace()
    1.85 + */
    1.86 +status = inSection(9);
    1.87 +actual = str.replace(re, 'Z');
    1.88 +expect = str;
    1.89 +addThis();
    1.90 +
    1.91 +status = inSection(10);
    1.92 +actual = str.replace(re, 'Z', 'i');
    1.93 +expect = str;
    1.94 +addThis();
    1.95 +
    1.96 +status = inSection(11);
    1.97 +actual = str.replace(re, 'Z', 'g', '');
    1.98 +expect = str;
    1.99 +addThis();
   1.100 +
   1.101 +status = inSection(12);
   1.102 +actual = str.replace(re, 'Z', 'z', new Object(), new Date());
   1.103 +expect = str;
   1.104 +addThis();
   1.105 +
   1.106 +
   1.107 +
   1.108 +/*
   1.109 + * Now test the case where str.match()'s first argument is not a regexp object.
   1.110 + * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a
   1.111 + * regexp object using the argument as a regexp pattern, but then extends ECMA
   1.112 + * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig').
   1.113 + *
   1.114 + * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10
   1.115 + */
   1.116 +status = inSection(13);
   1.117 +actual = str.match('a').toString();
   1.118 +expect = str.match(/a/).toString();
   1.119 +addThis();
   1.120 +
   1.121 +status = inSection(14);
   1.122 +actual = str.match('a', 'i').toString();
   1.123 +expect = str.match(/a/i).toString();
   1.124 +addThis();
   1.125 +
   1.126 +status = inSection(15);
   1.127 +actual = str.match('a', 'ig').toString();
   1.128 +expect = str.match(/a/ig).toString();
   1.129 +addThis();
   1.130 +
   1.131 +status = inSection(16);
   1.132 +actual = str.match('\\s', 'm').toString();
   1.133 +expect = str.match(/\s/m).toString();
   1.134 +addThis();
   1.135 +
   1.136 +
   1.137 +/*
   1.138 + * Now try the previous three cases with extraneous parameters
   1.139 + */
   1.140 +status = inSection(17);
   1.141 +actual = str.match('a', 'i', 'g').toString();
   1.142 +expect = str.match(/a/i).toString();
   1.143 +addThis();
   1.144 +
   1.145 +status = inSection(18);
   1.146 +actual = str.match('a', 'ig', new Object()).toString();
   1.147 +expect = str.match(/a/ig).toString();
   1.148 +addThis();
   1.149 +
   1.150 +status = inSection(19);
   1.151 +actual = str.match('\\s', 'm', 999).toString();
   1.152 +expect = str.match(/\s/m).toString();
   1.153 +addThis();
   1.154 +
   1.155 +
   1.156 +/*
   1.157 + * Try an invalid second parameter (i.e. an invalid regexp flag)
   1.158 + */
   1.159 +status = inSection(20);
   1.160 +try
   1.161 +{
   1.162 +  actual = str.match('a', 'z').toString();
   1.163 +  expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
   1.164 +  addThis();
   1.165 +}
   1.166 +catch (e)
   1.167 +{
   1.168 +  actual = e instanceof SyntaxError;
   1.169 +  expect = true;
   1.170 +  addThis();
   1.171 +}
   1.172 +
   1.173 +
   1.174 +
   1.175 +/*
   1.176 + * Now test str.search() where the first argument is not a regexp object.
   1.177 + * The same considerations as above apply -
   1.178 + *
   1.179 + * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
   1.180 + */
   1.181 +status = inSection(21);
   1.182 +actual = str.search('a');
   1.183 +expect = str.search(/a/);
   1.184 +addThis();
   1.185 +
   1.186 +status = inSection(22);
   1.187 +actual = str.search('a', 'i');
   1.188 +expect = str.search(/a/i);
   1.189 +addThis();
   1.190 +
   1.191 +status = inSection(23);
   1.192 +actual = str.search('a', 'ig');
   1.193 +expect = str.search(/a/ig);
   1.194 +addThis();
   1.195 +
   1.196 +status = inSection(24);
   1.197 +actual = str.search('\\s', 'm');
   1.198 +expect = str.search(/\s/m);
   1.199 +addThis();
   1.200 +
   1.201 +
   1.202 +/*
   1.203 + * Now try the previous three cases with extraneous parameters
   1.204 + */
   1.205 +status = inSection(25);
   1.206 +actual = str.search('a', 'i', 'g');
   1.207 +expect = str.search(/a/i);
   1.208 +addThis();
   1.209 +
   1.210 +status = inSection(26);
   1.211 +actual = str.search('a', 'ig', new Object());
   1.212 +expect = str.search(/a/ig);
   1.213 +addThis();
   1.214 +
   1.215 +status = inSection(27);
   1.216 +actual = str.search('\\s', 'm', 999);
   1.217 +expect = str.search(/\s/m);
   1.218 +addThis();
   1.219 +
   1.220 +
   1.221 +/*
   1.222 + * Try an invalid second parameter (i.e. an invalid regexp flag)
   1.223 + */
   1.224 +status = inSection(28);
   1.225 +try
   1.226 +{
   1.227 +  actual = str.search('a', 'z');
   1.228 +  expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
   1.229 +  addThis();
   1.230 +}
   1.231 +catch (e)
   1.232 +{
   1.233 +  actual = e instanceof SyntaxError;
   1.234 +  expect = true;
   1.235 +  addThis();
   1.236 +}
   1.237 +
   1.238 +
   1.239 +
   1.240 +/*
   1.241 + * Now test str.replace() where the first argument is not a regexp object.
   1.242 + * The same considerations as above apply, EXCEPT for meta-characters.
   1.243 + * See introduction to testcase above. References:
   1.244 + *
   1.245 + * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
   1.246 + * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21
   1.247 + */
   1.248 +status = inSection(29);
   1.249 +actual = str.replace('a', 'Z');
   1.250 +expect = str.replace(/a/, 'Z');
   1.251 +addThis();
   1.252 +
   1.253 +status = inSection(30);
   1.254 +actual = str.replace('a', 'Z', 'i');
   1.255 +expect = str.replace(/a/i, 'Z');
   1.256 +addThis();
   1.257 +
   1.258 +status = inSection(31);
   1.259 +actual = str.replace('a', 'Z', 'ig');
   1.260 +expect = str.replace(/a/ig, 'Z');
   1.261 +addThis();
   1.262 +
   1.263 +status = inSection(32);
   1.264 +actual = str.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg!
   1.265 +actual = str.replace(' ', 'Z', 'm');   //<--- Have to do this instead
   1.266 +expect = str.replace(/\s/m, 'Z');
   1.267 +addThis();
   1.268 +
   1.269 +
   1.270 +/*
   1.271 + * Now try the previous three cases with extraneous parameters
   1.272 + */
   1.273 +status = inSection(33);
   1.274 +actual = str.replace('a', 'Z', 'i', 'g');
   1.275 +expect = str.replace(/a/i, 'Z');
   1.276 +addThis();
   1.277 +
   1.278 +status = inSection(34);
   1.279 +actual = str.replace('a', 'Z', 'ig', new Object());
   1.280 +expect = str.replace(/a/ig, 'Z');
   1.281 +addThis();
   1.282 +
   1.283 +status = inSection(35);
   1.284 +actual = str.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg!
   1.285 +actual = str.replace(' ', 'Z', 'm', 999);   //<--- Have to do this instead
   1.286 +expect = str.replace(/\s/m, 'Z');
   1.287 +addThis();
   1.288 +
   1.289 +
   1.290 +/*
   1.291 + * Try an invalid third parameter (i.e. an invalid regexp flag)
   1.292 + */
   1.293 +status = inSection(36);
   1.294 +try
   1.295 +{
   1.296 +  actual = str.replace('a', 'Z', 'z');
   1.297 +  expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
   1.298 +  addThis();
   1.299 +}
   1.300 +catch (e)
   1.301 +{
   1.302 +  actual = e instanceof SyntaxError;
   1.303 +  expect = true;
   1.304 +  addThis();
   1.305 +}
   1.306 +
   1.307 +
   1.308 +
   1.309 +
   1.310 +//-----------------------------------------------------------------------------
   1.311 +test();
   1.312 +//-----------------------------------------------------------------------------
   1.313 +
   1.314 +
   1.315 +
   1.316 +function addThis()
   1.317 +{
   1.318 +  statusitems[UBound] = status;
   1.319 +  actualvalues[UBound] = actual;
   1.320 +  expectedvalues[UBound] = expect;
   1.321 +  UBound++;
   1.322 +}
   1.323 +
   1.324 +
   1.325 +function test()
   1.326 +{
   1.327 +  enterFunc('test');
   1.328 +  printBugNumber(BUGNUMBER);
   1.329 +  printStatus(summary);
   1.330 +
   1.331 +  for (var i=0; i<UBound; i++)
   1.332 +  {
   1.333 +    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   1.334 +  }
   1.335 +
   1.336 +  exitFunc ('test');
   1.337 +}

mercurial