Tuesday, 31 January 2017

Test cases of dup2() for radix tree__alloc_fd

I have written a test case of dup2()
This is dup2() functionality test case. The dup2() makes newfd be the copy of oldfd, closing newfd first if necessary.
This test case covers below scenarios
  * Provide invalid file descriptor to dup2()
  * Fill up all file descriptor table and try to duplicate a file descriptor using dup2().
  * Using dup2() duplicate both file descriptor return by the pipe.
  * 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#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 dup2_misc_tc()
{
 int *fd, i, maxfd;
 int ret, goodfd;

goodfd = open("test/file1", O_CREAT | O_RDWR, 0644);
 
 if(goodfd == -1)
 {
  printf("dup2_misc_tc() : dup2 failed...\n");
  return -1;
  }
 ret = dup2(-1, goodfd);
 if(ret == -1)
 {
  printf("dup2_misc_tc1 : Pass \n");
 }
 else
 {
  printf("dup2_misc_tc1: fail \n");
 }
 ret = dup2(goodfd, -1);
 if(ret == -1)
 {
  printf("dup2_misc_tc1 : Pass \n");
 }
 else
 {
  printf("dup2_misc_tc1: fail \n");
 }

close(goodfd);
 unlink("test/file1");

return 0;
}

int max_fd_dup2()
{
 int *fd, i;
 int ret;
 char buf[15];
 maxfd = getdtablesize();
 
 fd = malloc(maxfd * sizeof(int));
 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_dup2() :: open() failed...\n");
   return -1;
  }
 }
 printf("here ***************\n");
 ret = dup2(10,24);
 if(ret == -1)
  printf("max_fd_dup2: tc: Fail\n");
 else
  printf("max_fd_dup2: tc: Pass\n");
 
 ret = dup2(10,maxfd);
 if(ret == -1)
  printf("max_fd_dup2: tc: Pass\n");
 else
  printf("max_fd_dup2: tc: Fail\n");

return 0;
 
}

int pipe_dup2_tc()
{
 int fd[2];
 int ret;
 ret = pipe(fd);
 if(ret == -1)
 {
  printf("pipe_dup2_tc : Pipe Failed\n");
  return -1;
 }
 ret = dup2(fd[0],5);
 
 if(ret == -1)
 
  printf("pipe_dup2_tc : fail\n");
 else
  printf("pipe_dup2_tc: pass1\n");

ret = dup2(fd[1],6); 
 if(ret == -1)
 
  printf("pipe_dup2_tc : fail\n");
 else
  printf("pipe_dup2_tc: pass2\n");
 
 close(fd[0]);
 close(fd[1]);
 return 0;
}

int dup2_ino_tc()
{
        struct stat buf1, buf2;
        char buf[20];
        int ret, oldfd, newfd, fd_fd;

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

ret = dup2(oldfd,newfd);
        if (ret == -1)
        {
                printf("dup2_ino_tc : fail\n");
        }
        else
        {
                printf("dup2_ino_tc : pass\n");
        }

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

ret = fstat(newfd, &buf2);
        if(ret == -1)
        {
                printf("dup2_ino_tc() : fstat() failed...\n");
                return -1;
        }

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

int 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 main(int argc, char **argv)
{
 int err = 0;
 char *name;
 int goodfd;
 if(argc < 2)
 {
  printf("usage: %s <tcNUM> \n", argv[0]);
  printf("Provide oprion to program as tc1, tc2, tc3, tc4\n");
  return -1;
 }
 name = argv[1];
 
 if(strcmp(name, "tc1") == 0)
 {
  err = dup2_misc_tc();
  if(err == -1)
  {
   return -1;
  }
 }
 else if(strcmp(name, "tc2") == 0)
 {
  max_fd_dup2();
  close_fd(maxfd-3);
  unlink_files(maxfd);
 }
 
 else if(strcmp(name, "tc3") == 0)
 {
  pipe_dup2_tc(); 
 }
 
 else if(strcmp(name, "tc4") == 0)
 {
  dup2_ino_tc();
 }

return 0;
}

No comments:

Post a Comment