Model.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. 'use strict';
  2. const common = require('../common');
  3. const fc = require('fast-check');
  4. describe('Model', function () {
  5. describe('getExpirationDefault', function () {
  6. before(function () {
  7. PrivateBin.Model.reset();
  8. globalThis.cleanup();
  9. });
  10. it('returns the contents of the element with id "pasteExpiration"', () => {
  11. fc.assert(fc.property(
  12. fc.array(fc.string({minLength: 1}), {minLength: 1}),
  13. fc.string(),
  14. fc.nat(),
  15. function (keys, value, key) {
  16. keys = keys.map(PrivateBin.Helper.htmlEntities);
  17. value = PrivateBin.Helper.htmlEntities(value);
  18. var content = keys.length > key ? keys[key] : keys[0],
  19. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  20. keys.forEach(function(item) {
  21. contents += '<option value="' + item + '"';
  22. if (item === content) {
  23. contents += ' selected="selected"';
  24. }
  25. contents += '>' + value + '</option>';
  26. });
  27. contents += '</select>';
  28. document.body.innerHTML = contents;
  29. var result = PrivateBin.Helper.htmlEntities(
  30. PrivateBin.Model.getExpirationDefault()
  31. );
  32. PrivateBin.Model.reset();
  33. return content === result;
  34. }
  35. ));
  36. });
  37. });
  38. describe('getFormatDefault', function () {
  39. before(function () {
  40. PrivateBin.Model.reset();
  41. });
  42. after(function () {
  43. globalThis.cleanup();
  44. });
  45. it('returns the contents of the element with id "pasteFormatter"', () => {
  46. fc.assert(fc.property(
  47. fc.array(fc.string({minLength: 1}), {minLength: 1}),
  48. fc.string(),
  49. fc.nat(),
  50. function (keys, value, key) {
  51. keys = keys.map(PrivateBin.Helper.htmlEntities);
  52. value = PrivateBin.Helper.htmlEntities(value);
  53. var content = keys.length > key ? keys[key] : keys[0],
  54. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  55. keys.forEach(function(item) {
  56. contents += '<option value="' + item + '"';
  57. if (item === content) {
  58. contents += ' selected="selected"';
  59. }
  60. contents += '>' + value + '</option>';
  61. });
  62. contents += '</select>';
  63. document.body.innerHTML = contents;
  64. var result = PrivateBin.Helper.htmlEntities(
  65. PrivateBin.Model.getFormatDefault()
  66. );
  67. PrivateBin.Model.reset();
  68. return content === result;
  69. }
  70. ));
  71. });
  72. });
  73. describe('getPasteId', function () {
  74. this.timeout(30000);
  75. beforeEach(function () {
  76. PrivateBin.Model.reset();
  77. });
  78. it('returns the query string without separator, if any', () => {
  79. fc.assert(fc.property(
  80. common.fcUrl(true, false),
  81. fc.array(common.fcHexString(), {minLength: 16, maxLength: 16}),
  82. fc.array(common.fcQueryString()),
  83. fc.array(common.fcQueryString()),
  84. function (url, pasteId, queryStart, queryEnd) {
  85. if (queryStart.length > 0) {
  86. queryStart.push('&');
  87. }
  88. if (queryEnd.length > 0) {
  89. queryEnd.unshift('&');
  90. }
  91. url.query = queryStart.concat(pasteId, queryEnd);
  92. const pasteIdString = pasteId.join(''),
  93. clean = globalThis.cleanup('', {url: common.urlToString(url)});
  94. const result = PrivateBin.Model.getPasteId();
  95. PrivateBin.Model.reset();
  96. clean();
  97. return pasteIdString === result;
  98. }
  99. ));
  100. });
  101. it('throws exception on empty query string', () => {
  102. fc.assert(fc.property(
  103. common.fcUrl(true, false),
  104. function (url) {
  105. const clean = globalThis.cleanup('', {url: common.urlToString(url)});
  106. let result = false;
  107. try {
  108. PrivateBin.Model.getPasteId();
  109. }
  110. catch(err) {
  111. result = true;
  112. }
  113. PrivateBin.Model.reset();
  114. clean();
  115. return result;
  116. }
  117. ));
  118. });
  119. it('throws exception on non-hexadecimal document IDs', () => {
  120. const clean = globalThis.cleanup('', {
  121. url: 'https://example.com/?gggggggggggggggg#key'
  122. });
  123. assert.throws(() => PrivateBin.Model.getPasteId());
  124. PrivateBin.Model.reset();
  125. clean();
  126. });
  127. });
  128. describe('getPasteKey', function () {
  129. this.timeout(30000);
  130. beforeEach(function () {
  131. PrivateBin.Model.reset();
  132. });
  133. it('throws exception on v1 URLs', () => {
  134. fc.assert(fc.property(
  135. common.fcUrl(),
  136. function (url) {
  137. url.fragment = '0OIl'; // any non-base58 string
  138. const clean = globalThis.cleanup('', {url: common.urlToString(url)});
  139. let result = false;
  140. try {
  141. PrivateBin.Model.getPasteId();
  142. }
  143. catch(err) {
  144. result = true;
  145. }
  146. PrivateBin.Model.reset();
  147. clean();
  148. return result;
  149. }
  150. ));
  151. });
  152. it('returns the fragment stripped of trailing query parts', () => {
  153. fc.assert(fc.property(
  154. common.fcUrl(),
  155. fc.array(common.fcHashString()),
  156. function (url, trail) {
  157. const fragment = url.fragment.padStart(32, '\u0000');
  158. url.fragment = PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  159. const clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  160. result = PrivateBin.Model.getPasteKey();
  161. PrivateBin.Model.reset();
  162. clean();
  163. return fragment === result;
  164. }
  165. ));
  166. });
  167. it('returns the fragment of a v2 URL', () => {
  168. fc.assert(fc.property(
  169. common.fcUrl(),
  170. function (url) {
  171. // base58 strips leading NULL bytes, so the string is padded with these if not found
  172. const fragment = url.fragment.padStart(32, '\u0000');
  173. url.fragment = PrivateBin.CryptTool.base58encode(fragment);
  174. const clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  175. result = PrivateBin.Model.getPasteKey();
  176. PrivateBin.Model.reset();
  177. clean();
  178. return fragment === result;
  179. }
  180. ));
  181. });
  182. it('returns the v2 fragment stripped of trailing query parts', () => {
  183. fc.assert(fc.property(
  184. common.fcUrl(),
  185. fc.array(common.fcHashString()),
  186. function (url, trail) {
  187. // base58 strips leading NULL bytes, so the string is padded with these if not found
  188. const fragment = url.fragment.padStart(32, '\u0000');
  189. url.fragment = PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  190. const clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  191. result = PrivateBin.Model.getPasteKey();
  192. PrivateBin.Model.reset();
  193. clean();
  194. return fragment === result;
  195. }
  196. ));
  197. });
  198. it('throws exception on empty fragment of the URL', () => {
  199. fc.assert(fc.property(
  200. common.fcUrl(false),
  201. function (url) {
  202. let clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  203. result = false;
  204. try {
  205. PrivateBin.Model.getPasteKey();
  206. }
  207. catch(err) {
  208. result = true;
  209. }
  210. PrivateBin.Model.reset();
  211. clean();
  212. return result;
  213. }
  214. ));
  215. });
  216. });
  217. describe('getTemplate', function () {
  218. beforeEach(function () {
  219. PrivateBin.Model.reset();
  220. });
  221. it('returns the contents of the element with id "[name]template"', () => {
  222. fc.assert(fc.property(
  223. fc.array(common.fcAlnumString(), {minLength: 1}),
  224. fc.array(common.fcA2zString(), {minLength: 1}),
  225. fc.array(common.fcAlnumString(), {minLength: 1}),
  226. function (id, element, value) {
  227. id = id.join('');
  228. element = element.join('');
  229. value = value.join('').trim();
  230. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  231. // table tags can't be alone, so test with a <p> instead
  232. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  233. element = 'p';
  234. }
  235. document.body.innerHTML = (
  236. '<div id="templates"><' + element + ' id="' + id +
  237. 'template">' + value + '</' + element + '></div>'
  238. );
  239. PrivateBin.Model.init();
  240. var template = '<' + element + ' id="' + id + '">' + value +
  241. '</' + element + '>',
  242. templateEl = PrivateBin.Model.getTemplate(id),
  243. wrapper = document.createElement('p');
  244. wrapper.appendChild(templateEl.cloneNode(true));
  245. var result = wrapper.innerHTML;
  246. PrivateBin.Model.reset();
  247. return template === result;
  248. }
  249. ));
  250. });
  251. });
  252. });