File: split_process_line_test.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (920 lines) | stat: -rw-r--r-- 45,226 bytes parent folder | download | duplicates (4)
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
#include <test.h>

#define TEST_UNIT_TEST

#include <processes_select.c>

static void free_and_null_strings(char **strings)
{
    for (int i = 0; i < CF_PROCCOLS; ++i)
    {
        free(strings[i]);
        strings[i] = NULL;
    }
}

/* Actual ps output witnessed. */
static void test_split_line_challenges(void)
{
    /* Collect all test data in one array to make alignments visible: */
    static const char *lines[] = {
        "USER       PID    SZ    VSZ   RSS NLWP STIME     ELAPSED     TIME COMMAND",
        "operatic 14338 1042534 4170136 2122012 9 Sep15 4-06:11:34 2-09:27:49 /usr/lib/opera/opera"
    };
    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, nlwp = 5;

    memset(name, 0, sizeof(name));
    memset(field, 0, sizeof(field));

    /* Prepare data needed by tests and assert things tests can then assume: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[nlwp], "NLWP");

    assert_true(SplitProcLine(lines[1], 1, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "operatic");
    assert_string_equal(field[nlwp], "9");

    /* Finally, tidy away fields and headers: */
    free_and_null_strings(field);
    free_and_null_strings(name);
}

static void test_split_line_elapsed(void)
{
    /* Collect all test data in one array to make alignments visible: */
    static const char *lines[] = {
        "USER       PID STAT    VSZ  NI   RSS NLWP STIME     ELAPSED     TIME COMMAND",
        "space    14604 S         0   0     0    1 Sep 02 4-03:40:00 00:00:01 true",
        "block    14604 S         0   0     0    1 Sep02  4-03:40:00 00:00:01 true"
    };
    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, stime = 7;
    time_t pstime = 1410000000;        /* 2014-09-06 12:40 */
    const char began[] = "1409641200"; /* 2014-09-02  9:0 */

    /* Prepare data needed by tests and assert things tests can then assume: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[stime], "STIME");

    assert_true(SplitProcLine(lines[2], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "block");
    /* Copes when STIME is a date with a space in it. */
    assert_string_equal(field[stime], began);

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[1], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "space");
    /* Copes when STIME is a date with a space in it. */
    assert_string_equal(field[stime], began);

    /* Finally, tidy away headers: */
    free_and_null_strings(name);
    free_and_null_strings(field);
}

static void test_split_line_noelapsed(void)
{
    /* Collect all test data in one array to make alignments visible: */
    static const char *lines[] = {
        "USER       PID STAT    VSZ  NI   RSS NLWP STIME     TIME COMMAND",
        "space    14604 S         0   0     0   1 Jul 02 00:00:01 true",
        "block    14604 S         0   0     0   1  Jul02 00:00:01 true"
    };
    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, stime = 7;
    time_t pstime = 1410000000;

    /* Prepare data needed by tests and assert things tests can then assume: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[stime], "STIME");

    assert_true(SplitProcLine(lines[2], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "block");
    /* Copes when STIME is a date with a space in it. */
    assert_string_equal(field[stime], "Jul02");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[1], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "space");
    /* Copes when STIME is a date with a space in it. */
    assert_string_equal(field[stime], "Jul 02");

    /* Finally, tidy away headers: */
    free_and_null_strings(name);
    free_and_null_strings(field);
}

static void test_split_line_longcmd(void)
{
    static const char *lines[] = {
        "USER       PID STAT    VSZ  NI   RSS NLWP STIME     ELAPSED     TIME COMMAND",
        "longcmd    923 S     32536   0   784    1 10:30 71-00:07:43 00:01:49 "
          "java can end up with some insanely long command-lines, so we need to "
          "be sure that we don't artificially limit the length of the command "
          "field that we see when looking at the process table to match for the "
          "details that the user might be interested in - so here we have an "
          "absurdly long 'command' field just for the sake of testing that it "
          "does not get truncated - see RedMine ticket 3974 for working round the "
          "problems when ps itself does such truncation, but this test is not about "
          "that so much as our own code not doing such truncation - and now for "
          "some random drivel repeated a lot to bulk this command line length out "
          "to more than 4k:"
          /* 638 bytes thus far, +72 per repeat, * 50 for 4238 > 4096: */
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens"
          " the quick brown fox jumped over the lazy dogs after eating the chickens",
    };
    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, command = 10;
    time_t pstime = 1410000000;

    /* Prepare data needed by tests and assert things assumed by test: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[command], "COMMAND");

    assert_true(SplitProcLine(lines[1], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "longcmd");
    /* Does not truncate the command. */
    assert_string_equal(field[command], lines[1] + start[command]);

    /* Finally, tidy away headers: */
    free_and_null_strings(name);
    free_and_null_strings(field);
}

