summaryrefslogtreecommitdiffstats
path: root/testsuites/fstests/fslink/test.c
blob: 1e5816286089ff964d80469aa9be3b5c2f7b52d7 (plain) (blame)
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
/*
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#include <stdint.h>
#include <math.h>

#include "fstest.h"
#include "pmacros.h"

/*
 * Test if the successful call works as expect
 */
static void link_test01 (void)
{
  char *name0 = "t0";
  char *name1 = "t1";
  char *name2 = "t2";


  int status;
  int fd;

  mode_t mode = 0644;
  struct stat statbuf;


  puts ("link creates hardlinks");
  fd = creat (name0, mode);
  status = close (fd);
  rtems_test_assert (status == 0);

  status = stat (name0, &statbuf);
  rtems_test_assert (status == 0);
  rtems_test_assert (statbuf.st_nlink == 1);

  puts ("test if the stat is the same");
  status = link (name0, name1);
  rtems_test_assert (status == 0);

  status = stat (name0, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 2);

  status = stat (name1, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 2);

  /*
   * link the file and check the nlink
   */
  status = link (name1, name2);
  rtems_test_assert (status == 0);

  status = stat (name0, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 3);

  status = stat (name1, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 3);

  status = stat (name2, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 3);

  /*
   *  call chmod and chown and test.
   */
  puts ("chmod and chown");

  chown (name1, 65534, 65533);

  status = stat (name0, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 3);
  rtems_test_assert (statbuf.st_uid = 65534);
  rtems_test_assert (statbuf.st_gid = 65533);

  status = stat (name1, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 3);
  rtems_test_assert (statbuf.st_uid = 65534);
  rtems_test_assert (statbuf.st_gid = 65533);

  status = stat (name2, &statbuf);
  rtems_test_assert (status == 0);

  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 3);
  rtems_test_assert (statbuf.st_uid = 65534);
  rtems_test_assert (statbuf.st_gid = 65533);

  /*
   *
   *  unlink then test if the nlink changes
   */
  puts ("unlink then stat the file ");

  status = unlink (name0);
  rtems_test_assert (status == 0);

  status = stat (name0, &statbuf);
  rtems_test_assert (status == -1);
  rtems_test_assert (errno = ENOENT);

  status = stat (name1, &statbuf);
  rtems_test_assert (status == 0);
  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 2);

  status = stat (name2, &statbuf);
  rtems_test_assert (status == 0);
  rtems_test_assert (S_ISREG (statbuf.st_mode));
  rtems_test_assert (statbuf.st_nlink == 2);

  status = unlink (name1);
  rtems_test_assert (status == 0);

  status = unlink (name2);
  rtems_test_assert (status == 0);

}

void test (void)
{
  puts ("\n\n*** LINK TEST ***");
  link_test01 ();
  puts ("*** END OF LINK TEST ***");
}