Skip to content

Commit

Permalink
tests: add a basic test framework
Browse files Browse the repository at this point in the history
and add a simple example test.

Signed-off-by: Ronnie Sahlberg <[email protected]>
  • Loading branch information
sahlberg committed Oct 4, 2024
1 parent ce3edde commit 9250e57
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ pkgconfig_DATA = libsmb2.pc

EXTRA_DIST = \
libsmb2.pc.in

test: $(SUBDIRS)
cd tests; make test
10 changes: 6 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AC_INIT([libsmb2], [4.0.0], [[email protected]])
AC_INIT([libsmb2],[4.0.0],[[email protected]])

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
Expand All @@ -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

Expand Down Expand Up @@ -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
18 changes: 18 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions tests/README
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions tests/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
. ./setup.local

success() {
echo "[OK]"
}

failure() {
echo "[FAILED]"
exit 1
}

120 changes: 120 additions & 0 deletions tests/prog_ls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2016 by Ronnie Sahlberg <[email protected]>
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 <inttypes.h>
#if !defined(__amigaos4__) && !defined(__AMIGA__) && !defined(__AROS__)
#include <poll.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#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 <smb2-url>\n\n"
"URL format: "
"smb://[<domain;][<username>@]<host>[:<port>]/<share>/<path>\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;
}
11 changes: 11 additions & 0 deletions tests/test_0100_ls_basic.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9250e57

Please sign in to comment.