tester.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import test from 'tape';
  2. import { testScript, testBlacklist, resetBlacklist } from '#/background/utils/tester';
  3. import cache from '#/background/utils/cache';
  4. test.onFinish(cache.destroy);
  5. function buildScript(props) {
  6. return Object.assign({
  7. custom: {
  8. origInclude: true,
  9. origExclude: true,
  10. origMatch: true,
  11. origExcludeMatch: true,
  12. },
  13. meta: {},
  14. }, props);
  15. }
  16. test('scheme', (t) => {
  17. t.test('should match all', (q) => {
  18. {
  19. const script = buildScript({
  20. meta: {
  21. match: [
  22. '*://*/*',
  23. ],
  24. },
  25. });
  26. q.ok(testScript('http://www.google.com/', script), 'should match `http | https`');
  27. q.ok(testScript('https://www.google.com/', script), 'should match `http | https`');
  28. q.notOk(testScript('file:///Users/Gerald/file', script), 'should not match `file`');
  29. }
  30. {
  31. const script = buildScript({
  32. meta: {
  33. match: [
  34. 'http*://*/*',
  35. ],
  36. },
  37. });
  38. q.ok(testScript('http://www.google.com/', script), 'should match `http | https`');
  39. q.ok(testScript('https://www.google.com/', script), 'should match `http | https`');
  40. q.notOk(testScript('file:///Users/Gerald/file', script), 'should not match `file`');
  41. }
  42. q.end();
  43. });
  44. t.test('should match exact', (q) => {
  45. const script = buildScript({
  46. meta: {
  47. match: [
  48. 'http://*/*',
  49. 'ftp://*/*',
  50. 'file:///*',
  51. ],
  52. },
  53. });
  54. q.ok(testScript('http://www.google.com/', script), 'should match `http`');
  55. q.notOk(testScript('https://www.google.com/', script), 'should not match `https`');
  56. q.ok(testScript('file:///Users/Gerald/file', script), 'should match `file`');
  57. q.ok(testScript('ftp://example.com/file', script), 'should match `ftp`');
  58. q.end();
  59. });
  60. t.end();
  61. });
  62. test('host', (t) => {
  63. t.test('should match domain', (q) => {
  64. const script = buildScript({
  65. meta: {
  66. match: [
  67. '*://docs.google.com/',
  68. ],
  69. },
  70. });
  71. q.ok(testScript('https://docs.google.com/', script), 'should match exact domain name');
  72. q.notOk(testScript('https://sub.docs.google.com/', script), 'should not match subdomains');
  73. q.notOk(testScript('https://docs.google.com.cn/', script), 'should not match suffixed domains');
  74. q.end();
  75. });
  76. t.test('should match wildcard', (q) => {
  77. const script = buildScript({
  78. meta: {
  79. match: [
  80. '*://*.google.com/',
  81. '*://www.example.*/',
  82. ],
  83. },
  84. });
  85. q.ok(testScript('https://www.google.com/', script), 'should match subdomains');
  86. q.ok(testScript('https://a.b.google.com/', script), 'should match subdomains');
  87. q.ok(testScript('https://google.com/', script), 'should match specified domain');
  88. q.notOk(testScript('https://www.google.com.hk/', script), 'should not match suffixed domains');
  89. q.ok(testScript('https://www.example.com/', script), 'should match prefix');
  90. q.ok(testScript('https://www.example.com.cn/', script), 'should match prefix');
  91. q.ok(testScript('https://www.example.g.com/', script), 'should match prefix');
  92. q.end();
  93. });
  94. t.test('should match tld', (q) => {
  95. const script = buildScript({
  96. meta: {
  97. match: [
  98. '*://www.google.tld/',
  99. '*://www.dummy.TLD/', // testing for a mistake: `.tld` should be lowercase
  100. ],
  101. },
  102. });
  103. q.ok(testScript('https://www.google.com/', script), 'should match subdomains');
  104. q.ok(testScript('https://www.google.com.cn/', script), 'should match subdomains');
  105. q.ok(testScript('https://www.google.jp/', script), 'should match tld');
  106. q.ok(testScript('https://www.google.no-ip.org/', script), 'should match a hyphened `no-ip.org` from Public Suffix List');
  107. q.notOk(testScript('https://www.google.example.com/', script), 'should not match subdomains');
  108. q.notOk(testScript('https://www.dummy.com/', script), '`.tld` should be lowercase');
  109. q.end();
  110. });
  111. t.test('should ignore case', (q) => {
  112. const script = buildScript({
  113. meta: {
  114. match: [
  115. '*://GOOGLE.com/',
  116. ],
  117. },
  118. });
  119. q.ok(testScript('https://google.COM/', script), 'should ignore case');
  120. q.end();
  121. });
  122. t.end();
  123. });
  124. test('path', (t) => {
  125. t.test('should match any', (q) => {
  126. const script = buildScript({
  127. meta: {
  128. match: [
  129. 'https://www.google.com/*',
  130. ],
  131. },
  132. });
  133. q.ok(testScript('https://www.google.com/', script), 'should match `/`');
  134. q.ok(testScript('https://www.google.com/hello/world', script), 'should match any');
  135. q.end();
  136. });
  137. t.test('should match exact', (q) => {
  138. const script = buildScript({
  139. meta: {
  140. match: [
  141. 'https://www.google.com/a/b/c',
  142. ],
  143. },
  144. });
  145. q.ok(testScript('https://www.google.com/a/b/c', script), 'should match exact');
  146. q.notOk(testScript('https://www.google.com/a/b/c/d', script), 'should match exact');
  147. q.end();
  148. });
  149. t.test('should ignore query string and hash', (q) => {
  150. const script = buildScript({
  151. meta: {
  152. match: [
  153. 'https://www.google.com/a',
  154. ],
  155. },
  156. });
  157. q.ok(testScript('https://www.google.com/a', script), 'should match without query and hash');
  158. q.ok(testScript('https://www.google.com/a#hash', script), 'should match with hash');
  159. q.ok(testScript('https://www.google.com/a?query', script), 'should match with query');
  160. q.ok(testScript('https://www.google.com/a?query#hash', script), 'should match with query and hash');
  161. q.end();
  162. });
  163. t.test('should match query string and hash if existed in rules', (q) => {
  164. const script = buildScript({
  165. meta: {
  166. match: [
  167. 'https://www.google.com/a?query',
  168. 'https://www.google.com/b#hash',
  169. 'https://www.google.com/c?query#hash',
  170. ],
  171. },
  172. });
  173. q.notOk(testScript('https://www.google.com/a', script), 'should match query');
  174. q.notOk(testScript('https://www.google.com/b', script), 'should match hash');
  175. q.ok(testScript('https://www.google.com/a?query', script), 'should match query');
  176. q.ok(testScript('https://www.google.com/a?query#hash', script), 'should match query and ignore hash');
  177. q.notOk(testScript('https://www.google.com/b?query#hash', script), 'should match query and hash');
  178. q.ok(testScript('https://www.google.com/b#hash', script), 'should match hash');
  179. q.ok(testScript('https://www.google.com/c?query#hash', script), 'should match query and hash');
  180. q.end();
  181. });
  182. t.test('should be case-sensitive', (q) => {
  183. const script = buildScript({
  184. meta: {
  185. match: [
  186. 'https://www.google.com/a?Query',
  187. 'https://www.google.com/b#Hash',
  188. ],
  189. },
  190. });
  191. q.ok(testScript('https://www.google.com/a?Query', script), 'query should be case-sensitive');
  192. q.notOk(testScript('https://www.google.com/a?query', script), 'query should be case-sensitive');
  193. q.ok(testScript('https://www.google.com/b#Hash', script), 'hash should be case-sensitive');
  194. q.notOk(testScript('https://www.google.com/b#hash', script), 'hash should be case-sensitive');
  195. q.end();
  196. });
  197. t.end();
  198. });
  199. test('include', (t) => {
  200. t.test('should include any', (q) => {
  201. const script = buildScript({
  202. meta: {
  203. include: [
  204. '*',
  205. ],
  206. },
  207. });
  208. q.ok(testScript('https://www.google.com/', script), 'should match `http | https`');
  209. q.ok(testScript('file:///Users/Gerald/file', script), 'should match `file`');
  210. q.end();
  211. });
  212. t.test('should include by glob', (q) => {
  213. const script = buildScript({
  214. meta: {
  215. include: [
  216. 'https://www.google.com/*',
  217. 'https://twitter.com/*',
  218. ],
  219. },
  220. });
  221. q.ok(testScript('https://www.google.com/', script), 'should match `/`');
  222. q.ok(testScript('https://www.google.com/hello/world', script), 'include by prefix');
  223. q.notOk(testScript('https://www.hello.com/', script), 'not include by prefix');
  224. q.end();
  225. });
  226. t.test('should include by regexp', (q) => {
  227. const script = buildScript({
  228. meta: {
  229. include: [
  230. '/invalid-regexp(/',
  231. '/https://www\\.google\\.com/.*/',
  232. ],
  233. },
  234. });
  235. q.ok(testScript('https://www.google.com/', script), 'should ignore the invalid regexp and match target');
  236. q.notOk(testScript('https://www.hello.com/', script), 'should not match nontarget');
  237. q.end();
  238. });
  239. t.test('should support magic TLD', (q) => {
  240. const script = buildScript({
  241. meta: {
  242. include: [
  243. 'https://www.google.tld/*',
  244. ],
  245. },
  246. });
  247. q.ok(testScript('https://www.google.com/', script), 'should match `.com`');
  248. q.ok(testScript('https://www.google.com.hk/', script), 'should match `.com.hk`');
  249. q.ok(testScript('https://www.google.no-ip.org/', script), 'should match a hyphened `no-ip.org` from Public Suffix List');
  250. q.notOk(testScript('https://www.google.example.com/', script), 'should not match subdomains');
  251. q.end();
  252. });
  253. t.test('should ignore case', (q) => {
  254. const script = buildScript({
  255. meta: {
  256. include: [
  257. 'https://www.google.*',
  258. '/regexp/',
  259. ],
  260. },
  261. });
  262. q.ok(testScript('https://www.GOOGLE.com/', script), 'should ignore case');
  263. q.ok(testScript('https://www.REGEXP.com/', script), 'should ignore case');
  264. q.end();
  265. });
  266. });
  267. test('exclude', (t) => {
  268. t.test('should exclude any', (q) => {
  269. const script = buildScript({
  270. meta: {
  271. match: [
  272. '*://*/*',
  273. ],
  274. exclude: [
  275. '*',
  276. ],
  277. },
  278. });
  279. q.notOk(testScript('https://www.google.com/', script), 'should exclude `http | https`');
  280. q.end();
  281. });
  282. t.test('should include by glob', (q) => {
  283. const script = buildScript({
  284. meta: {
  285. match: [
  286. '*://*/*',
  287. ],
  288. excludeMatch: [
  289. 'https://www.google.com/*',
  290. 'https://twitter.com/*',
  291. ],
  292. },
  293. });
  294. q.notOk(testScript('https://www.google.com/', script), 'should exclude `/`');
  295. q.notOk(testScript('https://www.google.com/hello/world', script), 'exclude by prefix');
  296. q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
  297. q.end();
  298. });
  299. t.test('should support magic TLD', (q) => {
  300. const script = buildScript({
  301. meta: {
  302. exclude: [
  303. 'https://www.google.tld/*',
  304. ],
  305. },
  306. });
  307. q.notOk(testScript('https://www.google.com/', script), 'should match `.com`');
  308. q.notOk(testScript('https://www.google.com.hk/', script), 'should match `.com.hk`');
  309. q.notOk(testScript('https://www.google.no-ip.org/', script), 'should match a hyphened `no-ip.org` from Public Suffix List');
  310. q.ok(testScript('https://www.google.example.com/', script), 'should not match subdomains');
  311. q.end();
  312. });
  313. });
  314. test('exclude-match', (t) => {
  315. t.test('should exclude any', (q) => {
  316. const script = buildScript({
  317. meta: {
  318. match: [
  319. '*://*/*',
  320. ],
  321. excludeMatch: [
  322. '*://*/*',
  323. ],
  324. },
  325. });
  326. q.notOk(testScript('https://www.google.com/', script), 'should exclude `http | https`');
  327. q.end();
  328. });
  329. t.test('should include by glob', (q) => {
  330. const script = buildScript({
  331. meta: {
  332. match: [
  333. '*://*/*',
  334. ],
  335. excludeMatch: [
  336. 'https://www.google.com/*',
  337. 'https://twitter.com/*',
  338. ],
  339. },
  340. });
  341. q.notOk(testScript('https://www.google.com/', script), 'should exclude `/`');
  342. q.notOk(testScript('https://www.google.com/hello/world', script), 'exclude by prefix');
  343. q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
  344. q.end();
  345. });
  346. t.test('should ignore case only in host', (q) => {
  347. const script = buildScript({
  348. meta: {
  349. match: [
  350. '*://GOOGLE.com/FOO?BAR#HASH',
  351. ],
  352. },
  353. });
  354. q.ok(testScript('https://google.COM/FOO?BAR#HASH', script), 'should ignore case in host');
  355. q.notOk(testScript('https://google.com/foo?bar#hash', script), 'should ignore case in host only');
  356. q.end();
  357. });
  358. });
  359. test('custom', (t) => {
  360. t.test('should ignore original rules', (q) => {
  361. const script = buildScript({
  362. custom: {
  363. match: [
  364. 'https://twitter.com/*',
  365. ],
  366. },
  367. meta: {
  368. match: [
  369. 'https://www.google.com/*',
  370. ],
  371. },
  372. });
  373. q.ok(testScript('https://twitter.com/', script), 'should match custom rules');
  374. q.notOk(testScript('https://www.google.com/', script), 'should not match original rules');
  375. q.end();
  376. });
  377. });
  378. test('blacklist', (t) => {
  379. t.test('should exclude match rules', (q) => {
  380. resetBlacklist(`\
  381. # match rules
  382. *://www.google.com/*
  383. `);
  384. q.ok(testBlacklist('http://www.google.com/'));
  385. q.ok(testBlacklist('https://www.google.com/'));
  386. q.notOk(testBlacklist('https://twitter.com/'));
  387. q.end();
  388. });
  389. t.test('should exclude domains', (q) => {
  390. resetBlacklist(`\
  391. # domains
  392. www.google.com
  393. `);
  394. q.ok(testBlacklist('http://www.google.com/'));
  395. q.ok(testBlacklist('https://www.google.com/'));
  396. q.notOk(testBlacklist('https://twitter.com/'));
  397. q.end();
  398. });
  399. t.test('should support @exclude rules', (q) => {
  400. resetBlacklist(`\
  401. # @exclude rules
  402. @exclude https://www.google.com/*
  403. `);
  404. q.ok(testBlacklist('https://www.google.com/'));
  405. q.ok(testBlacklist('https://www.google.com/whatever'));
  406. q.notOk(testBlacklist('http://www.google.com/'));
  407. q.end();
  408. });
  409. });