Model.js 10 KB

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