test/terminal_crosslink: API transition

Issue #1987
This commit is contained in:
Christian Prochaska 2017-01-17 12:02:59 +01:00 committed by Norman Feske
parent 3e8cd442a3
commit 9f7a4feab4

View File

@ -5,35 +5,40 @@
*/ */
/* /*
* Copyright (C) 2012-2013 Genode Labs GmbH * Copyright (C) 2012-2017 Genode Labs GmbH
* *
* This file is part of the Genode OS framework, which is distributed * This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2. * under the terms of the GNU General Public License version 2.
*/ */
#include <base/printf.h> #include <base/component.h>
#include <base/signal.h>
#include <base/sleep.h> #include <base/sleep.h>
#include <base/thread.h> #include <base/thread.h>
#include <terminal_session/connection.h> #include <terminal_session/connection.h>
using namespace Genode; namespace Test_terminal_crosslink {
using namespace Genode;
class Partner;
class Client;
class Server;
struct Main;
enum {
STACK_SIZE = sizeof(addr_t)*1024,
TEST_DATA_SIZE = 4097,
READ_BUFFER_SIZE = 8192
};
static const char *client_text = "Hello from client.";
static const char *server_text = "Hello from server, too.";
static char test_data[TEST_DATA_SIZE];
}
enum { class Test_terminal_crosslink::Partner : public Thread
STACK_SIZE = sizeof(addr_t)*1024,
SERVICE_BUFFER_SIZE = 4096,
TEST_DATA_SIZE = 4097,
READ_BUFFER_SIZE = 8192
};
static const char *client_text = "Hello from client.";
static const char *server_text = "Hello from server, too.";
static char test_data[TEST_DATA_SIZE];
class Partner : public Thread_deprecated<STACK_SIZE>
{ {
protected: protected:
@ -44,111 +49,129 @@ class Partner : public Thread_deprecated<STACK_SIZE>
Signal_receiver _sig_rec; Signal_receiver _sig_rec;
Signal_context _sig_ctx; Signal_context _sig_ctx;
void _write_all(void const *buf, Genode::size_t num_bytes)
{
Genode::size_t written_bytes = 0;
char const * const src = (char const *)buf;
while (written_bytes < num_bytes) {
written_bytes += _terminal.write(&src[written_bytes],
num_bytes - written_bytes);
}
}
void _read_all(void *buf, Genode::size_t buf_size)
{
Genode::size_t read_bytes = 0;
char * const dst = (char *)buf;
while (read_bytes < buf_size) {
_sig_rec.wait_for_signal();
read_bytes += _terminal.read(&dst[read_bytes],
buf_size - read_bytes);
}
}
public: public:
Partner(const char *name) : Thread_deprecated<STACK_SIZE>(name) Partner(Env &env, char const *name)
: Thread(env, name, STACK_SIZE),
_terminal(env)
{ {
_terminal.read_avail_sigh(_sig_rec.manage(&_sig_ctx)); _terminal.read_avail_sigh(_sig_rec.manage(&_sig_ctx));
} }
}; };
class Client : public Partner class Test_terminal_crosslink::Client : public Partner
{ {
public: public:
Client() : Partner("client") { } Client(Env &env) : Partner(env, "client") { }
void entry() void entry()
{ {
printf("Short message test\n"); log("Short message test");
/* write client text */ /* write client text */
_terminal.write(client_text, strlen(client_text) + 1); _write_all(client_text, strlen(client_text) + 1);
/* read server text */ /* read server text */
_sig_rec.wait_for_signal(); _read_all(_read_buffer, strlen(server_text) + 1);
_terminal.read(_read_buffer, sizeof(_read_buffer));
printf("Client received: %s\n", _read_buffer); log("Client received: ", Cstring(_read_buffer));
if (strcmp(_read_buffer, server_text) != 0) { if (strcmp(_read_buffer, server_text) != 0) {
printf("Error: received data is not as expected\n"); error("Received data is not as expected");
sleep_forever(); sleep_forever();
} }
/* write test data */ /* write test data */
printf("Long message test\n"); log("Long message test");
memset(test_data, 5, sizeof(test_data)); memset(test_data, 5, sizeof(test_data));
_terminal.write(test_data, sizeof(test_data)); _write_all(test_data, sizeof(test_data));
} }
}; };
class Server : public Partner class Test_terminal_crosslink::Server : public Partner
{ {
public: public:
Server() : Partner("server") { } Server(Env &env) : Partner(env, "server") { }
void entry() void entry()
{ {
/* read client text */ /* read client text */
_sig_rec.wait_for_signal(); _read_all(_read_buffer, strlen(client_text) + 1);
_terminal.read(_read_buffer, sizeof(_read_buffer));
printf("Server received: %s\n", _read_buffer); log("Server received: ", Cstring(_read_buffer));
if (strcmp(_read_buffer, client_text) != 0) { if (strcmp(_read_buffer, client_text) != 0) {
printf("Error: received data is not as expected\n"); error("Received data is not as expected");
sleep_forever(); sleep_forever();
} }
/* write server text */ /* write server text */
_terminal.write(server_text, strlen(server_text) + 1); _write_all(server_text, strlen(server_text) + 1);
/* read test data */ /* read test data */
size_t num_read = 0; _read_all(_read_buffer, TEST_DATA_SIZE);
size_t num_read_total = 0;
do { for (size_t i = 0; i < TEST_DATA_SIZE; i++)
_sig_rec.wait_for_signal(); if (_read_buffer[i] != 5) {
num_read = _terminal.read(_read_buffer, sizeof(_read_buffer)); error("Received data is not as expected");
num_read_total += num_read; sleep_forever();
}
for (size_t i = 0; i < num_read; i++) log("Test succeeded");
if (_read_buffer[i] != 5) {
printf("Error: received data is not as expected\n");
sleep_forever();
}
} while(num_read == SERVICE_BUFFER_SIZE);
if (num_read_total != TEST_DATA_SIZE) {
printf("Error: received an unexpected number of bytes\n");
sleep_forever();
}
printf("Test succeeded\n");
} }
}; };
int main(int, char **) struct Test_terminal_crosslink::Main
{ {
static Server server; Env &_env;
static Client client;
server.start(); Server server { _env };
client.start(); Client client { _env };
sleep_forever(); Main(Env &env) : _env(env)
{
server.start();
client.start();
}
};
return 0;
void Component::construct(Genode::Env &env)
{
static Test_terminal_crosslink::Main main(env);
} }