File: CeRapiInvoke.cc

package info (click to toggle)
librapi2 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,820 kB
  • ctags: 1,474
  • sloc: ansic: 14,036; sh: 10,572; cpp: 851; python: 338; makefile: 226
file content (292 lines) | stat: -rw-r--r-- 5,914 bytes parent folder | download | duplicates (2)
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
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <stdint.h>
#include <string.h>

#include "rapi.h"
#include "../../tools/pcommon.h"

using namespace std;
using namespace synce;

#define ANYFILE_BUFFER_SIZE (16*1024)

// copied from pcp.c
static bool anyfile_copy(const char* source_ascii, const char* dest_ascii, const char* name, size_t* bytes_copied)/*{{{*/
{
	bool success = false;
	size_t bytes_read;
	size_t bytes_written;
	uint8_t* buffer = NULL;
	AnyFile* source = NULL;
	AnyFile* dest   = NULL;

	if (!(buffer = (uint8_t*)malloc(ANYFILE_BUFFER_SIZE)))
	{
		fprintf(stderr, "%s: Failed to allocate buffer of size %i\n", name, ANYFILE_BUFFER_SIZE);
		goto exit;
	}

	if (!(source = anyfile_open(source_ascii, READ)))
	{
		fprintf(stderr, "%s: Failed to open source file '%s'\n", name, source_ascii);
		goto exit;
	}

	if (!(dest = anyfile_open(dest_ascii, WRITE)))
	{
		fprintf(stderr, "%s: Failed to open destination file '%s'\n", name, dest_ascii);
		goto exit;
	}

	for(;;)
	{
		if (!anyfile_read(source, buffer, ANYFILE_BUFFER_SIZE, &bytes_read))
		{
			fprintf(stderr, "%s: Failed to read from source file '%s'\n", name, source_ascii);
			goto exit;
		}

		if (0 == bytes_read)
		{
			/* End of file */
			break;
		}

		if (!anyfile_write(dest, buffer, bytes_read, &bytes_written))
		{
			fprintf(stderr, "%s: Failed to write to destination file '%s'\n", name, dest_ascii);
			goto exit;
		}

		if (bytes_written != bytes_read)
		{
			fprintf(stderr, "%s: Only wrote %zi bytes of %zi to destination file '%s'\n", name, 
					bytes_written, bytes_read, dest_ascii);
			goto exit;
		}

		*bytes_copied += bytes_written;
	}

	success = true;

exit:
	if (buffer)
		free(buffer);
	
	if (source)
	{
		anyfile_close(source);
		free(source);
	}

	if (dest)
	{
		anyfile_close(dest);
		free(dest);
	}

	return success;
}/*}}}*/

#define BASENAME    "invokeme.dll"
#define LOCAL_DLL   "./dll/" BASENAME
#define REMOTE_DLL  "/windows/" BASENAME

static bool test_ping_result()
{
  uint32_t expected = 0x12345678;
  uint32_t le_expected = htole32(expected);
  HRESULT hr = rapi_invoke(
      REMOTE_DLL,
      "PingResult",
      sizeof(le_expected), (BYTE*)&le_expected,
      NULL, NULL,
      NULL,
      0);
  return hr == (HRESULT)expected;
}

#define PING_BUFFER_SIZE  1000

static bool test_ping_buffer()
{
  BYTE buffer[PING_BUFFER_SIZE];
  DWORD output_size;
  BYTE* output_buffer;
  
  HRESULT hr = rapi_invoke(
      REMOTE_DLL,
      "PingBuffer",
      PING_BUFFER_SIZE, buffer,
      &output_size, &output_buffer,
      NULL,
      0);
  if (hr != S_OK)
    return false;

  bool success = 
    (PING_BUFFER_SIZE == output_size) &&
    memcmp(buffer, output_buffer, PING_BUFFER_SIZE) == 0;

  CeRapiFreeBuffer(output_buffer);

  return success;
}

