summaryrefslogtreecommitdiffstats
path: root/mojo/edk/system/core_unittest.cc
blob: a3397671e155665468a9ae6acecad8777fd95ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/edk/system/core.h"

#include <stdint.h>

#include <limits>

#include "base/bind.h"
#include "mojo/edk/embedder/embedder_internal.h"
#include "mojo/edk/system/awakable.h"
#include "mojo/edk/system/core_test_base.h"
#include "mojo/edk/system/test_utils.h"
#include "mojo/public/cpp/system/macros.h"

#if defined(OS_WIN)
#include "base/win/windows_version.h"
#endif

namespace mojo {
namespace edk {
namespace {

const MojoHandleSignalsState kEmptyMojoHandleSignalsState = {0u, 0u};
const MojoHandleSignalsState kFullMojoHandleSignalsState = {~0u, ~0u};
const MojoHandleSignals kAllSignals = MOJO_HANDLE_SIGNAL_READABLE |
                                      MOJO_HANDLE_SIGNAL_WRITABLE |
                                      MOJO_HANDLE_SIGNAL_PEER_CLOSED;

using CoreTest = test::CoreTestBase;

TEST_F(CoreTest, GetTimeTicksNow) {
  const MojoTimeTicks start = core()->GetTimeTicksNow();
  ASSERT_NE(static_cast<MojoTimeTicks>(0), start)
      << "GetTimeTicksNow should return nonzero value";
  test::Sleep(test::DeadlineFromMilliseconds(15));
  const MojoTimeTicks finish = core()->GetTimeTicksNow();
  // Allow for some fuzz in sleep.
  ASSERT_GE((finish - start), static_cast<MojoTimeTicks>(8000))
      << "Sleeping should result in increasing time ticks";
}

TEST_F(CoreTest, Basic) {
  MockHandleInfo info;

  ASSERT_EQ(0u, info.GetCtorCallCount());
  MojoHandle h = CreateMockHandle(&info);
  ASSERT_EQ(1u, info.GetCtorCallCount());
  ASSERT_NE(h, MOJO_HANDLE_INVALID);

  ASSERT_EQ(0u, info.GetWriteMessageCallCount());
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h, nullptr, 0, nullptr, 0,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  ASSERT_EQ(1u, info.GetWriteMessageCallCount());

  ASSERT_EQ(0u, info.GetReadMessageCallCount());
  uint32_t num_bytes = 0;
  ASSERT_EQ(
      MOJO_RESULT_OK,
      core()->ReadMessage(h, nullptr, &num_bytes, nullptr, nullptr,
                          MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(1u, info.GetReadMessageCallCount());
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(h, nullptr, nullptr, nullptr, nullptr,
                                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(2u, info.GetReadMessageCallCount());

  ASSERT_EQ(0u, info.GetWriteDataCallCount());
  ASSERT_EQ(MOJO_RESULT_UNIMPLEMENTED,
            core()->WriteData(h, nullptr, nullptr, MOJO_WRITE_DATA_FLAG_NONE));
  ASSERT_EQ(1u, info.GetWriteDataCallCount());

  ASSERT_EQ(0u, info.GetBeginWriteDataCallCount());
  ASSERT_EQ(MOJO_RESULT_UNIMPLEMENTED,
            core()->BeginWriteData(h, nullptr, nullptr,
                                   MOJO_WRITE_DATA_FLAG_NONE));
  ASSERT_EQ(1u, info.GetBeginWriteDataCallCount());

  ASSERT_EQ(0u, info.GetEndWriteDataCallCount());
  ASSERT_EQ(MOJO_RESULT_UNIMPLEMENTED, core()->EndWriteData(h, 0));
  ASSERT_EQ(1u, info.GetEndWriteDataCallCount());

  ASSERT_EQ(0u, info.GetReadDataCallCount());
  ASSERT_EQ(MOJO_RESULT_UNIMPLEMENTED,
            core()->ReadData(h, nullptr, nullptr, MOJO_READ_DATA_FLAG_NONE));
  ASSERT_EQ(1u, info.GetReadDataCallCount());

  ASSERT_EQ(0u, info.GetBeginReadDataCallCount());
  ASSERT_EQ(MOJO_RESULT_UNIMPLEMENTED,
            core()->BeginReadData(h, nullptr, nullptr,
                                  MOJO_READ_DATA_FLAG_NONE));
  ASSERT_EQ(1u, info.GetBeginReadDataCallCount());

  ASSERT_EQ(0u, info.GetEndReadDataCallCount());
  ASSERT_EQ(MOJO_RESULT_UNIMPLEMENTED, core()->EndReadData(h, 0));
  ASSERT_EQ(1u, info.GetEndReadDataCallCount());

  ASSERT_EQ(0u, info.GetAddAwakableCallCount());
  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, MOJO_DEADLINE_INDEFINITE,
                         nullptr));
  ASSERT_EQ(1u, info.GetAddAwakableCallCount());
  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, 0, nullptr));
  ASSERT_EQ(2u, info.GetAddAwakableCallCount());
  MojoHandleSignalsState hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, MOJO_DEADLINE_INDEFINITE,
                         &hss));
  ASSERT_EQ(3u, info.GetAddAwakableCallCount());
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(0u, hss.satisfiable_signals);
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, 10 * 1000, nullptr));
  ASSERT_EQ(4u, info.GetAddAwakableCallCount());
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->Wait(h, ~MOJO_HANDLE_SIGNAL_NONE, 10 * 1000, &hss));
  ASSERT_EQ(5u, info.GetAddAwakableCallCount());
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(0u, hss.satisfiable_signals);

  MojoHandleSignals handle_signals = ~MOJO_HANDLE_SIGNAL_NONE;
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->WaitMany(&h, &handle_signals, 1, MOJO_DEADLINE_INDEFINITE,
                       nullptr, nullptr));
  ASSERT_EQ(6u, info.GetAddAwakableCallCount());
  uint32_t result_index = static_cast<uint32_t>(-1);
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->WaitMany(&h, &handle_signals, 1, MOJO_DEADLINE_INDEFINITE,
                       &result_index, nullptr));
  ASSERT_EQ(7u, info.GetAddAwakableCallCount());
  ASSERT_EQ(0u, result_index);
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->WaitMany(&h, &handle_signals, 1, MOJO_DEADLINE_INDEFINITE,
                       nullptr, &hss));
  ASSERT_EQ(8u, info.GetAddAwakableCallCount());
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(0u, hss.satisfiable_signals);
  result_index = static_cast<uint32_t>(-1);
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->WaitMany(&h, &handle_signals, 1, MOJO_DEADLINE_INDEFINITE,
                       &result_index, &hss));
  ASSERT_EQ(9u, info.GetAddAwakableCallCount());
  ASSERT_EQ(0u, result_index);
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(0u, hss.satisfiable_signals);

  ASSERT_EQ(0u, info.GetDtorCallCount());
  ASSERT_EQ(0u, info.GetCloseCallCount());
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
  ASSERT_EQ(1u, info.GetCloseCallCount());
  ASSERT_EQ(1u, info.GetDtorCallCount());

  // No awakables should ever have ever been added.
  ASSERT_EQ(0u, info.GetRemoveAwakableCallCount());
}

