-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
387 lines (367 loc) · 16.3 KB
/
parse_test.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package iabtcf
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
// all consent strings generated by: https://iabtcf.com/#/encode
func TestConsentMethods(t *testing.T) {
type PurposeTestCase struct {
purposes []int
wantAllowed bool
}
type SpecialFeaturesTestCase struct {
specialFeatures []int
wantAllowed bool
}
type VendorTestCase struct {
vendor int
wantAllowed bool
}
type TestCase struct {
consentString string
wantErr string
wantVersion int
wantPurposesConsent string
purposeTestCases map[string]*PurposeTestCase
wantSpecialFeatureOptIns string
specialFeaturesTestCases map[string]*SpecialFeaturesTestCase
wantMaxVendorID int
wantIsRangeEncoded bool
wantRangeEntries []RangeEntry
wantVendors string
vendorTestCases map[string]*VendorTestCase
wantPublisherCC string
}
testCases := map[string]*TestCase{
"empty": {
consentString: "",
wantErr: "consent string is empty",
},
"parse-error": {
consentString: "BOv1FaTOv1FdvAHABBFRDG-AAAAvRr_7__7-_9_-_f__9uj3Or_v_f__32ccL59v_h_7v-_7fi_20nV4u_1vft9yfk1-5ctDztp507iakivXmqdeb1v_nz3_5pxP78k89r7337Ew_v8_v-b7BCON9YxEiAAA",
wantErr: "range entries parse failed: ReadInt failed: ReadBits failed: read bits (index=923, length=16): bits: length extends beyond range",
},
"v1": {
consentString: "BOzcJxTOzcJxTBcAAAENAiCMAP_AAAAAAAAADTwAQDTgAAAA.IF5EX2S5OI2tho2YdF7BEYYwfJxyigMgShgQIsS8NwIeFbBoGPmAAHBG4JAQAGBAkkACBAQIsHGBcCQABgIgRiRCMQEGMjzNKBJBAggkbI0FACCVmnkHS3ZCY70-6u__bA",
wantVersion: 1,
},
"10-purposes-2-special-features-vendor-ranges": {
consentString: "COzcJxTOzcJxTBcAAAENAiCMAP_AAAAAAAAADTwAQDTgAAAA.IF5EX2S5OI2tho2YdF7BEYYwfJxyigMgShgQIsS8NwIeFbBoGPmAAHBG4JAQAGBAkkACBAQIsHGBcCQABgIgRiRCMQEGMjzNKBJBAggkbI0FACCVmnkHS3ZCY70-6u__bA",
wantVersion: 2,
wantPurposesConsent: "11111111 11000000 00000000",
purposeTestCases: map[string]*PurposeTestCase{
"nil": {
purposes: nil,
wantAllowed: true, // empty = all
},
"empty": {
purposes: []int{},
wantAllowed: true, // empty = all
},
"zero": {
purposes: []int{0},
wantAllowed: false, // number should start by 1
},
"first": {
purposes: []int{1},
wantAllowed: true,
},
"last": {
purposes: []int{23},
wantAllowed: false,
},
"out-of-bound": {
purposes: []int{24},
wantAllowed: false,
},
"all": {
purposes: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
wantAllowed: true,
},
"some": {
purposes: []int{2, 5, 7, 9},
wantAllowed: true,
},
"none": {
purposes: []int{13, 17, 19},
wantAllowed: false,
},
},
wantSpecialFeatureOptIns: "11000000 00000000",
specialFeaturesTestCases: map[string]*SpecialFeaturesTestCase{
"nil": {
specialFeatures: nil,
wantAllowed: true, // empty = all
},
"empty": {
specialFeatures: []int{},
wantAllowed: true, // empty = all
},
"zero": {
specialFeatures: []int{0},
wantAllowed: false, // number should start by 1
},
"first": {
specialFeatures: []int{1},
wantAllowed: true,
},
"last": {
specialFeatures: []int{12},
wantAllowed: false,
},
"out-of-bound": {
specialFeatures: []int{13},
wantAllowed: false,
},
"all": {
specialFeatures: []int{1, 2},
wantAllowed: true,
},
"some": {
specialFeatures: []int{2},
wantAllowed: true,
},
"none": {
specialFeatures: []int{5, 9},
wantAllowed: false,
},
},
wantMaxVendorID: 423,
wantIsRangeEncoded: true,
wantRangeEntries: []RangeEntry{{StartOrOnlyVendorId: 423, EndVendorID: 423}},
vendorTestCases: map[string]*VendorTestCase{
"zero": {
vendor: 0,
wantAllowed: false, // out of bound
},
"first": {
vendor: 1,
wantAllowed: false,
},
"not-consented": {
vendor: 10,
wantAllowed: false,
},
"TA": {
vendor: 423,
wantAllowed: true,
},
"out-of-bound": {
vendor: 424,
wantAllowed: false,
},
},
wantPublisherCC: "AA",
},
"v2": {
consentString: "CP9Qr_AP9Qr_AAfETDFRAwEsAP_gAEPgAAigg1NX_H__bX9v-Xr36ft0eY1f99j77uQxBhfJs-4FzLvW_JwX32EzNE36tqYKmRIEu3bBIQFtHJnUTVihaogVrzHsYkGchTNKJ-BkiHMRe2dYCF5vmYtj-QKZ5_p_d3f52T_9_dv-3dzzz91nv3f9f-f1eLida59tH_v_bRKb-_If9_7-_4v0_t_rk2_eTVv_9evv79-u_t____9_9____4AAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAACAA",
wantVersion: 2,
wantPurposesConsent: "11111111 11100000 00000000",
purposeTestCases: map[string]*PurposeTestCase{
"nil": {
purposes: nil,
wantAllowed: true, // empty = all
},
"empty": {
purposes: []int{},
wantAllowed: true, // empty = all
},
"zero": {
purposes: []int{0},
wantAllowed: false, // number should start by 1
},
"first": {
purposes: []int{1},
wantAllowed: true,
},
"last": {
purposes: []int{23},
wantAllowed: false,
},
"out-of-bound": {
purposes: []int{24},
wantAllowed: false,
},
"all": {
purposes: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
wantAllowed: true,
},
"some": {
purposes: []int{2, 5, 7, 9},
wantAllowed: true,
},
"none": {
purposes: []int{13, 17, 19},
wantAllowed: false,
},
"mixed": {
purposes: []int{9, 13, 5, 7},
wantAllowed: false,
},
},
wantSpecialFeatureOptIns: "11000000 00000000",
specialFeaturesTestCases: map[string]*SpecialFeaturesTestCase{
"nil": {
specialFeatures: nil,
wantAllowed: true, // empty = all
},
"empty": {
specialFeatures: []int{},
wantAllowed: true, // empty = all
},
"zero": {
specialFeatures: []int{0},
wantAllowed: false, // number should start by 1
},
"first": {
specialFeatures: []int{1},
wantAllowed: true,
},
"last": {
specialFeatures: []int{12},
wantAllowed: false,
},
"out-of-bound": {
specialFeatures: []int{13},
wantAllowed: false,
},
"all": {
specialFeatures: []int{1, 2},
wantAllowed: true,
},
"some": {
specialFeatures: []int{2},
wantAllowed: true,
},
"none": {
specialFeatures: []int{5, 9},
wantAllowed: false,
},
},
wantMaxVendorID: 4202,
wantVendors: "11010101 11111111 00011111 11111111 11011011 01011111 11011011 11111110 01011110 10111101 11111010 01111110 11011101 00011110 01100011 01010111 11111101 11110110 00111110 11111011 10111001 00001100 01000001 10000101 11110010 01101100 11111011 10000001 01110011 00101110 11110101 10111111 00100111 00000101 11110111 11011000 01001100 11001101 00010011 01111110 10101101 10101001 10000010 10100110 01000100 10000001 00101110 11011101 10110000 01001000 01000000 01011011 01000111 00100110 01110101 00010011 01010110 00101000 01011010 10100010 00000101 01101011 11001100 01111011 00011000 10010000 01100111 00100001 01001100 11010010 10001001 11111000 00011001 00100010 00011100 11000100 01011110 11011001 11010110 00000010 00010111 10011011 11100110 01100010 11011000 11111110 01000000 10100110 01111001 11111110 10011111 11011101 11011101 11111110 01110110 01001111 11111111 01111111 01110110 11111111 10110111 01110111 00111100 11110011 11110111 01011001 11101111 11011101 11111111 01011111 11111001 11111101 01011110 00101110 00100111 01011010 11100111 11011011 01000111 11111110 11111111 11011011 01000100 10100110 11111110 11111100 10000111 11111101 11111111 10111111 10111111 11100010 11111101 00111111 10110111 11111010 11100100 11011011 11110111 10010011 01010110 11111111 11111101 01111010 11111011 11111011 11110111 11101011 10111111 10110111 11111111 11111111 11111111 11011111 11111101 11111111 11111111 11111111 11100000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001 00000000 00000000 00000000 01000000",
vendorTestCases: map[string]*VendorTestCase{
"zero": {
vendor: 0,
wantAllowed: false, // out of bound
},
"first": {
vendor: 1,
wantAllowed: true,
},
"not-consented": {
vendor: 41,
wantAllowed: false,
},
"consented": {
vendor: 42,
wantAllowed: true,
},
"travel-audience": {
vendor: 423,
wantAllowed: true,
},
"last": {
vendor: 4202,
wantAllowed: true,
},
"out-of-bound": {
vendor: 4203,
wantAllowed: false,
},
},
wantIsRangeEncoded: false,
wantPublisherCC: "EU",
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, gotErr := ParseCoreString(tc.consentString)
if tc.wantErr != "" {
require.Equal(t, tc.wantErr, gotErr.Error(), "expected error")
return
}
require.Nil(t, gotErr, "unexpected error")
require.Equal(t, tc.wantVersion, got.Version, "wrong version")
if got.Version != 2 {
return
}
require.Equal(t, tc.wantPurposesConsent, got.PurposesConsent.ToBitString(), "wrong purposes consent")
for subName, subTc := range tc.purposeTestCases {
gotAllowed := got.EveryPurposeAllowed(subTc.purposes)
require.Equal(t, subTc.wantAllowed, gotAllowed, "EveryPurposeAllowed failed for %s", subName)
}
require.Equal(t, tc.wantSpecialFeatureOptIns, got.SpecialFeatureOptIns.ToBitString(), "wrong special feature opt-ins")
for subName, subTc := range tc.specialFeaturesTestCases {
gotAllowed := got.EverySpecialFeatureAllowed(subTc.specialFeatures)
require.Equal(t, subTc.wantAllowed, gotAllowed, "EverySpecialFeatureAllowed failed for %s", subName)
}
require.Equal(t, tc.wantMaxVendorID, got.MaxVendorID, "wrong max vendor ID")
require.Equal(t, tc.wantIsRangeEncoded, got.IsRangeEncoding, "wrong range encoding")
require.Equal(t, tc.wantRangeEntries, got.RangeEntries, "wrong range entries")
require.Equal(t, tc.wantVendors, got.ConsentedVendors.ToBitString(), "wrong consented vendors")
for subName, subTc := range tc.vendorTestCases {
gotAllowed := got.VendorAllowed(subTc.vendor)
require.Equal(t, subTc.wantAllowed, gotAllowed, "[%s] VendorAllowed failed for vendor %d", subName, subTc.vendor)
}
require.Equal(t, tc.wantPublisherCC, got.PublisherCC, "wrong publisher country code")
})
}
}
func TestParseCoreString(t *testing.T) {
tests := []struct {
name string
c string
want *Consent
wantErr bool
}{
{
"empty",
"",
nil,
true,
},
{
"with-values",
"COzcJxTOzcJxTBcAAAENAiCMAP_AAAAAAAAADTwAQDTgAAAA.IF5EX2S5OI2tho2YdF7BEYYwfJxyigMgShgQIsS8NwIeFbBoGPmAAHBG4JAQAGBAkkACBAQIsHGBcCQABgIgRiRCMQEGMjzNKBJBAggkbI0FACCVmnkHS3ZCY70-6u__bA",
&Consent{
Version: 2,
CMPID: 92,
ConsentLanguage: "EN",
VendorListVersion: 34,
TcfPolicyVersion: 2,
SpecialFeatureOptIns: Bits{0xc0, 0x0},
PurposesConsent: Bits{0xff, 0xc0, 0x0},
PurposesLITransparency: Bits{0x0, 0x0, 0x0},
PublisherCC: "AA",
MaxVendorID: 423,
NumEntries: 1,
IsRangeEncoding: true,
RangeEntries: []RangeEntry{{StartOrOnlyVendorId: 423, EndVendorID: 423}},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseCoreString(tt.c)
if tt.wantErr {
require.Error(t, err, "missing error")
} else {
require.NoError(t, err, "unexpected error")
}
if err != nil {
return
}
// test first bits fields
require.Equal(t, tt.want.SpecialFeatureOptIns, got.SpecialFeatureOptIns, "wrong special features opt-ins")
require.Equal(t, tt.want.PurposesConsent, got.PurposesConsent, "wrong purposes consent")
require.Equal(t, tt.want.PurposesLITransparency, got.PurposesLITransparency, "wrong purposes LI transparency")
require.Equal(t, tt.want.ConsentedVendors, got.ConsentedVendors, "wrong consented vendors")
got.Created = time.Time{}
got.LastUpdated = time.Time{}
require.Equal(t, tt.want, got, "wrong consent")
})
}
}