common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. // testing prerequisites
  3. global.assert = require('assert');
  4. global.jsc = require('jsverify');
  5. global.jsdom = require('jsdom-global');
  6. global.cleanup = global.jsdom();
  7. global.fs = require('fs');
  8. // application libraries to test
  9. global.$ = global.jQuery = require('./jquery-3.4.1');
  10. global.sjcl = require('./sjcl-1.0.7');
  11. global.Base64 = require('./base64-2.4.5').Base64;
  12. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  13. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  14. require('./prettify');
  15. global.prettyPrint = window.PR.prettyPrint;
  16. global.prettyPrintOne = window.PR.prettyPrintOne;
  17. global.showdown = require('./showdown-1.9.1');
  18. global.DOMPurify = require('./purify-2.0.7');
  19. require('./bootstrap-3.3.7');
  20. require('./privatebin');
  21. // internal variables
  22. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  23. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  24. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  25. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  26. hashString = queryString.concat(['!']),
  27. base64String = alnumString.concat(['+','/','=']).concat(
  28. a2zString.map(function(c) {
  29. return c.toUpperCase();
  30. })
  31. ),
  32. schemas = ['ftp','gopher','http','https','ws','wss'],
  33. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  34. mimeTypes = ['image/png', 'application/octet-stream'],
  35. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  36. logFile = fs.createWriteStream('test.log'),
  37. mimeFile = fs.createReadStream('/etc/mime.types'),
  38. mimeLine = '';
  39. // redirect console messages to log file
  40. console.info = console.warn = console.error = function () {
  41. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  42. };
  43. // populate mime types from environment
  44. mimeFile.on('data', function(data) {
  45. mimeLine += data;
  46. var index = mimeLine.indexOf('\n');
  47. while (index > -1) {
  48. var line = mimeLine.substring(0, index);
  49. mimeLine = mimeLine.substring(index + 1);
  50. parseMime(line);
  51. index = mimeLine.indexOf('\n');
  52. }
  53. });
  54. mimeFile.on('end', function() {
  55. if (mimeLine.length > 0) {
  56. parseMime(mimeLine);
  57. }
  58. });
  59. function parseMime(line) {
  60. // ignore comments
  61. var index = line.indexOf('#');
  62. if (index > -1) {
  63. line = line.substring(0, index);
  64. }
  65. // ignore bits after tabs
  66. index = line.indexOf('\t');
  67. if (index > -1) {
  68. line = line.substring(0, index);
  69. }
  70. if (line.length > 0) {
  71. mimeTypes.push(line);
  72. }
  73. }
  74. // common testing helper functions
  75. // provides random lowercase characters from a to z
  76. exports.jscA2zString = function() {
  77. return jsc.elements(a2zString);
  78. };
  79. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  80. exports.jscAlnumString = function() {
  81. return jsc.elements(alnumString);
  82. };
  83. // provides random characters allowed in GET queries
  84. exports.jscQueryString = function() {
  85. return jsc.elements(queryString);
  86. };
  87. // provides random characters allowed in hash queries
  88. exports.jscHashString = function() {
  89. return jsc.elements(hashString);
  90. };
  91. // provides random characters allowed in base64 encoded strings
  92. exports.jscBase64String = function() {
  93. return jsc.elements(base64String);
  94. };
  95. // provides a random URL schema supported by the whatwg-url library
  96. exports.jscSchemas = function() {
  97. return jsc.elements(schemas);
  98. };
  99. // provides a random supported language string
  100. exports.jscSupportedLanguages = function() {
  101. return jsc.elements(supportedLanguages);
  102. };
  103. // provides a random mime type
  104. exports.jscMimeTypes = function() {
  105. return jsc.elements(mimeTypes);
  106. };
  107. // provides a random PrivateBin paste formatter
  108. exports.jscFormats = function() {
  109. return jsc.elements(formats);
  110. };