TEST_F(CoreTest, InvalidArguments) {
  // |Close()|:
  {
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(MOJO_HANDLE_INVALID));
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(10));
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(1000000000));

    // Test a double-close.
    MockHandleInfo info;
    MojoHandle h = CreateMockHandle(&info);
    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
    ASSERT_EQ(1u, info.GetCloseCallCount());
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(h));
    ASSERT_EQ(1u, info.GetCloseCallCount());
  }

  // |Wait()|:
  {
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->Wait(MOJO_HANDLE_INVALID, ~MOJO_HANDLE_SIGNAL_NONE,
                           MOJO_DEADLINE_INDEFINITE, nullptr));
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->Wait(10, ~MOJO_HANDLE_SIGNAL_NONE,
                           MOJO_DEADLINE_INDEFINITE, nullptr));

    MojoHandleSignalsState hss = kFullMojoHandleSignalsState;
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->Wait(MOJO_HANDLE_INVALID, ~MOJO_HANDLE_SIGNAL_NONE,
                           MOJO_DEADLINE_INDEFINITE, &hss));
    // On invalid argument, it shouldn't modify the handle signals state.
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfied_signals,
              hss.satisfied_signals);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfiable_signals,
              hss.satisfiable_signals);
    hss = kFullMojoHandleSignalsState;
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->Wait(10, ~MOJO_HANDLE_SIGNAL_NONE,
                           MOJO_DEADLINE_INDEFINITE, &hss));
    // On invalid argument, it shouldn't modify the handle signals state.
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfied_signals,
              hss.satisfied_signals);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfiable_signals,
              hss.satisfiable_signals);
  }

  // |WaitMany()|:
  {
    MojoHandle handles[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID};
    MojoHandleSignals signals[2] = {~MOJO_HANDLE_SIGNAL_NONE,
                                    ~MOJO_HANDLE_SIGNAL_NONE};
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->WaitMany(handles, signals, 0, MOJO_DEADLINE_INDEFINITE,
                         nullptr, nullptr));
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->WaitMany(nullptr, signals, 0, MOJO_DEADLINE_INDEFINITE,
                               nullptr, nullptr));
    // If |num_handles| is invalid, it should leave |result_index| and
    // |signals_states| alone.
    // (We use -1 internally; make sure that doesn't leak.)
    uint32_t result_index = 123;
    MojoHandleSignalsState hss = kFullMojoHandleSignalsState;
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->WaitMany(nullptr, signals, 0, MOJO_DEADLINE_INDEFINITE,
                               &result_index, &hss));
    ASSERT_EQ(123u, result_index);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfied_signals,
              hss.satisfied_signals);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfiable_signals,
              hss.satisfiable_signals);

    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->WaitMany(handles, nullptr, 0, MOJO_DEADLINE_INDEFINITE,
                               nullptr, nullptr));
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->WaitMany(handles, signals, 1, MOJO_DEADLINE_INDEFINITE, nullptr,
                         nullptr));
    // But if a handle is bad, then it should set |result_index| but still leave
    // |signals_states| alone.
    result_index = static_cast<uint32_t>(-1);
    hss = kFullMojoHandleSignalsState;
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->WaitMany(
                  handles, signals, 1, MOJO_DEADLINE_INDEFINITE, &result_index,
                  &hss));
    ASSERT_EQ(0u, result_index);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfied_signals,
              hss.satisfied_signals);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfiable_signals,
              hss.satisfiable_signals);

    MockHandleInfo info[2];
    handles[0] = CreateMockHandle(&info[0]);

    result_index = static_cast<uint32_t>(-1);
    hss = kFullMojoHandleSignalsState;
    ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
              core()->WaitMany(
                  handles, signals, 1, MOJO_DEADLINE_INDEFINITE, &result_index,
                  &hss));
    ASSERT_EQ(0u, result_index);
    ASSERT_EQ(0u, hss.satisfied_signals);
    ASSERT_EQ(0u, hss.satisfiable_signals);

    // On invalid argument, it'll leave |signals_states| alone.
    result_index = static_cast<uint32_t>(-1);
    hss = kFullMojoHandleSignalsState;
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->WaitMany(
                  handles, signals, 2, MOJO_DEADLINE_INDEFINITE, &result_index,
                  &hss));
    ASSERT_EQ(1u, result_index);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfied_signals,
              hss.satisfied_signals);
    ASSERT_EQ(kFullMojoHandleSignalsState.satisfiable_signals,
              hss.satisfiable_signals);
    handles[1] = handles[0] + 1;  // Invalid handle.
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->WaitMany(handles, signals, 2, MOJO_DEADLINE_INDEFINITE, nullptr,
                         nullptr));
    handles[1] = CreateMockHandle(&info[1]);
    ASSERT_EQ(
        MOJO_RESULT_FAILED_PRECONDITION,
        core()->WaitMany(handles, signals, 2, MOJO_DEADLINE_INDEFINITE, nullptr,
                         nullptr));

    // TODO(vtl): Test one where we get "failed precondition" only for the
    // second handle (and the first one is valid to wait on).

    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(handles[0]));
    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(handles[1]));
  }

  // |CreateMessagePipe()|: Nothing to check (apart from things that cause
  // death).

  // |WriteMessage()|:
  // Only check arguments checked by |Core|, namely |handle|, |handles|, and
  // |num_handles|.
  {
    ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
              core()->WriteMessage(MOJO_HANDLE_INVALID, nullptr, 0,
                                   nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));

    MockHandleInfo info;
    MojoHandle h = CreateMockHandle(&info);
    MojoHandle handles[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID};

    // Huge handle count (implausibly big on some systems -- more than can be
    // stored in a 32-bit address space).
    // Note: This may return either |MOJO_RESULT_INVALID_ARGUMENT| or
    // |MOJO_RESULT_RESOURCE_EXHAUSTED|, depending on whether it's plausible or
    // not.
    ASSERT_NE(
        MOJO_RESULT_OK,
        core()->WriteMessage(h, nullptr, 0, handles,
                             std::numeric_limits<uint32_t>::max(),
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(0u, info.GetWriteMessageCallCount());

    // Huge handle count (plausibly big).
    ASSERT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
              core()->WriteMessage(
                  h, nullptr, 0, handles,
                  std::numeric_limits<uint32_t>::max() / sizeof(handles[0]),
                  MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(0u, info.GetWriteMessageCallCount());

    // Invalid handle in |handles|.
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->WriteMessage(h, nullptr, 0, handles, 1,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(0u, info.GetWriteMessageCallCount());

    // Two invalid handles in |handles|.
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->WriteMessage(h, nullptr, 0, handles, 2,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(0u, info.GetWriteMessageCallCount());

    // Can't send a handle over itself.
    handles[0] = h;
    ASSERT_EQ(
        MOJO_RESULT_BUSY,
        core()->WriteMessage(h, nullptr, 0, handles, 1,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(0u, info.GetWriteMessageCallCount());

    MockHandleInfo info2;
    MojoHandle h2 = CreateMockHandle(&info2);

    // This is "okay", but |MockDispatcher| doesn't implement it.
    handles[0] = h2;
    ASSERT_EQ(
        MOJO_RESULT_UNIMPLEMENTED,
        core()->WriteMessage(h, nullptr, 0, handles, 1,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(1u, info.GetWriteMessageCallCount());

    // One of the |handles| is still invalid.
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->WriteMessage(h, nullptr, 0, handles, 2,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(1u, info.GetWriteMessageCallCount());

    // One of the |handles| is the same as |handle|.
    handles[1] = h;
    ASSERT_EQ(
        MOJO_RESULT_BUSY,
        core()->WriteMessage(h, nullptr, 0, handles, 2,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(1u, info.GetWriteMessageCallCount());

    // Can't send a handle twice in the same message.
    handles[1] = h2;
    ASSERT_EQ(
        MOJO_RESULT_BUSY,
        core()->WriteMessage(h, nullptr, 0, handles, 2,
                             MOJO_WRITE_MESSAGE_FLAG_NONE));
    ASSERT_EQ(1u, info.GetWriteMessageCallCount());

    // Note: Since we never successfully sent anything with it, |h2| should
    // still be valid.
    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h2));

    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
  }

  // |ReadMessage()|:
  // Only check arguments checked by |Core|, namely |handle|, |handles|, and
  // |num_handles|.
  {
    ASSERT_EQ(
        MOJO_RESULT_INVALID_ARGUMENT,
        core()->ReadMessage(MOJO_HANDLE_INVALID, nullptr, nullptr, nullptr,
                            nullptr, MOJO_READ_MESSAGE_FLAG_NONE));

    MockHandleInfo info;
    MojoHandle h = CreateMockHandle(&info);

    // Okay.
    uint32_t handle_count = 0;
    ASSERT_EQ(MOJO_RESULT_OK,
              core()->ReadMessage(
                  h, nullptr, nullptr, nullptr, &handle_count,
                  MOJO_READ_MESSAGE_FLAG_NONE));
    // Checked by |Core|, shouldn't go through to the dispatcher.
    ASSERT_EQ(1u, info.GetReadMessageCallCount());

    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
  }
}

// These test invalid arguments that should cause death if we're being paranoid
// about checking arguments (which we would want to do if, e.g., we were in a
// true "kernel" situation, but we might not want to do otherwise for
// performance reasons). Probably blatant errors like passing in null pointers
// (for required pointer arguments) will still cause death, but perhaps not
// predictably.
TEST_F(CoreTest, InvalidArgumentsDeath) {
  const char kMemoryCheckFailedRegex[] = "Check failed";

  // |WaitMany()|:
  {
    MojoHandle handle = MOJO_HANDLE_INVALID;
    MojoHandleSignals signals = ~MOJO_HANDLE_SIGNAL_NONE;
    ASSERT_DEATH_IF_SUPPORTED(
        core()->WaitMany(nullptr, &signals, 1, MOJO_DEADLINE_INDEFINITE,
                         nullptr, nullptr),
        kMemoryCheckFailedRegex);
    ASSERT_DEATH_IF_SUPPORTED(
        core()->WaitMany(&handle, nullptr, 1, MOJO_DEADLINE_INDEFINITE, nullptr,
                         nullptr),
        kMemoryCheckFailedRegex);
    // TODO(vtl): |result_index| and |signals_states| are optional. Test them
    // with non-null invalid pointers?
  }

  // |CreateMessagePipe()|:
  {
    MojoHandle h;
    ASSERT_DEATH_IF_SUPPORTED(
        core()->CreateMessagePipe(nullptr, nullptr, nullptr),
        kMemoryCheckFailedRegex);
    ASSERT_DEATH_IF_SUPPORTED(
        core()->CreateMessagePipe(nullptr, &h, nullptr),
        kMemoryCheckFailedRegex);
    ASSERT_DEATH_IF_SUPPORTED(
        core()->CreateMessagePipe(nullptr, nullptr, &h),
        kMemoryCheckFailedRegex);
  }

  // |WriteMessage()|:
  // Only check arguments checked by |Core|, namely |handle|, |handles|, and
  // |num_handles|.
  {
    MockHandleInfo info;
    MojoHandle h = CreateMockHandle(&info);

    // Null |handles| with nonzero |num_handles|.
    ASSERT_DEATH_IF_SUPPORTED(
        core()->WriteMessage(h, nullptr, 0, nullptr, 1,
                             MOJO_WRITE_MESSAGE_FLAG_NONE),
        kMemoryCheckFailedRegex);

    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
  }

  // |ReadMessage()|:
  // Only check arguments checked by |Core|, namely |handle|, |handles|, and
  // |num_handles|.
  {
    MockHandleInfo info;
    MojoHandle h = CreateMockHandle(&info);

    uint32_t handle_count = 1;
    ASSERT_DEATH_IF_SUPPORTED(
        core()->ReadMessage(h, nullptr, nullptr, nullptr, &handle_count,
                            MOJO_READ_MESSAGE_FLAG_NONE),
        kMemoryCheckFailedRegex);

    ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
  }
}

// TODO(vtl): test |Wait()| and |WaitMany()| properly
//  - including |WaitMany()| with the same handle more than once (with
//    same/different signals)

TEST_F(CoreTest, MessagePipe) {
  MojoHandle h[2];
  MojoHandleSignalsState hss[2];
  uint32_t result_index;

  ASSERT_EQ(MOJO_RESULT_OK, core()->CreateMessagePipe(nullptr, &h[0], &h[1]));
  // Should get two distinct, valid handles.
  ASSERT_NE(h[0], MOJO_HANDLE_INVALID);
  ASSERT_NE(h[1], MOJO_HANDLE_INVALID);
  ASSERT_NE(h[0], h[1]);

  // Neither should be readable.
  MojoHandleSignals signals[2] = {MOJO_HANDLE_SIGNAL_READABLE,
                                  MOJO_HANDLE_SIGNAL_READABLE};
  result_index = static_cast<uint32_t>(-1);
  hss[0] = kEmptyMojoHandleSignalsState;
  hss[1] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_DEADLINE_EXCEEDED,
      core()->WaitMany(h, signals, 2, 0, &result_index, hss));
  ASSERT_EQ(static_cast<uint32_t>(-1), result_index);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[0].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[0].satisfiable_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[1].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[1].satisfiable_signals);

  // Try to read anyway.
  char buffer[1] = {'a'};
  uint32_t buffer_size = 1;
  ASSERT_EQ(
      MOJO_RESULT_SHOULD_WAIT,
      core()->ReadMessage(h[0], buffer, &buffer_size, nullptr, nullptr,
                          MOJO_READ_MESSAGE_FLAG_NONE));
  // Check that it left its inputs alone.
  ASSERT_EQ('a', buffer[0]);
  ASSERT_EQ(1u, buffer_size);

  // Both should be writable.
  hss[0] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK, core()->Wait(h[0], MOJO_HANDLE_SIGNAL_WRITABLE,
                                         1000000000, &hss[0]));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[0].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[0].satisfiable_signals);
  hss[0] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK, core()->Wait(h[1], MOJO_HANDLE_SIGNAL_WRITABLE,
                                         1000000000, &hss[0]));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[0].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[0].satisfiable_signals);

  // Also check that |h[1]| is writable using |WaitMany()|.
  signals[0] = MOJO_HANDLE_SIGNAL_READABLE;
  signals[1] = MOJO_HANDLE_SIGNAL_WRITABLE;
  result_index = static_cast<uint32_t>(-1);
  hss[0] = kEmptyMojoHandleSignalsState;
  hss[1] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_OK,
      core()->WaitMany(h, signals, 2, MOJO_DEADLINE_INDEFINITE, &result_index,
                       hss));
  ASSERT_EQ(1u, result_index);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[0].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[0].satisfiable_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[1].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[1].satisfiable_signals);

  // Write to |h[1]|.
  buffer[0] = 'b';
  ASSERT_EQ(
      MOJO_RESULT_OK,
      core()->WriteMessage(h[1], buffer, 1, nullptr, 0,
                           MOJO_WRITE_MESSAGE_FLAG_NONE));

  // Check that |h[0]| is now readable.
  signals[0] = MOJO_HANDLE_SIGNAL_READABLE;
  signals[1] = MOJO_HANDLE_SIGNAL_READABLE;
  result_index = static_cast<uint32_t>(-1);
  hss[0] = kEmptyMojoHandleSignalsState;
  hss[1] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_OK,
      core()->WaitMany(h, signals, 2, MOJO_DEADLINE_INDEFINITE, &result_index,
                       hss));
  ASSERT_EQ(0u, result_index);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss[0].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[0].satisfiable_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[1].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[1].satisfiable_signals);

  // Read from |h[0]|.
  // First, get only the size.
  buffer_size = 0;
  ASSERT_EQ(
      MOJO_RESULT_RESOURCE_EXHAUSTED,
      core()->ReadMessage(h[0], nullptr, &buffer_size, nullptr, nullptr,
                          MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(1u, buffer_size);
  // Then actually read it.
  buffer[0] = 'c';
  buffer_size = 1;
  ASSERT_EQ(
      MOJO_RESULT_OK,
      core()->ReadMessage(h[0], buffer, &buffer_size, nullptr, nullptr,
                          MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ('b', buffer[0]);
  ASSERT_EQ(1u, buffer_size);

  // |h[0]| should no longer be readable.
  hss[0] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
            core()->Wait(h[0], MOJO_HANDLE_SIGNAL_READABLE, 0, &hss[0]));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss[0].satisfied_signals);
  ASSERT_EQ(kAllSignals, hss[0].satisfiable_signals);

  // Write to |h[0]|.
  buffer[0] = 'd';
  ASSERT_EQ(
      MOJO_RESULT_OK,
      core()->WriteMessage(h[0], buffer, 1, nullptr, 0,
                           MOJO_WRITE_MESSAGE_FLAG_NONE));

  // Close |h[0]|.
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h[0]));

  // Wait for |h[1]| to learn about the other end's closure.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h[1], MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1000000000,
                         &hss[0]));

  // Check that |h[1]| is no longer writable (and will never be).
  hss[0] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->Wait(h[1], MOJO_HANDLE_SIGNAL_WRITABLE, 1000000000,
                         &hss[0]));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss[0].satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss[0].satisfiable_signals);

  // Check that |h[1]| is still readable (for the moment).
  hss[0] = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK, core()->Wait(h[1], MOJO_HANDLE_SIGNAL_READABLE,
                                         1000000000, &hss[0]));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss[0].satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss[0].satisfiable_signals);

  // Discard a message from |h[1]|.
  ASSERT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
            core()->ReadMessage(h[1], nullptr, nullptr, nullptr, nullptr,
                                MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));

  // |h[1]| is no longer readable (and will never be).
  hss[0] = kFullMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->Wait(h[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss[0]));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss[0].satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss[0].satisfiable_signals);

  // Try writing to |h[1]|.
  buffer[0] = 'e';
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->WriteMessage(h[1], buffer, 1, nullptr, 0,
                           MOJO_WRITE_MESSAGE_FLAG_NONE));

  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h[1]));
}

