common.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.URL = require('jsdom-url').URL;
  8. global.fs = require('fs');
  9. global.WebCrypto = require('@peculiar/webcrypto').Crypto;
  10. require('fake-indexeddb/auto');
  11. // application libraries to test
  12. global.$ = global.jQuery = require('./jquery-3.4.1');
  13. global.RawDeflate = require('./rawinflate-0.3').RawDeflate;
  14. global.zlib = require('./zlib-1.2.11').zlib;
  15. require('./prettify');
  16. global.prettyPrint = window.PR.prettyPrint;
  17. global.prettyPrintOne = window.PR.prettyPrintOne;
  18. global.showdown = require('./showdown-1.9.1');
  19. global.DOMPurify = require('./purify-2.0.8');
  20. global.baseX = require('./base-x-3.0.7').baseX;
  21. global.Legacy = require('./legacy').Legacy;
  22. require('./bootstrap-3.3.7');
  23. require('./privatebin');
  24. // internal variables
  25. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  26. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  27. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  28. alnumString = a2zString.concat(digitString),
  29. hexString = digitString.concat(['a','b','c','d','e','f']),
  30. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  31. hashString = queryString.concat(['!']),
  32. base64String = alnumString.concat(['+','/','=']).concat(
  33. a2zString.map(function(c) {
  34. return c.toUpperCase();
  35. })
  36. ),
  37. schemas = ['ftp','http','https'],
  38. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  39. mimeTypes = ['image/png', 'application/octet-stream'],
  40. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  41. mimeFile = fs.createReadStream('/etc/mime.types'),
  42. mimeLine = '';
  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. exports.atob = atob;
  76. exports.btoa = btoa;
  77. // provides random lowercase characters from a to z
  78. exports.jscA2zString = function() {
  79. return jsc.elements(a2zString);
  80. };
  81. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  82. exports.jscAlnumString = function() {
  83. return jsc.elements(alnumString);
  84. };
  85. //provides random characters allowed in hexadecimal notation
  86. exports.jscHexString = function() {
  87. return jsc.elements(hexString);
  88. };
  89. // provides random characters allowed in GET queries
  90. exports.jscQueryString = function() {
  91. return jsc.elements(queryString);
  92. };
  93. // provides random characters allowed in hash queries
  94. exports.jscHashString = function() {
  95. return jsc.elements(hashString);
  96. };
  97. // provides random characters allowed in base64 encoded strings
  98. exports.jscBase64String = function() {
  99. return jsc.elements(base64String);
  100. };
  101. // provides a random URL schema supported by the whatwg-url library
  102. exports.jscSchemas = function() {
  103. return jsc.elements(schemas);
  104. };
  105. // provides a random supported language string
  106. exports.jscSupportedLanguages = function() {
  107. return jsc.elements(supportedLanguages);
  108. };
  109. // provides a random mime type
  110. exports.jscMimeTypes = function() {
  111. return jsc.elements(mimeTypes);
  112. };
  113. // provides a random PrivateBin paste formatter
  114. exports.jscFormats = function() {
  115. return jsc.elements(formats);
  116. };