common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.1');
  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'],
  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. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  69. exports.fcAlnumString = function() {
  70. return fc.constantFrom(...alnumString);
  71. };
  72. //provides random characters allowed in hexadecimal notation
  73. exports.fcHexString = function() {
  74. return fc.constantFrom(...hexString);
  75. };
  76. // provides random characters allowed in GET queries
  77. exports.fcQueryString = function() {
  78. return fc.constantFrom(...queryString);
  79. };
  80. // provides random characters allowed in hash queries
  81. exports.fcHashString = function() {
  82. return fc.constantFrom(...hashString);
  83. };
  84. // provides random characters allowed in base64 encoded strings
  85. exports.fcBase64String = function() {
  86. return fc.constantFrom(...base64String);
  87. };
  88. // provides a random URL schema supported by the whatwg-url library
  89. exports.fcSchemas = function(withFtp = true) {
  90. return fc.constantFrom(...(withFtp ? schemas : schemas.slice(1)));
  91. };
  92. // provides a random supported language string
  93. exports.fcSupportedLanguages = function() {
  94. return fc.constantFrom(...supportedLanguages);
  95. };
  96. // provides a random mime type
  97. exports.fcMimeTypes = function() {
  98. return fc.constantFrom(...mimeTypes);
  99. };
  100. // provides a random PrivateBin document formatter
  101. exports.fcFormats = function() {
  102. return fc.constantFrom(...formats);
  103. };
  104. // provides random URLs
  105. exports.fcUrl = function(withFragment = true, withQuery = true) {
  106. let url = {
  107. schema: exports.fcSchemas(),
  108. address: fc.array(exports.fcA2zString(), {minLength: 1}),
  109. };
  110. if (withFragment) {
  111. url.fragment = fc.string();
  112. }
  113. if(withQuery) {
  114. url.query = fc.array(exports.fcQueryString());
  115. }
  116. return fc.record(url);
  117. };
  118. exports.urlToString = function (url) {
  119. return url.schema + '://' + url.address.join('') + '/' + (url.query ? '?' +
  120. encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
  121. (url.fragment ? '#' + encodeURI(url.fragment) : '');
  122. };
  123. exports.enableClipboard = function () {
  124. // @ts-ignore
  125. navigator.clipboard = (function () {
  126. let savedText = "";
  127. async function writeText(text) {
  128. savedText = text;
  129. };
  130. async function readText() {
  131. return savedText;
  132. };
  133. return {
  134. writeText,
  135. readText,
  136. };
  137. })();
  138. };