// Tests passing a message pipe handle.
TEST_F(CoreTest, MessagePipeBasicLocalHandlePassing1) {
  const char kHello[] = "hello";
  const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello));
  const char kWorld[] = "world!!!";
  const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld));
  char buffer[100];
  const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
  uint32_t num_bytes;
  MojoHandle handles[10];
  uint32_t num_handles;
  MojoHandleSignalsState hss;
  MojoHandle h_received;

  MojoHandle h_passing[2];
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->CreateMessagePipe(nullptr, &h_passing[0], &h_passing[1]));

  // Make sure that |h_passing[]| work properly.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize, nullptr, 0,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passing[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kHelloSize, num_bytes);
  ASSERT_STREQ(kHello, buffer);
  ASSERT_EQ(0u, num_handles);

  // Make sure that you can't pass either of the message pipe's handles over
  // itself.
  ASSERT_EQ(MOJO_RESULT_BUSY,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize,
                                 &h_passing[0], 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize,
                                 &h_passing[1], 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));

  MojoHandle h_passed[2];
  MojoCreateMessagePipeOptions options;
  options.struct_size = sizeof(MojoCreateMessagePipeOptions);
  options.flags = MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->CreateMessagePipe(&options, &h_passed[0], &h_passed[1]));

  // Make sure that |h_passed[]| work properly.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passed[0], kHello, kHelloSize, nullptr, 0,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passed[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passed[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kHelloSize, num_bytes);
  ASSERT_STREQ(kHello, buffer);
  ASSERT_EQ(0u, num_handles);

  // Send |h_passed[1]| from |h_passing[0]| to |h_passing[1]|.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passing[0], kWorld, kWorldSize,
                                 &h_passed[1], 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passing[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kWorldSize, num_bytes);
  ASSERT_STREQ(kWorld, buffer);
  ASSERT_EQ(1u, num_handles);
  h_received = handles[0];
  ASSERT_NE(h_received, MOJO_HANDLE_INVALID);
  ASSERT_NE(h_received, h_passing[0]);
  ASSERT_NE(h_received, h_passing[1]);
  ASSERT_NE(h_received, h_passed[0]);

  // Note: We rely on the Mojo system not re-using handle values very often.
  ASSERT_NE(h_received, h_passed[1]);

  // |h_passed[1]| should no longer be valid; check that trying to close it
  // fails. See above note.
  ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(h_passed[1]));

  // Write to |h_passed[0]|. Should receive on |h_received|.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passed[0], kHello, kHelloSize, nullptr, 0,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_received, MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_received, buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kHelloSize, num_bytes);
  ASSERT_STREQ(kHello, buffer);
  ASSERT_EQ(0u, num_handles);

  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[0]));
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[1]));
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h_passed[0]));
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h_received));
}

