File indexing completed on 2024-11-17 04:55:14

0001 /* This Source Code Form is subject to the terms of the Mozilla Public
0002  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
0003  * You can obtain one at http://mozilla.org/MPL/2.0/. */
0004 /* global describe, it, before */
0005 
0006 const assert = require('assert')
0007 const fs = require('fs')
0008 const { makeAdBlockClientFromString } = require('../../lib/util')
0009 const { FilterOptions } = require('../..')
0010 
0011 describe('parsing', function () {
0012   describe('newlines', function () {
0013     before(function () {
0014       const data = fs.readFileSync('./test/data/easylist.txt', 'utf8')
0015       this.rawData = data.replace(/\r/g, '').split('\n').slice(0, 100).join('\n')
0016       this.matchArgs = ['http://www.brianbondy.com/public/ad/adbanner.gif&ad_box_=1&ad_type=3', FilterOptions.image, 'slashdot.org']
0017     })
0018 
0019     it('\\r newline is handled the same as \\n', function (cb) {
0020       Promise.all([
0021         makeAdBlockClientFromString(this.rawData.replace(/\n/g, '\r')),
0022         makeAdBlockClientFromString(this.rawData)
0023       ]).then(([client1, client2]) => {
0024         const buffer1 = client1.serialize()
0025         const buffer2 = client2.serialize()
0026         assert.strictEqual(buffer1.length, buffer2.length)
0027         assert(buffer2.toString() === buffer1.toString().replace(/\n/g, '\r'))
0028         assert(client1.matches(...this.matchArgs))
0029         assert(client2.matches(...this.matchArgs))
0030         cb()
0031       }).catch((e) => {
0032         console.log(e)
0033         assert(false)
0034       })
0035     })
0036 
0037     it('\\r\\n newline is handled the same as \\n', function (cb) {
0038       Promise.all([
0039         makeAdBlockClientFromString(this.rawData.replace(/\n/g, '\r\n')),
0040         makeAdBlockClientFromString(this.rawData)
0041       ]).then(([client1, client2]) => {
0042         const buffer1 = client1.serialize()
0043         const buffer2 = client2.serialize()
0044         assert.strictEqual(buffer1.length, buffer2.length)
0045         assert(buffer2.toString() === buffer1.toString().replace(/\n/g, '\r'))
0046         assert(client1.matches(...this.matchArgs))
0047         assert(client2.matches(...this.matchArgs))
0048         cb()
0049       }).catch((e) => {
0050         console.log(e)
0051         assert(false)
0052       })
0053     })
0054   })
0055   describe('single chararacters', function () {
0056     it('with \'/\'', function (cb) {
0057       makeAdBlockClientFromString('/').then((client) => {
0058         assert(client.matches('http://www.brianbondy.com/a', FilterOptions.image, 'slashdot.org'))
0059         cb()
0060       })
0061     })
0062 
0063     it('with normal char \'a\'', function (cb) {
0064       makeAdBlockClientFromString('a').then((client) => {
0065         assert(client.matches('http://www.brianbondy.com/', FilterOptions.image, 'slashdot.org'))
0066         assert(!client.matches('http://www.zzz.com/', FilterOptions.image, 'slashdot.org'))
0067         cb()
0068       })
0069     })
0070 
0071     it('does not crash with unfinshed rules', function (cb) {
0072       Promise.all([
0073         makeAdBlockClientFromString('a'),
0074         makeAdBlockClientFromString('\r'),
0075         makeAdBlockClientFromString('\n'),
0076         makeAdBlockClientFromString('\t'),
0077         makeAdBlockClientFromString(' '),
0078         makeAdBlockClientFromString('|'),
0079         makeAdBlockClientFromString('@'),
0080         makeAdBlockClientFromString('!'),
0081         makeAdBlockClientFromString('['),
0082         makeAdBlockClientFromString('$'),
0083         makeAdBlockClientFromString('#'),
0084         makeAdBlockClientFromString('/'),
0085         makeAdBlockClientFromString('.'),
0086         makeAdBlockClientFromString('^')
0087       ]).then(() => {
0088         cb()
0089       })
0090     })
0091   })
0092   describe('regex parsing', function () {
0093     // See https://github.com/brave/ad-block/issues/173 for details
0094     it('regex should not check last character of input buffer', function (cb) {
0095       makeAdBlockClientFromString('/filter1\nfilter2/').then((client) => {
0096         // regex are considered noFingerprintFilters
0097         assert.strictEqual(client.getFilters('filters').length, 2)
0098         cb()
0099       })
0100     })
0101     it('regex should not be a no fingerprint filter', function (cb) {
0102       makeAdBlockClientFromString('/filter1/\nfilter2/').then((client) => {
0103         // regex are considered noFingerprintFilters
0104         assert.strictEqual(client.getFilters('filters').length, 1)
0105         assert.strictEqual(client.getFilters('noFingerprintFilters').length, 1)
0106         cb()
0107       })
0108     })
0109     // See https://github.com/brave/ad-block/issues/173 for details
0110     it('should serialize correctly', function (cb) {
0111       makeAdBlockClientFromString('/filter1\nfilter2/\n').then((client) => {
0112         makeAdBlockClientFromString('/filter1\nfilter2/').then((client2) => {
0113           // regex are considered noFingerprintFilters
0114           assert.strictEqual(client.serialize().length, client2.serialize().length)
0115           cb()
0116         })
0117       })
0118     })
0119   })
0120 })