Răsfoiți Sursa

test: remove placeholders from random strings without creating new ones

The sprintf, I18n and AttachmentViewer property tests escaped %s and %d
in random strings with replace(/%(s|d)/g, '%%') or removed them, both of
which can create a new placeholder, i.e. "%dd" turns into "%%d", which
still contains %d. fast-check occasionally generates such strings, so
the tests failed at random, as in CI for the I18n translate test with
["",[""],"%dd"]. The new common.removePlaceholders removes the whole run
of percent signs before an s or d instead.
El RIDO 5 zile în urmă
părinte
comite
619f900c9c
4 a modificat fișierele cu 34 adăugiri și 28 ștergeri
  1. 6 0
      js/common.js
  2. 2 2
      js/test/AttachmentViewer.js
  3. 13 13
      js/test/Helper.js
  4. 13 13
      js/test/I18n.js

+ 6 - 0
js/common.js

@@ -72,6 +72,12 @@ exports.fcA2zString = function() {
     return fc.constantFrom(...a2zString);
 };
 
+// removes %s and %d placeholders from a string, including the whole run of
+// percent signs before them, so the removal can't create new ones (i.e. %%dd)
+exports.removePlaceholders = function(string) {
+    return string.replace(/%+(?=[sd])/g, '');
+};
+
 // provides random lowercase alpha numeric characters (a to z and 0 to 9)
 exports.fcAlnumString = function() {
     return fc.constantFrom(...alnumString);

+ 2 - 2
js/test/AttachmentViewer.js

@@ -45,8 +45,8 @@ describe('AttachmentViewer', function () {
                     if (filename === '\u0000') {
                         filename = '';
                     }
-                    prefix  = prefix.replace(/%(s|d)/g, '%%');
-                    postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<|>/g, '');
+                    prefix  = common.removePlaceholders(prefix);
+                    postfix = common.removePlaceholders(postfix).replace(/<|>/g, '');
                     document.body.innerHTML = bodyTemplate;
                     mockCreateObjectUrl(false);
                     PrivateBin.AttachmentViewer.init();

+ 13 - 13
js/test/Helper.js

@@ -200,9 +200,9 @@ describe('Helper', function () {
                 fc.array(fc.string(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix    =    prefix.replace(/%(s|d)/g, '%%');
-                    params[0] = params[0].replace(/%(s|d)/g, '%%');
-                    postfix   =   postfix.replace(/%(s|d)/g, '%%');
+                    prefix    =    common.removePlaceholders(prefix);
+                    params[0] = common.removePlaceholders(params[0]);
+                    postfix   =   common.removePlaceholders(postfix);
                     var result = prefix + params[0] + postfix;
                     params.unshift(prefix + '%s' + postfix);
                     return result === PrivateBin.Helper.sprintf.apply(this, params);
@@ -215,8 +215,8 @@ describe('Helper', function () {
                 fc.array(fc.nat(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix  =  prefix.replace(/%(s|d)/g, '%%');
-                    postfix = postfix.replace(/%(s|d)/g, '%%');
+                    prefix  =  common.removePlaceholders(prefix);
+                    postfix = common.removePlaceholders(postfix);
                     var result = prefix + params[0] + postfix;
                     params.unshift(prefix + '%d' + postfix);
                     return result === PrivateBin.Helper.sprintf.apply(this, params);
@@ -229,8 +229,8 @@ describe('Helper', function () {
                 fc.array(fc.falsy(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix  =  prefix.replace(/%(s|d)/g, '%%');
-                    postfix = postfix.replace(/%(s|d)/g, '%%');
+                    prefix  =  common.removePlaceholders(prefix);
+                    postfix = common.removePlaceholders(postfix);
                     var result = prefix + '0' + postfix;
                     params.unshift(prefix + '%d' + postfix);
                     return result === PrivateBin.Helper.sprintf.apply(this, params);
@@ -245,9 +245,9 @@ describe('Helper', function () {
                 fc.string(),
                 fc.string(),
                 function (prefix, uint, middle, string, postfix) {
-                    prefix  =  prefix.replace(/%(s|d)/g, '');
-                    middle  =  middle.replace(/%(s|d)/g, '');
-                    postfix = postfix.replace(/%(s|d)/g, '');
+                    prefix  =  common.removePlaceholders(prefix);
+                    middle  =  common.removePlaceholders(middle);
+                    postfix = common.removePlaceholders(postfix);
                     var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
                         result = prefix + uint + middle + string + postfix;
                     return result === PrivateBin.Helper.sprintf.apply(this, params);
@@ -262,9 +262,9 @@ describe('Helper', function () {
                 fc.string(),
                 fc.string(),
                 function (prefix, uint, middle, string, postfix) {
-                    prefix  =  prefix.replace(/%(s|d)/g, '');
-                    middle  =  middle.replace(/%(s|d)/g, '');
-                    postfix = postfix.replace(/%(s|d)/g, '');
+                    prefix  =  common.removePlaceholders(prefix);
+                    middle  =  common.removePlaceholders(middle);
+                    postfix = common.removePlaceholders(postfix);
                     var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
                         result = prefix + string + middle + uint + postfix;
                     return result === PrivateBin.Helper.sprintf.apply(this, params);

+ 13 - 13
js/test/I18n.js

@@ -13,7 +13,7 @@ describe('I18n', function () {
             fc.assert(fc.property(
                 fc.string(),
                 function (messageId) {
-                    messageId   = messageId.replace(/%(s|d)/g, '%%');
+                    messageId   = common.removePlaceholders(messageId);
                     const plurals = [messageId, messageId + 's'],
                           fake    = [messageId],
                           result  = PrivateBin.I18n.translate(messageId);
@@ -56,9 +56,9 @@ describe('I18n', function () {
                 fc.array(fc.string(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix    =    prefix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
-                    params[0] = params[0].replace(/%(s|d)/g, '%%');
-                    postfix   =   postfix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
+                    prefix    =    common.removePlaceholders(prefix).replace(/<a/g, '');
+                    params[0] = common.removePlaceholders(params[0]);
+                    postfix   =   common.removePlaceholders(postfix).replace(/<a/g, '');
                     const translation = PrivateBin.Helper.htmlEntities(prefix + params[0] + postfix);
                     params.unshift(prefix + '%s' + postfix);
                     const result = PrivateBin.I18n.translate.apply(this, params);
@@ -75,9 +75,9 @@ describe('I18n', function () {
                 fc.array(fc.string(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix    =    prefix.replace(/%(s|d)/g, '%%');
-                    params[0] = params[0].replace(/%(s|d)/g, '%%');
-                    postfix   =   postfix.replace(/%(s|d)/g, '%%');
+                    prefix    =    common.removePlaceholders(prefix);
+                    params[0] = common.removePlaceholders(params[0]);
+                    postfix   =   common.removePlaceholders(postfix);
                     const translation = DOMPurify.sanitize(
                         prefix + '<a href="' + params[0] + '"></a>' + postfix, {
                             ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|magnet):)/i,
@@ -100,9 +100,9 @@ describe('I18n', function () {
                 fc.array(fc.string(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix    =    prefix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
-                    params[0] = params[0].replace(/%(s|d)/g, '%%');
-                    postfix   =   postfix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
+                    prefix    =    common.removePlaceholders(prefix).replace(/<a/g, '');
+                    params[0] = common.removePlaceholders(params[0]);
+                    postfix   =   common.removePlaceholders(postfix).replace(/<a/g, '');
                     const tempDiv = document.createElement('textarea');
                     tempDiv.textContent = (prefix + params[0] + postfix);
                     const translation = tempDiv.textContent;
@@ -133,9 +133,9 @@ describe('I18n', function () {
                 fc.array(fc.string(), {minLength: 1}),
                 fc.string(),
                 function (prefix, params, postfix) {
-                    prefix    =    prefix.replace(/%(s|d)/g, '%%').trim();
-                    params[0] = params[0].replace(/%(s|d)/g, '%%').trim();
-                    postfix   =   postfix.replace(/%(s|d)/g, '%%').trim();
+                    prefix    =    common.removePlaceholders(prefix).trim();
+                    params[0] = common.removePlaceholders(params[0]).trim();
+                    postfix   =   common.removePlaceholders(postfix).trim();
                     const translation = DOMPurify.sanitize(
                         prefix + '<a href="' + params[0] + '"></a>' + postfix, {
                             ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|magnet):)/i,