common.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // wrap cleanup so that calling it recreates a fresh jsdom environment
  9. global.cleanup = function (...args) {
  10. // remove previous environment
  11. if (typeof currentCleanup === 'function') {
  12. currentCleanup();
  13. }
  14. // create a new jsdom environment
  15. currentCleanup = global.jsdom(...args);
  16. // Make sure window and document are available in global scope for module loading
  17. // jsdom-global sets them, but we need to ensure they're accessible
  18. if (typeof window === 'undefined') {
  19. throw new Error('jsdom-global failed to set up window');
  20. }
  21. if (typeof document === 'undefined') {
  22. throw new Error('jsdom-global failed to set up document');
  23. }
  24. // Clear module cache to ensure modules are re-evaluated with new jsdom environment
  25. delete require.cache[require.resolve('./privatebin')];
  26. delete require.cache[require.resolve('./legacy')];
  27. require('./privatebin');
  28. if (typeof PrivateBin === 'undefined') {
  29. throw new Error('PrivateBin module did not load correctly');
  30. }
  31. // also re-export the PrivateBin namespace if available
  32. if (typeof window !== 'undefined' && window.PrivateBin) {
  33. global.PrivateBin = window.PrivateBin;
  34. if (global.$) {
  35. global.$.PrivateBin = window.PrivateBin;
  36. }
  37. }
  38. return global.cleanup;
  39. };
  40. global.fs = require('fs');
  41. global.WebCrypto = require('@peculiar/webcrypto').Crypto;
  42. // application libraries to test
  43. global.$ = global.jQuery = require('./jquery-3.7.1');
  44. global.zlib = require('./zlib').zlib;
  45. require('./prettify');
  46. global.prettyPrint = window.PR ? window.PR.prettyPrint : function() {};
  47. global.prettyPrintOne = window.PR ? window.PR.prettyPrintOne : function() {};
  48. global.showdown = require('./showdown-2.1.0');
  49. global.DOMPurify = require('./purify-3.4.1');
  50. global.baseX = require('./base-x-5.0.1').baseX;
  51. global.Legacy = require('./legacy').Legacy;
  52. require('./privatebin');
  53. // provide global access to the namespace so tests can reference it directly
  54. if (typeof window !== 'undefined' && window.PrivateBin) {
  55. global.PrivateBin = window.PrivateBin;
  56. // keep the old jQuery alias around just in case some tests still use it
  57. if (global.$) {
  58. global.$.PrivateBin = window.PrivateBin;
  59. }
  60. }
  61. // internal variables
  62. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  63. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  64. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  65. alnumString = a2zString.concat(digitString),
  66. hexString = digitString.concat(['a','b','c','d','e','f']),
  67. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  68. hashString = queryString.concat(['!']),
  69. base64String = alnumString.concat(['+','/','=']).concat(
  70. a2zString.map(function(c) {
  71. return c.toUpperCase();
  72. })
  73. ),
  74. schemas = ['ftp','http','https'],
  75. 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'],
  76. mimeTypes = ['image/png', 'application/octet-stream'].concat(Object.keys(require('mime-db'))),
  77. formats = ['plaintext', 'markdown', 'syntaxhighlighting'];
  78. // common testing helper functions
  79. // as of jsDOM 22 the base64 functions provided in the DOM are more restrictive
  80. // than browser implementation and throw when being passed invalid unicode
  81. // codepoints - as we use these in the encryption with binary data, we need
  82. // these to be character encoding agnostic
  83. exports.atob = function(encoded) {
  84. return Buffer.from(encoded, 'base64').toString('binary');
  85. };
  86. exports.btoa = function(text) {
  87. return Buffer.from(text, 'binary').toString('base64');
  88. };
  89. // provides random lowercase characters from a to z
  90. exports.fcA2zString = function() {
  91. return fc.constantFrom(...a2zString);
  92. };
  93. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  94. exports.fcAlnumString = function() {
  95. return fc.constantFrom(...alnumString);
  96. };
  97. //provides random characters allowed in hexadecimal notation
  98. exports.fcHexString = function() {
  99. return fc.constantFrom(...hexString);
  100. };
  101. // provides random characters allowed in GET queries
  102. exports.fcQueryString = function() {
  103. return fc.constantFrom(...queryString);
  104. };
  105. // provides random characters allowed in hash queries
  106. exports.fcHashString = function() {
  107. return fc.constantFrom(...hashString);
  108. };
  109. // provides random characters allowed in base64 encoded strings
  110. exports.fcBase64String = function() {
  111. return fc.constantFrom(...base64String);
  112. };
  113. // provides a random URL schema supported by the whatwg-url library
  114. exports.fcSchemas = function(withFtp = true) {
  115. return fc.constantFrom(...(withFtp ? schemas : schemas.slice(1)));
  116. };
  117. // provides a random supported language string
  118. exports.fcSupportedLanguages = function() {
  119. return fc.constantFrom(...supportedLanguages);
  120. };
  121. // provides a random mime type
  122. exports.fcMimeTypes = function() {
  123. return fc.constantFrom(...mimeTypes);
  124. };
  125. // provides a random PrivateBin document formatter
  126. exports.fcFormats = function() {
  127. return fc.constantFrom(...formats);
  128. };
  129. // provides random URLs
  130. exports.fcUrl = function(withFragment = true, withQuery = true) {
  131. let url = {
  132. schema: exports.fcSchemas(),
  133. address: fc.array(exports.fcA2zString(), {minLength: 1}),
  134. };
  135. if (withFragment) {
  136. url.fragment = fc.string();
  137. }
  138. if(withQuery) {
  139. url.query = fc.array(exports.fcQueryString());
  140. }
  141. return fc.record(url);
  142. };
  143. exports.urlToString = function (url) {
  144. return url.schema + '://' + url.address.join('') + '/' + (url.query ? '?' +
  145. encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
  146. (url.fragment ? '#' + encodeURI(url.fragment) : '');
  147. };
  148. exports.enableClipboard = function () {
  149. // @ts-ignore
  150. navigator.clipboard = (function () {
  151. let savedText = "";
  152. async function writeText(text) {
  153. savedText = text;
  154. };
  155. async function readText() {
  156. return savedText;
  157. };
  158. return {
  159. writeText,
  160. readText,
  161. };
  162. })();
  163. };