NFSv4: Add tests for truncate()
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>libnfs-4.0.0-vitalif
parent
73b81297e7
commit
d39017a43a
|
@ -6,7 +6,8 @@ LDADD = ../lib/libnfs.la
|
|||
|
||||
noinst_PROGRAMS = prog_create prog_fstat prog_ftruncate prog_link prog_lstat \
|
||||
prog_mkdir prog_mknod prog_mount prog_open_read prog_open_write \
|
||||
prog_rename prog_rmdir prog_stat prog_symlink prog_timeout prog_unlink
|
||||
prog_rename prog_rmdir prog_stat prog_symlink prog_timeout \
|
||||
prog_truncate prog_unlink
|
||||
|
||||
EXTRA_PROGRAMS = ld_timeout
|
||||
CLEANFILES = ld_timeout.o ld_timeout.so
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/* -*- 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/types.h>
|
||||
|
||||
#include "libnfs.h"
|
||||
|
||||
void usage(void)
|
||||
{
|
||||
fprintf(stderr, "Usage: prog_truncate <url> <cwd> <path> <offset>\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct nfs_context *nfs;
|
||||
struct nfs_url *url;
|
||||
uint64_t length;
|
||||
struct nfs_stat_64 st;
|
||||
|
||||
if (argc != 5) {
|
||||
usage();
|
||||
}
|
||||
|
||||
length = strtol(argv[4], NULL, 10);
|
||||
|
||||
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));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nfs_chdir(nfs, argv[2]) != 0) {
|
||||
fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
|
||||
argv[2], nfs_get_error(nfs));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nfs_truncate(nfs, argv[3], length)) {
|
||||
fprintf(stderr, "Failed to truncate file : %s\n",
|
||||
nfs_get_error(nfs));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nfs_stat64(nfs, argv[3], &st)) {
|
||||
fprintf(stderr, "Failed to stat file : %s\n",
|
||||
nfs_get_error(nfs));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (st.nfs_size != length) {
|
||||
fprintf(stderr, "Unexpected file size. Expected %d got %d\n",
|
||||
(int)length, (int)st.nfs_size);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
nfs_destroy_url(url);
|
||||
nfs_destroy_context(nfs);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./functions.sh
|
||||
|
||||
echo "NFSv${VERS} Basic nfs_truncate() tests."
|
||||
|
||||
start_share
|
||||
|
||||
dd if=/dev/zero of=testdata/testfile count=1 bs=32768 2>/dev/null
|
||||
|
||||
echo -n "test nfs_truncate() ... "
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "." /testfile 1998 || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_fstat64() ... "
|
||||
./prog_fstat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_size ... "
|
||||
grep "nfs_size:1998" "${TESTDIR}/output" >/dev/null || failure
|
||||
success
|
||||
|
||||
stop_share
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,46 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./functions.sh
|
||||
|
||||
echo "NFSv${VERS} nfs_truncate() path tests."
|
||||
|
||||
start_share
|
||||
|
||||
mkdir "${TESTDIR}/subdir"
|
||||
mkdir "${TESTDIR}/subdir2"
|
||||
|
||||
echo -n "test nfs_truncate() for a root file (abs) (1)... "
|
||||
touch "${TESTDIR}/trunc1"
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "." /trunc1 2027 >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_truncate() for a root file (rel) (2)... "
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "." trunc1 2028 >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_truncate() for a subdir file (abs) (3)... "
|
||||
touch "${TESTDIR}/subdir/trunc3"
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "." /subdir/trunc3 2029 >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_truncate() for a subdir file (rel) (4)... "
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "." subdir/trunc3 2030 >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_truncate() from a different cwd (rel) (5)... "
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "subdir2" ../subdir/trunc3 2031 >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_truncate() outside the share (rel) (6)... "
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "subdir2" ../../subdir/trunc3 2032 >/dev/null 2>&1 && failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_truncate() when target is a symlink (7)... "
|
||||
touch "${TESTDIR}/trunc7"
|
||||
ln -s trunc7 "${TESTDIR}/symlink7"
|
||||
./prog_truncate "${TESTURL}/?version=${VERS}" "." symlink7 2033 || failure
|
||||
success
|
||||
|
||||
stop_share
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue