summaryrefslogtreecommitdiffstats
path: root/net/adblock-fast/tests/lib/mocklib/uci.uc
blob: 501ca1c92d8b7b092a070c5fb4e956806b0f0c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// UCI mock for adblock-fast tests.
// Based on mwan4's UCI mock, extended with set/save/commit/changes/delete.

let mocklib = global.mocklib; // ucode-lsp disable

let byte = (str, off) => { // ucode-lsp disable
	let v = ord(str, off);
	return length(v) ? v[0] : v;
};

let hash = (s) => { // ucode-lsp disable
	let h = 7;

	for (let i = 0; i < length(s); i++)
		h = h * 31 + byte(s, i);

	return h;
};

let id = (config, t, n) => { // ucode-lsp disable
	while (true) {
		let id = sprintf('cfg%08x', hash(t + n));

		if (!exists(config, id))
			return id;

		n++;
	}
};

let fixup_config = (config) => { // ucode-lsp disable
	let rv = {};
	let n_section = 0;

	for (let stype in config) {
		switch (type(config[stype])) {
		case 'object':
			config[stype] = [ config[stype] ];
			/* fall through */

		case 'array':
			for (let idx, sobj in config[stype]) {
				let sid, anon;

				if (exists(sobj, '.name') && !exists(rv, sobj['.name'])) {
					sid = sobj['.name'];
					anon = false;
				}
				else {
					sid = id(rv, stype, idx);
					anon = true;
				}

				rv[sid] = {
					'.index': n_section++,
					...sobj,
					'.name': sid,
					'.type': stype,
					'.anonymous': anon
				};
			}

			break;
		}
	}

	for (let n, sid in sort(keys(rv), (a, b) => rv[a]['.index'] - rv[b]['.index']))
		rv[sid]['.index'] = n;

	return rv;
};

return {
	cursor: () => ({
		_configs: {},
		_dirty: {},

		load: function(file) {
			let basename = replace(file, /^.+\//, ''),
			    path = sprintf("uci/%s.json", basename),
			    mock = mocklib.read_json_file(path);

			if (!mock || mock != mock) {
				mocklib.I("No configuration fixture defined for uci package %s.", file);
				mocklib.I("Provide a mock configuration through the following JSON file:\n%s\n", path);

				return null;
			}

			this._configs[basename] = fixup_config(mock);
		},

		_get_section: function(config, section) {
			if (!exists(this._configs, config)) {
				this.load(config);

				if (!exists(this._configs, config))
					return null;
			}

			let cfg = this._configs[config],
			    extended = match(section, "^@([A-Za-z0-9_-]+)\\[(-?[0-9]+)\\]$");

			if (extended) {
				let stype = extended[1],
				    sindex = +extended[2];

				let sids = sort(
					filter(keys(cfg), sid => cfg[sid]['.type'] == stype),
					(a, b) => cfg[a]['.index'] - cfg[b]['.index']
				);

				if (sindex < 0)
					sindex = sids.length + sindex;

				return cfg[sids[sindex]];
			}

			return cfg[section];
		},

		get: function(config, section, option) {
			let sobj = this._get_section(config, section);

			if (option && index(option, ".") == 0)
				return null;
			else if (sobj && option)
				return sobj[option];
			else if (sobj)
				return sobj[".type"];
		},

		get_all: function(config, section) {
			return section ? this._get_section(config, section) : this._configs[config];
		},

		foreach: function(config, stype, cb) {
			let rv = false;

			if (!exists(this._configs, config))
				this.load(config);

			if (exists(this._configs, config)) {
				let cfg = this._configs[config],
				    sids = sort(keys(cfg), (a, b) => cfg[a]['.index'] - cfg[b]['.index']);

				for (let i, sid in sids) {
					if (stype == null || cfg[sid]['.type'] == stype) {
						if (cb({ ...(cfg[sid]) }) === false)
							break;

						rv = true;
					}
				}
			}

			return rv;
		},

		// -- Extensions for adblock-fast --

		set: function(config, section, option, value) {
			let sobj = this._get_section(config, section);
			if (sobj) {
				sobj[option] = value;
				this._dirty[config] = true;
			}
		},

		save: function(config) {
			return true;
		},

		commit: function(config) {
			delete this._dirty[config];
			return true;
		},

		changes: function(config) {
			return this._dirty[config] ? [['set']] : [];
		},

		revert: function(config) {
			delete this._dirty[config];
		},

		delete: function(config, section, option) {
			if (option) {
				let sobj = this._get_section(config, section);
				if (sobj) delete sobj[option];
			} else if (section) {
				if (exists(this._configs, config))
					delete this._configs[config][section];
			}
		},

		list_add: function(config, section, option, value) {
			let sobj = this._get_section(config, section);
			if (!sobj) return;
			if (type(sobj[option]) != 'array')
				sobj[option] = sobj[option] ? [sobj[option]] : [];
			push(sobj[option], value);
			this._dirty[config] = true;
		},

		list_remove: function(config, section, option, value) {
			let sobj = this._get_section(config, section);
			if (!sobj || type(sobj[option]) != 'array') return;
			sobj[option] = filter(sobj[option], v => v != value);
			this._dirty[config] = true;
		},
	})
};