common.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // Recreates the jsdom environment and reloads privatebin.js into it.
  9. // The reload is necessary because modules close over DOM element references
  10. // in their init() methods, and pending async callbacks (e.g. setTimeout in
  11. // CopyToClipboard) would otherwise fire against elements from a dead window.
  12. global.cleanup = function (...args) {
  13. if (typeof currentCleanup === 'function') {
  14. currentCleanup();
  15. }
  16. currentCleanup = global.jsdom(...args);
  17. delete require.cache[require.resolve('./privatebin')];
  18. delete require.cache[require.resolve('./legacy')];
  19. require('./privatebin');
  20. global.PrivateBin = window.PrivateBin;
  21. return global.cleanup;
  22. };
  23. global.fs = require('fs');
  24. global.WebCrypto = require('@peculiar/webcrypto').Crypto;
  25. // application libraries to test
  26. global.zlib = require('./zlib').zlib;
  27. require('./prettify');
  28. global.prettyPrint = window.PR ? window.PR.prettyPrint : function() {};
  29. global.prettyPrintOne = window.PR ? window.PR.prettyPrintOne : function() {};
  30. global.showdown = require('./showdown-2.1.0');
  31. global.DOMPurify = require('./purify-3.4.17');
  32. global.baseX = require('./base-x-5.0.1').baseX;
  33. global.Legacy = require('./legacy').Legacy;
  34. require('./privatebin');
  35. global.PrivateBin = window.PrivateBin;
  36. // internal variables
  37. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  38. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  39. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  40. alnumString = a2zString.concat(digitString),
  41. hexString = digitString.concat(['a','b','c','d','e','f']),
  42. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  43. hashString = queryString.concat(['!']),
  44. base64String = alnumString.concat(['+','/','=']).concat(
  45. a2zString.map(function(c) {
  46. return c.toUpperCase();
  47. })
  48. ),
  49. schemas = ['ftp','http','https'],
  50. 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', 'zh-tw'],
  51. mimeTypes = ['image/png', 'application/octet-stream'].concat(Object.keys(require('mime-db'))),
  52. formats = ['plaintext', 'markdown', 'syntaxhighlighting'];
  53. // common testing helper functions
  54. // as of jsDOM 22 the base64 functions provided in the DOM are more restrictive
  55. // than browser implementation and throw when being passed invalid unicode
  56. // codepoints - as we use these in the encryption with binary data, we need
  57. // these to be character encoding agnostic
  58. exports.atob = function(encoded) {
  59. return Buffer.from(encoded, 'base64').toString('binary');
  60. };
  61. exports.btoa = function(text) {
  62. return Buffer.from(text, 'binary').toString('base64');
  63. };
  64. // provides random lowercase characters from a to z
  65. exports.fcA2zString = function() {
  66. return fc.constantFrom(...a2zString);
  67. };
  68. // removes %s and %d placeholders from a string, including the whole run of
  69. // percent signs before them, so the removal can't create new ones (i.e. %%dd)
  70. exports.removePlaceholders = function(string) {
  71. return string.replace(/%+(?=[sd])/g, '');
  72. };
  73. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  74. exports.fcAlnumString = function() {
  75. return fc.constantFrom(...alnumString);
  76. };
  77. //provides random characters allowed in hexadecimal notation
  78. exports.fcHexString = function() {
  79. return fc.constantFrom(...hexString);
  80. };
  81. // provides random characters allowed in GET queries
  82. exports.fcQueryString = function() {
  83. return fc.constantFrom(...queryString);
  84. };
  85. // provides random characters allowed in hash queries
  86. exports.fcHashString = function() {
  87. return fc.constantFrom(...hashString);
  88. };
  89. // provides random characters allowed in base64 encoded strings
  90. exports.fcBase64String = function() {
  91. return fc.constantFrom(...base64String);
  92. };
  93. // provides a random URL schema supported by the whatwg-url library
  94. exports.fcSchemas = function(withFtp = true) {
  95. return fc.constantFrom(...(withFtp ? schemas : schemas.slice(1)));
  96. };
  97. // provides a random supported language string
  98. exports.fcSupportedLanguages = function() {
  99. return fc.constantFrom(...supportedLanguages);
  100. };
  101. // provides a random mime type
  102. exports.fcMimeTypes = function() {
  103. return fc.constantFrom(...mimeTypes);
  104. };
  105. // provides a random PrivateBin document formatter
  106. exports.fcFormats = function() {
  107. return fc.constantFrom(...formats);
  108. };
  109. // provides random URLs
  110. exports.fcUrl = function(withFragment = true, withQuery = true) {
  111. let url = {
  112. schema: exports.fcSchemas(),
  113. address: fc.array(exports.fcA2zString(), {minLength: 1}),
  114. };
  115. if (withFragment) {
  116. url.fragment = fc.string();
  117. }
  118. if(withQuery) {
  119. url.query = fc.array(exports.fcQueryString());
  120. }
  121. return fc.record(url);
  122. };
  123. exports.urlToString = function (url) {
  124. return url.schema + '://' + url.address.join('') + '/' + (url.query ? '?' +
  125. encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
  126. (url.fragment ? '#' + encodeURI(url.fragment) : '');
  127. };
  128. exports.enableClipboard = function () {
  129. // @ts-ignore
  130. navigator.clipboard = (function () {
  131. let savedText = "";
  132. async function writeText(text) {
  133. savedText = text;
  134. };
  135. async function readText() {
  136. return savedText;
  137. };
  138. return {
  139. writeText,
  140. readText,
  141. };
  142. })();
  143. };