File indexing completed on 2024-11-24 04:54:32
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 { makeAdBlockClientFromString } = require('../../lib/util') 0008 const { AdBlockClient, FilterOptions } = require('../..') 0009 0010 describe('serialization', function () { 0011 before(function (cb) { 0012 const ruleData = ` 0013 [Adblock Plus 2.0] 0014 &video_ads_ 0015 &videoadid= 0016 &view=ad& 0017 +advertorial. 0018 +adverts/ 0019 -2/ads/ 0020 -2011ad_ 0021 -300x100ad2. 0022 -ad-001- 0023 -ad-180x150px. 0024 -ad-200x200- 0025 ! comment here 0026 ` 0027 makeAdBlockClientFromString(ruleData).then((client) => { 0028 this.client = client 0029 this.data = this.client.serialize() 0030 this.client2 = new AdBlockClient() 0031 this.client2.deserialize(this.data) 0032 // Just to make sure things work properly with repeated deserializes 0033 this.client2.deserialize(this.data) 0034 cb() 0035 }) 0036 }) 0037 0038 it('blocks things the same when created from serialized', function () { 0039 assert(this.client.matches('http://www.brianbondy.com?c=a&view=ad&b=2', FilterOptions.image, 'slashdot.org')) 0040 assert(!this.client.matches('http://www.brianbondy.com?c=a&view1=ad&b=2', FilterOptions.image, 'slashdot.org')) 0041 assert(this.client2.matches('http://www.brianbondy.com?c=a&view=ad&b=2', FilterOptions.image, 'slashdot.org')) 0042 assert(!this.client2.matches('http://www.brianbondy.com?c=a&view1=ad&b=2', FilterOptions.image, 'slashdot.org')) 0043 }) 0044 it('deserialized client serializes the same', function () { 0045 this.client2.deserialize(this.data) 0046 // Just to make sure things work properly with repeated deserializes 0047 this.client2.deserialize(this.data) 0048 const data2 = this.client2.serialize() 0049 assert(this.data.equals(data2)) 0050 }) 0051 it('deserializes with the same number of filters', function () { 0052 const nonCommentFilterCount = 11 0053 assert.strictEqual(this.client.getParsingStats().numFilters, nonCommentFilterCount) 0054 assert.strictEqual(this.client2.getParsingStats().numFilters, nonCommentFilterCount) 0055 }) 0056 it('serialized data does not include comment data', function () { 0057 assert(!this.data.toString().includes('comment')) 0058 assert(!this.data.toString().includes('Adblock Plus')) 0059 }) 0060 0061 describe('deserializing input', function () { 0062 it('does not throw on valid input', function () { 0063 const client = new AdBlockClient() 0064 client.deserialize(this.data) 0065 // Just to make sure things work properly with repeated deserializes 0066 client.deserialize(this.data) 0067 }) 0068 0069 it('throws on invalid input', function () { 0070 const badInput = 'not-good-data' 0071 let caughtError = false 0072 const newClient = new AdBlockClient() 0073 // Check to make sure the below doesn't throw 0074 try { 0075 newClient.deserialize(badInput) 0076 // Just to make sure things work properly with repeated deserializes 0077 newClient.deserialize(badInput) 0078 } catch (_) { 0079 caughtError = true 0080 } 0081 assert(caughtError) 0082 }) 0083 }) 0084 describe('tags', function () { 0085 it('preserves filter tags', function () { 0086 const client = new AdBlockClient() 0087 client.parse('testfilter$third-party,tag=blah') 0088 const filters1 = client.getFilters('filters') 0089 console.log('filters1', filters1) 0090 assert.strictEqual(filters1.length, 1) 0091 assert.strictEqual(filters1[0].tag, 'blah') 0092 0093 const data = client.serialize() 0094 const client2 = new AdBlockClient() 0095 client2.deserialize(data) 0096 // Just to make sure things work properly with repeated deserializes 0097 client2.deserialize(data) 0098 const filters2 = client2.getFilters('filters') 0099 console.log('filters2', filters2) 0100 assert.strictEqual(filters2.length, 1) 0101 assert.strictEqual(filters2[0].tag, 'blah') 0102 }) 0103 }) 0104 })