diff --git a/Makefile.am b/Makefile.am index 13051ecc..daa44558 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,3 +10,6 @@ pkgconfig_DATA = libsmb2.pc EXTRA_DIST = \ libsmb2.pc.in + +test: $(SUBDIRS) + cd tests; make test diff --git a/configure.ac b/configure.ac index 3d978f9f..f1bcd667 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,6 @@ -AC_INIT([libsmb2], [4.0.0], [ronniesahlberg@gmail.com]) +AC_INIT([libsmb2],[4.0.0],[ronniesahlberg@gmail.com]) -AC_PREREQ([2.58]) +AC_PREREQ([2.71]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([-Wall foreign subdir-objects 1.11]) AC_CANONICAL_HOST @@ -11,7 +11,7 @@ m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) dnl Do not add default CFLAGS in AC_PROG_CC : ${CFLAGS=""} AC_PROG_CC -AC_PROG_LIBTOOL +LT_INIT AM_PROG_CC_C_O @@ -174,6 +174,8 @@ AC_CONFIG_FILES([ examples/Makefile include/Makefile lib/Makefile + tests/Makefile ]) -AC_OUTPUT([libsmb2.pc]) +AC_CONFIG_FILES([libsmb2.pc]) +AC_OUTPUT diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 00000000..4ab55a35 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,18 @@ +AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/smb2 \ + "-D_U_=__attribute__((unused))" \ + "-D_R_(A,B)=__attribute__((format(printf,A,B)))" +AM_CFLAGS = $(WARN_CFLAGS) +LDADD = ../lib/libsmb2.la + +noinst_PROGRAMS = prog_ls + +T = `ls test_*.sh` + +test: $(noinst_PROGRAMS) + for TEST in $(T); do \ + echo "Running $$TEST"; \ + echo "--------------"; \ + sh $$TEST || exit 1; \ + echo "--------------"; \ + echo; \ + done diff --git a/tests/README b/tests/README new file mode 100644 index 00000000..ea0ea08e --- /dev/null +++ b/tests/README @@ -0,0 +1,28 @@ +To run the test you need an SMB server that exports a share and +two configurations files: + +1, ./tests/NTLM +=============== +This file contains the username to password mapping you need to authenticate +to the server with NTLM. + +Example: +$ cat tests/NTLM +win16-1:Administrator:mypassword +$ + +2, tests/setup.local +==================== +This file sets two environment variables the tests need, NTLM_USER_FILE +and TESTURL. + +Example: +$ cat tests/setup.local +TESTURL=smb://Administrator@win16-1/Share +NTLM_USER_FILE=`pwd`/NTLM +$ + +A good idea is to use a special share/subdirectory on the server +so that the tests do not overwrite/corrupt/delete important files. +Create a dedicated share on the server that is only used for testing +and be prepared that any data in this share can be randomly deleted. diff --git a/tests/functions.sh b/tests/functions.sh new file mode 100755 index 00000000..d8be8f14 --- /dev/null +++ b/tests/functions.sh @@ -0,0 +1,11 @@ +. ./setup.local + +success() { + echo "[OK]" +} + +failure() { + echo "[FAILED]" + exit 1 +} + diff --git a/tests/prog_ls.c b/tests/prog_ls.c new file mode 100644 index 00000000..22b17c45 --- /dev/null +++ b/tests/prog_ls.c @@ -0,0 +1,120 @@ +/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */ +/* + Copyright (C) 2016 by Ronnie Sahlberg + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#define _GNU_SOURCE + +#include +#if !defined(__amigaos4__) && !defined(__AMIGA__) && !defined(__AROS__) +#include +#endif +#include +#include +#include +#include +#include + +#include "smb2.h" +#include "libsmb2.h" +#include "libsmb2-raw.h" + +#ifdef __AROS__ +#include "asprintf.h" +#endif + +int usage(void) +{ + fprintf(stderr, "Usage:\n" + "smb2-ls-sync \n\n" + "URL format: " + "smb://[@][:]//\n"); + exit(1); +} + +int main(int argc, char *argv[]) +{ + struct smb2_context *smb2; + struct smb2_url *url; + struct smb2dir *dir; + struct smb2dirent *ent; + char *link; + + if (argc < 2) { + usage(); + } + + smb2 = smb2_init_context(); + if (smb2 == NULL) { + fprintf(stderr, "Failed to init context\n"); + exit(0); + } + + url = smb2_parse_url(smb2, argv[1]); + if (url == NULL) { + fprintf(stderr, "Failed to parse url: %s\n", + smb2_get_error(smb2)); + exit(0); + } + + smb2_set_security_mode(smb2, SMB2_NEGOTIATE_SIGNING_ENABLED); + if (smb2_connect_share(smb2, url->server, url->share, url->user) < 0) { + printf("smb2_connect_share failed. %s\n", smb2_get_error(smb2)); + exit(10); + } + + dir = smb2_opendir(smb2, url->path); + if (dir == NULL) { + printf("smb2_opendir failed. %s\n", smb2_get_error(smb2)); + exit(10); + } + + while ((ent = smb2_readdir(smb2, dir))) { + char *type; + time_t t; + + t = (time_t)ent->st.smb2_mtime; + switch (ent->st.smb2_type) { + case SMB2_TYPE_LINK: + type = "LINK"; + break; + case SMB2_TYPE_FILE: + type = "FILE"; + break; + case SMB2_TYPE_DIRECTORY: + type = "DIRECTORY"; + break; + default: + type = "unknown"; + break; + } + printf("%-20s %-9s %15"PRIu64" %s", ent->name, type, ent->st.smb2_size, asctime(localtime(&t))); + if (ent->st.smb2_type == SMB2_TYPE_LINK) { + char buf[256]; + + if (url->path && url->path[0]) { + asprintf(&link, "%s/%s", url->path, ent->name); + } else { + asprintf(&link, "%s", ent->name); + } + smb2_readlink(smb2, link, buf, 256); + printf(" -> [%s]\n", buf); + free(link); + } + } + + smb2_closedir(smb2, dir); + smb2_disconnect_share(smb2); + smb2_destroy_url(url); + smb2_destroy_context(smb2); + + return 0; +} diff --git a/tests/test_0100_ls_basic.sh b/tests/test_0100_ls_basic.sh new file mode 100755 index 00000000..19c3c2b9 --- /dev/null +++ b/tests/test_0100_ls_basic.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +. ./functions.sh + +echo "basic ls test" + +echo -n "Testing prog_ls on root of share ... " +./prog_ls "${TESTURL}/" > /dev/null || failure +success + +exit 0