summaryrefslogtreecommitdiff
path: root/libviengoos/t-rpc.c
blob: c2d9614ded22854e8e4b15cd0735b90546742db5 (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
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>

char *program_name = "t-rpc";
int output_debug = 1;

#define RPC_STUB_PREFIX rpc
#define RPC_ID_PREFIX RPC

#include <viengoos/rpc.h>

/* Exception message ids.  */
enum
  {
    RPC_noargs = 0x1ABE100,
    RPC_onein,
    RPC_oneout,
    RPC_onlyin,
    RPC_onlyout,
    RPC_mix,
    RPC_caps,
  };

struct foo
{
  int a;
  char b;
};

RPC(noargs, 0, 0, 0)
RPC(onein, 1, 0, 0, uint32_t, arg)
RPC(oneout, 0, 1, 0, uint32_t, arg)
RPC(onlyin, 4, 0, 0, uint32_t, arg, uint32_t, idx, struct foo, foo, bool, p)
RPC(onlyout, 0, 4, 0, uint32_t, arg, uint32_t, idx, struct foo, foo, bool, p)
RPC(mix, 2, 3, 0, uint32_t, arg, uint32_t, idx,
    struct foo, foo, bool, p, int, i)
RPC(caps, 3, 2, 2,
    /* In: */
    int, i, cap_t, c, struct foo, foo,
    /* Out: */
    int, a, int, b, cap_t, x, cap_t, y)

#undef RPC_STUB_PREFIX
#undef RPC_ID_PREFIX

int
main (int argc, char *argv[])
{
  printf ("Checking RPC... ");

  error_t err;
  struct vg_message *msg;


#define REPLY VG_ADDR (0x1000, VG_ADDR_BITS - 12)
  vg_addr_t reply = REPLY;

  msg = malloc (sizeof (*msg));
  rpc_noargs_send_marshal (msg, REPLY);
  err = rpc_noargs_send_unmarshal (msg, &reply);
  assert (! err);
  assert (VG_ADDR_EQ (reply, REPLY));
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_noargs_reply_marshal (msg);
  err = rpc_noargs_reply_unmarshal (msg);
  assert (err == 0);
  free (msg);


  msg = malloc (sizeof (*msg));
#define VALUE 0xfde8963a
  uint32_t arg = VALUE;
  uint32_t arg_out;

  rpc_onein_send_marshal (msg, arg, REPLY);
  err = rpc_onein_send_unmarshal (msg, &arg_out, &reply);
  assert (! err);
  assert (arg_out == VALUE);
  assert (VG_ADDR_EQ (reply, REPLY));
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_onein_reply_marshal (msg);
  err = rpc_onein_reply_unmarshal (msg);
  assert (! err);
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_oneout_send_marshal (msg, REPLY);
  err = rpc_oneout_send_unmarshal (msg, &reply);
  assert (! err);
  assert (VG_ADDR_EQ (reply, REPLY));
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_oneout_reply_marshal (msg, arg);
  err = rpc_oneout_reply_unmarshal (msg, &arg_out);
  assert (! err);
  assert (arg_out == VALUE);
  free (msg);

  msg = malloc (sizeof (*msg));

  struct foo foo;
  foo.a = 1 << 31;
  foo.b = 'l';
  uint32_t idx_out;
  struct foo foo_out;
  bool p_out;

  rpc_onlyin_send_marshal (msg, 0x1234567, 0xABC, foo, true, REPLY);
  err = rpc_onlyin_send_unmarshal (msg, &arg_out, &idx_out, &foo_out, &p_out,
				   &reply);
  assert (! err);
  assert (arg_out == 0x1234567);
  assert (idx_out == 0xABC);
  assert (foo_out.a == foo.a);
  assert (foo_out.b == foo.b);
  assert (p_out == true);
  assert (VG_ADDR_EQ (reply, REPLY));
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_onlyin_reply_marshal (msg);
  err = rpc_onlyin_reply_unmarshal (msg);
  assert (! err);
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_onlyout_send_marshal (msg, REPLY);
  err = rpc_onlyout_send_unmarshal (msg, &reply);
  assert (! err);
  assert (VG_ADDR_EQ (reply, REPLY));
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_onlyout_reply_marshal (msg, 0x1234567, 321, foo, true);
  err = rpc_onlyout_reply_unmarshal (msg, &arg_out, &idx_out,
				     &foo_out, &p_out);
  assert (! err);
  assert (arg_out == 0x1234567);
  assert (idx_out == 321);
  assert (foo_out.a == foo.a);
  assert (foo_out.b == foo.b);
  assert (p_out == true);
  free (msg);


  msg = malloc (sizeof (*msg));
  rpc_mix_send_marshal (msg, arg, 456789, REPLY);
  err = rpc_mix_send_unmarshal (msg, &arg_out, &idx_out, &reply);
  assert (! err);
  assert (arg_out == arg);
  assert (idx_out == 456789);
  assert (VG_ADDR_EQ (reply, REPLY));
  free (msg);

  msg = malloc (sizeof (*msg));
  int i_out = 0;
  rpc_mix_reply_marshal (msg, foo, false, 4200042);
  err = rpc_mix_reply_unmarshal (msg, &foo_out, &p_out, &i_out);
  assert (! err);
  assert (foo_out.a == foo.a);
  assert (foo_out.b == foo.b);
  assert (p_out == false);
  assert (i_out == 4200042);
  free (msg);

  msg = malloc (sizeof (*msg));
  rpc_caps_send_marshal (msg, 54, VG_ADDR (1, VG_ADDR_BITS), foo, REPLY);
  vg_addr_t addr;
  err = rpc_caps_send_unmarshal (msg, &i_out, &addr, &foo_out, &reply);
  assert (! err);
  assert (i_out == 54);
  assert (VG_ADDR_EQ (addr, VG_ADDR (1, VG_ADDR_BITS)));
  assert (foo_out.a == foo.a);
  assert (foo_out.b == foo.b);
  free (msg);

  printf ("ok\n");
  return 0;
}