trie.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import { createTrie } from './trie';
  2. import { describe, it } from 'mocha';
  3. import { expect } from 'chai';
  4. // describe('hostname to tokens', () => {
  5. // it('should split hostname into tokens.', () => {
  6. // expect(hostnameToTokens('.blog.skk.moe')).to.deep.equal([
  7. // '.',
  8. // 'blog',
  9. // '.',
  10. // 'skk',
  11. // '.',
  12. // 'moe'
  13. // ]);
  14. // expect(hostnameToTokens('blog.skk.moe')).to.deep.equal([
  15. // 'blog',
  16. // '.',
  17. // 'skk',
  18. // '.',
  19. // 'moe'
  20. // ]);
  21. // expect(hostnameToTokens('skk.moe')).to.deep.equal([
  22. // 'skk',
  23. // '.',
  24. // 'moe'
  25. // ]);
  26. // expect(hostnameToTokens('moe')).to.deep.equal([
  27. // 'moe'
  28. // ]);
  29. // });
  30. // });
  31. describe('Trie', () => {
  32. it('should be possible to add domains to a Trie.', () => {
  33. const trie = createTrie(null, false);
  34. trie.add('a.skk.moe');
  35. trie.add('skk.moe');
  36. trie.add('anotherskk.moe');
  37. expect(trie.size).to.equal(3);
  38. expect(trie.has('a.skk.moe'), 'a.skk.moe').to.equal(true);
  39. expect(trie.has('skk.moe'), 'skk.moe').to.equal(true);
  40. expect(trie.has('anotherskk.moe'), 'anotherskk.moe').to.equal(true);
  41. expect(trie.has('example.com'), 'example.com').to.equal(false);
  42. expect(trie.has('skk.mo'), 'skk.mo').to.equal(false);
  43. expect(trie.has('another.skk.moe'), 'another.skk.moe').to.equal(false);
  44. });
  45. it('adding the same item several times should not increase size.', () => {
  46. const trie = createTrie(null, false);
  47. trie.add('skk.moe');
  48. trie.add('blog.skk.moe');
  49. // eslint-disable-next-line sukka/no-element-overwrite -- deliberately do testing
  50. trie.add('skk.moe');
  51. expect(trie.size).to.equal(2);
  52. expect(trie.has('skk.moe')).to.equal(true);
  53. });
  54. it('should be possible to set the null sequence.', () => {
  55. const trie = createTrie(null, false);
  56. trie.add('');
  57. expect(trie.has('')).to.equal(true);
  58. const trie2 = createTrie(null, true);
  59. trie2.add('');
  60. expect(trie2.has('')).to.equal(true);
  61. });
  62. it('should be possible to delete items.', () => {
  63. const trie = createTrie(null, false);
  64. trie.add('skk.moe');
  65. trie.add('blog.skk.moe');
  66. trie.add('example.com');
  67. trie.add('moe.sb');
  68. expect(trie.delete('')).to.equal(false);
  69. expect(trie.delete('')).to.equal(false);
  70. expect(trie.delete('example.org')).to.equal(false);
  71. expect(trie.delete('skk.moe')).to.equal(true);
  72. expect(trie.has('skk.moe')).to.equal(false);
  73. expect(trie.has('moe.sb')).to.equal(true);
  74. expect(trie.size).to.equal(3);
  75. expect(trie.delete('example.com')).to.equal(true);
  76. expect(trie.size).to.equal(2);
  77. expect(trie.delete('moe.sb')).to.equal(true);
  78. expect(trie.size).to.equal(1);
  79. });
  80. it('should be possible to check the existence of a sequence in the Trie.', () => {
  81. const trie = createTrie(null, true);
  82. trie.add('example.org.skk.moe');
  83. expect(trie.has('example.org.skk.moe')).to.equal(true);
  84. expect(trie.has('skk.moe')).to.equal(false);
  85. expect(trie.has('example.org')).to.equal(false);
  86. expect(trie.has('')).to.equal(false);
  87. });
  88. it('should be possible to retrieve items matching the given prefix.', () => {
  89. const trie = createTrie(null, false);
  90. trie.add('example.com');
  91. trie.add('blog.example.com');
  92. trie.add('cdn.example.com');
  93. trie.add('example.org');
  94. expect(trie.find('example.com'), 'example.com').to.deep.equal(['example.com', 'cdn.example.com', 'blog.example.com']);
  95. expect(trie.find('com'), 'com').to.deep.equal(['example.com', 'cdn.example.com', 'blog.example.com']);
  96. expect(trie.find('.example.com'), '.example.com').to.deep.equal(['cdn.example.com', 'blog.example.com']);
  97. expect(trie.find('org'), 'prg').to.deep.equal(['example.org']);
  98. expect(trie.find('example.net'), 'example.net').to.deep.equal([]);
  99. expect(trie.find(''), '').to.deep.equal(['example.org', 'example.com', 'cdn.example.com', 'blog.example.com']);
  100. });
  101. it('should be possible to retrieve items matching the given prefix even with a smol trie', () => {
  102. const trie = createTrie(null, true);
  103. trie.add('.example.com');
  104. trie.add('example.com');
  105. trie.add('blog.example.com');
  106. trie.add('cdn.example.com');
  107. trie.add('example.org');
  108. expect(trie.find('example.com')).to.deep.equal(['.example.com']);
  109. expect(trie.find('com')).to.deep.equal(['.example.com']);
  110. expect(trie.find('.example.com')).to.deep.equal(['.example.com']);
  111. expect(trie.find('org')).to.deep.equal(['example.org']);
  112. expect(trie.find('example.net')).to.deep.equal([]);
  113. expect(trie.find('')).to.deep.equal(['example.org', '.example.com']);
  114. });
  115. it('should be possible to create a trie from an arbitrary iterable.', () => {
  116. let trie = createTrie(['skk.moe', 'blog.skk.moe'], false);
  117. expect(trie.size).to.equal(2);
  118. expect(trie.has('skk.moe')).to.equal(true);
  119. trie = createTrie(new Set(['skk.moe', 'example.com']), false);
  120. expect(trie.size).to.equal(2);
  121. expect(trie.has('skk.moe')).to.equal(true);
  122. });
  123. });
  124. describe('surge domainset dedupe', () => {
  125. it('should not remove same entry', () => {
  126. const trie = createTrie(['.skk.moe', 'noc.one'], false);
  127. expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe']);
  128. expect(trie.find('noc.one')).to.deep.equal(['noc.one']);
  129. });
  130. it('should match subdomain - 1', () => {
  131. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false);
  132. expect(trie.find('.skk.moe')).to.deep.equal(['image.cdn.skk.moe', 'blog.skk.moe']);
  133. expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
  134. });
  135. it('should match subdomain - 2', () => {
  136. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false);
  137. expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
  138. expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
  139. });
  140. it('should not remove non-subdomain', () => {
  141. const trie = createTrie(['skk.moe', 'sukkaskk.moe'], false);
  142. expect(trie.find('.skk.moe')).to.deep.equal([]);
  143. });
  144. });
  145. describe('smol tree', () => {
  146. it('should create simple tree - 1', () => {
  147. const trie = createTrie([
  148. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe',
  149. 'www.noc.one', 'cdn.noc.one',
  150. '.blog.sub.example.com', 'sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  151. ], true);
  152. expect(trie.dump()).to.deep.equal([
  153. '.sub.example.com',
  154. 'cdn.noc.one', 'www.noc.one',
  155. '.skk.moe'
  156. ]);
  157. });
  158. it('should create simple tree - 2', () => {
  159. const trie = createTrie([
  160. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe'
  161. ], true);
  162. expect(trie.dump()).to.deep.equal([
  163. '.skk.moe'
  164. ]);
  165. });
  166. it('should create simple tree - 3', () => {
  167. const trie = createTrie([
  168. '.blog.sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  169. ], true);
  170. expect(trie.dump()).to.deep.equal([
  171. '.sub.example.com'
  172. ]);
  173. trie.add('.sub.example.com');
  174. expect(trie.dump()).to.deep.equal([
  175. '.sub.example.com'
  176. ]);
  177. });
  178. it('should create simple tree - 3', () => {
  179. const trie = createTrie([
  180. 'commercial.shouji.360.cn',
  181. 'act.commercial.shouji.360.cn',
  182. 'cdn.creative.medialytics.com',
  183. 'px.cdn.creative.medialytics.com'
  184. ], true);
  185. expect(trie.dump()).to.deep.equal([
  186. 'cdn.creative.medialytics.com',
  187. 'px.cdn.creative.medialytics.com',
  188. 'commercial.shouji.360.cn',
  189. 'act.commercial.shouji.360.cn'
  190. ]);
  191. });
  192. it('should dedupe subdomain properly', () => {
  193. const trie = createTrie([
  194. 'skk.moe',
  195. 'anotherskk.moe',
  196. 'blog.anotherskk.moe',
  197. 'blog.skk.moe'
  198. ], true);
  199. expect(trie.dump()).to.deep.equal([
  200. 'anotherskk.moe',
  201. 'blog.anotherskk.moe',
  202. 'skk.moe',
  203. 'blog.skk.moe'
  204. ]);
  205. });
  206. it('should efficiently whitelist domains', () => {
  207. const trie = createTrie([
  208. 'skk.moe',
  209. 'anotherskk.moe',
  210. 'blog.anotherskk.moe',
  211. 'blog.skk.moe',
  212. '.cdn.local',
  213. 'blog.img.skk.local',
  214. 'img.skk.local'
  215. ], true);
  216. expect(trie.dump(), '1').to.deep.equal([
  217. 'img.skk.local',
  218. 'blog.img.skk.local',
  219. '.cdn.local',
  220. 'anotherskk.moe',
  221. 'blog.anotherskk.moe',
  222. 'skk.moe',
  223. 'blog.skk.moe'
  224. ]);
  225. trie.whitelist('.skk.moe');
  226. expect(trie.dump(), '2').to.deep.equal([
  227. 'img.skk.local',
  228. 'blog.img.skk.local',
  229. '.cdn.local',
  230. 'anotherskk.moe',
  231. 'blog.anotherskk.moe'
  232. ]);
  233. trie.whitelist('anotherskk.moe');
  234. expect(trie.dump(), '3').to.deep.equal([
  235. 'img.skk.local',
  236. 'blog.img.skk.local',
  237. '.cdn.local',
  238. 'blog.anotherskk.moe'
  239. ]);
  240. trie.add('anotherskk.moe');
  241. trie.whitelist('.anotherskk.moe');
  242. expect(trie.dump(), '4').to.deep.equal([
  243. 'img.skk.local',
  244. 'blog.img.skk.local',
  245. '.cdn.local'
  246. ]);
  247. trie.whitelist('img.skk.local');
  248. expect(trie.dump(), '5').to.deep.equal([
  249. 'blog.img.skk.local',
  250. '.cdn.local'
  251. ]);
  252. trie.whitelist('cdn.local');
  253. expect(trie.dump(), '6').to.deep.equal([
  254. 'blog.img.skk.local'
  255. ]);
  256. trie.whitelist('.skk.local');
  257. expect(trie.dump(), '7').to.deep.equal([]);
  258. });
  259. it('should whitelist trie correctly', () => {
  260. const trie = createTrie([
  261. '.t.co',
  262. 't.co',
  263. 'example.t.co',
  264. '.skk.moe',
  265. 'blog.cdn.example.com',
  266. 'cdn.example.com'
  267. ], true);
  268. expect(trie.dump()).to.deep.equal([
  269. 'cdn.example.com', 'blog.cdn.example.com',
  270. '.skk.moe',
  271. '.t.co'
  272. ]);
  273. trie.whitelist('.t.co');
  274. expect(trie.dump()).to.deep.equal([
  275. 'cdn.example.com', 'blog.cdn.example.com',
  276. '.skk.moe'
  277. ]);
  278. trie.whitelist('skk.moe');
  279. expect(trie.dump()).to.deep.equal(['cdn.example.com', 'blog.cdn.example.com']);
  280. trie.whitelist('cdn.example.com');
  281. expect(trie.dump()).to.deep.equal(['blog.cdn.example.com']);
  282. });
  283. });