TESTS: add tests for mknod

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
libnfs-4.0.0-vitalif
Ronnie Sahlberg 2017-07-02 11:48:28 +10:00
parent aff3099f00
commit 9fdc774bf3
6 changed files with 172 additions and 22 deletions

View File

@ -808,7 +808,8 @@ int nfs_creat(struct nfs_context *nfs, const char *path, int mode, struct nfsfh
/*
* mknod()
*/
static void mknod_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
static void
mknod_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
{
struct sync_cb_data *cb_data = private_data;
@ -816,19 +817,22 @@ static void mknod_cb(int status, struct nfs_context *nfs, void *data, void *priv
cb_data->status = status;
if (status < 0) {
nfs_set_error(nfs, "mknod call failed with \"%s\"", (char *)data);
nfs_set_error(nfs, "mknod call failed with \"%s\"",
(char *)data);
return;
}
}
int nfs_mknod(struct nfs_context *nfs, const char *path, int mode, int dev)
int
nfs_mknod(struct nfs_context *nfs, const char *path, int mode, int dev)
{
struct sync_cb_data cb_data;
cb_data.is_finished = 0;
if (nfs_mknod_async(nfs, path, mode, dev, mknod_cb, &cb_data) != 0) {
nfs_set_error(nfs, "nfs_creat_async failed");
nfs_set_error(nfs, "nfs_creat_async failed. %s",
nfs_get_error(nfs));
return -1;
}

View File

@ -2960,6 +2960,8 @@ nfs3_mknod_continue_internal(struct nfs_context *nfs,
char *str = cb_data->path;
MKNOD3args args;
memset(&args, 0, sizeof(args));
str = &str[strlen(str) + 1];
args.where.dir.data.data_len = data->fh.len;
@ -3021,21 +3023,25 @@ nfs3_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev,
return -1;
}
cb_data->path = strdup(path);
if (cb_data->path == NULL) {
nfs_set_error(nfs, "Out of memory, failed to allocate"
"mode buffer for path");
free(cb_data);
return -1;
}
ptr = strrchr(cb_data->path, '/');
if (ptr == NULL) {
nfs_set_error(nfs, "Invalid path %s", path);
free_mknod_cb_data(cb_data);
return -1;
}
*ptr = 0;
ptr = strrchr(path, '/');
if (ptr) {
cb_data->path = strdup(path);
if (cb_data->path == NULL) {
nfs_set_error(nfs, "Out of memory, failed to allocate "
"buffer for mknod path");
return -1;
}
ptr = strrchr(cb_data->path, '/');
*ptr = 0;
} else {
cb_data->path = malloc(strlen(path) + 2);
if (cb_data->path == NULL) {
nfs_set_error(nfs, "Out of memory, failed to allocate "
"buffer for mknod path");
return -1;
}
sprintf(cb_data->path, "%c%s", '\0', path);
}
cb_data->mode = mode;
cb_data->major = major(dev);

View File

@ -4,8 +4,8 @@ AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/nfsc \
AM_CFLAGS = $(WARN_CFLAGS)
LDADD = ../lib/libnfs.la
noinst_PROGRAMS = prog_fstat prog_link prog_mkdir prog_rename prog_rmdir \
prog_stat prog_symlink prog_timeout prog_unlink
noinst_PROGRAMS = prog_fstat prog_link prog_mkdir prog_mknod prog_rename
prog_rmdir prog_stat prog_symlink prog_timeout prog_unlink
EXTRA_PROGRAMS = ld_timeout
CLEANFILES = ld_timeout.o ld_timeout.so

View File

@ -5,4 +5,3 @@ To run the tests :
The tests will ask for your password as it needs sudo access to
run exportfs to create/remove the share we use for testing.

93
tests/prog_mknod.c Normal file
View File

@ -0,0 +1,93 @@
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2017
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "libnfs.h"
void usage(void)
{
fprintf(stderr, "Usage: prog-mknod <url> <cwd> <path> <mode(octal)>"
"<dev(hex)>\n");
exit(1);
}
int main(int argc, char *argv[])
{
struct nfs_context *nfs = NULL;
struct nfs_url *url = NULL;
int ret = 0;
int mode, dev;
if (argc != 6) {
usage();
}
mode = strtol(argv[4], NULL, 8);
dev = strtol(argv[5], NULL, 16);
nfs = nfs_init_context();
if (nfs == NULL) {
printf("failed to init context\n");
exit(1);
}
url = nfs_parse_url_full(nfs, argv[1]);
if (url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(nfs));
exit(1);
}
if (nfs_mount(nfs, url->server, url->path) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
if (nfs_chdir(nfs, argv[2]) != 0) {
fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
argv[2], nfs_get_error(nfs));
ret = 1;
goto finished;
}
if (nfs_mknod(nfs, argv[3], mode, dev)) {
fprintf(stderr, "Failed to unlink: %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
finished:
nfs_destroy_url(url);
nfs_destroy_context(nfs);
return ret;
}

48
tests/test_1000_mknod.sh Executable file
View File

@ -0,0 +1,48 @@
#!/bin/sh
#
# NFS servers generally want us to be root in order to create device nodes.
# We can lie and impersonate root by setting uid=0 in the URL.
. ./functions.sh
echo "basic mknod test"
start_share
mkdir "${TESTDIR}/subdir"
mkdir "${TESTDIR}/subdir2"
echo -n "Create a chrdev in the root (abs) (1)... "
./prog_mknod "${TESTURL}/?uid=0" "." /mknod1 020775 0x1234 || failure
success
echo -n "Stat the node ... "
./prog_stat "${TESTURL}/mknod1" > "${TESTDIR}/output" || failure
echo -n "Testing nfs_mode and verify it is a CHRDEV ... "
grep "nfs_mode:20755" "${TESTDIR}/output" >/dev/null || failure
success
echo -n "Create a chrdev in the root (rel) (2)... "
./prog_mknod "${TESTURL}/?uid=0" "." mknod2 020775 0x1234 || failure
success
echo -n "Create a chrdev in a subdirectory (abs) (3)... "
./prog_mknod "${TESTURL}/?uid=0" "." /subdir/mknod3 020775 0x1234 || failure
success
echo -n "Create a chrdev in a subdirectory (abs) (4)... "
./prog_mknod "${TESTURL}/?uid=0" "." subdir/mknod4 020775 0x1234 || failure
success
echo -n "Create a chrdev from a different cwd (abs) (5)... "
./prog_mknod "${TESTURL}/?uid=0" "subdir" ../subdir2/mknod5 020775 0x1234 || failure
success
echo -n "Create a chrdev outside the share (abs) (6)... "
./prog_mknod "${TESTURL}/?uid=0" "." ../subdir2/mknod6 020775 0x1234 2>/dev/null && failure
success
stop_share
exit 0