Skip to content

Commit

Permalink
tests: import test infrastructure
Browse files Browse the repository at this point in the history
Add new b1-test infrastructure, modeled around the old test suite. Two
initial tests are added, which test the filesystem implementation and
basic peer operations.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
  • Loading branch information
David Herrmann committed Dec 14, 2015
1 parent 194aa9e commit 99de80e
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,4 +6,5 @@ Module.symvers
*.o
*.swp
.tmp_versions
b1-test
tags
8 changes: 3 additions & 5 deletions Makefile
Expand Up @@ -71,11 +71,9 @@ module:
# integration..
#
tests:
CFLAGS="-g -O0" $(MAKE) -C tools/testing/selftests/bus1/
.PHONY: tests

# XXX: implement
# CFLAGS="-g -O0" $(MAKE) -C tools/testing/selftests/bus1/

#
# Bus1 Build Target
# Run 'make b' to build the bus1 out-of-tree module as part of the bus1 build
Expand Down Expand Up @@ -130,10 +128,10 @@ tt-prepare: module tests
.PHONY: tt-prepare

tt: tt-prepare
tools/testing/selftests/bus1/bus1-test -m bus$(BUS1_EXT) ; (R=$$? ; dmesg ; exit $$R)
tools/testing/selftests/bus1/b1-test --module bus$(BUS1_EXT) ; (R=$$? ; dmesg ; exit $$R)
.PHONY: tt

stt: tt-prepare
sudo tools/testing/selftests/bus1/bus1-test -m bus$(BUS1_EXT) ; (R=$$? ; dmesg ; exit $$R)
sudo tools/testing/selftests/bus1/b1-test --module bus$(BUS1_EXT) ; (R=$$? ; dmesg ; exit $$R)
.PHONY: stt

22 changes: 22 additions & 0 deletions tools/testing/selftests/bus1/Makefile
@@ -0,0 +1,22 @@
TEST_PROGS := b1-test

OBJS = \
b1-client.c \
b1-test.c \
test-filesystem.c \
test-peer.c

CFLAGS += -I../../../../usr/include/

all: $(TEST_PROGS)

include ../lib.mk

clean:
$(RM) $(TEST_PROGS)

%.o: %.c b1-client.h b1-test.h
$(CC) $(CFLAGS) -c $< -o $@

b1-test: $(OBJS)
$(CC) $(CFLAGS) $^ $(LDLIBS) -o $@
123 changes: 123 additions & 0 deletions tools/testing/selftests/bus1/b1-client.c
@@ -0,0 +1,123 @@
/*
* Copyright (C) 2013-2016 Red Hat, Inc.
*
* bus1 is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*/

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <linux/bus1.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "b1-client.h"

struct b1_client {
int fd;
void *pool;
size_t pool_size;
};

int b1_client_new_from_fd(struct b1_client **out, int fd)
{
struct b1_client *client;

assert(out);
assert(fd >= 0);

client = malloc(sizeof(*client));
if (!client)
return -ENOMEM;

client->fd = fd;
client->pool = MAP_FAILED;

*out = client;
return 0;
}

int b1_client_new_from_path(struct b1_client **out, const char *path)
{
int r, fd;

if (!path)
path = "/sys/fs/bus1/bus";

fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
return -errno;

r = b1_client_new_from_fd(out, fd);
if (r < 0)
close(fd);

return r;
}

int b1_client_new_from_mount(struct b1_client **out, const char *mount_path)
{
char *path = NULL;
size_t length;

if (mount_path) {
length = strlen(mount_path);
if (length > PATH_MAX)
return -ENAMETOOLONG;

path = alloca(length + 5);
memcpy(path, mount_path, length);
memcpy(path + length, "/bus", 5);
}

return b1_client_new_from_path(out, path);
}

struct b1_client *b1_client_free(struct b1_client *client)
{
if (!client)
return client;

if (client->pool != MAP_FAILED)
munmap(client->pool, client->pool_size);
if (client->fd >= 0)
close(client->fd);
free(client);

return NULL;
}

int b1_client_resolve(struct b1_client *client, uint64_t *out_id, const char *name)
{
struct bus1_cmd_resolve *cmd;
size_t namelen;
int r;

assert(client);
assert(name);

namelen = strlen(name) + 1;
if (namelen > BUS1_NAME_MAX_SIZE)
return -EMSGSIZE;

cmd = alloca(sizeof(*cmd) + namelen);
cmd->size = sizeof(*cmd) + namelen;
cmd->flags = 0;
cmd->unique_id = 0;
memcpy(cmd->name, name, namelen);

r = ioctl(client->fd, BUS1_CMD_RESOLVE, &cmd);
if (r < 0)
return -errno;

if (out_id)
*out_id = cmd->unique_id;
return 0;
}
40 changes: 40 additions & 0 deletions tools/testing/selftests/bus1/b1-client.h
@@ -0,0 +1,40 @@
#ifndef __B1_CLIENT_H
#define __B1_CLIENT_H

/*
* Copyright (C) 2013-2016 Red Hat, Inc.
*
* bus1 is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*/

/**
* Bus1 Client API
*
* XXX
*/

#include <inttypes.h>
#include <linux/bus1.h>

struct b1_client;

int b1_client_new_from_fd(struct b1_client **out, int fd);
int b1_client_new_from_path(struct b1_client **out, const char *path);
int b1_client_new_from_mount(struct b1_client **out, const char *mount_path);
struct b1_client *b1_client_free(struct b1_client *client);

int b1_client_resolve(struct b1_client *client, uint64_t *out_id, const char *name);

int b1_client_connect(struct b1_client *client, const char **names, size_t n_names);
int b1_client_disconnect(struct b1_client *client);

int b1_client_track(struct b1_client *client, uint64_t id);
int b1_client_untrack(struct b1_client *client, uint64_t id);

int b1_client_send(struct b1_client *client, ...);
int b1_client_recv(struct b1_client *client, ...);

#endif /* __B1_CLIENT_H */

0 comments on commit 99de80e

Please sign in to comment.