common.js 6.9 KB

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