trie.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { createTrie, hostnameToTokens } 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')).to.equal(true);
  39. expect(trie.has('skk.moe')).to.equal(true);
  40. expect(trie.has('anotherskk.moe')).to.equal(true);
  41. expect(trie.has('example.com')).to.equal(false);
  42. expect(trie.has('skk.mo')).to.equal(false);
  43. expect(trie.has('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('example.com');
  66. trie.add('moe.sb');
  67. expect(trie.delete('')).to.equal(false);
  68. expect(trie.delete('')).to.equal(false);
  69. expect(trie.delete('example.org')).to.equal(false);
  70. expect(trie.delete('skk.moe')).to.equal(true);
  71. expect(trie.has('skk.moe')).to.equal(false);
  72. expect(trie.has('moe.sb')).to.equal(true);
  73. expect(trie.size).to.equal(2);
  74. expect(trie.delete('example.com')).to.equal(true);
  75. expect(trie.size).to.equal(1);
  76. expect(trie.delete('moe.sb')).to.equal(true);
  77. expect(trie.size).to.equal(0);
  78. });
  79. it('should be possible to check the existence of a sequence in the Trie.', () => {
  80. const trie = createTrie(null, true);
  81. trie.add('example.org.skk.moe');
  82. expect(trie.has('example.org.skk.moe')).to.equal(true);
  83. expect(trie.has('skk.moe')).to.equal(false);
  84. expect(trie.has('example.org')).to.equal(false);
  85. expect(trie.has('')).to.equal(false);
  86. });
  87. it('should be possible to retrieve items matching the given prefix.', () => {
  88. const trie = createTrie(null, false);
  89. trie.add('example.com');
  90. trie.add('blog.example.com');
  91. trie.add('cdn.example.com');
  92. trie.add('example.org');
  93. expect(trie.find('example.com')).to.deep.equal(['example.com', 'cdn.example.com', 'blog.example.com']);
  94. expect(trie.find('com')).to.deep.equal(['example.com', 'cdn.example.com', 'blog.example.com']);
  95. expect(trie.find('.example.com')).to.deep.equal(['cdn.example.com', 'blog.example.com']);
  96. expect(trie.find('org')).to.deep.equal(['example.org']);
  97. expect(trie.find('example.net')).to.deep.equal([]);
  98. expect(trie.find('')).to.deep.equal(['example.org', 'example.com', 'cdn.example.com', 'blog.example.com']);
  99. });
  100. it('should be possible to retrieve items matching the given prefix even with a smol trie.', () => {
  101. const trie = createTrie(null, true);
  102. trie.add('.example.com');
  103. trie.add('example.com');
  104. trie.add('blog.example.com');
  105. trie.add('cdn.example.com');
  106. trie.add('example.org');
  107. expect(trie.find('example.com')).to.deep.equal(['.example.com']);
  108. expect(trie.find('com')).to.deep.equal(['.example.com']);
  109. expect(trie.find('.example.com')).to.deep.equal(['.example.com']);
  110. expect(trie.find('org')).to.deep.equal(['example.org']);
  111. expect(trie.find('example.net')).to.deep.equal([]);
  112. expect(trie.find('')).to.deep.equal(['example.org', '.example.com']);
  113. });
  114. it('should be possible to create a trie from an arbitrary iterable.', () => {
  115. let trie = createTrie(['skk.moe', 'blog.skk.moe'], false);
  116. expect(trie.size).to.equal(2);
  117. expect(trie.has('skk.moe')).to.equal(true);
  118. trie = createTrie(new Set(['skk.moe', 'example.com']), false);
  119. expect(trie.size).to.equal(2);
  120. expect(trie.has('skk.moe')).to.equal(true);
  121. });
  122. });
  123. describe('surge domainset dedupe', () => {
  124. it('should not remove same entry', () => {
  125. const trie = createTrie(['.skk.moe', 'noc.one'], false);
  126. expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe']);
  127. expect(trie.find('noc.one')).to.deep.equal(['noc.one']);
  128. });
  129. it('should match subdomain - 1', () => {
  130. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false);
  131. expect(trie.find('.skk.moe')).to.deep.equal(['image.cdn.skk.moe', 'blog.skk.moe']);
  132. expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
  133. });
  134. it('should match subdomain - 2', () => {
  135. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], false);
  136. expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
  137. expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
  138. });
  139. it('should not remove non-subdomain', () => {
  140. const trie = createTrie(['skk.moe', 'sukkaskk.moe'], false);
  141. expect(trie.find('.skk.moe')).to.deep.equal([]);
  142. });
  143. });
  144. describe('smol tree', () => {
  145. it('should create simple tree - 1', () => {
  146. const trie = createTrie([
  147. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe',
  148. 'www.noc.one', 'cdn.noc.one',
  149. '.blog.sub.example.com', 'sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  150. ], true);
  151. expect(trie.dump()).to.deep.equal([
  152. '.sub.example.com',
  153. 'cdn.noc.one', 'www.noc.one',
  154. '.skk.moe'
  155. ]);
  156. });
  157. it('should create simple tree - 2', () => {
  158. const trie = createTrie([
  159. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe'
  160. ], true);
  161. expect(trie.dump()).to.deep.equal([
  162. '.skk.moe'
  163. ]);
  164. });
  165. it('should create simple tree - 2', () => {
  166. const trie = createTrie([
  167. '.blog.sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  168. ], true);
  169. expect(trie.dump()).to.deep.equal([
  170. '.sub.example.com'
  171. ]);
  172. trie.add('.sub.example.com');
  173. expect(trie.dump()).to.deep.equal([
  174. '.sub.example.com'
  175. ]);
  176. });
  177. it('should create simple tree - 3', () => {
  178. const trie = createTrie([
  179. 'commercial.shouji.360.cn',
  180. 'act.commercial.shouji.360.cn',
  181. 'cdn.creative.medialytics.com',
  182. 'px.cdn.creative.medialytics.com'
  183. ], true);
  184. expect(trie.dump()).to.deep.equal([
  185. 'cdn.creative.medialytics.com',
  186. 'px.cdn.creative.medialytics.com',
  187. 'commercial.shouji.360.cn',
  188. 'act.commercial.shouji.360.cn'
  189. ]);
  190. });
  191. it('should dedupe subdomain properly', () => {
  192. const trie = createTrie([
  193. 'skk.moe',
  194. 'anotherskk.moe',
  195. 'blog.anotherskk.moe',
  196. 'blog.skk.moe'
  197. ], true);
  198. expect(trie.dump()).to.deep.equal([
  199. 'anotherskk.moe',
  200. 'blog.anotherskk.moe',
  201. 'skk.moe',
  202. 'blog.skk.moe'
  203. ]);
  204. });
  205. it('should efficiently whitelist domains', () => {
  206. const trie = createTrie([
  207. 'skk.moe',
  208. 'anotherskk.moe',
  209. 'blog.anotherskk.moe',
  210. 'blog.skk.moe'
  211. ], true);
  212. expect(trie.dump()).to.deep.equal([
  213. 'anotherskk.moe',
  214. 'blog.anotherskk.moe',
  215. 'skk.moe',
  216. 'blog.skk.moe'
  217. ]);
  218. trie.whitelist('.skk.moe');
  219. expect(trie.dump()).to.deep.equal([
  220. 'anotherskk.moe',
  221. 'blog.anotherskk.moe'
  222. ]);
  223. trie.whitelist('anotherskk.moe');
  224. expect(trie.dump()).to.deep.equal([
  225. 'blog.anotherskk.moe'
  226. ]);
  227. trie.add('anotherskk.moe');
  228. trie.whitelist('.anotherskk.moe');
  229. expect(trie.dump()).to.deep.equal([]);
  230. });
  231. it('should whitelist trie correctly', () => {
  232. const trie = createTrie([
  233. '.t.co',
  234. 't.co',
  235. 'example.t.co',
  236. '.skk.moe',
  237. 'blog.cdn.example.com',
  238. 'cdn.example.com'
  239. ], true);
  240. expect(trie.dump()).to.deep.equal([
  241. 'cdn.example.com', 'blog.cdn.example.com',
  242. '.skk.moe',
  243. '.t.co'
  244. ]);
  245. trie.whitelist('.t.co');
  246. expect(trie.dump()).to.deep.equal([
  247. 'cdn.example.com', 'blog.cdn.example.com',
  248. '.skk.moe'
  249. ]);
  250. trie.whitelist('skk.moe');
  251. expect(trie.dump()).to.deep.equal(['cdn.example.com', 'blog.cdn.example.com']);
  252. trie.whitelist('cdn.example.com');
  253. expect(trie.dump()).to.deep.equal(['blog.cdn.example.com']);
  254. });
  255. });