source: network-demos/tftpTest/test.c @ 0fe6b0d

4.11network-demos-4-10-branch
Last change on this file since 0fe6b0d was 557c93a, checked in by Joel Sherrill <joel.sherrill@…>, on 06/21/07 at 18:26:28

2007-06-21 Joel Sherrill <joel.sherrill@…>

  • init.c, test.c: More warnings removed.
  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * Test RTEMS TFTP Client Filesystem
3 *
4 * This program may be distributed and used for any purpose.
5 * I ask only that you:
6 *      1. Leave this author information intact.
7 *      2. Document any changes you make.
8 *
9 * W. Eric Norum
10 * Saskatchewan Accelerator Laboratory
11 * University of Saskatchewan
12 * Saskatoon, Saskatchewan, CANADA
13 * eric@skatter.usask.ca
14 *
15 *  $Id$
16 */
17
18#include <stdio.h>
19#include <errno.h>
20#include <string.h>
21#include <rtems.h>
22#include <rtems/error.h>
23#include <sys/fcntl.h>
24#include <unistd.h>
25#include <stdlib.h>
26
27static char cbuf[1024];
28static char *fullname;
29static rtems_interval then, now;
30
31static void
32showRate (unsigned long totalRead)
33{
34        int elapsed;
35
36        printf ("Read %lu bytes", totalRead);
37        elapsed = now - then;
38        if (elapsed) {
39                rtems_interval ticksPerSecond;
40                rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
41                printf (" (%ld bytes/sec)",
42                                (long)(((long long)totalRead * ticksPerSecond)
43                                                                / elapsed));
44        }
45        printf (".\n");
46}
47
48static void
49testRawRead (void)
50{
51        int fd;
52        int nread;
53        unsigned long totalRead = 0;
54
55        fd = open (fullname, O_RDONLY);
56        if (fd < 0) {
57                printf ("Open failed: %s\n", strerror (errno));
58                return;
59        }
60
61        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
62        for (;;) {
63                nread = read (fd, cbuf, sizeof cbuf);
64                if (nread < 0) {
65                        printf ("Read failed: %s\n", strerror (errno));
66                        close (fd);
67                        return;
68                }
69                if (nread == 0)
70                        break;
71                totalRead += nread;
72        }
73        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
74        close (fd);
75        showRate (totalRead);
76}
77
78static void
79testFread (void)
80{
81        FILE *fp;
82        int nread;
83        unsigned long totalRead = 0;
84
85        fp = fopen (fullname, "r");
86        if (fp == NULL) {
87                printf ("Open failed: %s\n", strerror (errno));
88                return;
89        }
90
91        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
92        for (;;) {
93                nread = fread (cbuf, sizeof cbuf[0], sizeof cbuf, fp);
94                if (nread < 0) {
95                        printf ("Read failed: %s\n", strerror (errno));
96                        fclose (fp);
97                        return;
98                }
99                if (nread == 0)
100                        break;
101                totalRead += nread;
102        }
103        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
104        fclose (fp);
105        showRate (totalRead);
106}
107
108static int
109makeFullname (const char *hostname, const char *file)
110{
111        if (hostname == NULL)
112                hostname = "";
113        fullname = realloc (fullname, 8 + strlen (file) + strlen (hostname));
114        sprintf (fullname, "/TFTP/%s/%s", hostname, file);
115        printf ("Read `%s'.\n", fullname);
116        return 1;
117}
118
119void
120testTFTP (const char *hostname, const char *filename)
121{
122        /*
123         * Check that invalid file names are reported as such
124         */
125        if (makeFullname (hostname, "")) {
126                testRawRead ();
127                testFread ();
128        }
129
130        /*
131         * Check that non-existent files are reported as such
132         */
133        if (makeFullname (hostname, "BAD-FILE-NAME")) {
134                testRawRead ();
135                testFread ();
136        }
137
138        /*
139         * Check that read works
140         */
141        if (makeFullname (hostname, filename)) {
142                testRawRead ();
143                testFread ();
144        }
145}
Note: See TracBrowser for help on using the repository browser.