TEST: add tests for nfs_link

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
libnfs-4.0.0-vitalif
Ronnie Sahlberg 2017-07-02 10:42:19 +10:00
parent c6ac1b5a14
commit aff3099f00
5 changed files with 221 additions and 27 deletions

View File

@ -1081,7 +1081,7 @@ nfs3_mount_async(struct nfs_context *nfs, const char *server,
struct nfs_link_data {
char *oldpath;
struct nfs_fh oldfh;
char *newpath;
char *newparent;
char *newobject;
struct nfs_fh newdir;
};
@ -1091,18 +1091,11 @@ free_nfs_link_data(void *mem)
{
struct nfs_link_data *data = mem;
if (data->oldpath != NULL) {
free(data->oldpath);
}
if (data->oldfh.val != NULL) {
free(data->oldfh.val);
}
if (data->newpath != NULL) {
free(data->newpath);
}
if (data->newdir.val != NULL) {
free(data->newdir.val);
}
free(data->oldpath);
free(data->oldfh.val);
free(data->newparent);
free(data->newobject);
free(data->newdir.val);
free(data);
}
@ -1125,7 +1118,8 @@ nfs3_link_cb(struct rpc_context *rpc, int status, void *command_data,
res = command_data;
if (res->status != NFS3_OK) {
nfs_set_error(nfs, "NFS: LINK %s -> %s/%s failed with "
"%s(%d)", link_data->oldpath, link_data->newpath,
"%s(%d)", link_data->oldpath,
link_data->newparent,
link_data->newobject,
nfsstat3_to_str(res->status),
nfsstat3_to_errno(res->status));
@ -1179,7 +1173,7 @@ nfs3_link_continue_1_internal(struct nfs_context *nfs,
link_data->oldfh = data->fh;
data->fh.val = NULL;
if (nfs3_lookuppath_async(nfs, link_data->newpath, 0,
if (nfs3_lookuppath_async(nfs, link_data->newparent, 0,
data->cb, data->private_data,
nfs3_link_continue_2_internal,
link_data, free_nfs_link_data, 0) != 0) {
@ -1218,23 +1212,29 @@ nfs3_link_async(struct nfs_context *nfs, const char *oldpath,
return -1;
}
link_data->newpath = strdup(newpath);
if (link_data->newpath == NULL) {
nfs_set_error(nfs, "Out of memory, failed to allocate "
"buffer for newpath");
link_data->newobject = strdup(newpath);
if (link_data->newobject == NULL) {
nfs_set_error(nfs, "Out of memory, failed to strdup "
"newpath");
free_nfs_link_data(link_data);
return -1;
}
ptr = strrchr(link_data->newpath, '/');
ptr = strrchr(link_data->newobject, '/');
if (ptr == NULL) {
nfs_set_error(nfs, "Invalid path %s", newpath);
link_data->newparent = NULL;
} else {
*ptr = 0;
link_data->newparent = link_data->newobject;
ptr++;
link_data->newobject = strdup(ptr);
}
if (link_data->newobject == NULL) {
nfs_set_error(nfs, "Out of memory, failed to allocate "
"buffer for newobject");
free_nfs_link_data(link_data);
return -1;
}
*ptr = 0;
ptr++;
link_data->newobject = ptr;
if (nfs3_lookuppath_async(nfs, link_data->oldpath, 0,
cb, private_data,

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_mkdir prog_rename prog_rmdir prog_stat \
prog_symlink prog_timeout prog_unlink
noinst_PROGRAMS = prog_fstat prog_link prog_mkdir prog_rename prog_rmdir \
prog_stat prog_symlink prog_timeout prog_unlink
EXTRA_PROGRAMS = ld_timeout
CLEANFILES = ld_timeout.o ld_timeout.so

88
tests/prog_link.c Normal file
View File

@ -0,0 +1,88 @@
/* -*- 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-link <url> <cwd> <oldpath> <newpath>\n");
exit(1);
}
int main(int argc, char *argv[])
{
struct nfs_context *nfs = NULL;
struct nfs_url *url = NULL;
int ret = 0;
if (argc != 5) {
usage();
}
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_link(nfs, argv[3], argv[4])) {
fprintf(stderr, "Failed to link: %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
finished:
nfs_destroy_url(url);
nfs_destroy_context(nfs);
return ret;
}

61
tests/test_0900_link.sh Executable file
View File

@ -0,0 +1,61 @@
#!/bin/sh
. ./functions.sh
echo "basic link test"
start_share
mkdir "${TESTDIR}/subdir"
mkdir "${TESTDIR}/subdir2"
echo "kangabanga" > "${TESTDIR}/testfile"
echo -n "Link a root path (abs -> abs) (1) ... "
./prog_link "${TESTURL}/" "." /testfile /link1 || failure
success
echo -n "Link a root path (abs -> rel) (2) ... "
./prog_link "${TESTURL}/" "." /testfile link2 || failure
success
echo -n "Link a root path (rel -> abs) (3) ... "
./prog_link "${TESTURL}/" "." testfile /link3 || failure
success
echo -n "Link a root path (rel -> rel) (4) ... "
./prog_link "${TESTURL}/" "." testfile link4 || failure
success
echo -n "Link a subdir path (abs -> abs) (5) ... "
./prog_link "${TESTURL}/" "." /testfile /subdir/link5 || failure
success
echo -n "Link a subdir path (abs -> rel) (6) ... "
./prog_link "${TESTURL}/" "." /subdir/link5 subdir2/link6 || failure
success
echo -n "Link a subdir path (rel -> abs) (7) ... "
./prog_link "${TESTURL}/" "." subdir/link5 /subdir2/link7 || failure
success
echo -n "Link a subdir path (rel -> rel) (8) ... "
./prog_link "${TESTURL}/" "." subdir2/link7 subdir/link8 || failure
success
echo -n "Link from a different cwd (rel -> rel) (9) ... "
./prog_link "${TESTURL}/" "subdir2" link7 ../subdir/link9 || failure
success
echo -n "Link from outside the share (rel -> rel) (10) ... "
./prog_link "${TESTURL}/" "subdir2" ../../link7 ../subdir/link10 2>/dev/null && failure
success
echo -n "Link to outside the share (rel -> rel) (11) ... "
./prog_link "${TESTURL}/" "subdir2" link7 ../../subdir/link11 2>/dev/null && failure
success
stop_share
exit 0

View File

@ -152,6 +152,51 @@ echo -n "Testing nfs_unlink for memory leaks (6) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_unlink "${TESTURL}/" "subdir" ../../subdir2/unlink 2>/dev/null || expr $? != 99 >/dev/null || failure
success
echo "kangabanga" > "${TESTDIR}/testfile"
echo -n "test nfs_link() for memory leaks (1) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." /testfile /link1 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (2) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." /testfile link2 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (3) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." testfile /link3 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (4) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." testfile link4 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (5) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." /testfile /subdir/link5 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (6) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." /subdir/link5 subdir2/link6 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (7) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." subdir/link5 /subdir2/link7 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (8) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "." subdir2/link7 /subdir/link8 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (9) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "subdir2" link7 ../subdir/link9 >/dev/null 2>&1 || failure
success
echo -n "test nfs_link() for memory leaks (10) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "subdir2" ../../link7 ../subdir/link10 2>/dev/null || expr $? != 99 >/dev/null || failure
success
echo -n "test nfs_link() for memory leaks (11) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_link "${TESTURL}/" "subdir2" link7 ../../subdir/link11 2>/dev/null || expr $? != 99 >/dev/null || failure
success
stop_share