static bool test_ping_stream()
{
  bool success = false;

  IRAPIStream* stream = NULL;

  DWORD le_size = htole32(PING_BUFFER_SIZE);
  HRESULT hr = rapi_invoke(
      REMOTE_DLL,
      "PingStream",
      sizeof(le_size), (BYTE*)&le_size,
      0, NULL,
      &stream,
      0);
  if (hr != S_OK)
    return false;

  cerr << "a ";
  
  ULONG count;

  BYTE input_buffer[PING_BUFFER_SIZE];
  hr = IRAPIStream_Write(stream, input_buffer, PING_BUFFER_SIZE, &count);
  if (FAILED(hr) || count != PING_BUFFER_SIZE)
  {
    cerr << "IRAPIStream_Write failed" << endl;
    goto exit;
  }
  cerr << "b ";

  BYTE output_buffer[PING_BUFFER_SIZE];
  hr = IRAPIStream_Read(stream, output_buffer, PING_BUFFER_SIZE, &count);
  if (FAILED(hr) || count != PING_BUFFER_SIZE)
  {
    cerr << "IRAPIStream_Read failed" << endl;
    goto exit;
  }
  cerr << "c ";

  success = 
    memcmp(input_buffer, output_buffer, PING_BUFFER_SIZE) == 0;

exit:
  IRAPIStream_Release(stream);
  return success;
}

static bool test_last_error()
{
  DWORD last_error = ERROR_INVALID_PARAMETER;
  HRESULT hr;
  hr = rapi_invoke(
      REMOTE_DLL,
      "TestLastError",
      sizeof(last_error), (BYTE*)&last_error,
      NULL, NULL,
      NULL,
      0);
  if (CeRapiGetLastError() != last_error)
  {
    cerr << "LastError: expected=0x" << hex << last_error <<
      " recevied=0x" << hex << CeRapiGetLastError() << endl;
    return false;
  }
  return true;
}


int main()
{
  int error_count = 0;
  WCHAR* remote_dll = wstr_from_ascii(REMOTE_DLL);

  HRESULT hr = CeRapiInit();
  if (FAILED(hr))
  {
    cerr << "CeRapiInit failed: " << synce_strerror(hr) << endl;
    ++error_count;
    goto exit;
  }

  size_t bytes_copied;
  if (!anyfile_copy(
        LOCAL_DLL,
        ":" REMOTE_DLL,
        BASENAME,
        &bytes_copied))
  {
    cerr << "Failed to copy DLL to device:" << synce_strerror(hr) << endl;
    ++error_count;
    goto exit;
  }

  cout << "Testing call to non-existent function... ";
  hr = rapi_invoke(
      REMOTE_DLL,
      "ThisMethodShouldNotExist",
      0, NULL,
      NULL, NULL,
      NULL,
      0);
  if (SUCCEEDED(hr))
  {
    cerr << "CeRapiInvoke succeeded to call non-existant method?!" << endl;
    ++error_count;
  } else
    cout << "ok" << endl;

  cout << "Testing setting error code... ";
  if (!test_last_error())
  {
    cerr << "TestLastError failed" << endl;
    ++error_count;
  } else
    cout << "ok" << endl;

  cout << "Testing setting return code... ";
  if (!test_ping_result())
  {
    cerr << "PingResult failed" << endl;
    ++error_count;
  } else
    cout << "ok" << endl;

  cout << "Testing \"ping\" by buffer... ";
  if (!test_ping_buffer())
  {
    cerr << "PingBuffer failed" << endl;
    ++error_count;
  }
    cout << "ok" << endl;

  cout << "Testing \"ping\" by stream... ";
  if (!test_ping_stream())
  {
    cerr << "PingStream failed" << endl;
    ++error_count;
  }
    cout << "ok" << endl;

exit:
#if 1
  if (!CeDeleteFile(remote_dll))
  {
    cerr << "CeDeleteFile failed." << endl;
    ++error_count;
  }
#endif

  CeRapiUninit();
  wstr_free_string(remote_dll);
  return error_count;
}