1
0

common.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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'],
  77. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  78. mimeFile = fs.createReadStream('/etc/mime.types'),
  79. mimeLine = '';
  80. // populate mime types from environment
  81. mimeFile.on('data', function(data) {
  82. mimeLine += data;
  83. var index = mimeLine.indexOf('\n');
  84. while (index > -1) {
  85. var line = mimeLine.substring(0, index);
  86. mimeLine = mimeLine.substring(index + 1);
  87. parseMime(line);
  88. index = mimeLine.indexOf('\n');
  89. }
  90. });
  91. mimeFile.on('end', function() {
  92. if (mimeLine.length > 0) {
  93. parseMime(mimeLine);
  94. }
  95. });
  96. function parseMime(line) {
  97. // ignore comments
  98. var index = line.indexOf('#');
  99. if (index > -1) {
  100. line = line.substring(0, index);
  101. }
  102. // ignore bits after tabs
  103. index = line.indexOf('\t');
  104. if (index > -1) {
  105. line = line.substring(0, index);
  106. }
  107. if (line.length > 0) {
  108. mimeTypes.push(line);
  109. }
  110. }
  111. // common testing helper functions
  112. // as of jsDOM 22 the base64 functions provided in the DOM are more restrictive
  113. // than browser implementation and throw when being passed invalid unicode
  114. // codepoints - as we use these in the encryption with binary data, we need
  115. // these to be character encoding agnostic
  116. exports.atob = function(encoded) {
  117. return Buffer.from(encoded, 'base64').toString('binary');
  118. };
  119. exports.btoa = function(text) {
  120. return Buffer.from(text, 'binary').toString('base64');
  121. };
  122. // provides random lowercase characters from a to z
  123. exports.fcA2zString = function() {
  124. return fc.constantFrom(...a2zString);
  125. };
  126. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  127. exports.fcAlnumString = function() {
  128. return fc.constantFrom(...alnumString);
  129. };
  130. //provides random characters allowed in hexadecimal notation
  131. exports.fcHexString = function() {
  132. return fc.constantFrom(...hexString);
  133. };
  134. // provides random characters allowed in GET queries
  135. exports.fcQueryString = function() {
  136. return fc.constantFrom(...queryString);
  137. };
  138. // provides random characters allowed in hash queries
  139. exports.fcHashString = function() {
  140. return fc.constantFrom(...hashString);
  141. };
  142. // provides random characters allowed in base64 encoded strings
  143. exports.fcBase64String = function() {
  144. return fc.constantFrom(...base64String);
  145. };
  146. // provides a random URL schema supported by the whatwg-url library
  147. exports.fcSchemas = function(withFtp = true) {
  148. return fc.constantFrom(...(withFtp ? schemas : schemas.slice(1)));
  149. };
  150. // provides a random supported language string
  151. exports.fcSupportedLanguages = function() {
  152. return fc.constantFrom(...supportedLanguages);
  153. };
  154. // provides a random mime type
  155. exports.fcMimeTypes = function() {
  156. return fc.constantFrom(...mimeTypes);
  157. };
  158. // provides a random PrivateBin document formatter
  159. exports.fcFormats = function() {
  160. return fc.constantFrom(...formats);
  161. };
  162. // provides random URLs
  163. exports.fcUrl = function(withFragment = true, withQuery = true) {
  164. let url = {
  165. schema: exports.fcSchemas(),
  166. address: fc.array(exports.fcA2zString(), {minLength: 1}),
  167. };
  168. if (withFragment) {
  169. url.fragment = fc.string();
  170. }
  171. if(withQuery) {
  172. url.query = fc.array(exports.fcQueryString());
  173. }
  174. return fc.record(url);
  175. };
  176. exports.urlToString = function (url) {
  177. return url.schema + '://' + url.address.join('') + '/' + (url.query ? '?' +
  178. encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
  179. (url.fragment ? '#' + encodeURI(url.fragment) : '');
  180. };
  181. exports.enableClipboard = function () {
  182. // @ts-ignore
  183. navigator.clipboard = (function () {
  184. let savedText = "";
  185. async function writeText(text) {
  186. savedText = text;
  187. };
  188. async function readText() {
  189. return savedText;
  190. };
  191. return {
  192. writeText,
  193. readText,
  194. };
  195. })();
  196. };