1
0

Model.js 11 KB

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