Browse Source

Add tests for parsing share URIs.

Put in place a test suite infrastructure and add a test program to check
the URI parsing code.
master
Damien Goutte-Gattat 3 months ago
parent
commit
44d375af7e
  1. 4
      .gitignore
  2. 2
      Makefile.am
  3. 3
      configure.ac
  4. 7
      tests/Makefile.am
  5. 173
      tests/t-parseuri.c

4
.gitignore

@ -23,6 +23,10 @@ src/gfsec-split-gpg
lib/*.o
lib/.deps
lib/lib*.a
tests/test-suite.log
tests/t-*
!tests/t-*.c
tests/.deps
info/*.info
info/version.texi
man/*.1

2
Makefile.am

@ -1,4 +1,4 @@
SUBDIRS = po lib src man info
SUBDIRS = po lib src man info tests
ACLOCAL_AMFLAGS = -I m4 --install

3
configure.ac

@ -85,7 +85,8 @@ dnl Output files
AC_CONFIG_FILES([Makefile lib/Makefile po/Makefile.in
src/Makefile src/gfsec-split-gpg
info/Makefile man/Makefile man/gfsec-use.1
man/gfsec-split.1 man/gfsec-split-gpg.1])
man/gfsec-split.1 man/gfsec-split-gpg.1
tests/Makefile])
AC_OUTPUT
dnl Summary

7
tests/Makefile.am

@ -0,0 +1,7 @@
TESTS = t-parseuri
noinst_PROGRAMS = $(TESTS)
AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)/src
AM_LDFLAGS = -L$(top_builddir)/lib -L$(top_builddir)/src
LDADD = -lgfsec -lgfsecret

173
tests/t-parseuri.c

@ -0,0 +1,173 @@
/*
* gfsecret - Secret sharing tools
* Copyright (C) 2021 Damien Goutte-Gattat
*
* 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/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "secret.h"
#include "secretcfg.h"
struct test {
const char *uri;
int rc;
gfsec_scheme_t scheme;
const char *authority;
const char *path;
unsigned sharenr;
char is_full;
};
struct test tests[] = {
/* Empty string. */
{ "", GFSEC_ERR_CONFIG_UNKNOWN_SCHEME },
/* Not an URI. */
{ "asdfjklm", GFSEC_ERR_CONFIG_UNKNOWN_SCHEME },
/* Missing authority/path separator. */
{ "file://", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file://authority", GFSEC_ERR_CONFIG_INVALID_URI },
/* Recognizing the valid schemes. */
{ "file:///path/to/share.123",
0, GFSEC_SCHEME_FILE, "", "/path/to/share.123", 123, 0 },
{ "uuid:///path/to/share.123",
0, GFSEC_SCHEME_UUID, "", "/path/to/share.123", 123, 0 },
{ "mtp:///path/to/share.123",
0, GFSEC_SCHEME_MTP, "", "/path/to/share.123", 123, 0 },
{ "label:///path/to/share.123",
0, GFSEC_SCHEME_LABEL, "", "/path/to/share.123", 123, 0 },
{ "http:///path/to/share.123",
0, GFSEC_SCHEME_HTTP, "", "/path/to/share.123", 123, 0 },
{ "https:///path/to/share.123",
0, GFSEC_SCHEME_HTTPS, "", "/path/to/share.123", 123, 0 },
{ "scheme:///path/to/share.123", GFSEC_ERR_CONFIG_UNKNOWN_SCHEME },
/* Several parameters. */
{ "file:///path/to/share.123?param1=val1&param2=val2&param3=val3",
0, GFSEC_SCHEME_FILE, "", "/path/to/share.123", 123, 0 },
/* Parameter with no value. */
{ "file:///path/to/share.123?param", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.123?param1=val1&param2",
GFSEC_ERR_CONFIG_INVALID_URI },
/* Parameter with empty value. */
{ "file:///path/to/share.123?param=",
0, GFSEC_SCHEME_FILE, "", "/path/to/share.123", 123, 0 },
/* Handling the share parameter. */
{ "file:///path/to/share?share=no",
0, GFSEC_SCHEME_FILE, "", "/path/to/share", 0, 1 },
{ "file:///path/to/share.123?share=no",
0, GFSEC_SCHEME_FILE, "", "/path/to/share.123", 0, 1 },
{ "file:///path/to/share?share=invalid", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.123?share=invalid",
0, GFSEC_SCHEME_FILE, "", "/path/to/share.123", 123, 0 },
/* Hash with invalid length. */
{ "file:///path/to/share.123?sha256=", GFSEC_ERR_CONFIG_INVALID_HASH },
{ "file:///path/to/share.123?sha256=decafbad", GFSEC_ERR_CONFIG_INVALID_HASH },
{ "file:///path/to/share.123?sha256="
"decafbaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbadd",
GFSEC_ERR_CONFIG_INVALID_HASH },
{ "file:///path/to/share.123?sha256="
"decafbaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbad",
0, GFSEC_SCHEME_FILE, "", "/path/to/share.123", 123, 0 },
/* Hash with invalid characters. */
{ "file:///path/to/share.123?sha256="
"decafbaddecafLaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbad",
GFSEC_ERR_CONFIG_INVALID_HASH },
/* Invalid share number in path. */
{ "file:///path/to/share.2", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.000", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.256", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.abc", GFSEC_ERR_CONFIG_INVALID_URI },
{ NULL }
};
unsigned testnr;
unsigned sum;
static void
fail(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
fprintf(stderr, "Test #%u: ", testnr);
vfprintf(stderr, msg, ap);
va_end(ap);
sum += 1;
}
int
main(int argc, char **argv)
{
int rc;
gfsec_secret_t *secret;
gfsec_share_t *share;
struct test *test = tests;
testnr = 1;
sum = 0;
while ( test->uri ) {
secret = gfsec_secret_new();
rc = gfsec_parse_uri(test->uri, secret, 0);
if ( rc != test->rc )
fail("expected code %d, got %d\n", test->rc, rc);
if ( rc == 0 ) {
share = secret->shares[0];
if ( share->scheme != test->scheme )
fail("expected scheme %u, got %u\n", test->scheme, share->scheme);
if ( test->authority && strcmp(share->authority, test->authority) != 0 )
fail("expected authority '%s', got '%s'\n", test->authority, share->authority);
if ( strcmp(share->path, test->path) != 0 )
fail("expected path '%s', got '%s'\n", test->path, share->path);
if ( share->number != test->sharenr )
fail("expected share number %u, got %u\n", test->sharenr, share->number);
}
gfsec_secret_free(secret);
test += 1;
testnr += 1;
}
return sum == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Loading…
Cancel
Save