Model.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. });
  120. describe('hasDeleteToken', function () {
  121. it('only recognizes an exact query parameter name', () => {
  122. const cases = [
  123. ['https://example.com/?abcdefghijklmnop&deletetoken=secret', true],
  124. ['https://example.com/?abcdefghijklmnop&redirect=deletetoken', false],
  125. ['https://example.com/?abcdefghijklmnop&notdeletetoken=secret', false]
  126. ];
  127. cases.forEach(function (testCase) {
  128. const clean = globalThis.cleanup('', {url: testCase[0]});
  129. assert.strictEqual(PrivateBin.Model.hasDeleteToken(), testCase[1]);
  130. clean();
  131. });
  132. });
  133. });
  134. describe('getPasteKey', function () {
  135. this.timeout(30000);
  136. beforeEach(function () {
  137. PrivateBin.Model.reset();
  138. });
  139. it('throws exception on v1 URLs', () => {
  140. fc.assert(fc.property(
  141. common.fcUrl(),
  142. function (url) {
  143. url.fragment = '0OIl'; // any non-base58 string
  144. const clean = globalThis.cleanup('', {url: common.urlToString(url)});
  145. let result = false;
  146. try {
  147. PrivateBin.Model.getPasteId();
  148. }
  149. catch(err) {
  150. result = true;
  151. }
  152. PrivateBin.Model.reset();
  153. clean();
  154. return result;
  155. }
  156. ));
  157. });
  158. it('returns the fragment stripped of trailing query parts', () => {
  159. fc.assert(fc.property(
  160. common.fcUrl(),
  161. fc.array(common.fcHashString()),
  162. function (url, trail) {
  163. const fragment = url.fragment.padStart(32, '\u0000');
  164. url.fragment = PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  165. const clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  166. result = PrivateBin.Model.getPasteKey();
  167. PrivateBin.Model.reset();
  168. clean();
  169. return fragment === result;
  170. }
  171. ));
  172. });
  173. it('returns the fragment of a v2 URL', () => {
  174. fc.assert(fc.property(
  175. common.fcUrl(),
  176. function (url) {
  177. // base58 strips leading NULL bytes, so the string is padded with these if not found
  178. const fragment = url.fragment.padStart(32, '\u0000');
  179. url.fragment = PrivateBin.CryptTool.base58encode(fragment);
  180. const clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  181. result = PrivateBin.Model.getPasteKey();
  182. PrivateBin.Model.reset();
  183. clean();
  184. return fragment === result;
  185. }
  186. ));
  187. });
  188. it('returns the v2 fragment stripped of trailing query parts', () => {
  189. fc.assert(fc.property(
  190. common.fcUrl(),
  191. fc.array(common.fcHashString()),
  192. function (url, trail) {
  193. // base58 strips leading NULL bytes, so the string is padded with these if not found
  194. const fragment = url.fragment.padStart(32, '\u0000');
  195. url.fragment = PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  196. const clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  197. result = PrivateBin.Model.getPasteKey();
  198. PrivateBin.Model.reset();
  199. clean();
  200. return fragment === result;
  201. }
  202. ));
  203. });
  204. it('throws exception on empty fragment of the URL', () => {
  205. fc.assert(fc.property(
  206. common.fcUrl(false),
  207. function (url) {
  208. let clean = globalThis.cleanup('', {url: common.urlToString(url)}),
  209. result = false;
  210. try {
  211. PrivateBin.Model.getPasteKey();
  212. }
  213. catch(err) {
  214. result = true;
  215. }
  216. PrivateBin.Model.reset();
  217. clean();
  218. return result;
  219. }
  220. ));
  221. });
  222. });
  223. describe('getTemplate', function () {
  224. beforeEach(function () {
  225. PrivateBin.Model.reset();
  226. });
  227. it('returns the contents of the element with id "[name]template"', () => {
  228. fc.assert(fc.property(
  229. fc.array(common.fcAlnumString(), {minLength: 1}),
  230. fc.array(common.fcA2zString(), {minLength: 1}),
  231. fc.array(common.fcAlnumString(), {minLength: 1}),
  232. function (id, element, value) {
  233. id = id.join('');
  234. element = element.join('');
  235. value = value.join('').trim();
  236. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  237. // table tags can't be alone, so test with a <p> instead
  238. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  239. element = 'p';
  240. }
  241. document.body.innerHTML = (
  242. '<div id="templates"><' + element + ' id="' + id +
  243. 'template">' + value + '</' + element + '></div>'
  244. );
  245. PrivateBin.Model.init();
  246. var template = '<' + element + ' id="' + id + '">' + value +
  247. '</' + element + '>',
  248. templateEl = PrivateBin.Model.getTemplate(id),
  249. wrapper = document.createElement('p');
  250. wrapper.appendChild(templateEl.cloneNode(true));
  251. var result = wrapper.innerHTML;
  252. PrivateBin.Model.reset();
  253. return template === result;
  254. }
  255. ));
  256. });
  257. });
  258. });