all files / js/ common.js

98.39% Statements 61/62
87.5% Branches 7/8
100% Functions 16/16
98.39% Lines 61/62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                          26×                                                     641×       835× 835× 835× 835×                 835× 835× 33×       835× 835× 413×   835× 789×                             1624×   193×         20×       14×       11×                                          
'use strict';
 
// testing prerequisites
global.assert = require('assert');
global.jsc = require('jsverify');
global.jsdom = require('jsdom-global');
global.cleanup = global.jsdom();
global.fs = require('fs');
 
// application libraries to test
global.$ = global.jQuery = require('./jquery-3.3.1');
global.sjcl = require('./sjcl-1.0.7');
global.Base64 = require('./base64-2.4.5').Base64;
global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
require('./prettify');
global.prettyPrint = window.PR.prettyPrint;
global.prettyPrintOne = window.PR.prettyPrintOne;
global.showdown = require('./showdown-1.8.6');
global.DOMPurify = require('./purify-1.0.7');
require('./bootstrap-3.3.7');
require('./privatebin');
 
// internal variables
var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
                     'n','o','p','q','r','s','t','u','v','w','x','y','z'],
    alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
    queryString = alnumString.concat(['+','%','&','.','*','-','_']),
    hashString = queryString.concat(['!']),
    base64String = alnumString.concat(['+','/','=']).concat(
        a2zString.map(function(c) {
            return c.toUpperCase();
        })
    ),
    schemas = ['ftp','gopher','http','https','ws','wss'],
    supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
    mimeTypes = ['image/png', 'application/octet-stream'],
    formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
    /**
     * character to HTML entity lookup table
     *
     * @see    {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
     */
    entityMap = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#39;',
        '/': '&#x2F;',
        '`': '&#x60;',
        '=': '&#x3D;'
    },
    logFile = fs.createWriteStream('test.log'),
    mimeFile = fs.createReadStream('/etc/mime.types'),
    mimeLine = '';
 
// redirect console messages to log file
console.info = console.warn = console.error = function () {
    logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
};
 
// populate mime types from environment
mimeFile.on('data', function(data) {
    mimeLine += data;
    var index = mimeLine.indexOf('\n');
    while (index > -1) {
        var line = mimeLine.substring(0, index);
        mimeLine = mimeLine.substring(index + 1);
        parseMime(line);
        index = mimeLine.indexOf('\n');
    }
});
 
mimeFile.on('end', function() {
    Iif (mimeLine.length > 0) {
        parseMime(mimeLine);
    }
});
 
function parseMime(line) {
    // ignore comments
    var index = line.indexOf('#');
    if (index > -1) {
        line = line.substring(0, index);
    }
 
    // ignore bits after tabs
    index = line.indexOf('\t');
    if (index > -1) {
        line = line.substring(0, index);
    }
    if (line.length > 0) {
        mimeTypes.push(line);
    }
}
 
// common testing helper functions
 
/**
 * convert all applicable characters to HTML entities
 *
 * @see    {@link https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content}
 * @name   htmlEntities
 * @function
 * @param  {string} str
 * @return {string} escaped HTML
 */
exports.htmlEntities = function(str) {
    return String(str).replace(
        /[&<>"'`=\/]/g, function(s) {
            return entityMap[s];
        });
};
 
// provides random lowercase characters from a to z
exports.jscA2zString = function() {
    return jsc.elements(a2zString);
};
 
// provides random lowercase alpha numeric characters (a to z and 0 to 9)
exports.jscAlnumString = function() {
    return jsc.elements(alnumString);
};
 
// provides random characters allowed in GET queries
exports.jscQueryString = function() {
    return jsc.elements(queryString);
};
 
// provides random characters allowed in hash queries
exports.jscHashString = function() {
    return jsc.elements(hashString);
};
 
// provides random characters allowed in base64 encoded strings
exports.jscBase64String = function() {
    return jsc.elements(base64String);
};
 
// provides a random URL schema supported by the whatwg-url library
exports.jscSchemas = function() {
    return jsc.elements(schemas);
};
 
// provides a random supported language string
exports.jscSupportedLanguages = function() {
    return jsc.elements(supportedLanguages);
};
 
// provides a random mime type
exports.jscMimeTypes = function() {
    return jsc.elements(mimeTypes);
};
 
// provides a random PrivateBin paste formatter
exports.jscFormats = function() {
    return jsc.elements(formats);
};