Home Corporate Contacts

TETware Tutorial


Products

» TET
» TETware
» TETware GUI

» Download TET

Information

» Datasheet (pdf)
» Documentation
» FAQ
» Tutorial
» GUI Help Pages
» Knowledgebase

Appendix

Sample TET C API Test Suite Source Files

This page contains the sample C-API test suite used in the tutorial on writing a TETware test using the C language binding.


Table of Contents

Back to the Tutorial


tet_code


# TET reserved codes
0 "PASS"
1 "FAIL"
2 "UNRESOLVED"
3 "NOTINUSE"
4 "UNSUPPORTED"
5 "UNTESTED"
6 "UNINITIATED"
7 "NORESULT"

# Test suite additional codes
33 "INSPECT"
 

install


echo This is the C-API test suite install tool.
 

cleantool


exec make clean
Back to Table of Contents
 

tet_scen


#       chmod, fileno, stat, uname test suite.

all
        "Starting Full Test Suite"
        /ts/chmod/chmod-tc
        /ts/fileno/fileno-tc
        /ts/stat/stat-tc
        /ts/uname/uname-tc
        "Completed Full Test Suite"

chmod
        "Starting chmod Test Case"
        /ts/chmod/chmod-tc
        "Finished chmod Test Case"

fileno
        "Starting fileno Test Case"
        /ts/fileno/fileno-tc
        "Finished fileno Test Case"

stat
        "Starting stat Test Case"
        /ts/stat/stat-tc
        "Finished stat Test Case"

uname
        "Starting uname Test Case"
        /ts/uname/uname-tc
        "Finished uname Test Case"

# EOF 
 
 

tetbuild.cfg


TET_OUTPUT_CAPTURE=True
TET_BUILD_TOOL=make
TET_BUILD_FILE=-f makefile

tetexec.cfg


TET_OUTPUT_CAPTURE=False

# The name of a character device file (or "unsup" if not supported)
CHARDEV=/dev/null

# The name of a block device file (or "unsup" if not supported)
BLOCKDEV=unsup
 

tetclean.cfg

TET_OUTPUT_CAPTURE=True TET_CLEAN_TOOL=cleantool TET_CLEAN_FILE=

Back to Table of Contents


makefile for chmod-tc.c


TET_ROOT = ../../..
LIBDIR   = $(TET_ROOT)/lib/posix_c
INCDIR   = $(TET_ROOT)/inc/posix_c
CC       = cc
CFLAGS   = -I$(INCDIR) -D_POSIX_SOURCE

chmod-tc:       chmod-tc.c $(INCDIR)/tet_api.h
                $(CC) $(CFLAGS) -o chmod-tc chmod-tc.c $(LIBDIR)/tcm.o $(LIBDIR)/libapi.a
                -rm -f chmod-tc.o

clean:
                rm -f chmod-tc chmod-tc.o

lint:
                lint $(CFLAGS) chmod-tc.c -ltcm
 
 

chmod-tc.c


/* chmod-tc.c : test case for chmod() interface */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <tet_api.h>

static void tp1(), tp2(), tp3();
static void startup(), cleanup();

/* Initialize TCM data structures */
void (*tet_startup)() = startup;
void (*tet_cleanup)() = cleanup;
struct tet_testlist tet_testlist[] = {
    { tp1, 1 },
    { tp2, 2 },
    { tp3, 3 },
    { NULL, 0 }
};


/* Test Case Wide Declarations */
static char *tfile = "chmod.1";         /* test file name */
static char *tndir = "chmod.1/chmod.1"; /* path with non-directory in prefix */
static struct stat buf;                 /* buffer for stat(ing) file */
static char msg[256];                   /* buffer for info lines */

static void
startup()
{
    int fd;
    static char *reason = "Failed to create test file in startup";

    if ((fd=creat(tfile, S_IRWXU)) < 0)
    {
        (void) sprintf(msg,
            "creat(\"%s\", S_IRWXU) failed in startup - errno %d",
            tfile, errno);
        tet_infoline(msg);

        /* Prevent tests which use this file from executing */
        tet_delete(1, reason);
        tet_delete(3, reason);
    }
    else
        (void) close(fd);
}

