Home | History | Annotate | Download | only in RegExp
      1 /*
      2 * The contents of this file are subject to the Netscape Public
      3 * License Version 1.1 (the "License"); you may not use this file
      4 * except in compliance with the License. You may obtain a copy of
      5 * the License at http://www.mozilla.org/NPL/
      6 *
      7 * Software distributed under the License is distributed on an "AS
      8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      9 * implied. See the License for the specific language governing
     10 * rights and limitations under the License.
     11 *
     12 * The Original Code is mozilla.org code.
     13 *
     14 * The Initial Developer of the Original Code is Netscape
     15 * Communications Corporation.  Portions created by Netscape are
     16 * Copyright (C) 1998 Netscape Communications Corporation. All
     17 * Rights Reserved.
     18 *
     19 * Contributor(s): pschwartau (at) netscape.com
     20 * Date: 26 November 2000
     21 *
     22 *
     23 * SUMMARY:  This test arose from Bugzilla bug 57631:
     24 * "RegExp with invalid pattern or invalid flag causes segfault"
     25 *
     26 * Either error should throw an exception of type SyntaxError,
     27 * and we check to see that it does...
     28 */
     29 //-------------------------------------------------------------------------------------------------
     30 var bug = '57631';
     31 var summary = 'Testing new RegExp(pattern,flag) with illegal pattern or flag';
     32 var statprefix = 'Testing for error creating illegal RegExp object on pattern ';
     33 var statsuffix =  'and flag ';
     34 var cnSUCCESS = 'SyntaxError';
     35 var cnFAILURE = 'not a SyntaxError';
     36 var singlequote = "'";
     37 var i = -1; var j = -1; var s = ''; var f = '';
     38 var obj = {};
     39 var status = ''; var actual = ''; var expect = ''; var msg = '';
     40 var legalpatterns = new Array(); var illegalpatterns = new Array();
     41 var legalflags = new Array();  var illegalflags = new Array();
     42 
     43 
     44 // valid regular expressions to try -
     45 legalpatterns[0] = '';
     46 legalpatterns[1] = 'abc';
     47 legalpatterns[2] = '(.*)(3-1)\s\w';
     48 legalpatterns[3] = '(.*)(...)\\s\\w';
     49 legalpatterns[4] = '[^A-Za-z0-9_]';
     50 legalpatterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)';
     51 
     52 // invalid regular expressions to try -
     53 illegalpatterns[0] = '()';
     54 illegalpatterns[1] = '(a';
     55 illegalpatterns[2] = '( ]';
     56 illegalpatterns[3] = '\d{s}';
     57 
     58 // valid flags to try -
     59 legalflags[0] = 'i';
     60 legalflags[1] = 'g';
     61 legalflags[2] = 'm';
     62 legalflags[3] = undefined;
     63 
     64 // invalid flags to try -
     65 illegalflags[0] = 'a';
     66 illegalflags[1] = 123;
     67 illegalflags[2] = new RegExp();
     68 
     69 
     70 
     71 //-------------------------------------------------------------------------------------------------
     72 test();
     73 //-------------------------------------------------------------------------------------------------
     74 
     75 
     76 function test()
     77 {
     78   enterFunc ('test');
     79   printBugNumber (bug);
     80   printStatus (summary);
     81 
     82   testIllegalRegExps(legalpatterns, illegalflags);
     83   testIllegalRegExps(illegalpatterns, legalflags);
     84   testIllegalRegExps(illegalpatterns, illegalflags);
     85 
     86   exitFunc ('test');
     87 }
     88 
     89 
     90 // This function will only be called where all the patterns are illegal, or all the flags
     91 function testIllegalRegExps(patterns, flags)
     92 {
     93   for (i in patterns)
     94     {
     95       s = patterns[i];
     96 
     97       for (j in flags)
     98       {
     99         f = flags[j];
    100         status = getStatus(s, f);
    101 
    102         try
    103         {
    104           // This should cause an exception if either s or f is illegal -
    105           eval('obj = new RegExp(s, f);');
    106         }
    107         catch(e)
    108         {
    109           // We expect to get a SyntaxError - test for this:
    110           actual = (e instanceof SyntaxError)? cnSUCCESS : cnFAILURE;
    111           expect = cnSUCCESS;
    112           reportCompare(expect, actual, status);
    113         }
    114       }
    115     }
    116 }
    117 
    118 
    119 function getStatus(regexp, flag)
    120 {
    121   return (statprefix  +  quote(regexp) +  statsuffix  +   quote(flag));
    122 }
    123 
    124 
    125 function quote(text)
    126 {
    127   return (singlequote  +  text  + singlequote);
    128 }