common.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. // testing prerequisites
  3. global.assert = require('assert');
  4. const fc = require('fast-check');
  5. global.jsdom = require('jsdom-global');
  6. // initial DOM environment created by jsdom-global
  7. let currentCleanup = global.jsdom();
  8. // Loads bootstrap into the current jsdom environment. It registers its
  9. // data API event handlers on the document when loaded, so it needs to be
  10. // reloaded for every new window, like privatebin.js.
  11. function loadBootstrap() {
  12. delete require.cache[require.resolve('./bootstrap-5.3.8')];
  13. global.bootstrap = window.bootstrap = require('./bootstrap-5.3.8');
  14. }
  15. // Recreates the jsdom environment and reloads privatebin.js into it.
  16. // The reload is necessary because modules close over DOM element references
  17. // in their init() methods, and pending async callbacks (e.g. setTimeout in
  18. // CopyToClipboard) would otherwise fire against elements from a dead window.
  19. global.cleanup = function (...args) {
  20. if (typeof currentCleanup === 'function') {
  21. currentCleanup();
  22. }
  23. currentCleanup = global.jsdom(...args);
  24. loadBootstrap();
  25. delete require.cache[require.resolve('./privatebin')];
  26. delete require.cache[require.resolve('./legacy')];
  27. require('./privatebin');
  28. global.PrivateBin = window.PrivateBin;
  29. return global.cleanup;
  30. };
  31. global.fs = require('fs');
  32. global.WebCrypto = require('@peculiar/webcrypto').Crypto;
  33. // application libraries to test
  34. global.zlib = require('./zlib').zlib;
  35. require('./prettify');
  36. global.prettyPrint = window.PR ? window.PR.prettyPrint : function() {};
  37. global.prettyPrintOne = window.PR ? window.PR.prettyPrintOne : function() {};
  38. global.showdown = require('./showdown-2.1.0');
  39. global.DOMPurify = require('./purify-3.4.17');
  40. global.baseX = require('./base-x-5.0.1').baseX;
  41. global.Legacy = require('./legacy').Legacy;
  42. loadBootstrap();
  43. require('./privatebin');
  44. global.PrivateBin = window.PrivateBin;
  45. // internal variables
  46. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  47. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  48. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  49. alnumString = a2zString.concat(digitString),
  50. hexString = digitString.concat(['a','b','c','d','e','f']),
  51. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  52. hashString = queryString.concat(['!']),
  53. base64String = alnumString.concat(['+','/','=']).concat(
  54. a2zString.map(function(c) {
  55. return c.toUpperCase();
  56. })
  57. ),
  58. schemas = ['ftp','http','https'],
  59. supportedLanguages = ['ar', 'bg', 'ca', 'co', 'cs', 'de', 'el', 'es', 'et', 'fi', 'fr', 'he', 'hu', 'id', 'it', 'ja', 'jbo', 'lt', 'no', 'nl', 'pl', 'pt', 'oc', 'ru', 'sk', 'sl', 'th', 'tr', 'uk', 'zh', 'zh-tw'],
  60. mimeTypes = ['image/png', 'application/octet-stream'].concat(Object.keys(require('mime-db'))),
  61. formats = ['plaintext', 'markdown', 'syntaxhighlighting'];
  62. // common testing helper functions
  63. // as of jsDOM 22 the base64 functions provided in the DOM are more restrictive
  64. // than browser implementation and throw when being passed invalid unicode
  65. // codepoints - as we use these in the encryption with binary data, we need
  66. // these to be character encoding agnostic
  67. exports.atob = function(encoded) {
  68. return Buffer.from(encoded, 'base64').toString('binary');
  69. };
  70. exports.btoa = function(text) {
  71. return Buffer.from(text, 'binary').toString('base64');
  72. };
  73. // provides random lowercase characters from a to z
  74. exports.fcA2zString = function() {
  75. return fc.constantFrom(...a2zString);
  76. };
  77. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  78. exports.fcAlnumString = function() {
  79. return fc.constantFrom(...alnumString);
  80. };
  81. //provides random characters allowed in hexadecimal notation
  82. exports.fcHexString = function() {
  83. return fc.constantFrom(...hexString);
  84. };
  85. // provides random characters allowed in GET queries
  86. exports.fcQueryString = function() {
  87. return fc.constantFrom(...queryString);
  88. };
  89. // provides random characters allowed in hash queries
  90. exports.fcHashString = function() {
  91. return fc.constantFrom(...hashString);
  92. };
  93. // provides random characters allowed in base64 encoded strings
  94. exports.fcBase64String = function() {
  95. return fc.constantFrom(...base64String);
  96. };
  97. // provides a random URL schema supported by the whatwg-url library
  98. exports.fcSchemas = function(withFtp = true) {
  99. return fc.constantFrom(...(withFtp ? schemas : schemas.slice(1)));
  100. };
  101. // provides a random supported language string
  102. exports.fcSupportedLanguages = function() {
  103. return fc.constantFrom(...supportedLanguages);
  104. };
  105. // provides a random mime type
  106. exports.fcMimeTypes = function() {
  107. return fc.constantFrom(...mimeTypes);
  108. };
  109. // provides a random PrivateBin document formatter
  110. exports.fcFormats = function() {
  111. return fc.constantFrom(...formats);
  112. };
  113. // provides random URLs
  114. exports.fcUrl = function(withFragment = true, withQuery = true) {
  115. let url = {
  116. schema: exports.fcSchemas(),
  117. address: fc.array(exports.fcA2zString(), {minLength: 1}),
  118. };
  119. if (withFragment) {
  120. url.fragment = fc.string();
  121. }
  122. if(withQuery) {
  123. url.query = fc.array(exports.fcQueryString());
  124. }
  125. return fc.record(url);
  126. };
  127. exports.urlToString = function (url) {
  128. return url.schema + '://' + url.address.join('') + '/' + (url.query ? '?' +
  129. encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
  130. (url.fragment ? '#' + encodeURI(url.fragment) : '');
  131. };
  132. exports.enableClipboard = function () {
  133. // @ts-ignore
  134. navigator.clipboard = (function () {
  135. let savedText = "";
  136. async function writeText(text) {
  137. savedText = text;
  138. };
  139. async function readText() {
  140. return savedText;
  141. };
  142. return {
  143. writeText,
  144. readText,
  145. };
  146. })();
  147. };