static void test_split_line(void)
{
  /* Collect all test data in one array to make alignments visible: */
    static const char *lines[] = {
        /* Indent any continuation lines so that it's clear that's what they are. */
        /* Use the username field as a test name to confirm tests are in sync. */

        "USER       PID STAT    VSZ  NI   RSS NLWP STIME     ELAPSED     TIME COMMAND",
        "timeboth   923 S     32536   0   784    1 10:30 1271-00:07:43 00:01:49 true"
          " to itself",
        /* timeright: adapted from a Jenkins run that failed. */
        "timeright  941 S     39752   0  4552    1 May20 92-21:17:55 1-02:07:14 true",
        "timesleft  923 S     32536   0   784  1 10:30 1271-00:07:43 00:01:49 true",
        "timeleft   923 S     32536   0   784   1 10:30 271-00:07:43 00:01:49 true",
        "numboth    923 S     32536   0 12784321 1 10:30 71-00:07:43 00:01:49 true",
        "numleft    923 S 123432536   0   784    1 10:30 71-00:07:43 00:01:49 true",
        "wordytoright 4 S         0   0     0    1 10:30       54:29 00:00:01 true",
        "wordright    4 S         0   0     0    1 10:30       54:29 00:00:01 true",

        /* Long-standing: */
        "inspace    923 S     32536   0   784    1 10:30 71-00:07:43 00:01:49 echo"
          " preserve\t embedded   spaces in   a text   field",
        "spacey     923 S     32536   0   784    1 10:30 71-00:07:43 00:01:49    echo "
          "\t \t                     \r\n\f\n\v\n",
        "basic      923 S     32536   0   784    1 10:30 71-00:07:43 00:01:49 true",
        "",
        NULL
    };
    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, vsz = 3, rss = 5, stime = 7, command = 10;
    time_t pstime = 1410000000;

    /* Prepare data needed by tests and assert things tests can then assume: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[vsz], "VSZ");
    assert_string_equal(name[rss], "RSS");
    assert_string_equal(name[stime], "STIME");
    assert_string_equal(name[command], "COMMAND");
    assert_int_equal(start[command], 69);

    size_t line = sizeof(lines) / sizeof(const char *);
    /* Higher indexed tests first; test lines[line] then decrement line. */
    assert_false(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field)); /* NULL */

    free_and_null_strings(field);
    assert_false(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field)); /* empty */

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field)); /* basic */
    {
        /* Each field is as expected: */
        const char *each[] = {
            "basic", "923", "S", "32536", "0", "784", "1",
            "10:30", "71-00:07:43", "00:01:49", "true"
        };
        for (int i = 0; name[i] != NULL; i++)
        {
            assert_in_range(i, 0, sizeof(each) / sizeof(char *) - 1);
            bool valid = field[i] != NULL && each[i] != NULL;
            assert_true(valid);
            assert_string_equal(field[i], each[i]);
        }
        /* That incidentally covers numeric (VSZ) and time (ELAPSED,
         * TIME) fields overflowing to the left (but not so far as to
         * go under the previous field's header). */
    }
    /* See field[user] checks for names of remaining tests. */

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "spacey");
    /* Discards leading and dangling space in command. */
    assert_string_equal(field[command], "echo");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "inspace");
    /* Preserves spaces within a text field. */
    assert_string_equal(field[command], lines[line] + start[command]);

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    /* Handle a text field overflowing to the right. */
    assert_string_equal(field[user], "wordright");
    /* Shouldn't pollute PID: */
    assert_string_equal(field[user + 1], "4");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    /* Handle a text field overflowing under next header. */
    assert_string_equal(field[user], "wordytoright");
    /* Shouldn't pollute PID: */
    assert_string_equal(field[user + 1], "4");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "numleft");
    /* Handle numeric field overflowing under previous header. */
    assert_string_equal(field[vsz], "123432536");
    /* Shouldn't pollute STAT: */
    assert_string_equal(field[vsz - 1], "S");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "numboth");
    /* Handle numeric field overflowing under previous header. */
    assert_string_equal(field[rss], "12784321");
    /* Shouldn't pollute STAT or NI: */
    assert_string_equal(field[rss - 1], "0");
    assert_string_equal(field[rss + 1], "1");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "timeleft");
    /* Handle time fields overflowing almost under previous header. */
    assert_string_equal(field[stime + 1], "271-00:07:43");
    assert_string_equal(field[stime], "10:30");
    /* Shouldn't pollute NLWP: */
    assert_string_equal(field[stime - 1], "1");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "timesleft");
    /* Handle time fields overflowing under previous header. */
    assert_string_equal(field[stime + 1], "1271-00:07:43");
    assert_string_equal(field[stime], "10:30");
    /* Shouldn't pollute NLWP: */
    assert_string_equal(field[stime - 1], "1");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "timeright");
    /* Handle time field overflowing under next header. */
    assert_string_equal(field[command - 1], "1-02:07:14");
    /* Shouldn't pollute ELAPSED or COMMAND: */
    assert_string_equal(field[stime + 1], "92-21:17:55");
    assert_string_equal(field[command], "true");

    free_and_null_strings(field);
    assert_true(SplitProcLine(lines[--line], pstime, name, start, end, PCA_AllColumnsPresent, field));
    assert_string_equal(field[user], "timeboth");
    assert_int_equal(command, stime + 3); /* with elapsed and time between */
    /* Handle a time field overflowing almost under previous header
     * while also overflowing right and thus causing the next to
     * overflow under the field beyond it. */
    assert_string_equal(field[command - 1], "00:01:49");
    assert_string_equal(field[stime + 1], "1271-00:07:43");
    /* Should shunt COMMAND two bytes to the right: */
    assert_string_equal(field[command], lines[line] + 2 + start[command]);
    /* Shouldn't pollute COMMAND, NLWP or STIME: */
    assert_string_equal(field[stime], "10:30");
    assert_string_equal(field[stime - 1], "1");

    assert_int_equal(line, 1);
    /* Finally, tidy away headers: */
    free_and_null_strings(name);
    free_and_null_strings(field);
}

