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

mojo / core / quota_unittest.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "mojo/core/embedder/embedder.h"
#include "mojo/core/test/mojo_test_base.h"
#include "mojo/public/c/system/quota.h"

namespace mojo {
namespace core {
namespace {

using QuotaTest = test::MojoTestBase;

void QuotaExceededEventHandler(const MojoTrapEvent* event) {
  // Always treat trigger context as the address of a bool to set to |true|.
  if (event->result == MOJO_RESULT_OK)
    *reinterpret_cast<bool*>(event->trigger_context) = true;
}

TEST_F(QuotaTest, InvalidArguments) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoSetQuota(MOJO_HANDLE_INVALID,
                         MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, 2, nullptr));

  const MojoQuotaType kInvalidQuotaType = 0xfffffffful;
  MojoHandle message_pipe0, message_pipe1;
  CreateMessagePipe(&message_pipe0, &message_pipe1);
  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoSetQuota(message_pipe0, kInvalidQuotaType, 0, nullptr));

  const MojoSetQuotaOptions kInvalidSetQuotaOptions = {0};
  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoSetQuota(message_pipe0, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, 0,
                         &kInvalidSetQuotaOptions));

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoQueryQuota(message_pipe0, kInvalidQuotaType, nullptr, &limit,
                           &usage));

  const MojoQueryQuotaOptions kInvalidQueryQuotaOptions = {0};
  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoQueryQuota(message_pipe0, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH,
                           &kInvalidQueryQuotaOptions, &limit, &usage));

  MojoClose(message_pipe0);
  MojoClose(message_pipe1);

  MojoHandle producer, consumer;
  CreateDataPipe(&producer, &consumer, 1);
  EXPECT_EQ(
      MOJO_RESULT_INVALID_ARGUMENT,
      MojoSetQuota(producer, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, 0, nullptr));
  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoSetQuota(producer, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE, 0,
                         nullptr));
  EXPECT_EQ(
      MOJO_RESULT_INVALID_ARGUMENT,
      MojoSetQuota(consumer, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, 0, nullptr));
  EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
            MojoSetQuota(consumer, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE, 0,
                         nullptr));
  MojoClose(producer);
  MojoClose(consumer);
}

TEST_F(QuotaTest, BasicReceiveQueueLength) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(MOJO_QUOTA_LIMIT_NONE, limit);
  EXPECT_EQ(0u, usage);

  const uint64_t kTestLimit = 42;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, kTestLimit,
                         nullptr));

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kTestLimit, limit);
  EXPECT_EQ(0u, usage);

  const std::string kTestMessage = "doot";
  WriteMessage(b, kTestMessage);
  WaitForSignals(a, MOJO_HANDLE_SIGNAL_READABLE);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kTestLimit, limit);
  EXPECT_EQ(1u, usage);
  EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
  EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}

TEST_F(QuotaTest, BasicReceiveQueueMemorySize) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                           nullptr, &limit, &usage));
  EXPECT_EQ(MOJO_QUOTA_LIMIT_NONE, limit);
  EXPECT_EQ(0u, usage);

  const uint64_t kTestLimit = 42;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                         kTestLimit, nullptr));

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                           nullptr, &limit, &usage));
  EXPECT_EQ(kTestLimit, limit);
  EXPECT_EQ(0u, usage);

  const std::string kTestMessage = "doot";
  WriteMessage(b, kTestMessage);
  WaitForSignals(a, MOJO_HANDLE_SIGNAL_READABLE);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                           nullptr, &limit, &usage));
  EXPECT_EQ(kTestLimit, limit);
  EXPECT_EQ(usage, kTestMessage.size());

  MojoClose(a);
  MojoClose(b);
}

TEST_F(QuotaTest, ReceiveQueueLengthLimitExceeded) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  const uint64_t kMaxMessages = 1;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, kMaxMessages,
                         nullptr));

  MojoHandleSignalsState signals;
  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  const std::string kTestMessage = "this message is lit, fam";
  WriteMessage(a, kTestMessage);
  WaitForSignals(b, MOJO_HANDLE_SIGNAL_READABLE);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxMessages, limit);
  EXPECT_EQ(1u, usage);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  // Push the endpoint over quota and ensure that it signals accordingly.
  WriteMessage(a, kTestMessage);
  WaitForSignals(b, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_TRUE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxMessages, limit);
  EXPECT_EQ(2u, usage);

  // Read a message and wait for QUOTA_EXCEEDED to go back low.
  EXPECT_EQ(kTestMessage, ReadMessage(b));
  WaitForSignals(b, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
                 MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxMessages, limit);
  EXPECT_EQ(1u, usage);

  MojoClose(a);
  MojoClose(b);
}

