common.js 6.7 KB

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