static void test_split_line_serious_overspill(void)
{
    /*
       Test that columns spilling over into other columns do not confuse the
       parser.
       Collect all test data in one array to make alignments visible:
    */
    static const char *lines[] = {
        "    USER   PID %CPU %MEM   SZ  RSS TT      S    STIME        TIME COMMAND",
        " johndoe  8263  0.0  0.2 19890 116241 ?       S   Jan_16    08:41:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4",
        "noaccess  8264  0.0  0.2 19890 116242 ?       S   Jan_16    08:41:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4",
        "noaccess  8265  0.0  0.2 19890 116243 ?       S   Jan_16    08:41:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4",
        NULL
    };

    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, pid = 1, sz = 4, rss = 5, command = 10;
    time_t pstime = 1410000000;

    /* Prepare data needed by tests and assert things tests can then assume: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[pid], "PID");
    assert_string_equal(name[sz], "SZ");
    assert_string_equal(name[rss], "RSS");
    assert_string_equal(name[command], "COMMAND");
    assert_int_equal(start[command], 66);

    // Test content
    {
        assert_true(SplitProcLine(lines[1], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "johndoe");
        assert_string_equal(field[pid], "8263");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116241");
        assert_string_equal(field[command], lines[1] + 69);
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[2], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "noaccess");
        assert_string_equal(field[pid], "8264");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116242");
        assert_string_equal(field[command], lines[2] + 69);
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[3], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "noaccess");
        assert_string_equal(field[pid], "8265");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116243");
        assert_string_equal(field[command], lines[3] + 69);
        free_and_null_strings(field);
    }
    free_and_null_strings(name);
}

typedef struct
{
    FILE *fp;
    const char **lines;
} LWData;

static void *ListWriter(void *arg)
{
    LWData *data = (LWData *)arg;
    for (int i = 0; data->lines[i]; i++)
    {
        fprintf(data->fp, "%s\n", data->lines[i]);
    }
    fclose(data->fp);

    return NULL;
}

static void test_platform_extra_table(void)
{
    static const char *lines[] = {
        "    USER   PID %CPU %MEM   SZ  RSS TT      S    STIME        TIME COMMAND",
        " johndoe  8263  0.0  0.2 19890 116241 ?       S   Jan_16    08:41:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4",
        "noaccess  8264  0.0  0.2 19890 116242 ?       S   Jan_16    08:41:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4",
        "noaccess  8265  0.0  0.2 19890 116243 ?       S   Jan_16    08:41:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4",
        " jenkins 22306    -    -    0    0 ?       Z        -       00:00 <defunct>",
        " jenkins 22307    -    -    0    0 ?       Z        -       00:00 <defunct>",
        " jenkins 22308    -    -    0    0 ?       Z        -       00:00 <defunct>",
        NULL
    };
    static const char *ucb_lines[] = {
        "   PID TT       S  TIME COMMAND",
        "  8263 ?        S 521:40 /usr/java/bin/java blahblah",
        // Takes from Solaris 10. Yep, the line really is that long.
        "  8264 ?        S 521:40 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4 -classpath /usr/share/webconsole/private/container/bin/bootstrap.jar:/usr/share/webconsole/private/container/bin/commons-logging.jar:/usr/share/webconsole/private/container/bin/log4j.jar:/usr/java/lib/tools.jar:/usr/java/jre/lib/jsse.jar -Djava.security.manager -Djava.security.policy==/var/webconsole/domains/console/conf/console.policy -Djavax.net.ssl.trustStore=/var/webconsole/domains/console/conf/keystore.jks -Djava.security.auth.login.config=/var/webconsole/domains/console/conf/consolelogin.conf -Dcatalina.home=/usr/share/webconsole/private/container -Dcatalina.base=/var/webconsole/domains/console -Dcom.sun.web.console.home=/usr/share/webconsole -Dcom.sun.web.console.conf=/etc/webconsole/console -Dcom.sun.web.console.base=/var/webconsole/domains/console -Dcom.sun.web.console.logdir=/var/log/webconsole/console -Dcom.sun.web.console.native=/usr/lib/webconsole -Dcom.sun.web.console.appbase=/var/webconsole/domains/console/webapps -Dcom.sun.web.console.secureport=6789 -Dcom.sun.web.console.unsecureport=6788 -Dcom.sun.web.console.unsecurehost=127.0.0.1 -Dwebconsole.default.file=/etc/webconsole/console/default.properties -Dwebconsole.config.file=/etc/webconsole/console/service.properties -Dcom.sun.web.console.startfile=/var/webconsole/tmp/console_start.tmp -Djava.awt.headless=true -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog org.apache.catalina.startup.Bootstrap start",
        "  8265 ?        S 521:40 /usr/java/bin/java blahblah",
        // Taken from Solaris 10, notice the missing fields.
        " 22306          Z  0:00  <defunct>",
        " 22307          Z  0:00 ",
        " 22308          Z  0:00",
        NULL
    };
    char *name[CF_PROCCOLS] = { 0 }; /* Headers */
    char *field[CF_PROCCOLS] = { 0 }; /* Content */
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };
    int user = 0, pid = 1, sz = 4, rss = 5, command = 10;
    time_t pstime = 1410000000;

    // Prepare to fill "/usr/ucb/ps" table with data.
    ClearPlatformExtraTable();
    int pipefd[2];
    assert_int_equal(pipe(pipefd), 0);
    LWData data;
    data.fp = fdopen(pipefd[1], "w");
    data.lines = ucb_lines;

    // Feed the pipe from a separate thread.
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_t tid;
    pthread_create(&tid, &attr, &ListWriter, &data);
    pthread_attr_destroy(&attr);

    FILE *cmd_output = fdopen(pipefd[0], "r");

    UCB_PS_MAP = StringMapNew();
    ReadFromUcbPsPipe(cmd_output);

    /* Prepare data needed by tests and assert things tests can then assume: */
    GetProcessColumnNames(lines[0], name, start, end);
    assert_string_equal(name[user], "USER");
    assert_string_equal(name[pid], "PID");
    assert_string_equal(name[sz], "SZ");
    assert_string_equal(name[rss], "RSS");
    assert_string_equal(name[command], "COMMAND");
    assert_int_equal(start[command], 66);

    // Test content
    {
        assert_true(SplitProcLine(lines[1], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "johndoe");
        assert_string_equal(field[pid], "8263");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116241");
        assert_string_equal(field[command], lines[1] + 69);

        ApplyPlatformExtraTable(name, field);
        // Now check new and corrected values.
        assert_string_equal(field[user], "johndoe");
        assert_string_equal(field[pid], "8263");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116241");
        assert_string_equal(field[command], ucb_lines[1] + 25);
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[2], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "noaccess");
        assert_string_equal(field[pid], "8264");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116242");
        assert_string_equal(field[command], lines[2] + 69);

        ApplyPlatformExtraTable(name, field);
        // Now check new and corrected values.
        assert_string_equal(field[user], "noaccess");
        assert_string_equal(field[pid], "8264");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116242");
        assert_string_equal(field[command], ucb_lines[2] + 25);
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[3], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "noaccess");
        assert_string_equal(field[pid], "8265");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116243");
        assert_string_equal(field[command], lines[3] + 69);

        ApplyPlatformExtraTable(name, field);
        // Now check new and corrected values.
        assert_string_equal(field[user], "noaccess");
        assert_string_equal(field[pid], "8265");
        assert_string_equal(field[sz], "19890");
        assert_string_equal(field[rss], "116243");
        assert_string_equal(field[command], ucb_lines[3] + 25);
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[4], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "jenkins");
        assert_string_equal(field[pid], "22306");
        assert_string_equal(field[sz], "0");
        assert_string_equal(field[rss], "0");
        assert_string_equal(field[command], lines[4] + 66);

        ApplyPlatformExtraTable(name, field);
        // Now check new and corrected values.
        assert_string_equal(field[user], "jenkins");
        assert_string_equal(field[pid], "22306");
        assert_string_equal(field[sz], "0");
        assert_string_equal(field[rss], "0");
        assert_string_equal(field[command], "<defunct>");
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[5], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "jenkins");
        assert_string_equal(field[pid], "22307");
        assert_string_equal(field[sz], "0");
        assert_string_equal(field[rss], "0");
        assert_string_equal(field[command], lines[5] + 66);

        ApplyPlatformExtraTable(name, field);
        // Now check new and corrected values.
        assert_string_equal(field[user], "jenkins");
        assert_string_equal(field[pid], "22307");
        assert_string_equal(field[sz], "0");
        assert_string_equal(field[rss], "0");
        assert_string_equal(field[command], "");
        free_and_null_strings(field);
    }

    {
        assert_true(SplitProcLine(lines[6], pstime, name, start, end, PCA_AllColumnsPresent, field));
        assert_string_equal(field[user], "jenkins");
        assert_string_equal(field[pid], "22308");
        assert_string_equal(field[sz], "0");
        assert_string_equal(field[rss], "0");
        assert_string_equal(field[command], lines[6] + 66);

        ApplyPlatformExtraTable(name, field);
        // Now check new and corrected values.
        assert_string_equal(field[user], "jenkins");
        assert_string_equal(field[pid], "22308");
        assert_string_equal(field[sz], "0");
        assert_string_equal(field[rss], "0");
        assert_string_equal(field[command], "");
        free_and_null_strings(field);
    }
    free_and_null_strings(name);
    fclose(cmd_output);
}

