michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 26 November 2000 michael@0: * michael@0: * michael@0: * SUMMARY: This test arose from Bugzilla bug 57631: michael@0: * "RegExp with invalid pattern or invalid flag causes segfault" michael@0: * michael@0: * Either error should throw an exception of type SyntaxError, michael@0: * and we check to see that it does... michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = '57631'; michael@0: var summary = 'Testing new RegExp(pattern,flag) with illegal pattern or flag'; michael@0: var statprefix = 'Testing for error creating illegal RegExp object on pattern '; michael@0: var statsuffix = 'and flag '; michael@0: var cnSUCCESS = 'SyntaxError'; michael@0: var cnFAILURE = 'not a SyntaxError'; michael@0: var singlequote = "'"; michael@0: var i = -1; var j = -1; var s = ''; var f = ''; michael@0: var obj = {}; michael@0: var status = ''; var actual = ''; var expect = ''; var msg = ''; michael@0: var legalpatterns = new Array(); var illegalpatterns = new Array(); michael@0: var legalflags = new Array(); var illegalflags = new Array(); michael@0: michael@0: michael@0: // valid regular expressions to try - michael@0: legalpatterns[0] = ''; michael@0: legalpatterns[1] = 'abc'; michael@0: legalpatterns[2] = '(.*)(3-1)\s\w'; michael@0: legalpatterns[3] = '(.*)(...)\\s\\w'; michael@0: legalpatterns[4] = '[^A-Za-z0-9_]'; michael@0: legalpatterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)'; michael@0: michael@0: // invalid regular expressions to try - michael@0: illegalpatterns[0] = '(?)'; michael@0: illegalpatterns[1] = '(a'; michael@0: illegalpatterns[2] = '( ]'; michael@0: //illegalpatterns[3] = '\d{1,s}'; michael@0: michael@0: // valid flags to try - michael@0: legalflags[0] = 'i'; michael@0: legalflags[1] = 'g'; michael@0: legalflags[2] = 'm'; michael@0: legalflags[3] = undefined; michael@0: michael@0: // invalid flags to try - michael@0: illegalflags[0] = 'a'; michael@0: illegalflags[1] = 123; michael@0: illegalflags[2] = new RegExp(); michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------------------------- michael@0: test(); michael@0: //------------------------------------------------------------------------------------------------- michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: testIllegalRegExps(legalpatterns, illegalflags); michael@0: testIllegalRegExps(illegalpatterns, legalflags); michael@0: testIllegalRegExps(illegalpatterns, illegalflags); michael@0: michael@0: exitFunc ('test'); michael@0: } michael@0: michael@0: michael@0: // This function will only be called where all the patterns are illegal, or all the flags michael@0: function testIllegalRegExps(patterns, flags) michael@0: { michael@0: for (i in patterns) michael@0: { michael@0: s = patterns[i]; michael@0: michael@0: for (j in flags) michael@0: { michael@0: f = flags[j]; michael@0: status = getStatus(s, f); michael@0: actual = cnFAILURE; michael@0: expect = cnSUCCESS; michael@0: michael@0: try michael@0: { michael@0: // This should cause an exception if either s or f is illegal - michael@0: eval('obj = new RegExp(s, f);'); michael@0: } michael@0: catch(e) michael@0: { michael@0: // We expect to get a SyntaxError - test for this: michael@0: if (e instanceof SyntaxError) michael@0: actual = cnSUCCESS; michael@0: } michael@0: michael@0: reportCompare(expect, actual, status); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: function getStatus(regexp, flag) michael@0: { michael@0: return (statprefix + quote(regexp) + statsuffix + quote(flag)); michael@0: } michael@0: michael@0: michael@0: function quote(text) michael@0: { michael@0: return (singlequote + text + singlequote); michael@0: }