-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_rum.c
1248 lines (1061 loc) · 46.5 KB
/
test_rum.c
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 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <errno.h>
#include <glob.h>
#include <stddef.h>
#include <stdio.h>
#include "nr_attributes.h"
#include "nr_rum.h"
#include "nr_rum_private.h"
#include "nr_txn.h"
#include "util_errno.h"
#include "util_memory.h"
#include "util_strings.h"
#include "util_text.h"
#include "tlib_main.h"
static void test_do_autorum(void) {
int do_autorum;
nrtxn_t txn;
txn.status.background = 0;
txn.options.autorum_enabled = 1;
do_autorum = nr_rum_do_autorum(0);
tlib_pass_if_true("null txn", 0 == do_autorum, "do_autorum=%d", do_autorum);
txn.status.background = 1;
do_autorum = nr_rum_do_autorum(&txn);
tlib_pass_if_true("background txn", 0 == do_autorum, "do_autorum=%d",
do_autorum);
txn.status.background = 0;
txn.options.autorum_enabled = 0;
do_autorum = nr_rum_do_autorum(&txn);
tlib_pass_if_true("autorum disabled", 0 == do_autorum, "do_autorum=%d",
do_autorum);
txn.options.autorum_enabled = 1;
do_autorum = nr_rum_do_autorum(&txn);
tlib_pass_if_true("success", 1 == do_autorum, "do_autorum=%d", do_autorum);
}
static void test_produce_header_bad_params(void) {
char* hdr;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
nrobj_t* app_connect_reply
= nro_create_from_json("{\"js_agent_loader\":\"LOADER\"}");
int tags = 1;
int autorum = 1;
txn->status.ignore = 0;
txn->options.autorum_enabled = 1;
txn->status.rum_header = 0;
txn->app_connect_reply = app_connect_reply;
txn->status.ignore = 1;
hdr = nr_rum_produce_header(txn, tags, autorum);
tlib_pass_if_true("Header: Ignore txn", 0 == hdr, "hdr=%p", hdr);
tlib_pass_if_true("Header: Ignore txn", 0 == txn->status.rum_header,
"status.rum_header=%d", txn->status.rum_header);
txn->status.ignore = 0;
txn->options.autorum_enabled = 0;
hdr = nr_rum_produce_header(txn, tags, autorum);
tlib_pass_if_true("Header: Auto-RUM disabled", 0 == hdr, "hdr=%p", hdr);
tlib_pass_if_true("Header: Auto-RUM disabled", 0 == txn->status.rum_header,
"status.rum_header=%d", txn->status.rum_header);
txn->options.autorum_enabled = 1;
hdr = nr_rum_produce_header(0, tags, autorum);
tlib_pass_if_true("Header: Null txn", 0 == hdr, "hdr=%p", hdr);
txn->status.rum_header = 1;
hdr = nr_rum_produce_header(txn, tags, autorum);
tlib_pass_if_true("Header: Previous Header", 0 == hdr, "hdr=%p", hdr);
tlib_pass_if_true("Header: Previous Header", 1 == txn->status.rum_header,
"txn->status.rum_header=%d", txn->status.rum_header);
txn->status.rum_header = 0;
txn->app_connect_reply = 0;
hdr = nr_rum_produce_header(txn, tags, autorum);
tlib_pass_if_true("Header: No loader", 0 == hdr, "hdr=%p", hdr);
tlib_pass_if_true("Header: No loader", 0 == txn->status.rum_header,
"txn->status.rum_header=%d", txn->status.rum_header);
txn->app_connect_reply = app_connect_reply;
/* Verify that the inputs above were good when not altered. */
hdr = nr_rum_produce_header(txn, tags, autorum);
tlib_pass_if_true("Header: Good inputs", 0 != hdr, "hdr=%p", hdr);
tlib_pass_if_true("Header: Good inputs", txn->status.rum_header > 1,
"txn->status.rum_header=%d", txn->status.rum_header);
nr_free(hdr);
nro_delete(app_connect_reply);
}
static void test_produce_header(void) {
char* hdr;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
nrobj_t* app_connect_reply
= nro_create_from_json("{\"js_agent_loader\":\"LOADER\"}");
txn->status.ignore = 0;
txn->options.autorum_enabled = 1;
txn->status.rum_header = 0;
txn->app_connect_reply = app_connect_reply;
txn->status.rum_header = 0;
hdr = nr_rum_produce_header(txn, 1, 0);
tlib_pass_if_true(
"Header: manual tags",
0 == nr_strcmp("<script type=\"text/javascript\">LOADER</script>", hdr),
"hdr=%s", NRSAFESTR(hdr));
tlib_pass_if_true("Header: manual tags", 1 == txn->status.rum_header,
"txn->status.rum_header=%d", txn->status.rum_header);
nr_free(hdr);
txn->options.autorum_enabled = 0;
txn->status.rum_header = 0;
hdr = nr_rum_produce_header(txn, 1, 0);
tlib_pass_if_true(
"Header: autorum disabled manual tags",
0 == nr_strcmp("<script type=\"text/javascript\">LOADER</script>", hdr),
"hdr=%s", NRSAFESTR(hdr));
tlib_pass_if_true("Header: autorum disabled manual tags",
1 == txn->status.rum_header, "txn->status.rum_header=%d",
txn->status.rum_header);
nr_free(hdr);
txn->options.autorum_enabled = 1;
txn->status.rum_header = 0;
hdr = nr_rum_produce_header(txn, 0, 0);
tlib_pass_if_true("Header: manual no tags", 0 == nr_strcmp("LOADER", hdr),
"hdr=%s", NRSAFESTR(hdr));
tlib_pass_if_true("Header: manual no tags", 1 == txn->status.rum_header,
"txn->status.rum_header=%d", txn->status.rum_header);
nr_free(hdr);
txn->status.rum_header = 0;
hdr = nr_rum_produce_header(txn, 0, 1);
tlib_pass_if_true("Header: autorum no tags", 0 == nr_strcmp("LOADER", hdr),
"hdr=%s", NRSAFESTR(hdr));
tlib_pass_if_true("Header: autorum no tags", 2 == txn->status.rum_header,
"txn->status.rum_header=%d", txn->status.rum_header);
nr_free(hdr);
txn->status.rum_header = 0;
hdr = nr_rum_produce_header(txn, 1, 1);
tlib_pass_if_true(
"Header: autorum tags",
0 == nr_strcmp("<script type=\"text/javascript\">LOADER</script>", hdr),
"hdr=%s", NRSAFESTR(hdr));
tlib_pass_if_true("Header: autorum tags", 2 == txn->status.rum_header,
"txn->status.rum_header=%d", txn->status.rum_header);
nr_free(hdr);
nro_delete(app_connect_reply);
}
typedef struct _rum_mock_txn_t {
nrtxn_t txn;
nr_status_t fake_freeze_name_return;
nrtime_t fake_queue_time;
nrtime_t unfinished_duration;
} rum_mock_txn_t;
nr_status_t nr_txn_freeze_name_update_apdex(nrtxn_t* txn) {
return ((const rum_mock_txn_t*)txn)->fake_freeze_name_return;
}
nrtime_t nr_txn_queue_time(const nrtxn_t* txn) {
return ((const rum_mock_txn_t*)txn)->fake_queue_time;
}
nrtime_t nr_txn_unfinished_duration(const nrtxn_t* txn) {
return ((const rum_mock_txn_t*)txn)->unfinished_duration;
}
static void test_produce_footer_bad_params(void) {
char* ftr;
rum_mock_txn_t tnn;
nrtxn_t* txn = &tnn.txn;
int tags = 1;
int autorum = 1;
tnn.fake_queue_time = 3 * NR_TIME_DIVISOR_MS;
tnn.unfinished_duration = 5 * NR_TIME_DIVISOR_MS;
txn->status.ignore = 0;
txn->options.autorum_enabled = 1;
txn->status.rum_header = 1;
txn->status.rum_footer = 0;
tnn.fake_freeze_name_return = NR_SUCCESS;
txn->attributes = 0;
txn->license = nr_strdup("0123456789abcdefghijklmnopqrstuvwxyz1234");
txn->app_connect_reply = 0;
txn->name = nr_strdup("WebTransaction/brink/of/glory");
txn->options.tt_threshold = 1 * NR_TIME_DIVISOR_MS;
ftr = nr_rum_produce_footer(0, tags, autorum);
tlib_pass_if_true("null txn", 0 == ftr, "ftr=%p", ftr);
tlib_pass_if_true("null txn", 0 == txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
txn->status.ignore = 1;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("ignore txn", 0 == ftr, "ftr=%p", ftr);
tlib_pass_if_true("ignore txn", 0 == txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
txn->status.ignore = 0;
txn->options.autorum_enabled = 0;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("autorum disabled", 0 == ftr, "ftr=%p", ftr);
tlib_pass_if_true("autorum disabled", 0 == txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
txn->options.autorum_enabled = 1;
txn->status.rum_header = 0;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("header not produced", 0 == ftr, "ftr=%p", ftr);
tlib_pass_if_true("header not produced", 0 == txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
txn->status.rum_header = 1;
txn->status.rum_footer = 1;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("footer already produced", 0 == ftr, "ftr=%p", ftr);
tlib_pass_if_true("footer already produced", 1 == txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
txn->status.rum_footer = 0;
tnn.fake_freeze_name_return = NR_FAILURE;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("freeze name failure", 0 == ftr, "ftr=%p", ftr);
tlib_pass_if_true("freeze name failure", 0 == txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
tnn.fake_freeze_name_return = NR_SUCCESS;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("tests are valid", 0 != ftr, "ftr=%p", ftr);
tlib_pass_if_true("tests are valid", 0 != txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
nr_free(txn->license);
nr_free(txn->name);
}
static nr_status_t obj_to_attributes_iter(const char* key,
const nrobj_t* val,
void* ptr) {
nr_attributes_t* attributes = (nr_attributes_t*)ptr;
nr_attributes_user_add_string(attributes, NR_ATTRIBUTE_DESTINATION_BROWSER,
key, nro_get_string(val, 0));
return NR_SUCCESS;
}
static void test_produce_footer_testcases(void) {
char* json = 0;
nrobj_t* array = 0;
nrotype_t otype;
int i;
#define TEST_RUM_FOOTER_TEST_FILE \
CROSS_AGENT_TESTS_DIR "/rum_client_config.json"
json = nr_read_file_contents(TEST_RUM_FOOTER_TEST_FILE, 10 * 1000 * 1000);
tlib_pass_if_true("tests valid", 0 != json, "json=%p", json);
if (0 == json) {
return;
}
array = nro_create_from_json(json);
tlib_pass_if_true("tests valid", 0 != array, "array=%p", array);
otype = nro_type(array);
tlib_pass_if_true("tests valid", NR_OBJECT_ARRAY == otype, "otype=%d",
(int)otype);
if (array && (NR_OBJECT_ARRAY == nro_type(array))) {
for (i = 1; i <= nro_getsize(array); i++) {
const nrobj_t* hash = nro_get_array_hash(array, i, 0);
const char* testname = nro_get_hash_string(hash, "testname", 0);
nrtime_t apptime_milliseconds
= (nrtime_t)nro_get_hash_int(hash, "apptime_milliseconds", 0);
nrtime_t queuetime_milliseconds
= (nrtime_t)nro_get_hash_int(hash, "queuetime_milliseconds", 0);
nrtime_t trace_threshold_microseconds
= (nrtime_t)nro_get_hash_int(hash, "trace_threshold_microseconds", 0);
int browser_monitoring_attributes_enabled = nro_get_hash_boolean(
hash, "browser_monitoring.attributes.enabled", 0);
const char* txnname = nro_get_hash_string(hash, "transaction_name", 0);
const char* license_key = nro_get_hash_string(hash, "license_key", 0);
const nrobj_t* connect_reply
= nro_get_hash_hash(hash, "connect_reply", 0);
const nrobj_t* user_attributes
= nro_get_hash_hash(hash, "user_attributes", 0);
const nrobj_t* expected = nro_get_hash_hash(hash, "expected", 0);
tlib_pass_if_true("tests valid", 0 != hash, "hash=%p", hash);
if (hash) {
rum_mock_txn_t mock;
nrtxn_t* txn = &mock.txn;
char* actual;
int tags = 0;
int autorum = 0;
nr_attributes_t* attributes;
nr_attribute_config_t* config = nr_attribute_config_create();
if (0 == browser_monitoring_attributes_enabled) {
nr_attribute_config_disable_destinations(
config, NR_ATTRIBUTE_DESTINATION_BROWSER);
}
attributes = nr_attributes_create(config);
nr_attribute_config_destroy(&config);
txn->options.autorum_enabled = 1;
mock.fake_queue_time = NR_TIME_DIVISOR_MS * queuetime_milliseconds;
mock.unfinished_duration = NR_TIME_DIVISOR_MS * apptime_milliseconds;
txn->options.tt_threshold
= NR_TIME_DIVISOR_MS * (trace_threshold_microseconds);
txn->status.ignore = 0;
txn->status.rum_header = 1;
txn->status.rum_footer = 0;
mock.fake_freeze_name_return = NR_SUCCESS;
txn->attributes = attributes;
nro_iteratehash(user_attributes, obj_to_attributes_iter, attributes);
txn->app_connect_reply = nro_copy(connect_reply);
txn->license = nr_strdup(license_key);
txn->name = nr_strdup(txnname);
actual = nr_rum_produce_footer(txn, tags, autorum);
if (expected) {
int j = 0;
char* expected_json = nro_to_json(expected);
/* Remove the prefix so as to only compare the config hash */
if (actual) {
for (j = 0; nr_rum_footer_prefix[j] && actual[j]; j++) {
if (nr_rum_footer_prefix[j] == actual[j]) {
continue;
} else {
break;
}
}
}
/*
* This comparison assumes that the hash fields are in the
* same order.
*/
tlib_pass_if_true(testname ? testname : "unknown",
0 == nr_strcmp(actual + j, expected_json),
"j=%d"
"\n"
"> actual_json=%s"
"\n"
">expected_json=%s",
j, NRSAFESTR(actual + j), NRSAFESTR(expected_json));
nr_free(expected_json);
} else {
tlib_pass_if_true(testname ? testname : "unknown", 0 == actual,
"actual=%p", actual);
}
nr_free(actual);
nr_free(txn->license);
nr_free(txn->name);
nro_delete(txn->app_connect_reply);
nr_attributes_destroy(&attributes);
}
}
}
nro_delete(array);
nr_free(json);
}
static void test_produce_footer_all_fields(void) {
char* ftr;
rum_mock_txn_t tnn;
nrtxn_t* txn = &tnn.txn;
int tags = 1;
int autorum = 1;
tnn.fake_queue_time = 3 * NR_TIME_DIVISOR_MS;
tnn.unfinished_duration = 5 * NR_TIME_DIVISOR_MS;
txn->status.ignore = 0;
txn->options.autorum_enabled = 1;
txn->status.rum_header = 1;
txn->status.rum_footer = 0;
tnn.fake_freeze_name_return = NR_SUCCESS;
txn->attributes = nr_attributes_create(0);
nr_attributes_user_add_string(
txn->attributes, NR_ATTRIBUTE_DESTINATION_BROWSER, "user", "my/user");
nr_attributes_user_add_string(txn->attributes,
NR_ATTRIBUTE_DESTINATION_BROWSER, "account",
"my/account");
nr_attributes_user_add_string(txn->attributes,
NR_ATTRIBUTE_DESTINATION_BROWSER, "product",
"my/product");
nr_attributes_agent_add_string(
txn->attributes, NR_ATTRIBUTE_DESTINATION_BROWSER, "zip", "zap");
txn->license = nr_strdup("0123456789abcdefghijklmnopqrstuvwxyz1234");
txn->app_connect_reply = nro_new_hash();
nro_set_hash_string(txn->app_connect_reply, "beacon", "my_beacon");
nro_set_hash_string(txn->app_connect_reply, "browser_key", "my_browser_key");
nro_set_hash_string(txn->app_connect_reply, "application_id",
"my_application_id");
nro_set_hash_string(txn->app_connect_reply, "error_beacon",
"my_error_beacon");
nro_set_hash_string(txn->app_connect_reply, "js_agent_file",
"my_js_agent_file");
txn->name = nr_strdup("WebTransaction/brink/of/glory");
txn->options.tt_threshold = 1 * NR_TIME_DIVISOR_MS;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true(
"footer with all fields",
0
== nr_strcmp(ftr,
"<script type=\"text/javascript\">"
"window.NREUM||(NREUM={});NREUM.info="
"{"
"\"beacon\":\"my_beacon\","
"\"licenseKey\":\"my_browser_key\","
"\"applicationID\":\"my_application_id\","
"\"transactionName\":"
"\"Z1RQZ0ZUWERZWhULDF4eUEFdW10YV19OBQ9fQ0s=\","
"\"queueTime\":3,"
"\"applicationTime\":5,"
"\"atts\":"
"\"SxNHEQ5OFEdKVgUXAEQTCBFZTGoYSEsOBhZTRRAfFlRVVFdMDxZB"
"ChNfSmgaV1RbVhQMFxIdEEZHUEQVAhsMGz8fREFWRhdLGxpYQ1gYEk"
"tbQxYPFE1ZSUMfHg==\","
"\"errorBeacon\":\"my_error_beacon\","
"\"agent\":\"my_js_agent_file\""
"}"
"</script>"),
"ftr=%s", ftr);
tlib_pass_if_true("footer with all fields", 0 != txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
nr_attributes_destroy(&txn->attributes);
nro_delete(txn->app_connect_reply);
nr_free(txn->license);
nr_free(txn->name);
}
static void test_produce_footer_no_fields(void) {
char* ftr;
rum_mock_txn_t tnn;
nrtxn_t* txn = &tnn.txn;
int tags = 1;
int autorum = 1;
/* Reverse chronological order */
tnn.fake_queue_time = 0;
tnn.unfinished_duration = 0;
txn->status.ignore = 0;
txn->options.autorum_enabled = 1;
txn->status.rum_header = 1;
txn->status.rum_footer = 0;
tnn.fake_freeze_name_return = NR_SUCCESS;
txn->attributes = 0;
txn->app_connect_reply = 0;
txn->name = 0;
txn->options.tt_threshold = 1 * NR_TIME_DIVISOR_MS;
ftr = nr_rum_produce_footer(txn, tags, autorum);
tlib_pass_if_true("footer with no fields",
0
== nr_strcmp(ftr,
"<script type=\"text/javascript\">"
"window.NREUM||(NREUM={});NREUM.info="
"{"
"\"beacon\":\"\","
"\"licenseKey\":\"\","
"\"applicationID\":\"\","
"\"transactionName\":\"\","
"\"queueTime\":0,"
"\"applicationTime\":0,"
"\"atts\":\"\","
"\"errorBeacon\":\"\","
"\"agent\":\"\""
"}"
"</script>"),
"ftr=%s", ftr);
tlib_pass_if_true("footer with no fields", 0 != txn->status.rum_footer,
"txn->status.rum_footer=%d", txn->status.rum_footer);
nr_free(ftr);
nro_delete(txn->app_connect_reply);
}
static void test_get_attributes(void) {
nr_attributes_t* user;
nr_attributes_t* agent;
nr_attributes_t* user_and_agent;
nr_attributes_t* empty;
char* json;
user = nr_attributes_create(0);
agent = nr_attributes_create(0);
user_and_agent = nr_attributes_create(0);
empty = nr_attributes_create(0);
nr_attributes_user_add_string(user, NR_ATTRIBUTE_DESTINATION_BROWSER,
"im_user", "zap");
nr_attributes_user_add_string(
user_and_agent, NR_ATTRIBUTE_DESTINATION_BROWSER, "im_user", "zap");
nr_attributes_agent_add_string(agent, NR_ATTRIBUTE_DESTINATION_BROWSER,
"im_agent", "zup");
nr_attributes_agent_add_string(
user_and_agent, NR_ATTRIBUTE_DESTINATION_BROWSER, "im_agent", "zup");
json = nr_rum_get_attributes(0);
tlib_pass_if_true("null attributes", 0 == json, "json=%p", json);
json = nr_rum_get_attributes(empty);
tlib_pass_if_true("empty attributes", 0 == json, "json=%p", json);
json = nr_rum_get_attributes(user);
tlib_pass_if_true("user",
0 == nr_strcmp("{\"u\":{\"im_user\":\"zap\"}}", json),
"json=%s", NRSAFESTR(json));
nr_free(json);
json = nr_rum_get_attributes(agent);
tlib_pass_if_true("agent",
0 == nr_strcmp("{\"a\":{\"im_agent\":\"zup\"}}", json),
"json=%s", NRSAFESTR(json));
nr_free(json);
json = nr_rum_get_attributes(user_and_agent);
tlib_pass_if_true(
"user_and_agent",
0
== nr_strcmp(
"{\"u\":{\"im_user\":\"zap\"},\"a\":{\"im_agent\":\"zup\"}}",
json),
"json=%s", NRSAFESTR(json));
nr_free(json);
nr_attributes_destroy(&user);
nr_attributes_destroy(&agent);
nr_attributes_destroy(&user_and_agent);
nr_attributes_destroy(&empty);
}
static char* remove_substring(const char* str, const char* substring) {
const char* begin;
const char* end;
char* new_str;
begin = nr_strstr(str, substring);
if (0 == begin) {
return 0;
}
end = begin + nr_strlen(substring);
new_str = (char*)nr_malloc(nr_strlen(str) + 1);
new_str[0] = '\0';
nr_strncat(new_str, str, begin - str);
nr_strcat(new_str, end);
return new_str;
}
static char* insert_substring_at_index(const char* str,
const char* substring,
int idx) {
int len = nr_strlen(str) + nr_strlen(substring);
char* s = (char*)nr_malloc(len + 1);
s[0] = '\0';
snprintf(s, len + 1, "%.*s%s%s", idx, str, substring, str + idx);
return s;
}
#define test_scan_html_predicate(N, M, H) \
test_scan_html_predicate_f(N, M, H, nr_rum_scan_html_for_head, __FILE__, \
__LINE__)
static void test_scan_html_predicate_f(
const char* name,
const char* marker,
const char* html_with_marker,
const char* (*scan_html)(const char* input, const uint input_len),
const char* file,
int line) {
int actual_offset = 0;
int expected_offset = 0;
char* html = 0;
char* html_diff = 0;
const char* actual_location = 0;
const char* marker_location = 0;
marker_location = nr_strstr(html_with_marker, marker);
/*
* If the marker is not present, no insertion should be performed.
*/
if (0 == marker_location) {
actual_location = scan_html(html_with_marker, nr_strlen(html_with_marker));
test_pass_if_true(name, 0 == actual_location,
"(int)(actual_location - html_with_marker)=%d",
(int)(actual_location - html_with_marker));
return;
}
/*
* If a marker is present, RUM insertion should be performed. Remove
* the marker and re-scan the html. Validate that insertion was performed
* and the insertion point is at the same position as the marker.
*/
html = remove_substring(html_with_marker, marker);
actual_location = scan_html(html, nr_strlen(html));
test_pass_if_true(name, 0 != actual_location, "insertion location not found");
if (0 == actual_location) {
nr_free(html);
return;
}
expected_offset = marker_location - html_with_marker;
actual_offset = actual_location - html;
if (actual_offset < expected_offset) {
/* Insertion occurred to before the marker. */
char* tmp
= insert_substring_at_index(html, "EXPECTED_HERE", expected_offset);
html_diff = insert_substring_at_index(tmp, "FOUND_HERE", actual_offset);
nr_free(tmp);
} else if (actual_offset > expected_offset) {
/* Insertion occurred after the marker. */
char* tmp = insert_substring_at_index(html, "FOUND_HERE", actual_offset);
html_diff
= insert_substring_at_index(tmp, "EXPECTED_HERE", expected_offset);
nr_free(tmp);
}
test_pass_if_true(
name, actual_offset == expected_offset,
"RUM insertion occured at the wrong offset: expected=%d, actual=%d\n%s",
expected_offset, actual_offset, html_diff);
nr_free(html_diff);
nr_free(html);
}
/*
* These tests show the behavior of the header scanning logic
* on faulty and miscellaneous html fragments. Note that the spec
* does not properly handle situations involving comments and html embedded
* within strings.
*/
static void test_scan_html(void) {
test_scan_html_predicate("head null 0", "%HERE%", 0);
test_scan_html_predicate("head null 1", "%HERE%", "");
test_scan_html_predicate("head missing 0", "%HERE%", "foobar");
test_scan_html_predicate("head missing 1", "%HERE%", "<html> foobar </html>");
test_scan_html_predicate(
"head missing 2", "%HERE%",
"</head> foobat <head>%HERE%foobar"); /* that's right, at end of <head>
tag */
test_scan_html_predicate(
"head missing 3", "%HERE%",
"<html><head>%HERE% foobat foobar</html>"); /* hmm: does not have an
ending </head > */
test_scan_html_predicate(
"head basic 0", "%HERE%",
"<head>%HERE% foobar </head></html>"); /* trailing space important */
test_scan_html_predicate(
"head basic 1", "%HERE%",
"<html><head>%HERE% foobar </head></html>"); /* trailing space important
*/
test_scan_html_predicate("head basic 2", "%HERE%",
"<html><head>%HERE% foobar</head></html>");
test_scan_html_predicate("head basic 2", "%HERE%",
"<html><head>%HERE% foobar foobat </head></html>");
test_scan_html_predicate("head basic 3", "%HERE%",
"<head>%HERE% foobar</head></html>");
test_scan_html_predicate(
"head comment 0", "%HERE%",
"<!-- comment start --><html><head>%HERE% foobar</head></html>");
test_scan_html_predicate(
"head comment 1", "%HERE%",
"<html><!-- comment XX --> <head>%HERE% foobar</head></html>");
test_scan_html_predicate(
"head comment 2", "%HERE%",
"<html><head>%HERE% <!-- comment XX -->foobar</head></html>");
test_scan_html_predicate(
"head comment 3", "%HERE%",
"<html><head>%HERE% foobat<!-- comment XX -->foobar</head></html>");
test_scan_html_predicate(
"head comment 4", "%HERE%",
"<html><head>%HERE% foobar</head><!-- comment XX --></html>");
test_scan_html_predicate(
"head comment 5", "%HERE%",
"<html><head>%HERE% foobar</head></html><!-- comment XX -->");
test_scan_html_predicate(
"head comment 6", "%HERE%",
"<html><!-- <head>%HERE% foobat</head> --> <head> foobar</head> </html>");
test_scan_html_predicate("head comment 7", "%HERE%",
"<!--<html><head>%HERE% foobat</head> --> "
"<html><head> foobar</head> </html>");
test_scan_html_predicate("head comment 8", "%HERE%",
"<!--<html><head>%HERE% foobat</head></html> "
"-->foobar"); /* no head to be found, so 0 return */
test_scan_html_predicate("head mangled comment 0", "%HERE%",
"<!-><html><head>%HERE% foobat</head></html>foobar");
test_scan_html_predicate(
"head mangled comment 1", "%HERE%",
"<!- --><html><head>%HERE% foobat</head></html>foobar");
test_scan_html_predicate("head mangled comment 2", "%HERE%",
"--><html><head>%HERE% foobat</head></html>foobar");
test_scan_html_predicate(
"head mangled comment 3", "%HERE%",
"<!-- -><html><head>%HERE% foobat</head></html>foobar"); /* unclosed
comment */
test_scan_html_predicate(
"head mangled comment 4", "%HERE%",
"<!X -><html><head>%HERE% foobat</head></html>foobar");
test_scan_html_predicate(
"head mangled comment 5", "%HERE%",
"<!-- --><html><head>%HERE% foobat</head></html>foobar");
test_scan_html_predicate("head mangled comment 6", "%HERE%",
"<html><head>%HERE% foobat</head></html>foobar");
test_scan_html_predicate("head mangled comment 6", "%HERE%",
"< !-- -><html><head>%HERE% "
"foobat</head></html>foobar"); /* BUG: should be
"foobat"; that
really is not a
comment */
test_scan_html_predicate(
"head mangled comment 7", "%HERE%",
"<!-- --X<html><head>%HERE% foobat</head></html>foobar"); /* unclosed
comment */
test_scan_html_predicate(
"head mangled comment 8", "%HERE%",
"<!-- -><html><head>%HERE% foobat</head></html>foobar"); /* unclosed
comment */
test_scan_html_predicate("inhead mangled comment 1", "%HERE%",
"<html><head>%HERE% blivet<! comment XX "
"-->foobar</head></html>"); /* stops before unopened
comment */
test_scan_html_predicate("inhead mangled comment 2", "%HERE%",
"<html><head>%HERE% blivet<!- comment XX "
"-->foobar</head></html>"); /* stops before unopened
comment */
test_scan_html_predicate("inhead mangled comment 3", "%HERE%",
"<html><head>%HERE% blivet<!-X comment XX "
"-->foobar</head></html>"); /* stops before unopened
comment */
test_scan_html_predicate("inhead mangled comment 4", "%HERE%",
"<html><head>%HERE%blavet<!-- comment XX "
"->foobar</head></html>blivet"); /* unclosed comment,
but still in head
*/
test_scan_html_predicate("inhead mangled comment 5", "%HERE%",
"<html><head>%HERE%blavet<!-- comment XX "
"-X>foobar</head></html>blivet"); /* unclosed
comment, but
still in head */
test_scan_html_predicate("inhead mangled comment 6", "%HERE%",
"<html><head>%HERE%blavet <!-- comment XX "
"X->foobar</head></html>"); /* unclosed comment */
test_scan_html_predicate("inhead mangled comment 7", "%HERE%",
"<html><head>%HERE%blavet <!-- comment XX -- "
"foobar</head></html>"); /* unclosed comment */
test_scan_html_predicate(
"head meta 1", "%HERE%",
"<html><head>%HERE%<meta name=\"foobat\"> foobar</head></html>");
test_scan_html_predicate("head meta 1", "%HERE%",
"<html><head>%HERE%<meta name=\"foobat\"><meta "
"name=\"blivet\">foobar</head></html>");
test_scan_html_predicate("unterminated head", "%HERE%",
"<html><head alpha=\"beta\"");
test_scan_html_predicate("head with attribute", "%HERE%",
"<html><head alpha=\"beta\">%HERE%");
test_scan_html_predicate(
"close head > within string", "%HERE%",
"<html><head alpha=\">%HERE%\">"); /* Expected but not optimal */
test_scan_html_predicate("headline tag", "%HERE%", "<html><headline>");
test_scan_html_predicate("bodysomething tag", "%HERE%",
"<html><bodysomething>");
}
#define cross_agent_header_testcase(N) \
cross_agent_rum_testcase_f((N), "EXPECTED_RUM_LOADER_LOCATION", \
nr_rum_scan_html_for_head, __FILE__, __LINE__)
#define cross_agent_footer_testcase(N) \
cross_agent_rum_testcase_f((N), "EXPECTED_RUM_FOOTER_LOCATION", \
nr_rum_scan_html_for_foot, __FILE__, __LINE__)
static void cross_agent_rum_testcase_f(
const char* filename,
const char* marker,
const char* (*scan_html)(const char* input, const uint input_len),
const char* file,
int line) {
char* text;
text = nr_read_file_contents(filename, 10 * 1000 * 1000);
test_scan_html_predicate_f(filename, marker, text, scan_html, file, line);
nr_free(text);
}
static void test_scan_html_for_head_from_cross_agent_tests(void) {
int rv = 0;
char pattern[512];
size_t i = 0;
glob_t glob_buf;
nr_memset(&glob_buf, 0, sizeof(glob_buf));
pattern[0] = '\0';
snprintf(pattern, sizeof(pattern), "%s/rum_loader_insertion_location/*.html",
CROSS_AGENT_TESTS_DIR);
rv = glob(pattern, 0, NULL, &glob_buf);
tlib_pass_if_true(
"cross agent header insertion", (0 == rv) || (GLOB_NOMATCH == rv),
"failed to glob test files: errno=%s, glob=%s", nr_errno(errno), pattern);
for (i = 0; i < glob_buf.gl_pathc; ++i) {
cross_agent_header_testcase(glob_buf.gl_pathv[i]);
}
globfree(&glob_buf);
}
static char* nr_malloc_wrapper(int size) {
return (char*)nr_malloc(size);
}
static char* produce_header_HEAD(nrtxn_t* txn NRUNUSED,
int tags NRUNUSED,
int autorum NRUNUSED) {
return nr_strdup("HEAD");
}
static char* produce_header_null(nrtxn_t* txn NRUNUSED,
int tags NRUNUSED,
int autorum NRUNUSED) {
return 0;
}
static char* produce_footer_TAIL(nrtxn_t* txn NRUNUSED,
int tags NRUNUSED,
int autorum NRUNUSED) {
return nr_strdup("TAIL");
}
static char* produce_footer_null(nrtxn_t* txn NRUNUSED,
int tags NRUNUSED,
int autorum NRUNUSED) {
return 0;
}
#define test_rum_injection_normal(...) \
test_rum_injection_normal_f(__VA_ARGS__, __FILE__, __LINE__)
static void test_rum_injection_normal_f(int have_header,
int have_footer,
const char* stimulus,
const char* expect_string,
const char* file,
int line) {
nr_rum_control_block_t control_block;
nrtxn_t* txn = 0;
char* output = 0;
size_t output_len = 0;
char* handled_output = 0;
size_t handled_output_len = 0;
int debug_autorum = 1;
int has_response_content_length = 0;
nr_memset(&control_block, 0, sizeof(control_block));
control_block.malloc_worker = nr_malloc_wrapper;
control_block.produce_header = produce_header_HEAD;
control_block.produce_footer = produce_footer_TAIL;
txn = (nrtxn_t*)nr_zalloc(sizeof(nrtxn_t));
txn->options.autorum_enabled = 1;
txn->status.ignore = 0;
txn->status.rum_header = have_header;
txn->status.rum_footer = have_footer;
output = nr_strdup(stimulus);
output_len = nr_strlen(output);
nr_rum_output_handler_worker(&control_block, txn, output, output_len,
&handled_output, &handled_output_len,
has_response_content_length, "text/html",
debug_autorum);
if (0 == expect_string) {
test_pass_if_true("NULL expected", 0 == handled_output, "handled_output=%p",
handled_output);
test_pass_if_true("NULL expected", 0 == handled_output_len,
"handled_output_len=%zu", handled_output_len);
} else {
test_pass_if_true("correct output length",
(int)handled_output_len == nr_strlen(handled_output),
"handled_output_len=%zu nr_strlen (handled_output)=%d",
handled_output_len, nr_strlen(handled_output));
test_pass_if_true("correct output",
0 == nr_strcmp(expect_string, handled_output),
"expect={%s}\n result={%s}", NRSAFESTR(expect_string),
NRSAFESTR(handled_output));
}
nr_free(output);
nr_free(handled_output);
nr_free(txn);
}
static void test_rum_injection_oddball(void) {
nr_rum_control_block_t control_block;
nrtxn_t* txn = 0;
char* output = 0;
size_t output_len = 0;
char* handled_output = 0;
size_t handled_output_len = 0;
int debug_autorum = 1;
nr_memset(&control_block, 0, sizeof(control_block));
control_block.malloc_worker = nr_malloc_wrapper;
control_block.produce_header = produce_header_HEAD;
control_block.produce_footer = produce_footer_TAIL;
nr_rum_output_handler_worker(0, 0, 0, 0, &handled_output, &handled_output_len,
0, "text/html", debug_autorum);
tlib_pass_if_true("null handled_output",
(0 == handled_output) && (0 == handled_output_len),
"output and output_len non zero");
nr_rum_output_handler_worker(&control_block, 0, 0, 0, &handled_output,
&handled_output_len, 0, "text/html",
debug_autorum);
tlib_pass_if_true("null handled_output",
(0 == handled_output) && (0 == handled_output_len),
"output and output_len non zero");
txn = (nrtxn_t*)nr_zalloc(sizeof(nrtxn_t));
txn->options.autorum_enabled = 0;
txn->status.ignore = 0;
nr_rum_output_handler_worker(&control_block, txn, 0, 0, &handled_output,
&handled_output_len, 0, "text/html",
debug_autorum);
tlib_pass_if_true("handled_output",
(0 == handled_output) && (0 == handled_output_len),