Start to DRY driver unit tests.

This commit is contained in:
R. Bernstein
2010-01-24 06:01:24 -05:00
parent f0d742229a
commit abb378299a
8 changed files with 169 additions and 113 deletions

View File

@@ -8,6 +8,7 @@
/bincue /bincue
/bincue /bincue
/bincue.c /bincue.c
/cdrdao
/cdrdao.c /cdrdao.c
/follow_symlink /follow_symlink
/freebsd /freebsd

View File

@@ -16,9 +16,11 @@
INCLUDES = -I$(top_srcdir) $(LIBCDIO_CFLAGS) $(LIBISO9660_CFLAGS) INCLUDES = -I$(top_srcdir) $(LIBCDIO_CFLAGS) $(LIBISO9660_CFLAGS)
DATA_DIR = $(abs_top_srcdir)/test/data DATA_DIR = $(abs_top_srcdir)/test/data
bincue_SOURCES = helper.c bincue.c
bincue_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) bincue_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
bincue_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" bincue_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
cdrdao_SOURCES = helper.c cdrdao.c
cdrdao_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) cdrdao_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
cdrdao_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" cdrdao_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
@@ -28,12 +30,14 @@ freebsd_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
realpath_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) realpath_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
realpath_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" realpath_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
gnu_linux_SOURCES= helper.c gnu_linux.c
gnu_linux_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) gnu_linux_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
gnu_linux_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" gnu_linux_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
mmc_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) mmc_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
mmc_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" mmc_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
nrg_SOURCES = helper.c nrg.c
nrg_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) nrg_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
nrg_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" nrg_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
@@ -54,6 +58,7 @@ TESTS = $(check_PROGRAMS)
EXTRA_DIST = \ EXTRA_DIST = \
bincue.c.in \ bincue.c.in \
helper.c \
cdrdao.c.in \ cdrdao.c.in \
nrg.c.in nrg.c.in

View File

@@ -33,7 +33,12 @@
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
#include <stdlib.h> #include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H
#include <string.h> #include <string.h>
#endif
#include "helper.h"
#ifndef DATA_DIR #ifndef DATA_DIR
#define DATA_DIR "@abs_top_srcdir@/test/data" #define DATA_DIR "@abs_top_srcdir@/test/data"
@@ -117,6 +122,11 @@ main(int argc, const char *argv[])
#endif #endif
{ {
psz_device = cdio_get_default_device(p_cdio); psz_device = cdio_get_default_device(p_cdio);
check_mmc_supported(p_cdio, 1);
check_access_mode(p_cdio, "image");
// check_get_arg_source(p_cdio, psz_device);
/* Could chdir to srcdir to hedge the bet? */ /* Could chdir to srcdir to hedge the bet? */
if (psz_device) if (psz_device)
free(psz_device); free(psz_device);
@@ -127,41 +137,6 @@ main(int argc, const char *argv[])
drc = cdio_set_speed(p_cdio, 5); drc = cdio_set_speed(p_cdio, 5);
} }
{
const char *psz_response = cdio_get_arg(p_cdio, "mmc-supported?");
if ( psz_response == NULL ||
((0 != strncmp("false", psz_response, sizeof("false")))) ) {
fprintf(stderr,
"cdio_get_arg(\"mmc-supported?\") should return \"false\"; got: \"%s\".\n",
psz_response);
exit(3);
}
}
{
const char *psz_access_mode = cdio_get_arg(p_cdio, "access-mode");
if ( psz_access_mode == NULL ||
((0 != strncmp("image", psz_access_mode, sizeof("image")))) ) {
fprintf(stderr,
"cdio_get_arg(\"access-mode?\") should return \"image\"; got: \"%s\".\n",
psz_access_mode);
exit(4);
}
}
{
const char *psz_source = cdio_get_arg(p_cdio, "source");
const char *psz_device = cdio_get_default_device(p_cdio);
if ( psz_source == NULL ||
(0 != strcmp(psz_device, psz_device)) ) {
fprintf(stderr,
"cdio_get_arg(\"source\") should return \"%s\"; got: \"%s\".\n",
psz_source, psz_device);
exit(5);
}
}
cdio_destroy(p_cdio); cdio_destroy(p_cdio);
} }

View File

