From: <tw...@us...> - 2005-11-06 19:17:37
|
Update of /cvsroot/synce/librapi2/tests/CeRapiInvoke In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5389/tests/CeRapiInvoke Added Files: .cvsignore CeRapiInvoke.cc Makefile.am Log Message: Test for CeRapiInvoke --- NEW FILE: .cvsignore --- CeRapiInvoke .deps .libs Makefile Makefile.in --- NEW FILE: CeRapiInvoke.cc --- #include <cstdlib> #include <cstdio> #include <iostream> #include <stdint.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(char* source_ascii, 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 %i bytes of %i 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 = CeRapiInvokeA( REMOTE_DLL, "PingResult", sizeof(le_expected), (BYTE*)&le_expected, NULL, NULL, NULL, 0); return hr == expected; } #define PING_BUFFER_SIZE 1000 static bool test_ping_buffer() { BYTE buffer[PING_BUFFER_SIZE]; DWORD output_size; BYTE* output_buffer; HRESULT hr = CeRapiInvokeA( REMOTE_DLL, "PingBuffer", PING_BUFFER_SIZE, buffer, &output_size, &output_buffer, NULL, 0); if (FAILED(hr)) 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 = CeRapiInvokeA( REMOTE_DLL, "PingStream", sizeof(le_size), (BYTE*)&le_size, 0, NULL, &stream, 0); if (FAILED(hr)) return false; 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; } DWORD output_size; 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; } 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 = CeRapiInvokeA( 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." << 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." << endl; ++error_count; goto exit; } hr = CeRapiInvokeA( REMOTE_DLL, "ThisMethodShouldNotExist", 0, NULL, NULL, NULL, NULL, 0); if (SUCCEEDED(hr)) { cerr << "CeRapiInvoke succeeded to call non-existant method?!" << endl; ++error_count; } if (!test_last_error()) { cerr << "TestLastError failed" << endl; ++error_count; } if (!test_ping_result()) { cerr << "PingResult failed" << endl; ++error_count; } if (!test_ping_buffer()) { cerr << "PingBuffer failed" << endl; ++error_count; } if (!test_ping_stream()) { cerr << "PingStream failed" << endl; ++error_count; } exit: #if 0 if (!CeDeleteFile(remote_dll)) { cerr << "CeDeleteFile failed." << endl; ++error_count; } #endif CeRapiUninit(); wstr_free_string(remote_dll); return error_count; } --- NEW FILE: Makefile.am --- AM_CFLAGS = -Wall INCLUDES = -I../../src noinst_PROGRAMS = CeRapiInvoke CeRapiInvoke_SOURCES = CeRapiInvoke.cc ../../tools/pcommon.c CeRapiInvoke_LDADD = ../../src/librapi.la TESTS = CeRapiInvoke |