common.js 7.1 KB

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