@@ -33,7 +33,11 @@
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
#include <stdlib.h> #include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H
#include <string.h> #include <string.h>
#endif
#include "helper.h"
#ifndef DATA_DIR #ifndef DATA_DIR
#define DATA_DIR "@abs_top_srcdir@/test/data" #define DATA_DIR "@abs_top_srcdir@/test/data"
@@ -90,8 +94,8 @@ main(int argc, const char *argv[])
snprintf(psz_tocfile, sizeof(psz_tocfile)-1, snprintf(psz_tocfile, sizeof(psz_tocfile)-1,
"%s/%s", DATA_DIR, toc_file[i]); "%s/%s", DATA_DIR, toc_file[i]);
if (!cdio_is_tocfile(psz_tocfile)) { if (!cdio_is_tocfile(psz_tocfile)) {
printf("Incorrect: %s doesn't parse as a cdrdao TOC file.\n", fprintf(stderr,
toc_file[i]); "Incorrect: %s doesn't parse as a cdrdao TOC file.\n", toc_file[i]);
ret=i+1; ret=i+1;
} else { } else {
if (verbose) if (verbose)
@@ -108,12 +112,33 @@ main(int argc, const char *argv[])
printf("Correct: %s doesn't parse as a cdrdao TOC file.\n", printf("Correct: %s doesn't parse as a cdrdao TOC file.\n",
badtoc_file[i]); badtoc_file[i]);
} else { } else {
printf("Incorrect: %s parses as a cdrdao TOC file.\n", fprintf(stderr,
badtoc_file[i]); "Incorrect: %s parses as a cdrdao TOC file.\n",
badtoc_file[i]);
ret+=50*i+1; ret+=50*i+1;
break; break;
} }
} }
{
CdIo_t *p_cdio;
snprintf(psz_tocfile, sizeof(psz_tocfile)-1,
"%s/%s", DATA_DIR, "cdda.toc");
p_cdio = cdio_open (psz_tocfile, DRIVER_CDRDAO);
if (!p_cdio) {
fprintf(stderr, "Can't open %s as a cdrdao TOC file.\n",
psz_tocfile);
exit(5);
}
check_mmc_supported(p_cdio, 1);
check_access_mode(p_cdio, "image");
check_get_arg_source(p_cdio, psz_tocfile);
cdio_destroy(p_cdio);
}
} }
return ret; return ret;

View File

@@ -33,7 +33,11 @@
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
#include <stdlib.h> #include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H
#include <string.h> #include <string.h>
#endif
#include "helper.h"
int int
main(int argc, const char *argv[]) main(int argc, const char *argv[])
@@ -54,52 +58,25 @@ main(int argc, const char *argv[])
p_cdio = cdio_open_linux(ppsz_drives[0]); p_cdio = cdio_open_linux(ppsz_drives[0]);
if (p_cdio) { if (p_cdio) {
const char *psz_source = cdio_get_arg(p_cdio, "source"); const char *psz_source = NULL, *scsi_tuple;
if (0 != strncmp(psz_source, ppsz_drives[0],
strlen(ppsz_drives[0]))) { check_get_arg_source(p_cdio, ppsz_drives[0]);
fprintf(stderr, check_mmc_supported(p_cdio, 3);
"Got %s; should get back %s, the name we opened.\n",
psz_source, ppsz_drives[0]); scsi_tuple = cdio_get_arg(p_cdio, "scsi-tuple");
exit(1); if (scsi_tuple == NULL) {
fprintf(stderr, "cdio_get_arg(\"scsi-tuple\") returns NULL.\n");
exit(3);
} }
} if (cdio_loglevel_default == CDIO_LOG_DEBUG)
printf("Drive '%s' has cdio_get_arg(\"scsi-tuple\") = '%s'\n",
{ psz_source, scsi_tuple);
const char *psz_response = cdio_get_arg(p_cdio, "mmc-supported?"); cdio_destroy(p_cdio);
if ( psz_response == NULL ||
((0 != strncmp("true", psz_response, sizeof("true"))) &&
(0 != strncmp("false", psz_response, sizeof("false")))) ) {
fprintf(stderr,
"cdio_get_arg(\"mmc-supported?\") should return \"true\" or \"false\"; got: \"%s\".\n",
psz_response);
exit(2);
}
} }
{
const char *psz_source = NULL, *scsi_tuple;
scsi_tuple = cdio_get_arg(p_cdio, "scsi-tuple");
if (scsi_tuple == NULL) {
fprintf(stderr, "cdio_get_arg(\"scsi-tuple\") returns NULL.\n");
exit(3);
}
if (cdio_loglevel_default == CDIO_LOG_DEBUG)
printf("Drive '%s' has cdio_get_arg(\"scsi-tuple\") = '%s'\n",
psz_source, scsi_tuple);
}
cdio_destroy(p_cdio);
p_cdio = cdio_open_am_linux(ppsz_drives[0], "MMC_RDWR"); p_cdio = cdio_open_am_linux(ppsz_drives[0], "MMC_RDWR");
if (p_cdio) { if (p_cdio) {
const char *psz_access_mode = cdio_get_arg(p_cdio, "access-mode"); check_access_mode(p_cdio, "MMC_RDWR");
if (0 != strncmp(psz_access_mode, "MMC_RDWR", strlen("MMC_RDWR"))) {
fprintf(stderr,
"Got \"%s\"; Should get back \"%s\", the access mode requested.\n",
psz_access_mode, "MMC_RDWR");
exit(4);
}
} }
cdio_destroy(p_cdio); cdio_destroy(p_cdio);

