-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest-cid.js
1059 lines (976 loc) · 33.2 KB
/
test-cid.js
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as lolex from 'lolex';
import * as url from '../../src/url';
import {Crypto, installCryptoService} from '../../src/service/crypto-impl';
import {Services} from '../../src/services';
import {
cidServiceForDocForTesting,
getProxySourceOrigin,
isOptedOutOfCid,
optOutOfCid,
} from '../../src/service/cid-impl';
import {getCookie, setCookie} from '../../src/cookies';
import {installCryptoPolyfill} from '../../extensions/amp-crypto-polyfill/0.1/amp-crypto-polyfill';
import {installDocService} from '../../src/service/ampdoc-impl';
import {installDocumentInfoServiceForDoc} from '../../src/service/document-info-impl';
import {installExtensionsService} from '../../src/service/extensions-impl';
import {installGlobalDocumentStateService} from '../../src/service/document-state';
import {installPlatformService} from '../../src/service/platform-impl';
import {installTimerService} from '../../src/service/timer-impl';
import {installViewerServiceForDoc} from '../../src/service/viewer-impl';
import {macroTask} from '../../testing/yield';
import {parseUrlDeprecated} from '../../src/url';
import {stubServiceForDoc} from '../../testing/test-helper';
const DAY = 24 * 3600 * 1000;
describe('cid', () => {
let sandbox;
let clock;
let fakeWin;
let ampdoc;
let viewer;
let storage;
let viewerStorage;
let cid;
let crypto;
let viewerSendMessageStub;
let whenFirstVisible;
let trustedViewer;
let shouldSendMessageTimeout;
let storageGetStub;
const hasConsent = Promise.resolve();
const timer = Services.timerFor(window);
beforeEach(() => {
let call = 1;
sandbox = sinon.sandbox;
clock = sandbox.useFakeTimers();
whenFirstVisible = Promise.resolve();
trustedViewer = true;
shouldSendMessageTimeout = false;
storage = {};
viewerStorage = null;
fakeWin = {
localStorage: {
setItem: (key, value) => {
expect(key).to.equal('amp-cid');
expect(value).to.be.string;
storage[key] = value;
},
getItem: key => {
// isExperimentOn() in the code paths causes "amp-experiment-toggles".
expect(['amp-cid', 'amp-experiment-toggles']).to.contain(key);
return storage[key];
},
},
location: {
href: 'https://cdn.ampproject.org/v/www.origin.com/foo/?f=0',
search: 'f=0',
},
crypto: {
getRandomValues: array => {
array[0] = call++;
array[1] = 2;
array[2] = 3;
array[15] = 15;
},
},
document: {
nodeType: /* DOCUMENT */ 9,
body: {},
querySelector: () => {},
},
navigator: window.navigator,
setTimeout: window.setTimeout,
clearTimeout: window.clearTimeout,
Math: window.Math,
Promise: window.Promise,
};
fakeWin.document.defaultView = fakeWin;
installDocService(fakeWin, /* isSingleDoc */ true);
installGlobalDocumentStateService(fakeWin);
ampdoc = Services.ampdocServiceFor(fakeWin).getSingleDoc();
installTimerService(fakeWin);
installPlatformService(fakeWin);
installDocumentInfoServiceForDoc(ampdoc);
installExtensionsService(fakeWin);
const extensions = Services.extensionsFor(fakeWin);
// stub extensions service to provide crypto-polyfill
sandbox.stub(extensions, 'preloadExtension').callsFake(extensionId => {
expect(extensionId).to.equal('amp-crypto-polyfill');
installCryptoPolyfill(fakeWin);
return Promise.resolve();
});
installViewerServiceForDoc(ampdoc);
storageGetStub = stubServiceForDoc(sandbox, ampdoc, 'storage', 'get');
viewer = Services.viewerForDoc(ampdoc);
sandbox.stub(viewer, 'whenFirstVisible').callsFake(function() {
return whenFirstVisible;
});
sandbox
.stub(viewer, 'isTrustedViewer')
.callsFake(() => Promise.resolve(trustedViewer));
viewerSendMessageStub = sandbox
.stub(viewer, 'sendMessageAwaitResponse')
.callsFake((eventType, opt_data) => {
if (eventType != 'cid') {
return Promise.reject();
}
if (shouldSendMessageTimeout) {
return timer.promise(15000);
}
if (opt_data) {
viewerStorage = opt_data;
}
return Promise.resolve(viewerStorage || undefined);
});
cid = cidServiceForDocForTesting(ampdoc);
sandbox.stub(cid, 'isScopeOptedIn_').callsFake(() => null);
installCryptoService(fakeWin);
crypto = Services.cryptoFor(fakeWin);
sandbox.stub(cid.cacheCidApi_, 'isSupported').returns(false);
});
afterEach(() => {
window.localStorage.removeItem('amp-cid');
sandbox.restore();
});
describe('with real crypto', () => {
it('should hash domain name and scope', () => {
// domain name: 'http://www.origin.com'
// scope: 'custom-cid-scope'
return compare(
'custom-cid-scope',
'tSSEaSFEU2-vkEO0Mb9HZejJui-npGRhXf3fD3H3iWeYQrjkQOTaRYdPyFTogNXA'
);
});
it('should hash domain name and scope', () => {
fakeWin.location.href =
'https://cdn.ampproject.org/v/www.DIFFERENT.com/foo/?f=0';
// domain name: 'http://www.different.com'
// scope: 'another-custom-cid-scope'
return compare(
'another-custom-cid-scope',
'25lYHB6Luck8Z5ddpiB-FBbj2pa9zx3WdnJlZVgFneJRcFsDT3kIoPoi6k6-oxrB'
);
});
});
describe('with crypto stub', () => {
const doesNotProvideError = '[CID] Viewer does not provide cap=cid';
const invalidFormatError = '[CID] invalid cid format';
beforeEach(() => {
crypto.sha384Base64 = val => {
if (val instanceof Uint8Array) {
val = '[' + Array.apply([], val).join(',') + ']';
}
return Promise.resolve('sha384(' + val + ')');
};
});
it('should depend on external id e1', () => {
clock.tick(123);
return compare(
'e1',
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come1)'
).then(() => {
expect(storage['amp-cid']).to.be.string;
const stored = JSON.parse(storage['amp-cid']);
expect(stored.cid).to.equal(
'sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])'
);
expect(stored.time).to.equal(123);
});
});
it('should depend on external id e2', () => {
return compare(
'e2',
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)'
);
});
it('should depend on domain', () => {
fakeWin.location.href =
'https://cdn.ampproject.org/v/www.DIFFERENT.com/foo/?f=0';
return compare(
'e2',
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.different.come2)'
);
});
it('should fallback to cookie value on custom domain.', () => {
fakeWin.location.href = 'https://abc.org/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.document.cookie = 'cookie_name=12345;scope_name=54321;';
return cid
.get(
{
scope: 'scope_name',
},
hasConsent
)
.then(c => {
expect(c).to.equal('54321');
});
});
it('should fallback to cookie of given name on custom domain.', () => {
fakeWin.location.href = 'https://abc.org/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.document.cookie = 'cookie_name=12345;scope_name=54321;';
return cid
.get(
{
scope: 'scope_name',
cookieName: 'cookie_name',
},
hasConsent
)
.then(c => {
expect(c).to.equal('12345');
});
});
it(
'should depend fall back to cookies on custom ' +
'domain and not create a cookie',
() => {
fakeWin.location.href =
'https://some.domain/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.document.cookie = 'cookie_name=12345;';
return compare('other_cookie', null);
}
);
it('should produce golden value', () => {
crypto.sha384Base64 = new Crypto(fakeWin).sha384Base64;
return compare(
'e2',
'q0pPfZTWGruPrtURDJHexzs-MgOkt9SJAsAZodzr8tx8hKv8BS62AVpbttaFX8fK'
);
});
it('should be stable with respect to a saved seed', () => {
const expected =
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)';
return compare('e2', expected).then(() => {
return compare('e2', expected).then(() => {
storage['amp-cid'] = undefined;
removeMemoryCacheOfCid();
return compare(
'e2',
'sha384(sha384([' +
// 2 because we increment the first value on each random
// call.
'2' +
',2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)'
);
});
});
});
it('should pick up the cid value from storage', () => {
storage['amp-cid'] = JSON.stringify({
cid: 'YYY',
time: Date.now(),
});
return compare('e2', 'sha384(YYYhttp://www.origin.come2)');
});
it('should return empty if opted out', () => {
storageGetStub.withArgs('amp-cid-optout').returns(Promise.resolve(true));
storage['amp-cid'] = JSON.stringify({
cid: 'YYY',
time: Date.now(),
});
return compare('e2', '');
});
it('should read from viewer storage if embedded', () => {
expectAsyncConsoleError(doesNotProvideError);
fakeWin.parent = {};
const expectedBaseCid = 'from-viewer';
viewerStorage = JSON.stringify({
time: 0,
cid: expectedBaseCid,
});
return Promise.all([
compare('e1', `sha384(${expectedBaseCid}http://www.origin.come1)`),
compare('e2', `sha384(${expectedBaseCid}http://www.origin.come2)`),
])
.then(() => {
expect(viewerSendMessageStub).to.be.calledOnce;
expect(viewerSendMessageStub).to.be.calledWith('cid');
// Ensure it's called only once since we cache it in memory.
return compare(
'e3',
`sha384(${expectedBaseCid}http://www.origin.come3)`
);
})
.then(() => {
expect(viewerSendMessageStub).to.be.calledOnce;
return expect(cid.baseCid_).to.eventually.equal(expectedBaseCid);
});
});
it(
'should read from viewer storage if embedded and convert cid to ' +
'new format',
() => {
expectAsyncConsoleError(doesNotProvideError);
expectAsyncConsoleError(invalidFormatError);
fakeWin.parent = {};
const expectedBaseCid = 'from-viewer';
// baseCid returned by legacy API
viewerStorage = expectedBaseCid;
return Promise.all([
compare('e1', `sha384(${expectedBaseCid}http://www.origin.come1)`),
compare('e2', `sha384(${expectedBaseCid}http://www.origin.come2)`),
]).then(() => {
expect(viewerSendMessageStub).to.be.calledOnce;
expect(viewerSendMessageStub).to.be.calledWith('cid');
});
}
);
it('should not read from untrusted viewer', () => {
fakeWin.parent = {};
trustedViewer = false;
const viewerBaseCid = 'from-viewer';
viewerStorage = JSON.stringify({
time: 0,
cid: viewerBaseCid,
});
return Promise.all([
compare(
'e1',
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come1)'
),
compare(
'e2',
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)'
),
]).then(() => {
expect(viewerSendMessageStub).to.not.be.called;
});
});
it('should store to viewer storage if embedded', () => {
expectAsyncConsoleError(doesNotProvideError, 2);
fakeWin.parent = {};
const expectedBaseCid = 'sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])';
return compare('e2', `sha384(${expectedBaseCid}http://www.origin.come2)`)
.then(() => {
expect(viewerSendMessageStub).to.be.calledWith(
'cid',
JSON.stringify({
time: 0,
cid: expectedBaseCid,
})
);
// Ensure it's called only once since we cache it in memory.
return compare(
'e3',
`sha384(${expectedBaseCid}http://www.origin.come3)`
);
})
.then(() => {
expect(viewerSendMessageStub).to.be.calledWith(sinon.match.string);
return expect(cid.baseCid_).to.eventually.equal(expectedBaseCid);
});
});
it('should prefer value in storage if present', () => {
fakeWin.parent = {};
storage['amp-cid'] = JSON.stringify({
cid: 'in-storage',
time: Date.now(),
});
return compare('e2', 'sha384(in-storagehttp://www.origin.come2)');
});
it('should expire on read after 365 days', () => {
const expected =
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)';
return compare('e2', expected).then(() => {
clock.tick(364 * DAY);
return compare('e2', expected).then(() => {
clock.tick(365 * DAY + 1);
removeMemoryCacheOfCid();
return compare(
'e2',
'sha384(sha384([' +
// 2 because we increment the first value on each random
// call.
'2' +
',2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)'
);
});
});
});
it('should expire on read after 365 days when embedded', () => {
expectAsyncConsoleError(doesNotProvideError, 3);
fakeWin.parent = {};
const expectedBaseCid = 'from-viewer';
viewerStorage = JSON.stringify({
time: 0,
cid: expectedBaseCid,
});
const expectedIdFromViewer = 'sha384(from-viewerhttp://www.origin.come2)';
const expectedNewId =
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)';
return compare('e2', expectedIdFromViewer).then(() => {
clock.tick(364 * DAY);
return compare('e2', expectedIdFromViewer).then(() => {
clock.tick(365 * DAY + 1);
removeMemoryCacheOfCid();
return compare('e2', expectedNewId);
});
});
});
it('should set last access time once a day', () => {
const expected =
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)';
function getStoredTime() {
return JSON.parse(storage['amp-cid']).time;
}
clock.tick(100);
return compare('e2', expected).then(() => {
expect(getStoredTime()).to.equal(100);
removeMemoryCacheOfCid();
clock.tick(3600);
return compare('e2', expected).then(() => {
expect(getStoredTime()).to.equal(100);
removeMemoryCacheOfCid();
clock.tick(DAY);
return compare('e2', expected).then(() => {
expect(getStoredTime()).to.equal(100 + 3600 + DAY);
});
});
});
});
it('should set last access time once a day when embedded', () => {
expectAsyncConsoleError(doesNotProvideError, 5);
fakeWin.parent = {};
const expected =
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)';
function getStoredTime() {
return JSON.parse(viewerStorage).time;
}
clock.tick(100);
return compare('e2', expected).then(() => {
expect(getStoredTime()).to.equal(100);
removeMemoryCacheOfCid();
clock.tick(3600);
return compare('e2', expected).then(() => {
expect(getStoredTime()).to.equal(100);
removeMemoryCacheOfCid();
clock.tick(DAY);
return compare('e2', expected).then(() => {
expect(getStoredTime()).to.equal(100 + 3600 + DAY);
});
});
});
});
it('should wait until after pre-rendering', () => {
let nonce = 'not visible';
whenFirstVisible = timer.promise(100).then(() => {
nonce = 'visible';
});
const p = cid.get({scope: 'test'}, hasConsent).then(unusedC => {
expect(nonce).to.equal('visible');
});
clock.tick(100);
return p;
});
it('should wait for consent', () => {
let nonce = 'before';
const consent = timer.promise(100).then(() => {
nonce = 'timer fired';
});
const p = cid.get({scope: 'test'}, consent).then(unusedC => {
expect(nonce).to.equal('timer fired');
});
clock.tick(100);
return p;
});
it('should fail on failed consent', () => {
return expect(cid.get({scope: 'abc'}, Promise.reject())).to.be.rejected;
});
it('should fail on invalid scope', () => {
allowConsoleError(() => {
expect(() => {
cid.get({scope: '$$$'}, Promise.resolve());
}).to.throw(/\$\$\$/);
});
});
it('should not store until persistence promise resolves', () => {
let resolve;
const persistencePromise = new Promise(r => {
resolve = r;
});
let sha384Promise;
crypto.sha384Base64 = val => {
if (val instanceof Uint8Array) {
val = '[' + Array.apply([], val).join(',') + ']';
}
return (sha384Promise = Promise.resolve('sha384(' + val + ')'));
};
return cid.get({scope: 'e2'}, hasConsent, persistencePromise).then(c => {
expect(c).to.equal(
'sha384(sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])http://www.origin.come2)'
);
expect(storage['amp-cid']).to.be.undefined;
clock.tick(777);
resolve();
return Promise.all([persistencePromise, sha384Promise]).then(() => {
expect(storage['amp-cid']).to.be.string;
const stored = JSON.parse(storage['amp-cid']);
expect(stored.cid).to.equal(
'sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])'
);
expect(stored.time).to.equal(777);
});
});
});
it('should not wait persistence consent for viewer storage', () => {
expectAsyncConsoleError(doesNotProvideError, 2);
fakeWin.parent = {};
const persistencePromise = new Promise(() => {
/* never resolves */
});
return cid.get({scope: 'e2'}, hasConsent, persistencePromise).then(() => {
expect(viewerStorage).to.equal(
JSON.stringify({
time: 0,
cid: 'sha384([1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,15])',
})
);
});
});
it('fallback with no window.crypto', () => {
fakeWin.crypto = undefined;
fakeWin.screen = {
width: '111',
height: '222',
};
fakeWin.Math = {
random: () => {
return 999;
},
};
clock.tick(7777);
return compare(
'e2',
'sha384(sha384(https://cdn.ampproject.org/v' +
'/www.origin.com/foo/?f=07777999111222)http://www.origin.come2)'
);
});
it('should NOT create fallback cookie by default with string scope', () => {
fakeWin.location.href = 'https://abc.org/v/www.DIFFERENT.com/foo/?f=0';
return cid.get({scope: 'cookie_name'}, hasConsent).then(c => {
expect(c).to.not.exist;
expect(fakeWin.document.cookie).to.not.exist;
});
});
it('should NOT create fallback cookie by default with struct scope', () => {
fakeWin.location.href = 'https://abc.org/v/www.DIFFERENT.com/foo/?f=0';
return cid.get({scope: 'cookie_name'}, hasConsent).then(c => {
expect(c).to.not.exist;
expect(fakeWin.document.cookie).to.not.exist;
});
});
it('should create fallback cookie when asked', () => {
fakeWin.location.href =
'https://foo.abc.org/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.location.hostname = 'foo.abc.org';
fakeWin.crypto.getRandomValues = array => {
array[0] = 0;
array[1] = 2;
array[2] = 4;
array[3] = 8;
array[4] = 16;
array[5] = 32;
array[6] = 64;
array[7] = 128;
array[8] = 255;
array[9] = 7;
array[10] = 11;
array[11] = 22;
array[12] = 33;
array[13] = 66;
array[14] = 200;
array[15] = 39;
};
return cid
.get({scope: 'scope_name', createCookieIfNotPresent: true}, hasConsent)
.then(c => {
expect(c).to.exist;
// Since various parties depend on the cookie values, please be careful
// about changing the format.
expect(c).to.equal('amp-AAIECBAgQID_BwsWIULIJw');
expect(fakeWin.document.cookie).to.equal(
'scope_name=' +
encodeURIComponent(c) +
'; path=/' +
'; domain=abc.org' +
'; expires=Fri, 01 Jan 1971 00:00:00 GMT'
); // 1 year from 0.
});
});
it('should create fallback cookie with provided name', () => {
fakeWin.location.href =
'https://foo.abc.org/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.location.hostname = 'foo.abc.org';
return cid
.get(
{
scope: 'scope_name',
createCookieIfNotPresent: true,
cookieName: 'cookie_name',
},
hasConsent
)
.then(c => {
expect(c).to.exist;
expect(c).to.equal('amp-AQIDAAAAAAAAAAAAAAAADw');
expect(fakeWin.document.cookie).to.equal(
'cookie_name=' +
encodeURIComponent(c) +
'; path=/' +
'; domain=abc.org' +
'; expires=Fri, 01 Jan 1971 00:00:00 GMT'
); // 1 year from 0.
});
});
it('should update fallback cookie expiration when present', () => {
fakeWin.location.href =
'https://foo.abc.org/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.location.hostname = 'foo.abc.org';
fakeWin.document.cookie = 'cookie_name=amp-12345';
return cid.get({scope: 'cookie_name'}, hasConsent).then(c => {
expect(fakeWin.document.cookie).to.equal(
'cookie_name=' +
encodeURIComponent(c) +
'; path=/' +
'; domain=abc.org' +
'; expires=Fri, 01 Jan 1971 00:00:00 GMT' // 1 year from 0.
);
});
});
it('should not update expiration when created externally', () => {
fakeWin.location.href =
'https://foo.abc.org/v/www.DIFFERENT.com/foo/?f=0';
fakeWin.location.hostname = 'foo.abc.org';
fakeWin.document.cookie = 'cookie_name=12345';
return cid.get({scope: 'cookie_name'}, hasConsent).then(() => {
expect(fakeWin.document.cookie).to.equal('cookie_name=12345');
});
});
it('should return same value for multiple calls on non-proxied urls', () => {
fakeWin.location.href = 'https://abc.org/foo/?f=0';
fakeWin.location.hostname = 'foo.abc.org';
const cid1 = cid.get(
{scope: 'cookie', createCookieIfNotPresent: true},
hasConsent
);
const cid2 = cid.get(
{scope: 'cookie', createCookieIfNotPresent: true},
hasConsent
);
return cid1.then(c1 => {
return cid2.then(c2 => {
expect(c1).to.equal(c2);
});
});
});
it('should return same value for multiple calls on proxied urls', () => {
fakeWin.location.href = 'https://cdn.ampproject.org/v/abc.org/foo/?f=0';
fakeWin.location.hostname = 'cdn.ampproject.org';
const cid1 = cid.get(
{scope: 'cookie', createCookieIfNotPresent: true},
hasConsent
);
return cid1.then(c1 => {
const cid2 = cid.get(
{scope: 'cookie', createCookieIfNotPresent: true},
hasConsent
);
return cid2.then(c2 => {
expect(c1).to.equal(c2);
});
});
});
it('should retreive cookie value with . in name', () => {
fakeWin.location.href = 'https://abc.org/';
fakeWin.document.cookie = '_sp_id.44=4567;';
return compare('_sp_id.44', '4567');
});
});
function compare(externalCidScope, compareValue) {
return cid.get({scope: externalCidScope}, hasConsent).then(c => {
expect(c).to.equal(compareValue);
});
}
function removeMemoryCacheOfCid(opt_cid) {
(opt_cid || cid).baseCid_ = null;
}
});
describe('getProxySourceOrigin', () => {
it('should fail on non-proxy origin', () => {
allowConsoleError(() => {
expect(() => {
getProxySourceOrigin(parseUrlDeprecated('https://abc.org/v/foo.com/'));
}).to.throw(/Expected proxy origin/);
});
});
});
describes.realWin('cid', {amp: true}, env => {
let cid;
let win;
let ampdoc;
let sandbox;
let clock;
const hasConsent = Promise.resolve();
beforeEach(() => {
win = env.win;
ampdoc = env.ampdoc;
sandbox = env.sandbox;
clock = lolex.install({
target: win,
toFake: ['Date', 'setTimeout', 'clearTimeout'],
});
cid = cidServiceForDocForTesting(ampdoc);
sandbox.stub(cid.cacheCidApi_, 'isSupported').returns(false);
});
afterEach(() => {
clock.uninstall();
});
it('should store CID in cookie when not in Viewer', function*() {
sandbox
.stub(cid.viewerCidApi_, 'isSupported')
.returns(Promise.resolve(false));
setCookie(win, 'foo', '', 0);
const fooCid = yield cid.get(
{
scope: 'foo',
createCookieIfNotPresent: true,
},
hasConsent
);
expect(fooCid).to.have.string('amp-');
const fooCid2 = yield cid.get(
{
scope: 'foo',
createCookieIfNotPresent: true,
},
hasConsent
);
expect(fooCid).to.equal(fooCid2);
});
it('get method should return CID when in Viewer ', () => {
sandbox
.stub(cid.viewerCidApi_, 'isSupported')
.returns(Promise.resolve(true));
win.parent = {};
stubServiceForDoc(
sandbox,
ampdoc,
'viewer',
'sendMessageAwaitResponse'
).returns(Promise.resolve('cid-from-viewer'));
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'isTrustedViewer').returns(
Promise.resolve(true)
);
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'hasCapability')
.withArgs('cid')
.returns(true);
sandbox.stub(url, 'isProxyOrigin').returns(true);
return expect(cid.get({scope: 'foo'}, hasConsent)).to.eventually.equal(
'cid-from-viewer'
);
});
it('get method should time out when in Viewer', function*() {
sandbox
.stub(cid.viewerCidApi_, 'isSupported')
.returns(Promise.resolve(true));
win.parent = {};
stubServiceForDoc(
sandbox,
ampdoc,
'viewer',
'sendMessageAwaitResponse'
).returns(new Promise(() => {}));
stubServiceForDoc(sandbox, ampdoc, 'viewer', 'isTrustedViewer').returns(
Promise.resolve(true)
);
const storageGetStub = stubServiceForDoc(sandbox, ampdoc, 'storage', 'get');
storageGetStub.withArgs('amp-cid-optout').returns(Promise.resolve(false));
sandbox.stub(url, 'isProxyOrigin').returns(true);
let scopedCid = undefined;
let resolved = false;
cid.get({scope: 'foo'}, hasConsent).then(result => {
scopedCid = result;
resolved = true;
});
yield macroTask();
clock.tick(9999);
yield macroTask();
expect(resolved).to.be.false;
expect(scopedCid).to.be.undefined;
yield macroTask();
});
describe('pub origin, CID API opt in', () => {
beforeEach(() => {
sandbox.stub(url, 'isProxyOrigin').returns(false);
ampdoc.win.document.head.innerHTML +=
'<meta name="amp-google-client-id-api" content="googleanalytics">';
setCookie(win, '_ga', '', 0);
});
afterEach(() => {
setCookie(win, '_ga', '', 0);
});
it('should use cid api on pub origin if opted in', () => {
cid.apiKeyMap_ = {'AMP_ECID_GOOGLE': 'cid-api-key'};
const getScopedCidStub = sandbox.stub(cid.cidApi_, 'getScopedCid');
getScopedCidStub.returns(Promise.resolve('cid-from-api'));
return cid
.get(
{
scope: 'AMP_ECID_GOOGLE',
cookieName: '_ga',
createCookieIfNotPresent: true,
},
hasConsent
)
.then(scopedCid => {
expect(getScopedCidStub).to.be.calledWith(
'cid-api-key',
'AMP_ECID_GOOGLE'
);
expect(scopedCid).to.equal('cid-from-api');
expect(getCookie(win, '_ga')).to.equal('cid-from-api');
});
});
it('should fallback to cookie if cid api returns nothing', () => {
sandbox.stub(cid.cidApi_, 'getScopedCid').returns(Promise.resolve());
return cid
.get(
{
scope: 'AMP_ECID_GOOGLE',
cookieName: '_ga',
createCookieIfNotPresent: true,
},
hasConsent
)
.then(scopedCid => {
expect(scopedCid).to.contain('amp-');
expect(getCookie(win, '_ga')).to.equal(scopedCid);
});
});
it('should respect CID API opt out', () => {
sandbox
.stub(cid.cidApi_, 'getScopedCid')
.returns(Promise.resolve('$OPT_OUT'));
return cid
.get(
{
scope: 'AMP_ECID_GOOGLE',
cookieName: '_ga',
createCookieIfNotPresent: true,
},
hasConsent
)
.then(scopedCid => {
expect(scopedCid).to.be.null;
expect(getCookie(win, '_ga')).to.be.null;
});
});
});
describe('isScopeOptedIn', () => {
it('should read predefined clients and custom API keys correctly', () => {
ampdoc.win.document.head.innerHTML +=
'<meta name="amp-google-client-id-api" ' +
'content="googleanalytics, ' +
'foo = foo-api-key,' +
'bar=bar-api-key ,' +
'hello=hello-api-key">';
expect(cid.isScopeOptedIn_('AMP_ECID_GOOGLE')).to.equal(
'AIzaSyA65lEHUEizIsNtlbNo-l2K18dT680nsaM'
);
expect(cid.isScopeOptedIn_('foo')).to.equal('foo-api-key');
expect(cid.isScopeOptedIn_('bar')).to.equal('bar-api-key');
expect(cid.isScopeOptedIn_('hello')).to.equal('hello-api-key');
expect(cid.isScopeOptedIn_('non-existing')).to.be.undefined;
});
it('should work if meta only contains predefined clients', () => {
ampdoc.win.document.head.innerHTML +=
'<meta name="amp-google-client-id-api" content="googleanalytics">';
expect(cid.isScopeOptedIn_('AMP_ECID_GOOGLE')).to.equal(
'AIzaSyA65lEHUEizIsNtlbNo-l2K18dT680nsaM'
);
});
it('should work if meta only contains custom scopes', () => {
ampdoc.win.document.head.innerHTML +=
'<meta name="amp-google-client-id-api" ' +
'content="' +
'foo=foo-api-key,' +
'bar=bar-api-key">';
expect(cid.isScopeOptedIn_('foo')).to.equal('foo-api-key');
expect(cid.isScopeOptedIn_('bar')).to.equal('bar-api-key');
});
it('should not work if vendor not whitelisted', () => {
ampdoc.win.document.head.innerHTML +=
'<meta name="amp-google-client-id-api" content="abodeanalytics">';
allowConsoleError(() => {
expect(cid.isScopeOptedIn_('AMP_ECID_GOOGLE')).to.equal(undefined);
});
});
});
});
describes.fakeWin('cid optout:', {amp: true}, env => {