static void
cleanup()
{
    /* remove file created by start-up */
    (void) unlink(tfile);
}

static void
tp1()         /* successful chmod of file: return 0 */
{
    int ret, err;
    mode_t mode;

    tet_infoline("SUCCESSFUL CHMOD OF FILE");

    /* change mode of file created in startup function */

    errno = 0;
    if ((ret=chmod(tfile, (mode_t)0)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "chmod(\"%s\", 0) returned %d, expected 0",
            tfile, ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
        return;
    }

    /* check mode was changed correctly */

    if (stat(tfile, &buf) == -1)
    {
        (void) sprintf(msg,
            "stat(\"%s\", buf) failed - errno %d", tfile, errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    mode = buf.st_mode & O_ACCMODE;
    if (mode != 0)
    {
        (void) sprintf(msg, "chmod(\"%s\", 0) set mode to 0%lo, expected 0",
            tfile, (long)mode);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}

static void
tp2()       /* chmod of non-existent file: return -1, errno ENOENT */
{
    int ret, err;

    tet_infoline("CHMOD OF NON-EXISTENT FILE");

    /* ensure file does not exist */

    if (stat("chmod.2", &buf) != -1 && unlink("chmod.2") == -1)
    {
        tet_infoline("could not unlink chmod.2");
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* check return value and errno set by call */

    errno = 0;
    ret = chmod("chmod.2", (mode_t)0);

    if (ret != -1 || errno != ENOENT)
    {
        err = errno;
        if (ret != -1)
        {
            (void) sprintf(msg,
                "chmod(\"chmod.2\", 0) returned %d, expected -1", ret);
            tet_infoline(msg);
        }

        if (err != ENOENT)
        {
            (void) sprintf(msg,
                "chmod(\"chmod.2\", 0) set errno to %d, expected %d (ENOENT)",
                err, ENOENT);
            tet_infoline(msg);
        }

        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}

static void
tp3()       /* non-directory path component: return -1, errno ENOTDIR */
{
    int ret, err;

    tet_infoline("CHMOD OF NON-DIRECTORY PATH PREFIX COMPONENT");

    /* tndir is a pathname containing a plain file (created by the
       startup function) in the prefix */

    errno = 0;
    ret = chmod(tndir, (mode_t)0);

    /* check return value and errno set by call */

    if (ret != -1 || errno != ENOTDIR)
    {
        err = errno;
        if (ret != -1)
        {
            (void) sprintf(msg,
                "chmod(\"%s\", 0) returned %d, expected -1", tndir, ret);
            tet_infoline(msg);
        }

        if (err != ENOTDIR)
        {
            (void) sprintf(msg,
                "chmod(\"%s\", 0) set errno to %d, expected %d (ENOTDIR)",
                tndir, err, ENOTDIR);
            tet_infoline(msg);
        }

        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}
 
 

makefile for fileno-tc.c


TET_ROOT = ../../..
LIBDIR   = $(TET_ROOT)/lib/posix_c
INCDIR   = $(TET_ROOT)/inc/posix_c
CC       = cc
CFLAGS   = -I$(INCDIR) -D_POSIX_SOURCE

fileno-tc:      fileno-t4 fileno-tc.c $(INCDIR)/tet_api.h
                $(CC) $(CFLAGS) -o fileno-tc fileno-tc.c $(LIBDIR)/tcm.o $(LIBDIR)/libapi.a
                -rm -f fileno-tc.o

fileno-t4:      fileno-t4.c $(INCDIR)/tet_api.h
                $(CC) $(CFLAGS) -o fileno-t4 fileno-t4.c $(LIBDIR)/tcmchild.o $(LIBDIR)/libapi.a
                -rm -f fileno-t4.o

clean:
                rm -f fileno-tc fileno-tc.o fileno-t4 fileno-t4.o

lint:
                lint $(CFLAGS) fileno-tc.c -ltcm
                lint $(CFLAGS) fileno-t4.c -ltcmc
 
 

fileno-tc.c


/* fileno-tc.c : test case for fileno() interface */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include <tet_api.h>

extern char **environ;

static void cleanup();
static void tp1(), tp2(), tp3(), tp4(), ch4();

/* Initialize TCM data structures */
void (*tet_startup)() = NULL;
void (*tet_cleanup)() = cleanup;
struct tet_testlist tet_testlist[] = {
    { tp1, 1 },
    { tp2, 2 },
    { tp3, 3 },
    { tp4, 4 },
    { NULL, 0 }
};

/* Test Case Wide Declarations */
static char msg[256];                   /* buffer for info lines */

static void
cleanup()
{
    (void) unlink("fileno.1");
    (void) unlink("fileno.4");
}

static void
tp1()         /* successful fileno: return fd associated with stream */
{
    FILE *fp;
    struct stat buf1, buf2;
    
    tet_infoline("FD RETURNED BY FILENO REFERS TO FILE OPEN ON STREAM");

    /* open stream to test file */

    if ((fp=fopen("fileno.1", "w")) == NULL)
    {
        (void) sprintf(msg, "fopen(\"fileno.1\", \"w\") failed - errno %d",
            errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* check device and inode numbers from file descriptor associated
       with the stream match those from the file itself */

    if (stat("fileno.1", &buf1) == -1)
    {
        (void) sprintf(msg, "stat(\"fileno.1\", buf1) failed - errno %d",
            errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }
        
    if (fstat(fileno(fp), &buf2) == -1)
    {
        (void) sprintf(msg, "fstat(fileno(fp), buf2) failed - errno %d",
            errno);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else if (buf1.st_ino != buf2.st_ino || buf1.st_dev != buf2.st_dev)
    {
        tet_infoline("fileno(fp) does not refer to same file as fp");
        (void) sprintf(msg, "st_dev, st_ino of file: 0x%lx, %ld",
            (long)buf1.st_dev, (long)buf1.st_ino);
        tet_infoline(msg);
        (void) sprintf(msg, "st_dev, st_ino of fileno(fp): 0x%lx, %ld",
            (long)buf2.st_dev, (long)buf2.st_ino);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);

    (void) fclose(fp);
}

static void
tp2()         /* fileno on stdin/stdout/stderr: return 0/1/2 */
{
    int fd, fail = 0;

    tet_infoline("FILENO ON STDIN/STDOUT/STDERR");

    /* check return value of fileno() for stdin/stdout/stderr */
    /* this code relies on the fact that the TCM does not interfere
       with these streams */

    if ((fd = fileno(stdin)) != 0)
    {
        (void) sprintf(msg, "fileno(stdin) returned %d, expected 0", fd);
        tet_infoline(msg);
        tet_result(TET_FAIL);
        fail = 1;
    }

    if ((fd = fileno(stdout)) != 1)
    {
        (void) sprintf(msg, "fileno(stdout) returned %d, expected 1", fd);
        tet_infoline(msg);
        tet_result(TET_FAIL);
        fail = 1;
    }

    if ((fd = fileno(stderr)) != 2)
    {
        (void) sprintf(msg, "fileno(stderr) returned %d, expected 2", fd);
        tet_infoline(msg);
        tet_result(TET_FAIL);
        fail = 1;
    }

    if (fail == 0)
        tet_result(TET_PASS);
}

static void
tp3()         /* on entry to main(), stdin is readable, stdout and stderr
                 are writable */
{
    int flags, fail = 0;

    tet_infoline("ON ENTRY TO MAIN, STDIN IS READABLE, STDOUT AND STDERR ARE WRITABLE");
    /* this code relies on the fact that the TCM does not interfere
       with these streams */

    /* check file descriptor associated with stdin is readable */

    if ((flags = fcntl(fileno(stdin), F_GETFL)) == -1)
    {
        (void) sprintf(msg, "fcntl(fileno(stdin), F_GETFL) failed - errno %d",
            errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    flags &= O_ACCMODE;
    if (flags != O_RDONLY && flags != O_RDWR)
    {
        tet_infoline("stdin is not readable");
        fail = 1;
    }

    /* check file descriptor associated with stdout is writable */

    if ((flags = fcntl(fileno(stdout), F_GETFL)) == -1)
    {
        (void) sprintf(msg, "fcntl(fileno(stdout), F_GETFL) failed - errno %d",
            errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    flags &= O_ACCMODE;
    if (flags != O_WRONLY && flags != O_RDWR)
    {
        tet_infoline("stdout is not writable");
        fail = 1;
    }

    /* check file descriptor associated with stderr is writable */

    if ((flags = fcntl(fileno(stderr), F_GETFL)) == -1)
    {
        (void) sprintf(msg, "fcntl(fileno(stderr), F_GETFL) failed - errno %d",
            errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    flags &= O_ACCMODE;
    if (flags != O_WRONLY && flags != O_RDWR)
    {
        tet_infoline("stderr is not writable");
        fail = 1;
    }

    if (fail == 0)
        tet_result(TET_PASS);
    else
        tet_result(TET_FAIL);
}

static void
tp4()         /* on entry to main(), stream position of stdin, stdout and
                 stderr is same as fileno(stream) */
{
    tet_infoline("ON ENTRY TO MAIN, STREAM POSITION OF STDIN, STDOUT AND STDERR");

    /* fork and execute subprogram, so that unique file positions can be
       set up on entry to main() in subprogram */

    (void) tet_fork(ch4, TET_NULLFP, 30, 0);
}

static void
ch4()
{
    int fd, ret;
    static char *args[] = { "./fileno-t4", NULL };

    /* set up file positions to be inherited by stdin/stdout/stderr
       in subprogram */

    for (fd = 0; fd < 3; fd++)
    {
        (void) close(fd);
        if ((ret=open("fileno.4", O_RDWR|O_CREAT, S_IRWXU)) != fd)
        {
            (void) sprintf(msg, "open() returned %d, expected %d", ret, fd);
            tet_infoline(msg);
            tet_result(TET_UNRESOLVED);
            return;
        }
        if (lseek(fd, (off_t)(123 + 45*fd), SEEK_SET) == -1)
        {
            (void) sprintf(msg, "lseek() failed - errno %d", errno);
            tet_infoline(msg);
            tet_result(TET_UNRESOLVED);
            return;
        }
    }

    /* execute subprogram to carry out remainder of test */

    (void) tet_exec(args[0], args, environ);

    (void) sprintf(msg, "tet_exec(\"%s\", args, env) failed - errno %d",
        args[0], errno);
    tet_infoline(msg);
    tet_result(TET_UNRESOLVED);
}
 
 

fileno-t4.c


/* fileno-t4.c : child program of test purpose 4 for fileno() */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <tet_api.h>

static char msg[256];                   /* buffer for info lines */

/* ARGSUSED */

int
tet_main(argc, argv)
int argc;
char **argv;
{
    long ret, pos;
    int fd, err, fail = 0;
    static FILE *streams[] =  {  stdin,   stdout,   stderr };
    static char *strnames[] = { "stdin", "stdout", "stderr" };

    /* check file positions of streams are same as set up in parent */

    for (fd = 0; fd < 3; fd++)
    {
        pos = 123 + 45*fd; /* must match lseek() in parent */
        errno = 0;
        if ((ret = ftell(streams[fd])) != pos)
        {
            err = errno;
            (void) sprintf(msg, "ftell(%s) returned %ld, expected %ld",
                strnames[fd], ret, pos);
            tet_infoline(msg);
            if (err != 0)
            {
                (void) sprintf(msg, "errno was set to %d", err);
                tet_infoline(msg);
            }
            fail = 1;
        }
    }
    
    if (fail == 0)
        tet_result(TET_PASS);
    else
        tet_result(TET_FAIL);

    return 0;
}
 
 

makefile for stat-tc.c


TET_ROOT = ../../..
LIBDIR   = $(TET_ROOT)/lib/posix_c
INCDIR   = $(TET_ROOT)/inc/posix_c
CC       = cc
CFLAGS   = -I$(INCDIR) -D_POSIX_SOURCE

stat-tc:        stat-tc.c $(INCDIR)/tet_api.h
                $(CC) $(CFLAGS) -o stat-tc stat-tc.c $(LIBDIR)/tcm.o $(LIBDIR)/libapi.a
                -rm -f stat-tc.o

clean:
                rm -f stat-tc stat-tc.o

lint:
                lint $(CFLAGS) stat-tc.c -ltcm
 
 

stat-tc.c


/* stat-tc.c : test case for stat() interface */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <tet_api.h>

static void tp1(), tp2(), tp3(), tp4(), tp5(), tp6(), tp7();
static void startup(), cleanup();

/* Initialize TCM data structures */
void (*tet_startup)() = startup;
void (*tet_cleanup)() = cleanup;
struct tet_testlist tet_testlist[] = {
    { tp1, 1 },
    { tp2, 2 },
    { tp3, 3 },
    { tp4, 4 },
    { tp5, 5 },
    { tp6, 6 },
    { tp7, 7 },
    { NULL, 0 }
};


/* Test Case Wide Declarations */
static char *tfile = "stat.1";          /* test file name */
static char *tndir = "stat.1/stat.1";   /* path with non-directory in prefix */
static struct stat buf;                 /* buffer for stat(ing) file */
static char msg[256];                   /* buffer for info lines */

static void
startup()
{
    int fd;
    static char *reason = "Failed to create test file in startup";

    if ((fd=creat(tfile, S_IRWXU)) < 0)
    {
        (void) sprintf(msg,
            "creat(\"%s\", S_IRWXU) failed in startup - errno %d",
            tfile, errno);
        tet_infoline(msg);

        /* Prevent tests which use this file from executing */
        tet_delete(1, reason);
        tet_delete(7, reason);
    }
    else
        (void) close(fd);
}

static void
cleanup()
{
    /* remove file created by start-up */
    (void) unlink(tfile);

    /* remove files created by test purposes, in case they don't run
       to completion */
    (void) rmdir("stat.d");
    (void) unlink("stat.p");
}

static void
tp1()         /* successful stat of plain file: return 0 */
{
    int ret, err;

    tet_infoline("SUCCESSFUL STAT OF PLAIN FILE");

    /* stat file created in startup function and check mode indicates
       a plain file */

    errno = 0;
    if ((ret=stat(tfile, &buf)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "stat(\"%s\", buf) returned %d, expected 0",
            tfile, ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
    }
    else if (!S_ISREG(buf.st_mode))
    {
        tet_infoline("S_ISREG(st_mode) was not true for plain file");
        (void) sprintf(msg, "st_mode = 0%lo", (long)buf.st_mode);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}

static void
tp2()         /* successful stat of directory: return 0 */
{
    int ret, err;
    char *tdir = "stat.d";

    tet_infoline("SUCCESSFUL STAT OF DIRECTORY");

    /* create a test directory */

    if (mkdir(tdir, S_IRWXU) == -1)
    {
        (void) sprintf(msg,
            "mkdir(\"%s\", S_IRWXU) failed in startup - errno %d",
            tdir, errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* stat the directory and check mode indicates a directory */

    errno = 0;
    if ((ret=stat(tdir, &buf)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "stat(\"%s\", buf) returned %d, expected 0",
            tdir, ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
    }
    else if (!S_ISDIR(buf.st_mode))
    {
        tet_infoline("S_ISDIR(st_mode) was not true for directory");
        (void) sprintf(msg, "st_mode = 0%lo", (long)buf.st_mode);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
    
    (void) rmdir(tdir);
}

static void
tp3()         /* successful stat of FIFO file: return 0 */
{
    int ret, err;
    char *tfifo = "stat.p";

    tet_infoline("SUCCESSFUL STAT OF FIFO");

    /* create a test FIFO */

    if (mkfifo(tfifo, S_IRWXU) == -1)
    {
        (void) sprintf(msg,
            "mkfifo(\"%s\", S_IRWXU) failed in startup - errno %d",
            tfifo, errno);
        tet_infoline(msg);
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* stat the FIFO and check mode indicates a FIFO */

    errno = 0;
    if ((ret=stat(tfifo, &buf)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "stat(\"%s\", buf) returned %d, expected 0",
            tfifo, ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
    }
    else if (!S_ISFIFO(buf.st_mode))
    {
        tet_infoline("S_ISIFO(st_mode) was not true for FIFO file");
        (void) sprintf(msg, "st_mode = 0%lo", (long)buf.st_mode);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
    
    (void) unlink(tfifo);
}

static void
tp4()         /* successful stat of character device file: return 0 */
{
    int ret, err;
    char *chardev;

    tet_infoline("SUCCESSFUL STAT OF CHARACTER DEVICE FILE");

    /* obtain device name from execution configuration parameter */

    chardev = tet_getvar("CHARDEV");
    if (chardev == NULL || *chardev == '\0')
    {
        tet_infoline("parameter CHARDEV is not set");
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* check if parameter indicates character devices are not supported */

    if (strcmp(chardev, "unsup") == 0)
    {
        tet_infoline("parameter CHARDEV is set to \"unsup\"");
        tet_result(TET_UNSUPPORTED);
        return;
    }

    /* stat the device and check mode indicates a character device */

    errno = 0;
    if ((ret=stat(chardev, &buf)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "stat(\"%s\", buf) returned %d, expected 0",
            chardev, ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
    }
    else if (!S_ISCHR(buf.st_mode))
    {
        (void) sprintf(msg, "S_ISCHR(st_mode) was not true for \"%s\"",
            chardev);
        tet_infoline(msg);
        (void) sprintf(msg, "st_mode = 0%lo", (long)buf.st_mode);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}

static void
tp5()         /* successful stat of block device file: return 0 */
{
    int ret, err;
    char *blockdev;

    tet_infoline("SUCCESSFUL STAT OF BLOCK DEVICE FILE");

    /* obtain device name from execution configuration parameter */

    blockdev = tet_getvar("BLOCKDEV");
    if (blockdev == NULL || *blockdev == '\0')
    {
        tet_infoline("parameter BLOCKDEV is not set");
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* check if parameter indicates block devices are not supported */

    if (strcmp(blockdev, "unsup") == 0)
    {
        tet_infoline("parameter BLOCKDEV is set to \"unsup\"");
        tet_result(TET_UNSUPPORTED);
        return;
    }

    /* stat the device and check mode indicates a block device */

    errno = 0;
    if ((ret=stat(blockdev, &buf)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "stat(\"%s\", buf) returned %d, expected 0",
            blockdev, ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
    }
    else if (!S_ISBLK(buf.st_mode))
    {
        (void) sprintf(msg, "S_ISBLK(st_mode) was not true for \"%s\"",
            blockdev);
        tet_infoline(msg);
        (void) sprintf(msg, "st_mode = 0%lo", (long)buf.st_mode);
        tet_infoline(msg);
        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}

static void
tp6()       /* stat of non-existent file: return -1, errno ENOENT */
{
    int ret, err;

    tet_infoline("STAT OF NON-EXISTENT FILE");

    /* ensure file does not exist */

    if (stat("stat.6", &buf) != -1 && unlink("stat.6") == -1)
    {
        tet_infoline("could not unlink stat.6");
        tet_result(TET_UNRESOLVED);
        return;
    }

    /* check return value and errno set by call */

    errno = 0;
    ret = stat("stat.6", &buf);

    if (ret != -1 || errno != ENOENT)
    {
        err = errno;
        if (ret != -1)
        {
            (void) sprintf(msg,
                "stat(\"stat.6\", 0) returned %d, expected -1", ret);
            tet_infoline(msg);
        }

        if (err != ENOENT)
        {
            (void) sprintf(msg,
                "stat(\"stat.6\", 0) set errno to %d, expected %d (ENOENT)",
                err, ENOENT);
            tet_infoline(msg);
        }

        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}

static void
tp7()       /* non-directory path component: return -1, errno ENOTDIR */
{
    int ret, err;

    tet_infoline("STAT OF NON-DIRECTORY PATH PREFIX COMPONENT");

    /* tndir is a pathname containing a plain file (created by the
       startup function) in the prefix */

    errno = 0;
    ret = stat(tndir, &buf);

    /* check return value and errno set by call */

    if (ret != -1 || errno != ENOTDIR)
    {
        err = errno;
        if (ret != -1)
        {
            (void) sprintf(msg,
                "stat(\"%s\", 0) returned %d, expected -1", tndir, ret);
            tet_infoline(msg);
        }

        if (err != ENOTDIR)
        {
            (void) sprintf(msg,
                "stat(\"%s\", 0) set errno to %d, expected %d (ENOTDIR)",
                tndir, err, ENOTDIR);
            tet_infoline(msg);
        }

        tet_result(TET_FAIL);
    }
    else
        tet_result(TET_PASS);
}
 
 

makefile for uname-tc.c


TET_ROOT = ../../..
LIBDIR   = $(TET_ROOT)/lib/posix_c
INCDIR   = $(TET_ROOT)/inc/posix_c
CC       = cc
CFLAGS   = -I$(INCDIR) -D_POSIX_SOURCE

uname-tc:       uname-tc.c $(INCDIR)/tet_api.h
                $(CC) $(CFLAGS) -o uname-tc uname-tc.c $(LIBDIR)/tcm.o $(LIBDIR)/libapi.a
                -rm -f uname-tc.o

clean:
                rm -f uname-tc uname-tc.o

lint:
                lint $(CFLAGS) uname-tc.c -ltcm
 
 

uname-tc.c


/* uname-tc.c : test case for uname() interface */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/utsname.h>

#include <tet_api.h>

#undef TET_INSPECT      /* must undefine because TET_ is reserved prefix */
#define TET_INSPECT 33  /* this would normally be in a test suite header */

static void tp1();

/* Initialize TCM data structures */
void (*tet_startup)() = NULL;   /* no start-up function */
void (*tet_cleanup)() = NULL;   /* no clean-up function */
struct tet_testlist tet_testlist[] = {
    { tp1, 1 },
    { NULL, 0 }
};


/* Test Case Wide Declarations */
static char msg[256];                   /* buffer for info lines */

static void
tp1()         /* successful uname: return 0 */
{
    int ret, err;
    struct utsname name;

    tet_infoline("UNAME OUTPUT FOR MANUAL CHECK");

    /* The test cannot determine automatically whether the information
       returned by uname() is correct.  It therefore outputs the
       information with an INSPECT result code for checking manually. */

    errno = 0;
    if ((ret=uname(&name)) != 0)
    {
        err = errno;
        (void) sprintf(msg, "uname() returned %d, expected 0", ret);
        tet_infoline(msg);
        if (err != 0)
        {
            (void) sprintf(msg, "errno was set to %d", err);
            tet_infoline(msg);
        }
        tet_result(TET_FAIL);
    }
    else
    {
        (void) sprintf(msg, "System Name:  \"%s\"", name.sysname);
        tet_infoline(msg);
        (void) sprintf(msg, "Node Name:    \"%s\"", name.nodename);
        tet_infoline(msg);
        (void) sprintf(msg, "Release:      \"%s\"", name.release);
        tet_infoline(msg);
        (void) sprintf(msg, "Version:      \"%s\"", name.version);
        tet_infoline(msg);
        (void) sprintf(msg, "Machine Type: \"%s\"", name.machine);
        tet_infoline(msg);

        tet_result(TET_INSPECT);
    }
}

 

Home Contacts Legal Copyright Corporate News

Copyright © The Open Group 1995-2012, All Rights Reserved