TEST_F(CoreTest, DataPipe) {
  MojoHandle ph, ch;  // p is for producer and c is for consumer.
  MojoHandleSignalsState hss;

  ASSERT_EQ(MOJO_RESULT_OK,
            core()->CreateDataPipe(nullptr, &ph, &ch));
  // Should get two distinct, valid handles.
  ASSERT_NE(ph, MOJO_HANDLE_INVALID);
  ASSERT_NE(ch, MOJO_HANDLE_INVALID);
  ASSERT_NE(ph, ch);

  // Producer should be never-readable, but already writable.
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->Wait(ph, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK, core()->Wait(ph, MOJO_HANDLE_SIGNAL_WRITABLE, 0,
                                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);

  // Consumer should be never-writable, and not yet readable.
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->Wait(ch, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss));
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_DEADLINE_EXCEEDED,
      core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);

  // Write.
  signed char elements[2] = {'A', 'B'};
  uint32_t num_bytes = 2u;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteData(ph, elements, &num_bytes,
                              MOJO_WRITE_DATA_FLAG_NONE));
  ASSERT_EQ(2u, num_bytes);

  // Wait for the data to arrive to the consumer.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 1000000000, &hss));

  // Consumer should now be readable.
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK, core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0,
                                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);

  // Peek one character.
  elements[0] = -1;
  elements[1] = -1;
  num_bytes = 1u;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadData(
                ch, elements, &num_bytes,
                MOJO_READ_DATA_FLAG_NONE | MOJO_READ_DATA_FLAG_PEEK));
  ASSERT_EQ('A', elements[0]);
  ASSERT_EQ(-1, elements[1]);

  // Read one character.
  elements[0] = -1;
  elements[1] = -1;
  num_bytes = 1u;
  ASSERT_EQ(MOJO_RESULT_OK, core()->ReadData(ch, elements, &num_bytes,
                                             MOJO_READ_DATA_FLAG_NONE));
  ASSERT_EQ('A', elements[0]);
  ASSERT_EQ(-1, elements[1]);

  // Two-phase write.
  void* write_ptr = nullptr;
  num_bytes = 0u;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->BeginWriteData(ph, &write_ptr, &num_bytes,
                                   MOJO_WRITE_DATA_FLAG_NONE));
  // We count on the default options providing a decent buffer size.
  ASSERT_GE(num_bytes, 3u);

  // Trying to do a normal write during a two-phase write should fail.
  elements[0] = 'X';
  num_bytes = 1u;
  ASSERT_EQ(MOJO_RESULT_BUSY,
            core()->WriteData(ph, elements, &num_bytes,
                              MOJO_WRITE_DATA_FLAG_NONE));

  // Actually write the data, and complete it now.
  static_cast<char*>(write_ptr)[0] = 'C';
  static_cast<char*>(write_ptr)[1] = 'D';
  static_cast<char*>(write_ptr)[2] = 'E';
  ASSERT_EQ(MOJO_RESULT_OK, core()->EndWriteData(ph, 3u));

  // Wait for the data to arrive to the consumer.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 1000000000, &hss));

  // Query how much data we have.
  num_bytes = 0;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadData(ch, nullptr, &num_bytes,
                             MOJO_READ_DATA_FLAG_QUERY));
  ASSERT_GE(num_bytes, 1u);

  // Try to query with peek. Should fail.
  num_bytes = 0;
  ASSERT_EQ(
      MOJO_RESULT_INVALID_ARGUMENT,
      core()->ReadData(ch, nullptr, &num_bytes,
                       MOJO_READ_DATA_FLAG_QUERY | MOJO_READ_DATA_FLAG_PEEK));
  ASSERT_EQ(0u, num_bytes);

  // Try to discard ten characters, in all-or-none mode. Should fail.
  num_bytes = 10;
  ASSERT_EQ(MOJO_RESULT_OUT_OF_RANGE,
            core()->ReadData(
                ch, nullptr, &num_bytes,
                MOJO_READ_DATA_FLAG_DISCARD | MOJO_READ_DATA_FLAG_ALL_OR_NONE));

  // Try to discard two characters, in peek mode. Should fail.
  num_bytes = 2;
  ASSERT_EQ(
      MOJO_RESULT_INVALID_ARGUMENT,
      core()->ReadData(ch, nullptr, &num_bytes,
                       MOJO_READ_DATA_FLAG_DISCARD | MOJO_READ_DATA_FLAG_PEEK));

  // Discard a character.
  num_bytes = 1;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadData(
                ch, nullptr, &num_bytes,
                MOJO_READ_DATA_FLAG_DISCARD | MOJO_READ_DATA_FLAG_ALL_OR_NONE));

  // Ensure the 3 bytes were read.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 1000000000, &hss));

  // Try a two-phase read of the remaining three bytes with peek. Should fail.
  const void* read_ptr = nullptr;
  num_bytes = 3;
  ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            core()->BeginReadData(ch, &read_ptr, &num_bytes,
                                  MOJO_READ_DATA_FLAG_PEEK));

  // Read the remaining two characters, in two-phase mode (all-or-none).
  num_bytes = 3;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->BeginReadData(ch, &read_ptr, &num_bytes,
                                  MOJO_READ_DATA_FLAG_ALL_OR_NONE));
  // Note: Count on still being able to do the contiguous read here.
  ASSERT_EQ(3u, num_bytes);

  // Discarding right now should fail.
  num_bytes = 1;
  ASSERT_EQ(MOJO_RESULT_BUSY,
            core()->ReadData(ch, nullptr, &num_bytes,
                             MOJO_READ_DATA_FLAG_DISCARD));

  // Actually check our data and end the two-phase read.
  ASSERT_EQ('C', static_cast<const char*>(read_ptr)[0]);
  ASSERT_EQ('D', static_cast<const char*>(read_ptr)[1]);
  ASSERT_EQ('E', static_cast<const char*>(read_ptr)[2]);
  ASSERT_EQ(MOJO_RESULT_OK, core()->EndReadData(ch, 3u));

  // Consumer should now be no longer readable.
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_DEADLINE_EXCEEDED,
      core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
  ASSERT_EQ(0u, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);

  // TODO(vtl): More.

  // Close the producer.
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(ph));

  // Wait for this to get to the consumer.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(ch, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1000000000, &hss));

  // The consumer should now be never-readable.
  hss = kFullMojoHandleSignalsState;
  ASSERT_EQ(
      MOJO_RESULT_FAILED_PRECONDITION,
      core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals);

  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(ch));
}

