1
0

common.js 5.3 KB

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