TEST_F(QuotaTest, ReceiveQueueMemorySizeLimitExceeded) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  const uint64_t kMaxMessageBytes = 6;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                         kMaxMessageBytes, nullptr));

  MojoHandleSignalsState signals;
  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  const std::string kTestMessage = "four";
  WriteMessage(a, kTestMessage);
  WaitForSignals(b, MOJO_HANDLE_SIGNAL_READABLE);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                           nullptr, &limit, &usage));
  EXPECT_EQ(kMaxMessageBytes, limit);
  EXPECT_EQ(kTestMessage.size(), usage);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  // Push the endpoint over quota and ensure that it signals accordingly.
  WriteMessage(a, kTestMessage);
  WaitForSignals(b, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_TRUE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                           nullptr, &limit, &usage));
  EXPECT_EQ(kMaxMessageBytes, limit);
  EXPECT_EQ(kTestMessage.size() * 2, usage);

  // Read a message and wait for QUOTA_EXCEEDED to go back low.
  EXPECT_EQ(kTestMessage, ReadMessage(b));
  WaitForSignals(b, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
                 MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE,
                           nullptr, &limit, &usage));
  EXPECT_EQ(kMaxMessageBytes, limit);
  EXPECT_EQ(kTestMessage.size(), usage);

  MojoClose(a);
  MojoClose(b);
}

TEST_F(QuotaTest, BasicUnreadMessageCount) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, nullptr,
                           &limit, &usage));
  EXPECT_EQ(MOJO_QUOTA_LIMIT_NONE, limit);
  EXPECT_EQ(0u, usage);

  const uint64_t kTestLimit = 42;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, kTestLimit,
                         nullptr));

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kTestLimit, limit);
  EXPECT_EQ(0u, usage);

  const std::string kTestMessage = "doot";
  WriteMessage(a, kTestMessage);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kTestLimit, limit);
  EXPECT_EQ(usage, 1u);

  MojoClose(a);
  MojoClose(b);
}

TEST_F(QuotaTest, UnreadMessageCountLimitExceeded) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  const uint64_t kMaxUnreadMessageCount = 4;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT,
                         kMaxUnreadMessageCount, nullptr));

  MojoHandleSignalsState signals;
  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  const std::string kTestMessage = "msg";
  WriteMessage(a, kTestMessage);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxUnreadMessageCount, limit);
  EXPECT_EQ(1u, usage);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  // Push the endpoint over quota and ensure that it signals accordingly.
  WriteMessage(a, kTestMessage);
  WriteMessage(a, kTestMessage);
  WriteMessage(a, kTestMessage);
  WriteMessage(a, kTestMessage);
  WaitForSignals(a, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &signals));
  EXPECT_TRUE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxUnreadMessageCount, limit);
  EXPECT_EQ(5u, usage);

  // Read the messages and wait for QUOTA_EXCEEDED on the other end to go back
  // low. There's some hysteresis in the signaling, so it's not sufficient to
  // read a single packet, but reading below the quota size should work.
  EXPECT_EQ(kTestMessage, ReadMessage(b));
  EXPECT_EQ(kTestMessage, ReadMessage(b));
  EXPECT_EQ(kTestMessage, ReadMessage(b));
  EXPECT_EQ(kTestMessage, ReadMessage(b));
  WaitForSignals(a, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
                 MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(a, MOJO_QUOTA_TYPE_UNREAD_MESSAGE_COUNT, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxUnreadMessageCount, limit);
  EXPECT_LE(1u, usage);

  MojoClose(a);
  MojoClose(b);
}

TEST_F(QuotaTest, TrapQuotaExceeded) {
  if (IsMojoIpczEnabled()) {
    GTEST_SKIP() << "Mojo quota APIs are not supported by MojoIpcz.";
  }

  // Simple sanity check to verify that QUOTA_EXCEEDED signals can be trapped
  // like any other signals.

  MojoHandle a, b;
  CreateMessagePipe(&a, &b);

  const uint64_t kMaxMessages = 42;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoSetQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, kMaxMessages,
                         nullptr));

  bool signal_event_fired = false;
  MojoHandle quota_trap;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoCreateTrap(&QuotaExceededEventHandler, nullptr, "a_trap));
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoAddTrigger(quota_trap, b, MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
                           MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
                           reinterpret_cast<uintptr_t>(&signal_event_fired),
                           nullptr));
  EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(quota_trap, nullptr, nullptr, nullptr));

  const std::string kTestMessage("sup");
  for (uint64_t i = 0; i < kMaxMessages; ++i)
    WriteMessage(a, kTestMessage);

  // We're at quota but not yet over.
  MojoHandleSignalsState signals;
  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_FALSE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);
  EXPECT_FALSE(signal_event_fired);

  // Push over quota. The event handler should be invoked before this returns.
  WriteMessage(a, kTestMessage);
  EXPECT_TRUE(signal_event_fired);

  EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &signals));
  EXPECT_TRUE(signals.satisfied_signals & MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);

  uint64_t limit = 0;
  uint64_t usage = 0;
  EXPECT_EQ(MOJO_RESULT_OK,
            MojoQueryQuota(b, MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH, nullptr,
                           &limit, &usage));
  EXPECT_EQ(kMaxMessages, limit);
  EXPECT_EQ(kMaxMessages + 1, usage);

  MojoClose(quota_trap);
  MojoClose(a);
  MojoClose(b);
}

}  // namespace
}  // namespace core
}  // namespace mojo