// Tests passing data pipe producer and consumer handles.
TEST_F(CoreTest, MessagePipeBasicLocalHandlePassing2) {
  const char kHello[] = "hello";
  const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello));
  const char kWorld[] = "world!!!";
  const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld));
  char buffer[100];
  const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
  uint32_t num_bytes;
  MojoHandle handles[10];
  uint32_t num_handles;
  MojoHandleSignalsState hss;

  MojoHandle h_passing[2];
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->CreateMessagePipe(nullptr, &h_passing[0], &h_passing[1]));

  MojoHandle ph, ch;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->CreateDataPipe(nullptr, &ph, &ch));

  // Send |ch| from |h_passing[0]| to |h_passing[1]|.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize, &ch, 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passing[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kHelloSize, num_bytes);
  ASSERT_STREQ(kHello, buffer);
  ASSERT_EQ(1u, num_handles);
  MojoHandle ch_received = handles[0];
  ASSERT_NE(ch_received, MOJO_HANDLE_INVALID);
  ASSERT_NE(ch_received, h_passing[0]);
  ASSERT_NE(ch_received, h_passing[1]);
  ASSERT_NE(ch_received, ph);

  // Note: We rely on the Mojo system not re-using handle values very often.
  ASSERT_NE(ch_received, ch);

  // |ch| should no longer be valid; check that trying to close it fails. See
  // above note.
  ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(ch));

  // Write to |ph|. Should receive on |ch_received|.
  num_bytes = kWorldSize;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteData(ph, kWorld, &num_bytes,
                              MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(ch_received, MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);
  num_bytes = kBufferSize;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadData(ch_received, buffer, &num_bytes,
                             MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kWorldSize, num_bytes);
  ASSERT_STREQ(kWorld, buffer);

  // Now pass |ph| in the same direction.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passing[0], kWorld, kWorldSize, &ph, 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passing[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kWorldSize, num_bytes);
  ASSERT_STREQ(kWorld, buffer);
  ASSERT_EQ(1u, num_handles);
  MojoHandle ph_received = handles[0];
  ASSERT_NE(ph_received, MOJO_HANDLE_INVALID);
  ASSERT_NE(ph_received, h_passing[0]);
  ASSERT_NE(ph_received, h_passing[1]);
  ASSERT_NE(ph_received, ch_received);

  // Again, rely on the Mojo system not re-using handle values very often.
  ASSERT_NE(ph_received, ph);

  // |ph| should no longer be valid; check that trying to close it fails. See
  // above note.
  ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(ph));

  // Write to |ph_received|. Should receive on |ch_received|.
  num_bytes = kHelloSize;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteData(ph_received, kHello, &num_bytes,
                              MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(ch_received, MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);
  num_bytes = kBufferSize;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadData(ch_received, buffer, &num_bytes,
                             MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kHelloSize, num_bytes);
  ASSERT_STREQ(kHello, buffer);

  ph = ph_received;
  ph_received = MOJO_HANDLE_INVALID;
  ch = ch_received;
  ch_received = MOJO_HANDLE_INVALID;

  // Make sure that |ph| can't be sent if it's in a two-phase write.
  void* write_ptr = nullptr;
  num_bytes = 0;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->BeginWriteData(ph, &write_ptr, &num_bytes,
                                   MOJO_WRITE_DATA_FLAG_NONE));
  ASSERT_GE(num_bytes, 1u);
  ASSERT_EQ(MOJO_RESULT_BUSY,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize, &ph, 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));

  // But |ch| can, even if |ph| is in a two-phase write.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize, &ch, 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  ch = MOJO_HANDLE_INVALID;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         nullptr));
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passing[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kHelloSize, num_bytes);
  ASSERT_STREQ(kHello, buffer);
  ASSERT_EQ(1u, num_handles);
  ch = handles[0];
  ASSERT_NE(ch, MOJO_HANDLE_INVALID);

  // Complete the two-phase write.
  static_cast<char*>(write_ptr)[0] = 'x';
  ASSERT_EQ(MOJO_RESULT_OK, core()->EndWriteData(ph, 1));

  // Wait for |ch| to be readable.
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK, core()->Wait(ch, MOJO_HANDLE_SIGNAL_READABLE,
                                         1000000000, &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
            hss.satisfiable_signals);

  // Make sure that |ch| can't be sent if it's in a two-phase read.
  const void* read_ptr = nullptr;
  num_bytes = 1;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->BeginReadData(ch, &read_ptr, &num_bytes,
                                  MOJO_READ_DATA_FLAG_ALL_OR_NONE));
  ASSERT_EQ(MOJO_RESULT_BUSY,
            core()->WriteMessage(h_passing[0], kHello, kHelloSize, &ch, 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));

  // But |ph| can, even if |ch| is in a two-phase read.
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->WriteMessage(h_passing[0], kWorld, kWorldSize, &ph, 1,
                                 MOJO_WRITE_MESSAGE_FLAG_NONE));
  ph = MOJO_HANDLE_INVALID;
  hss = kEmptyMojoHandleSignalsState;
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->Wait(h_passing[1], MOJO_HANDLE_SIGNAL_READABLE, 1000000000,
                         &hss));
  ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
            hss.satisfied_signals);
  ASSERT_EQ(kAllSignals, hss.satisfiable_signals);
  num_bytes = kBufferSize;
  num_handles = arraysize(handles);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->ReadMessage(
                h_passing[1], buffer, &num_bytes, handles, &num_handles,
                MOJO_READ_MESSAGE_FLAG_NONE));
  ASSERT_EQ(kWorldSize, num_bytes);
  ASSERT_STREQ(kWorld, buffer);
  ASSERT_EQ(1u, num_handles);
  ph = handles[0];
  ASSERT_NE(ph, MOJO_HANDLE_INVALID);

  // Complete the two-phase read.
  ASSERT_EQ('x', static_cast<const char*>(read_ptr)[0]);
  ASSERT_EQ(MOJO_RESULT_OK, core()->EndReadData(ch, 1));

  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[0]));
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h_passing[1]));
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(ph));
  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(ch));
}

