common.js 5.4 KB

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