static void test_platform_specific_ps_examples(void)
{
    enum {
        // Make sure the order matches the ps lines below.
        TEST_LINUX = 0,
        TEST_AIX,
        TEST_HPUX,
        TEST_SOLARIS9,
        TEST_SOLARIS10,
        TEST_SOLARIS11,
        TEST_FREEBSD11,
        TEST_ILLUMOS,
        NUM_OF_PLATFORMS
    };
    /* Simple, visual way of specifying the expected parse results, each row
       alternates between the line we want to parse (with header first) and the
       range we wish the parsing code to extract for us, denoted by '<' and '>'.
       'X' is used where they overlap, and '-' if the field is empty. '{' and
       '}' a special case and means it should get the expected output from the
       corresponding row in the exception table below. */
    const char *pslines[NUM_OF_PLATFORMS][20] = {
        {
            // Linux
            "USER       PID  PPID  PGID %CPU %MEM    VSZ  NI       RSS NLWP STIME     ELAPSED     TIME COMMAND",
            "<  >       < >  <  >  <  > <  > <  >    < >  <>       < > <  > <   >     <     >     <  > <     >",
            "a10040    4831     1  4116  5.4 17.3 2763416  0   1400812   54 Apr04  4-02:54:24 05:25:51 /usr/lib/firefox/firefox",
            "<    >    <  >     X  <  >  < > <  > <     >  X   <     >   <> {   }  <        > <      > <                      >",
            "a10040    5862  5851  4116  0.0  0.0      0   0         0    1 Apr04  4-02:38:28 00:00:00 [/usr/bin/termin] <defunct>",
            "<    >    <  >  <  >  <  >  < >  < >      X   X         X    X {   }  <        > <      > <                         >",
            NULL
        }, {
            // AIX
            "    USER     PID    PPID    PGID  %CPU  %MEM   VSZ NI ST    STIME        TIME COMMAND",
            "    <  >     < >    <  >    <  >  <  >  <  >   < > <> <>    <   >        <  > <     >",
            " jenkins 1036484  643150 1036484   0.0   0.0   584 20 A  09:29:20    00:00:00 bash",
            " <     > <     >  <    > <     >   < >   < >   < > <> X  <      >    <      > <  >",
            "          254232  729146  729146                   20 Z              00:00:00 <defunct>",
            "       -  <    >  <    >  <    >     -     -     - <> X         -    <      > <       >",
            " jenkins  205218       1  205218   0.0   7.0 125384 20 A    Mar 16    00:36:46 /usr/java6_64/bin/java -jar slave.jar -jnlpUrl http://jenkins.usdc.cfengine.com/computer/buildslave-aix-5.3-ppc64/slave-agent.jnlp",
            " <     >  <    >       X  <    >   < >   < > <    > <> X    <    >    <      > <                                                                                                                                >",
            // Extreme case, a lot of fields missing. This only works when the
            // process is a zombie and the platform uses the
            // PCA_ZombieSkipEmptyColumns algorithm (which AIX does).
            "          205218       1  205218                   20 Z",
            "       -  <    >       X  <    >     -     -     - <> X        -           - -",
            NULL
        }, {
            // HPUX
            "     UID   PID  PPID  C    STIME TTY       TIME COMMAND",
            "     < >   < >  <  >  X    <   > < >       <  > <     >",
            " jenkins 17345 17109  0 09:31:39 pts/0     0:00 perl -e my $v = fork(); if ($v != 0) { sleep(3600); }",
            " <     > <   > <   >  X <      > <   >     <  > <                                                   >",
            " jenkins 17374 17345  0 09:31:40 pts/0     0:00 <defunct>",
            " <     > <   > <   >  X <      > <   >     <  > <       >",
            "    root   832     1  0  May  4  ?         0:01 /usr/sbin/automountd",
            "    <  >   < >     X  X  <    >  X         <  > <                  >",
            NULL
        }, {
            // Solaris 9
            "    USER   PID %CPU %MEM   SZ  RSS TT      S    STIME        TIME COMMAND",
            "    <  >   < > <  > <  >   <>  < > <>      X    <   >        <  > <     >",
            " jenkins 29769  0.0  0.0  810 2976 pts/1   S 07:22:43        0:00 /usr/bin/perl ../../ps.pl",
            " <     > <   >  < >  < >  < > <  > <   >   X <      >        <  > <                       >",
            " jenkins 29835    -    -    0    0 ?       Z        -        0:00 <defunct>",
            " <     > <   >    X    X    X    X X       X        X        <  > <       >",
            " jenkins 10026  0.0  0.3 30927 143632 ?       S   Jan_21    01:18:58 /usr/jdk/jdk1.6.0_45/bin/java -jar slave.jar",
            " <     > <   >  < >  < > <   > <    > X       X   <    >    <      > <                                          >",
            NULL
        }, {
            // Solaris 10
            "    USER   PID %CPU %MEM   SZ  RSS TT      S    STIME        TIME COMMAND",
            "    <  >   < > <  > <  >   <>  < > <>      X    <   >        <  > <     >",
            "    root 19553  0.0  0.0  743 4680 ?       S 04:03:10       00:00 /usr/lib/ssh/sshd",
            "    <  > <   >  < >  < >  < > <  > X       X <      >       <   > <               >",
            " jenkins 29770    -    -    0    0 ?       Z        -       00:00 <defunct>",
            " <     > <   >    X    X    X    X X       X        X       <   > <       >",
            "noaccess  8264  0.0  0.2 19890 116240 ?       S   Jan_16    08:49:25 /usr/java/bin/java -server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4 ",
            "<      >  <  >  < >  < > <   > <    > X       X   <    >    <      > <                                                                            >",
            NULL
        }, {
            // Solaris 11
            "    USER   PID %CPU %MEM   SZ  RSS TT      S    STIME        TIME COMMAND",
            "    <  >   < > <  > <  >   <>  < > <>      X    <   >        <  > <     >",
            "    root 15449  0.0  0.0  835 4904 ?       S 04:03:29       00:00 /usr/lib/ssh/sshd",
            "    <  > <   >  < >  < >  < > <  > X       X <      >       <   > <               >",
            " jenkins  5409    -    -    0    0 ?       Z        -       00:00 <defunct>",
            " <     >  <  >    X    X    X    X X       X        X       <   > <       >",
            " jenkins 29997  0.0  0.5 51661 312120 ?       S   Jan_21    01:18:53 /usr/jdk/jdk1.6.0_45/bin/java -jar slave.jar",
            " <     > <   >  < >  < > <   > <    > X       X   <    >    <      > <                                          >",
            NULL
        }, {
            // FreeBSD 11
            "USER         PID  %CPU %MEM     VSZ    RSS TT  STAT STARTED        TIME COMMAND",
            "<  >         < >  <  > <  >     < >    < > <>  <  > <     >        <  > <     >",
            "skreuzer    6506   5.0  0.2   57192  25764  0- S+   12:39      10:13.37 mosh-server new -c 256 -s -l LANG=en_US.UTF-8",
            "<      >    <  >   < >  < >   <   >  <   >  <> <>   <   >      <      > <                                           >",
            "zookeeper    646   0.0  1.0 4538080 162140  -  I    28Feb16    21:46.80 /usr/local/openjdk7/bin/java -Dzookeeper.log.dir=/var/log/zookeeper -Dlog4j.configuration=file:/usr/local/etc/zookeeper/l",
            "<       >    < >   < >  < > <     > <    >  X  X    <     >    <      > <                                                                                                                       >",
            "skreuzer   40046   0.0  0.0       0      0  6  Z+   21:34       0:00.00 <defunct>",
            "<      >   <   >   < >  < >       X      X  X  <>   <   >       <     > <       >",
            "skreuzer   50293   0.0  0.0   20612   5332  3  Is    2Mar16     0:00.62 -zsh (zsh)",
            "<      >   <   >   < >  < >   <   >   <  >  X  <>    <    >     <     > <        >",
            NULL
        }, {
            // Illumos
            "    USER   PID %CPU %MEM   SZ  RSS TT      S    STIME        TIME COMMAND",
            "    <  >   < > <  > <  >   <>  < > <>      X    <   >        <  > <     >",
            " bahamat 63679  0.0  0.1 1831 4340 pts/13  S 00:15:39       00:00 perl -e $p = fork(); if ($p != 0) { sleep(60); }",
            " <     > <   >  < >  < > <  > <  > <    >  X <      >       <   > <                                              >",
            " bahamat 63680    -    -    0    0 ?       Z        -       00:00 <defunct>",
            " <     > <   >    X    X    X    X X       X        X       <   > <       >",
            "    root 72601  0.3  0.4 291631 1050796 ?       S   Feb_25    06:35:07 /opt/local/java/openjdk7/bin/java -Dhudson.DNSMultiCast.disabled=true -Xmx2048m",
            "    <  > <   >  < >  < > <    > <     > X       X   <    >    <      > <                                                                             >",
            NULL
        }
    };

    // Only half as many elements here, since there is only expected output.
    const char *exceptions[NUM_OF_PLATFORMS][10] = {
        {
            // Linux
            NULL,
            "1459762277",
            "1459763233",
            NULL
        }, {
            // AIX
            NULL
        }, {
            // HPUX
            NULL
        }, {
            // Solaris 9
            NULL
        }, {
            // Solaris 10
            NULL
        }, {
            // Solaris 11
            NULL
        }, {
            // FreeBSD 11
            NULL
        }, {
            // Illumos
            NULL
        }
    };

    char *names[CF_PROCCOLS] = { 0 };
    char *fields[CF_PROCCOLS] = { 0 };
    int start[CF_PROCCOLS] = { 0 };
    int end[CF_PROCCOLS] = { 0 };

    memset(names, 0, sizeof(names));
    memset(fields, 0, sizeof(fields));

    for (int platform = 0; platform < NUM_OF_PLATFORMS; platform++)
    {
        PsColumnAlgorithm pca;
        if (platform == TEST_AIX)
        {
            pca = PCA_ZombieSkipEmptyColumns;
        }
        else
        {
            pca = PCA_AllColumnsPresent;
        }

        for (int linenum = 0; pslines[platform][linenum]; linenum += 2)
        {
            char **fields_to_check;
            if (linenum == 0)
            {
                GetProcessColumnNames(pslines[platform][linenum], names, start, end);
                fields_to_check = names;
            }
            else
            {
                assert_true(SplitProcLine(pslines[platform][linenum], 1460118341, names, start, end, pca, fields));
                fields_to_check = fields;
            }

            bool in_field = false;
            int field_start = -1;
            int field_end = -1;
            int field_num = 0;
            bool exception = false;
            for (int pos = 0;; pos++)
            {
                if (!pslines[platform][linenum + 1][pos])
                {
                    // There should be no excess space at the end of the
                    // expected replies.
                    assert_int_not_equal(pslines[platform][linenum + 1][pos - 1], ' ');
                    assert_false(in_field);
                    // We should have reached the end of both arrays.
                    assert_int_equal(names[field_num], NULL);
                    if (linenum != 0)
                    {
                        assert_int_equal(fields[field_num], NULL);
                    }
                    break;
                }

                switch (pslines[platform][linenum + 1][pos])
                {
                case ' ':
                    break;
                case '{':
                    exception = true;
                    // fallthrough
                case '<':
                    assert_false(in_field);
                    in_field = true;
                    field_start = pos;
                    break;
                case '}':
                case '>':
                    assert_true(in_field);
                    in_field = false;
                    // + 1 because we want it to be the next byte.
                    field_end = pos + 1;
                    break;
                case 'X':
                    field_start = pos;
                    // + 1 because we want it to be the next byte.
                    field_end = pos + 1;
                    break;
                case '-':
                    field_start = pos;
                    // This corresponds to an empty string.
                    field_end = pos;
                    break;
                default:
                    assert_true(false);
                    break;
                }

                if (field_start != -1 && field_end != -1)
                {
                    if (exception)
                    {
                        assert_string_equal(fields_to_check[field_num],
                                            exceptions[platform][linenum / 2]);
                        exception = false;
                    }
                    else
                    {
                        int field_len = field_end - field_start;

#if 0                   /* DEBUG OUTPUT */
                        printf("Checking '%s' against '", fields_to_check[field_num]);
                        fwrite(pslines[platform][linenum] + field_start, field_len, 1, stdout);
                        printf("'\n");
#endif                  /* DEBUG OUTPUT */

                        // Check if fields are identical.
                        assert_memory_equal(fields_to_check[field_num],
                                            pslines[platform][linenum] + field_start,
                                            field_len);
                        // And that it's not longer than what we expect.
                        assert_int_equal(fields_to_check[field_num][field_len], '\0');
                    }
                    field_start = -1;
                    field_end = -1;
                    field_num++;
                }
            }

            free_and_null_strings(fields);
        }

        free_and_null_strings(names);
    }
}

int main(void)
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
        {
            unit_test(test_split_line_challenges),
            unit_test(test_split_line_noelapsed),
            unit_test(test_split_line_elapsed),
            unit_test(test_split_line_longcmd),
            unit_test(test_split_line),
            unit_test(test_split_line_serious_overspill),
            unit_test(test_platform_extra_table),
            unit_test(test_platform_specific_ps_examples),
        };

    return run_tests(tests);
}