struct TestAsyncWaiter {
  TestAsyncWaiter() : result(MOJO_RESULT_UNKNOWN) {}

  void Awake(MojoResult r) { result = r; }

  MojoResult result;
};

TEST_F(CoreTest, AsyncWait) {
  TestAsyncWaiter waiter;
  MockHandleInfo info;
  MojoHandle h = CreateMockHandle(&info);

  ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
            core()->AsyncWait(h, MOJO_HANDLE_SIGNAL_READABLE,
                              base::Bind(&TestAsyncWaiter::Awake,
                                         base::Unretained(&waiter))));
  ASSERT_EQ(0u, info.GetAddedAwakableSize());

  info.AllowAddAwakable(true);
  ASSERT_EQ(MOJO_RESULT_OK,
            core()->AsyncWait(h, MOJO_HANDLE_SIGNAL_READABLE,
                              base::Bind(&TestAsyncWaiter::Awake,
                                         base::Unretained(&waiter))));
  ASSERT_EQ(1u, info.GetAddedAwakableSize());

  ASSERT_FALSE(info.GetAddedAwakableAt(0)->Awake(MOJO_RESULT_BUSY, 0));
  ASSERT_EQ(MOJO_RESULT_BUSY, waiter.result);

  ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h));
}

// TODO(vtl): Test |DuplicateBufferHandle()| and |MapBuffer()|.

}  // namespace
}  // namespace edk
}  // namespace mojo