common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. // testing prerequisites
  3. global.assert = require('assert');
  4. global.jsc = require('jsverify');
  5. global.jsdom = require('jsdom-global');
  6. global.cleanup = global.jsdom();
  7. global.fs = require('fs');
  8. global.WebCrypto = require('@peculiar/webcrypto').Crypto;
  9. // application libraries to test
  10. global.$ = global.jQuery = require('./jquery-3.7.1');
  11. global.zlib = require('./zlib-1.3.1-1').zlib;
  12. require('./prettify');
  13. global.prettyPrint = window.PR.prettyPrint;
  14. global.prettyPrintOne = window.PR.prettyPrintOne;
  15. global.showdown = require('./showdown-2.1.0');
  16. global.DOMPurify = require('./purify-3.2.6');
  17. global.baseX = require('./base-x-4.0.0').baseX;
  18. global.Legacy = require('./legacy').Legacy;
  19. require('./bootstrap-3.4.1');
  20. require('./privatebin');
  21. // internal variables
  22. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  23. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  24. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  25. alnumString = a2zString.concat(digitString),
  26. hexString = digitString.concat(['a','b','c','d','e','f']),
  27. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  28. hashString = queryString.concat(['!']),
  29. base64String = alnumString.concat(['+','/','=']).concat(
  30. a2zString.map(function(c) {
  31. return c.toUpperCase();
  32. })
  33. ),
  34. schemas = ['ftp','http','https'],
  35. 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'],
  36. mimeTypes = ['image/png', 'application/octet-stream'],
  37. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  38. mimeFile = fs.createReadStream('/etc/mime.types'),
  39. mimeLine = '';
  40. // populate mime types from environment
  41. mimeFile.on('data', function(data) {
  42. mimeLine += data;
  43. var index = mimeLine.indexOf('\n');
  44. while (index > -1) {
  45. var line = mimeLine.substring(0, index);
  46. mimeLine = mimeLine.substring(index + 1);
  47. parseMime(line);
  48. index = mimeLine.indexOf('\n');
  49. }
  50. });
  51. mimeFile.on('end', function() {
  52. if (mimeLine.length > 0) {
  53. parseMime(mimeLine);
  54. }
  55. });
  56. function parseMime(line) {
  57. // ignore comments
  58. var index = line.indexOf('#');
  59. if (index > -1) {
  60. line = line.substring(0, index);
  61. }
  62. // ignore bits after tabs
  63. index = line.indexOf('\t');
  64. if (index > -1) {
  65. line = line.substring(0, index);
  66. }
  67. if (line.length > 0) {
  68. mimeTypes.push(line);
  69. }
  70. }
  71. // common testing helper functions
  72. // as of jsDOM 22 the base64 functions provided in the DOM are more restrictive
  73. // than browser implementation and throw when being passed invalid unicode
  74. // codepoints - as we use these in the encryption with binary data, we need
  75. // these to be character encoding agnostic
  76. exports.atob = function(encoded) {
  77. return Buffer.from(encoded, 'base64').toString('binary');
  78. }
  79. exports.btoa = function(text) {
  80. return Buffer.from(text, 'binary').toString('base64');
  81. }
  82. // provides random lowercase characters from a to z
  83. exports.jscA2zString = function() {
  84. return jsc.elements(a2zString);
  85. };
  86. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  87. exports.jscAlnumString = function() {
  88. return jsc.elements(alnumString);
  89. };
  90. //provides random characters allowed in hexadecimal notation
  91. exports.jscHexString = function() {
  92. return jsc.elements(hexString);
  93. };
  94. // provides random characters allowed in GET queries
  95. exports.jscQueryString = function() {
  96. return jsc.elements(queryString);
  97. };
  98. // provides random characters allowed in hash queries
  99. exports.jscHashString = function() {
  100. return jsc.elements(hashString);
  101. };
  102. // provides random characters allowed in base64 encoded strings
  103. exports.jscBase64String = function() {
  104. return jsc.elements(base64String);
  105. };
  106. // provides a random URL schema supported by the whatwg-url library
  107. exports.jscSchemas = function(withFtp = true) {
  108. return jsc.elements(withFtp ? schemas : schemas.slice(1));
  109. };
  110. // provides a random supported language string
  111. exports.jscSupportedLanguages = function() {
  112. return jsc.elements(supportedLanguages);
  113. };
  114. // provides a random mime type
  115. exports.jscMimeTypes = function() {
  116. return jsc.elements(mimeTypes);
  117. };
  118. // provides a random PrivateBin paste formatter
  119. exports.jscFormats = function() {
  120. return jsc.elements(formats);
  121. };
  122. // provides random URLs
  123. exports.jscUrl = function(withFragment = true, withQuery = true) {
  124. let url = {
  125. schema: exports.jscSchemas(),
  126. address: jsc.nearray(exports.jscA2zString()),
  127. };
  128. if (withFragment) {
  129. url.fragment = jsc.string;
  130. }
  131. if(withQuery) {
  132. url.query = jsc.array(exports.jscQueryString());
  133. }
  134. return jsc.record(url);
  135. };
  136. exports.urlToString = function (url) {
  137. return url.schema + '://' + url.address.join('') + '/' + (url.query ? '?' +
  138. encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
  139. (url.fragment ? '#' + encodeURI(url.fragment) : '');
  140. };
  141. exports.enableClipboard = function () {
  142. navigator.clipboard = (function () {
  143. let savedText = "";
  144. async function writeText(text) {
  145. savedText = text;
  146. };
  147. async function readText() {
  148. return savedText;
  149. };
  150. return {
  151. writeText,
  152. readText,
  153. };
  154. })();
  155. };