78
test/driver/helper.c Normal file
View File

@@ -0,0 +1,78 @@
/* -*- C -*-
Copyright (C) 2010 Rocky Bernstein <rocky@gnu.org>
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
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <cdio/cdio.h>
#include "helper.h"
void check_access_mode(CdIo_t *p_cdio, const char *psz_expected_access_mode)
{
const char *psz_access_mode = cdio_get_arg(p_cdio, "access-mode");
if ( psz_access_mode == NULL ||
(0 != strcmp(psz_expected_access_mode, psz_access_mode)) ) {
fprintf(stderr,
"cdio_get_arg(\"access-mode?\") should return \"%s\"; got: \"%s\".\n",
psz_expected_access_mode, psz_access_mode);
exit(10);
}
}
void check_get_arg_source(CdIo_t *p_cdio, const char *psz_expected_source)
{
const char *psz_source = cdio_get_arg(p_cdio, "source");
if ( psz_source == NULL ||
(0 != strcmp(psz_expected_source, psz_source)) ) {
fprintf(stderr,
"cdio_get_arg(\"source\") should return \"%s\"; got: \"%s\".\n",
psz_expected_source, psz_source);
exit(40);
}
}
/* i_expected: 1 => expect false
2 => expect true
3 => expect true or false
*/
void check_mmc_supported(CdIo_t *p_cdio, int i_expected) {
const char *psz_response = cdio_get_arg(p_cdio, "mmc-supported?");
if ( psz_response == NULL ) {
fprintf(stderr,
"cdio_get_arg(\"mmc-supported?\") returned NULL\n");
exit(30);
} else if ( (0 == (i_expected & 1)) &&
(0 == strncmp("false", psz_response, sizeof("false"))) ) {
fprintf(stderr,
"cdio_get_arg(\"mmc-supported?\") should not return \"false\"");
exit(31);
} else if ( (0 == (i_expected & 2)) &&
(0 == strncmp("true", psz_response, sizeof("true"))) ) {
fprintf(stderr,
"cdio_get_arg(\"mmc-supported?\") should not return \"true\"");
exit(32);
}
}

21
test/driver/helper.h Normal file
View File

@@ -0,0 +1,21 @@
/* -*- C -*-
Copyright (C) 2010 Rocky Bernstein <rocky@gnu.org>
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/>.
*/
void check_access_mode(CdIo_t *p_cdio, const char *psz_expected_access_mode);
void check_get_arg_source(CdIo_t *p_cdio, const char *psz_expected_source);
void check_mmc_supported(CdIo_t *p_cdio, int i_expected);

View File

@@ -35,7 +35,11 @@
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
#include <stdlib.h> #include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H
#include <string.h> #include <string.h>
#endif
#include "helper.h"
#ifndef DATA_DIR #ifndef DATA_DIR
#define DATA_DIR "@abs_top_srcdir@/test/data" #define DATA_DIR "@abs_top_srcdir@/test/data"
@@ -85,39 +89,9 @@ main(int argc, const char *argv[])
} }
} }
{ check_mmc_supported(p_cdio, 1);
const char *psz_response = cdio_get_arg(p_cdio, "mmc-supported?"); check_access_mode(p_cdio, "image");
if ( psz_response == NULL || check_get_arg_source(p_cdio, psz_nrgfile);
((0 != strncmp("false", psz_response, sizeof("false")))) ) {
fprintf(stderr,
"cdio_get_arg(\"mmc-supported?\") should return \"false\"; got: \"%s\".\n",
psz_response);
exit(3);
}
}
{
const char *psz_access_mode = cdio_get_arg(p_cdio, "access-mode");
if ( psz_access_mode == NULL ||
((0 != strncmp("image", psz_access_mode, sizeof("image")))) ) {
fprintf(stderr,
"cdio_get_arg(\"access-mode?\") should return \"image\"; got: \"%s\".\n",
psz_access_mode);
exit(4);
}
}
{
const char *psz_source = cdio_get_arg(p_cdio, "source");
if ( psz_source == NULL ||
(0 != strcmp(psz_nrgfile, psz_source)) ) {
fprintf(stderr,
"cdio_get_arg(\"source\") should return \"%s\"; got: \"%s\".\n",
psz_source, psz_nrgfile);
exit(4);
}
}
cdio_destroy(p_cdio); cdio_destroy(p_cdio);