Test cases of dup() for radix tree__alloc_fd

I have written the test case for dup( ).
This is dup( ) functionality test case. The dup( ) use for duplicate the file descriptor.
This test case covers below different scenarios:
* Duplicate file descriptor five times one by one.
* Fill up all file descriptor table and try to duplicate a file descriptor. dup( ) should not be succeeded.
* Give invalid file descriptor to the dup( ). dup( ) should be failed.
* Check inode number after passing newfd and oldfd to fstat( ). inode number should be same.
 Upon successful execution this program will return zero value, otherwise, it will return -1.



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int maxfd;

int dup_fd(int count)
{
 int fd[count],i;
 char buf[15];

for(i = 0; i < count; i++)
 {
 sprintf(buf, test/myfile%d, i);
 fd[i] = open(buf, O_CREAT | O_RDWR, 0644); 
 printf(open: %d\n, fd[i]);
 if(fd[i] == -1)
 {
 printf(open() failed\n);
 return -1;
 }
 fd [i+1] = dup(i);
 if (fd [i+1] == -1)
 {
 printf(dup() failed\n);
 return -1;
 }
 printf(dup: %d\n, fd [i+1]);
 }
 
 return 0;
}
void close_fd(int count)
{
 int i;
 for (i = 3; i < count; i++)
 {
 close(i);
 }
}

void unlink_files(int count)
{
 char buf[15];
 int i;

for(i = 0; i < count; i++)
 {
 sprintf(buf,test/myfile%d,i);
 unlink(buf);
 }
}

int max_fd_dup() 
{
 int *fd, i;
 int ret;
 char buf[15];
 maxfd = getdtablesize(); /* get descriptor table size */

 fd = malloc(maxfd * sizeof(int)); /* allocate the memory */

 printf(maxfd = %d\n,maxfd);

 for (i = 0; i < (maxfd-3); i++)
 {
 sprintf(buf, test/myfile%d, i);
 fd[i] = open(buf, O_CREAT | O_RDWR, 0644); 
 if (fd[i] == -1)
 {
 printf(max_fd_dup() :: open() failed\n);
 return -1;
 }
 }
 printf(here *****************\n);
 
 ret = dup(10); /* duplicate the file descriptor */
 
 if(ret == -1)
 printf(max_fd_dup: tc : pass\n);
 else
 printf(max_fd_dup: tc : fail\n);

return 0;
}
int dup_misc_tc()
{
 int *fd, i, maxfd;
 struct stat buf1, buf2;
 char buf[20];
 int ret, oldfd, newfd;

 ret = dup(10);
  if (ret == -1)
  {
  printf(dup_misc_tc1 : pass\n);
  }
  else
  {
  printf(dup_misc_tc1 : fail\n);
  }

 ret = dup(-1);

 if (ret == -1)
  {
  printf(dup_misc_tc2 : pass\n);
  }
 else
  {
  printf(dup_misc_tc2 : fail\n);
  }

 oldfd = open(test/file1, O_CREAT | O_RDWR, 0644);
 if(oldfd == -1)
 {
  printf(dup_misc_tc() : open() failed\n);
  return -1;
 }
 
 newfd = dup(oldfd);
 if(newfd == -1)
 {
  printf(dup_misc_tc() : dup() failed\n);
  return -1;
 }

 ret = fstat(oldfd, &buf1); /* get the file status */
 if(ret == -1)
 {
  printf(dup_misc_tc() : fstat() failed\n);
  return -1;
 }
 ret = fstat(newfd, &buf2);
 if(ret == -1)
 {
  printf(dup_misc_tc() : fstat() failed\n);
  return -1;
 }

 printf(buf1.st_ino = %d\n,buf1.st_ino);
 printf(buf2.st_ino = %d\n,buf2.st_ino);
 if(buf1.st_ino == buf2.st_ino)
  printf(dup_misc_tc3 : pass\n);
 else
  printf(dup_misc_tc3 : fail\n);
 close(oldfd);
 close(newfd);
 unlink(test/file1);
 return 0;
}

int main(int argc, char **argv)
{
 int err = 0;
 char *name;

if(argc < 2)
 {
 printf(usage: %s <tcNum> \n, argv[0]);
 printf(provide options to program as tc1, tc2, tc3 etc\n);
 return -1;
 }
 name = argv[1];

if(strcmp(name, tc1) == 0)
 {

err = dup_fd(5);
 if (err == -1)
 {
 return -1;
 }
 close_fd(10);
 unlink_files(5);
 }
 else if(strcmp(name, tc2) == 0)
 {
  max_fd_dup();
  close_fd(maxfd-3);
  unlink_files(maxfd);
 }
 else if(strcmp(name, tc3) == 0)
 {
  dup_misc_tc();
 }
 else
  printf(Invalid option \n);

return 0;
}

Comments

Popular posts from this blog

OPEN SOURCE SUMMIT NORTH AMERICA 2017

Test cases of dup2() for radix tree__alloc_fd

Threaded test case for radix tree __alloc_fd