toaruos/userspace/tests/test-pex.c

57 lines
1.4 KiB
C
Raw Normal View History

2014-04-02 11:26:15 +04:00
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
2014-04-03 09:50:32 +04:00
#include "lib/testing.h"
#include "lib/pex.h"
2014-04-02 11:26:15 +04:00
int main(int argc, char * argv[]) {
2014-04-03 09:50:32 +04:00
FILE * server = pex_bind("testex");
FILE * client = pex_connect("testex");
2014-04-02 11:26:15 +04:00
2014-04-03 09:50:32 +04:00
char * foo = "Hello World!";
pex_reply(client, strlen(foo)+1, foo);
2014-04-02 11:26:15 +04:00
2014-04-03 09:50:32 +04:00
pex_packet_t * p = calloc(PACKET_SIZE, 1);
pex_listen(server, p);
2014-04-02 11:26:15 +04:00
2014-04-03 09:50:32 +04:00
unsigned int client_id = p->source;
2014-04-02 11:42:35 +04:00
2014-04-03 09:50:32 +04:00
if (!strcmp("Hello World!", p->data)) {
PASS("Client-server message received.");
} else {
FAIL("Expected message of 'Hello World!', got %s", p->data);
2014-04-02 11:42:35 +04:00
}
2014-04-03 09:50:32 +04:00
free(p);
2014-04-02 11:42:35 +04:00
2014-04-03 09:50:32 +04:00
char * foo2 = "Hello everyone!\n";
pex_broadcast(server, strlen(foo2)+1, foo2);
2014-04-02 11:42:35 +04:00
2014-04-03 09:50:32 +04:00
char out[MAX_PACKET_SIZE];
size_t size = pex_recv(client, out);
if (!strcmp("Hello everyone!\n", out)) {
PASS("Server broadcast received.");
} else {
FAIL("Expected message of 'Hello everyone\\n!', got %s", out);
2014-04-02 11:42:35 +04:00
}
2014-04-03 09:50:32 +04:00
char * foo3 = malloc(MAX_PACKET_SIZE);
memset(foo3, 0x42, MAX_PACKET_SIZE);
for (int i = 0; i < 3; ++i) {
size_t size = pex_send(server, client_id, MAX_PACKET_SIZE, foo3);
if (size != MAX_PACKET_SIZE) FAIL("Bad packet size (%d)", size);
else PASS(".");
}
2014-04-02 11:26:15 +04:00
2014-04-03 09:50:32 +04:00
size_t tmp_size = pex_send(server, client_id, MAX_PACKET_SIZE, foo3);
if (tmp_size != -1) FAIL("Bad packet size (%d)", tmp_size);
else PASS("Packet dropped successfully.");
2014-04-02 11:26:15 +04:00
fclose(client);
fclose(server);
return 0;
}