common.js 5.4 KB

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