summaryrefslogtreecommitdiff
path: root/open-vm-tools/tests/testDebug/testDebug.c
blob: 331b63c7c9a75e72a98108fd14e81dad39a9c5f5 (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
/*********************************************************
 * Copyright (C) 2008-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @file testDebug.c
 *
 * A simple debug plugin that validates the messages sent by the service after
 * a "reset" is received, and also interacts with the test plugin to test the
 * functions provided by the service.
 */

#define G_LOG_DOMAIN "testDebug"
#include <glib-object.h>
#include <CUnit/CUnit.h>

#include "util.h"
#include "testData.h"
#include "xdrutil.h"
#include "vmware/guestrpc/tclodefs.h"
#include "vmware/tools/rpcdebug.h"

static gboolean
TestDebugValidateReset(RpcInData *data, gboolean ret);

static gboolean
TestDebugValidateUnknown(RpcInData *data, gboolean ret);

static gboolean
TestDebugValidateRpc3(RpcInData *data, gboolean ret);

#define SET_OPTION_TEST ("Set_Option " TOOLSOPTION_BROADCASTIP " 1")

/** RPC messages injected into the application using RpcDebug_SendNext(). */
static RpcDebugMsgMapping gRpcMessages[] = {
   { "reset", sizeof "reset", TestDebugValidateReset, FALSE },
   { "ping", sizeof "ping", NULL, FALSE },
   { "Capabilities_Register", sizeof "Capabilities_Register", NULL, FALSE },
   { "test.rpcin.unknown", sizeof "test.rpcin.unknown", TestDebugValidateUnknown, FALSE },
   /* This one is initialized manually, since it contains dynamic data. */
   { NULL, 0, NULL, TRUE },
   { "test.rpcin.msg2", sizeof "test.rpcin.msg2", NULL, FALSE },
   { "test.rpcin.msg3", sizeof "test.rpcin.msg3", TestDebugValidateRpc3, FALSE },
   { SET_OPTION_TEST, sizeof SET_OPTION_TEST, NULL, FALSE },
   { "Capabilities_Register", sizeof "Capabilities_Register", NULL, FALSE },
   /* NULL terminator. */
   { NULL, 0, NULL, FALSE }
};

static gboolean gSignalReceived = FALSE;
static ToolsAppCtx *gCtx;


/**
 * Handles a "test-signal" sent by the test plugin. Just sets a static variable
 * that will be checked by TestDebugReceiveRpc1 to make sure custom signals are
 * working.
 *
 * @param[in]  src      Unused.
 * @param[in]  data     Unused.
 */

static void
TestDebugHandleSignal(gpointer src,
                      gpointer data)
{
   g_debug("Received test signal.\n");
   gSignalReceived = TRUE;
}


/**
 * Validates the response from a "reset".
 *
 * @param[in]  data     RPC request data.
 * @param[in]  ret      Return value from RPC handler.
 *
 * @return @a ret.
 */

static gboolean
TestDebugValidateReset(RpcInData *data,
                       gboolean ret)
{
   RPCDEBUG_ASSERT(data->result != NULL, FALSE);
   CU_ASSERT_STRING_EQUAL(data->result, "ATR debug");
   return (gboolean) ret;
}


/**
 * Validates a "test.rpcout.msg1" message sent by the test plugin. This message
 * is sent when the plugin receives a "test.rpcin.msg1" RPC, and contains an
 * XDR-encoded TestPluginData struct.
 *
 * @param[in]  data        Incoming data.
 * @param[in]  dataLen     Size of incoming data.
 * @param[out] result      Result sent back to the application.
 * @param[out] resultLen   Length of result.
 *
 * @return TRUE (asserts on failure).
 */

static gboolean
TestDebugReceiveRpc1(char *data,
                     size_t dataLen,
                     char **result,
                     size_t *resultLen)
{
   TestPluginData *details = (TestPluginData *) data;

   CU_ASSERT(gSignalReceived);
   CU_ASSERT_STRING_EQUAL(details->data, "rpc1test");
   CU_ASSERT_EQUAL(details->f_int, 1357);
   CU_ASSERT(details->f_bool);

   return TRUE;
}


/**
 * Validates the response of the "msg3" RPC.
 *
 * @param[in] data   RPC data.
 * @param[in] ret    RPC result.
 *
 * @return Whether the RPC succeded.
 */

static gboolean
TestDebugValidateRpc3(RpcInData *data,
                      gboolean ret)
{
   TestPluginData pdata = { NULL, };

   CU_ASSERT(XdrUtil_Deserialize(data->result, data->resultLen,
                                 xdr_TestPluginData, &pdata));

   CU_ASSERT_STRING_EQUAL(pdata.data, "Hello World!");
   CU_ASSERT_EQUAL(pdata.f_int, 8642);
   CU_ASSERT(pdata.f_bool);

   VMX_XDR_FREE(xdr_TestPluginData, &pdata);
   return ret;
}


/**
 * Validates a "version" message sent during capability registration.
 * No validation is actually done - the message is just printed.
 *
 * @param[in]  data        Incoming data.
 * @param[in]  dataLen     Size of incoming data.
 * @param[out] result      Result sent back to the application.
 * @param[out] resultLen   Length of result.
 *
 * @return TRUE.
 */

static gboolean
TestDebugReceiveVersion(char *data,
                        size_t dataLen,
                        char **result,
                        size_t *resultLen)
{
   g_debug("Received tools version message: %s\n", data);
   RpcDebug_SetResult("", result, resultLen);
   return TRUE;
}


/**
 * Validates the results for an unknown RPC sent to the guest.
 *
 * @param[in]  data     RPC request data.
 * @param[in]  ret      Return value from RPC handler.
 *
 * @return The opposite of @a ret.
 */

static gboolean
TestDebugValidateUnknown(RpcInData *data,
                         gboolean ret)
{
   CU_ASSERT_STRING_EQUAL(data->result, "Unknown Command");
   return !ret;
}


/**
 * Populates the RPC request data with the next message in the queue.
 *
 * @param[in]  rpcdata     Data for the injected RPC request data.
 *
 * @return TRUE if sending messages, FALSE if no more messages to be sent.
 */

static gboolean
TestDebugSendNext(RpcDebugMsgMapping *rpcdata)
{
   static RpcDebugMsgList msgList = { gRpcMessages, 0 };
   if (!RpcDebug_SendNext(rpcdata, &msgList)) {
      /*
       * Test for bug 391553: send two resets in sequence without
       * pumping the main loop. The channel should handle the second
       * reset successfully instead of asserting.
       */
      int i;
      for (i = 0; i < 2; i++) {
         RpcInData data;
         memset(&data, 0, sizeof data);
         data.clientData = gCtx->rpc;
         data.appCtx = gCtx;
         data.args = "reset";
         data.argsSize = sizeof "reset";

         g_debug("reset test %d\n", i + 1);
         RpcChannel_Dispatch(&data);
      }
      return FALSE;
   }
   return TRUE;
}


/**
 * Returns the debug plugin's registration data.
 *
 * @param[in]  ctx      The application context.
 *
 * @return The application data.
 */

TOOLS_MODULE_EXPORT RpcDebugPlugin *
RpcDebugOnLoad(ToolsAppCtx *ctx)
{
   static RpcDebugRecvMapping recvFns[] = {
      { "tools.set.version", TestDebugReceiveVersion, NULL, 0 },
      { "test.rpcout.msg1", TestDebugReceiveRpc1,
        xdr_TestPluginData, sizeof (TestPluginData) },
      { NULL, NULL }
   };
   static ToolsPluginData pluginData = {
      "testDebug",
      NULL,
      NULL,
      NULL,
   };
   static RpcDebugPlugin regData = {
      recvFns,
      NULL,
      TestDebugSendNext,
      NULL,
      &pluginData,
   };

   /* Standard plugin interface, used to listen for signals. */
   {
      ToolsPluginSignalCb sigs[] = {
         { "test-signal", TestDebugHandleSignal, NULL },
      };
      ToolsAppReg regs[] = {
         { TOOLS_APP_SIGNALS, VMTOOLS_WRAP_ARRAY(sigs) },
      };

      pluginData.regs = VMTOOLS_WRAP_ARRAY(regs);
   }

   /* Initialize the paylod of the "test.rpcin.msg1" RPC. */
   {
      TestPluginData testdata;
      testdata.data = "rpc1test";
      testdata.f_int = 1357;
      testdata.f_bool = TRUE;

      /* Build the command for the "test.rpcin.msg1" RPC. */
      if (!RpcChannel_BuildXdrCommand("test.rpcin.msg1",
                                      xdr_TestPluginData,
                                      &testdata,
                                      &gRpcMessages[4].message,
                                      &gRpcMessages[4].messageLen)) {
         g_error("Failed to create test.rpcin.msg1 command.\n");
      }
   }

   gCtx = ctx;
   return &regData;
}