Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
11 changed files
with
530 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ Module.symvers | |
*.o | ||
*.swp | ||
.tmp_versions | ||
b1-test | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |
Oops, something went wrong.