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

ipc / ipc_sync_message_unittest.cc [blame]

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Unit test to make sure that the serialization of synchronous IPC messages
// works.  This ensures that the macros and templates were defined correctly.
// Doesn't test the IPC channel mechanism.

#include "base/check_op.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

#define IPC_MESSAGE_IMPL
#include "ipc/ipc_sync_message_unittest.h"

namespace {

static IPC::Message* g_reply;

class TestMessageReceiver {
 public:

  void On_0_1(bool* out1) {
    *out1 = false;
  }

  void On_0_2(bool* out1, int* out2) {
    *out1 = true;
    *out2 = 2;
  }

  void On_0_3(bool* out1, int* out2, std::string* out3) {
    *out1 = false;
    *out2 = 3;
    *out3 = "0_3";
  }

  void On_1_1(int in1, bool* out1) {
    DCHECK_EQ(1, in1);
    *out1 = true;
  }

  void On_1_2(bool in1, bool* out1, int* out2) {
    DCHECK(!in1);
    *out1 = true;
    *out2 = 12;
  }

  void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
    DCHECK_EQ(3, in1);
    *out1 = "1_3";
    *out2 = 13;
    *out3 = false;
  }

  void On_2_1(int in1, bool in2, bool* out1) {
    DCHECK_EQ(1, in1);
    DCHECK(!in2);
    *out1 = true;
  }

  void On_2_2(bool in1, int in2, bool* out1, int* out2) {
    DCHECK(!in1);
    DCHECK_EQ(2, in2);
    *out1 = true;
    *out2 = 22;
  }

  void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
    DCHECK_EQ(3, in1);
    DCHECK(in2);
    *out1 = "2_3";
    *out2 = 23;
    *out3 = false;
  }

  void On_3_1(int in1, bool in2, const std::string& in3, bool* out1) {
    DCHECK_EQ(1, in1);
    DCHECK(!in2);
    DCHECK_EQ("3_1", in3);
    *out1 = true;
  }

  void On_3_2(const std::string& in1,
              bool in2,
              int in3,
              bool* out1,
              int* out2) {
    DCHECK_EQ("3_2", in1);
    DCHECK(!in2);
    DCHECK_EQ(2, in3);
    *out1 = true;
    *out2 = 32;
  }

  void On_3_3(int in1,
              const std::string& in2,
              bool in3,
              std::string* out1,
              int* out2,
              bool* out3) {
    DCHECK_EQ(3, in1);
    DCHECK_EQ("3_3", in2);
    DCHECK(in3);
    *out1 = "3_3";
    *out2 = 33;
    *out3 = false;
  }

  void On_3_4(bool in1,
              int in2,
              const std::string& in3,
              int* out1,
              bool* out2,
              std::string* out3,
              bool* out4) {
    DCHECK(in1);
    DCHECK_EQ(3, in2);
    DCHECK_EQ("3_4", in3);
    *out1 = 34;
    *out2 = true;
    *out3 = "3_4";
    *out4 = false;
  }

  bool Send(IPC::Message* message) {
    // gets the reply message, stash in global
    DCHECK(g_reply == NULL);
    g_reply = message;
    return true;
  }

  bool OnMessageReceived(const IPC::Message& msg) {
    IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
      IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
      IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
      IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
      IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
      IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
      IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
      IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
      IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
      IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
      IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
      IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
      IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
      IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
      IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
      IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
      IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
      IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
      IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
      IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
      IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
      IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
      IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
      IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
      IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
      IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
      IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
    IPC_END_MESSAGE_MAP()
    return true;
  }

};

void Send(IPC::SyncMessage* msg) {
  static TestMessageReceiver receiver;

  std::unique_ptr<IPC::MessageReplyDeserializer> reply_serializer =
      msg->TakeReplyDeserializer();
  DCHECK(reply_serializer);

  // "send" the message
  receiver.OnMessageReceived(*msg);
  delete msg;

  // get the reply message from the global, and deserialize the output
  // parameters into the output pointers.
  DCHECK(g_reply != NULL);
  bool result = reply_serializer->SerializeOutputParameters(*g_reply);
  DCHECK(result);
  delete g_reply;
  g_reply = NULL;
}

TEST(IPCSyncMessageTest, Main) {
  bool bool1 = true;
  int int1 = 0;
  std::string string1;

  Send(new Msg_C_0_1(&bool1));
  DCHECK(!bool1);

  Send(new Msg_C_0_2(&bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(2, int1);

  Send(new Msg_C_0_3(&bool1, &int1, &string1));
  DCHECK(!bool1);
  DCHECK_EQ(3, int1);
  DCHECK_EQ("0_3", string1);

  bool1 = false;
  Send(new Msg_C_1_1(1, &bool1));
  DCHECK(bool1);

  bool1 = false;
  Send(new Msg_C_1_2(false, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(12, int1);

  bool1 = true;
  Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
  DCHECK_EQ("1_3", string1);
  DCHECK_EQ(13, int1);
  DCHECK(!bool1);

  bool1 = false;
  Send(new Msg_C_2_1(1, false, &bool1));
  DCHECK(bool1);

  bool1 = false;
  Send(new Msg_C_2_2(false, 2, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(22, int1);

  bool1 = true;
  Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
  DCHECK_EQ("2_3", string1);
  DCHECK_EQ(23, int1);
  DCHECK(!bool1);

  bool1 = false;
  Send(new Msg_C_3_1(1, false, "3_1", &bool1));
  DCHECK(bool1);

  bool1 = false;
  Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(32, int1);

  bool1 = true;
  Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
  DCHECK_EQ("3_3", string1);
  DCHECK_EQ(33, int1);
  DCHECK(!bool1);

  bool1 = false;
  bool bool2 = true;
  Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
  DCHECK_EQ(34, int1);
  DCHECK(bool1);
  DCHECK_EQ("3_4", string1);
  DCHECK(!bool2);

  // Routed messages, just a copy of the above but with extra routing paramater
  Send(new Msg_R_0_1(0, &bool1));
  DCHECK(!bool1);

  Send(new Msg_R_0_2(0, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(2, int1);

  Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
  DCHECK(!bool1);
  DCHECK_EQ(3, int1);
  DCHECK_EQ("0_3", string1);

  bool1 = false;
  Send(new Msg_R_1_1(0, 1, &bool1));
  DCHECK(bool1);

  bool1 = false;
  Send(new Msg_R_1_2(0, false, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(12, int1);

  bool1 = true;
  Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
  DCHECK_EQ("1_3", string1);
  DCHECK_EQ(13, int1);
  DCHECK(!bool1);

  bool1 = false;
  Send(new Msg_R_2_1(0, 1, false, &bool1));
  DCHECK(bool1);

  bool1 = false;
  Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(22, int1);

  bool1 = true;
  Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
  DCHECK(!bool1);
  DCHECK_EQ("2_3", string1);
  DCHECK_EQ(23, int1);

  bool1 = false;
  Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
  DCHECK(bool1);

  bool1 = false;
  Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
  DCHECK(bool1);
  DCHECK_EQ(32, int1);

  bool1 = true;
  Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
  DCHECK_EQ("3_3", string1);
  DCHECK_EQ(33, int1);
  DCHECK(!bool1);
}

}  // namespace