common.js 4.0 KB

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