File size: 8,815 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "../../unity/unity.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

/* The tests are included into the same translation unit as ln.c,
   so we can access atomic_link and the static flags:
   - symbolic_link
   - relative
   - beware_hard_dir_link
   - logical
*/

/* ---------- Test helpers ---------- */

static char *test_dir = NULL;
static int test_dfd = -1;

/* Join directory and base into a newly-allocated path string. */
static char *test_join(const char *dir, const char *base)
{
  size_t len_dir = strlen(dir);
  size_t len_base = strlen(base);
  int need_slash = (len_dir > 0 && dir[len_dir - 1] != '/');
  size_t total = len_dir + need_slash + len_base + 1;
  char *res = (char *)malloc(total);
  if (!res) return NULL;
  memcpy(res, dir, len_dir);
  if (need_slash) res[len_dir++] = '/';
  memcpy(res + len_dir, base, len_base);
  res[len_dir + len_base] = '\0';
  return res;
}

/* Create a temporary directory for each test. */
static char *make_temp_dir(void)
{
  char tmpl[] = "ln_atomic_link_test.XXXXXX";
  char *buf = (char *)malloc(sizeof(tmpl));
  if (!buf) return NULL;
  memcpy(buf, tmpl, sizeof(tmpl));
  char *d = mkdtemp(buf);
  if (!d)
    {
      free(buf);
      return NULL;
    }
  return buf; /* buf now contains the directory path */
}

/* Recursively remove a path. */
static int rm_rf(const char *path)
{
  struct stat st;
  if (lstat(path, &st) != 0)
    return (errno == ENOENT) ? 0 : -1;

  if (S_ISDIR(st.st_mode))
    {
      DIR *dir = opendir(path);
      if (!dir) return -1;
      struct dirent *de;
      while ((de = readdir(dir)) != NULL)
        {
          if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
            continue;
          char *child = test_join(path, de->d_name);
          if (!child)
            {
              closedir(dir);
              return -1;
            }
          if (rm_rf(child) != 0)
            {
              free(child);
              closedir(dir);
              return -1;
            }
          free(child);
        }
      closedir(dir);
      return rmdir(path);
    }
  else
    {
      return unlink(path);
    }
}

/* Create a file with optional content. */
static int create_file(const char *path, const char *content)
{
  int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  if (fd < 0) return -1;
  if (content)
    {
      size_t len = strlen(content);
      ssize_t wr = write(fd, content, len);
      if (wr < 0 || (size_t)wr != len)
        {
          int e = errno;
          close(fd);
          errno = e;
          return -1;
        }
    }
  return close(fd);
}

/* Assert that a path does not exist. */
static void assert_path_not_exists(const char *path)
{
  struct stat st;
  int r = lstat(path, &st);
  TEST_ASSERT_TRUE_MESSAGE(r != 0, "lstat unexpectedly succeeded");
  TEST_ASSERT_EQUAL_INT(ENOENT, errno);
}

/* ---------- Unity hooks ---------- */

void setUp(void)
{
  test_dir = make_temp_dir();
  TEST_ASSERT_NOT_NULL_MESSAGE(test_dir, "Failed to create temp directory");

  test_dfd = open(test_dir, O_RDONLY | O_DIRECTORY);
  TEST_ASSERT_TRUE_MESSAGE(test_dfd >= 0, "Failed to open temp directory");

  /* Ensure flags are in a known state before each test. */
  symbolic_link = false;
  relative = false;
  beware_hard_dir_link = false;
  logical = false;
}

void tearDown(void)
{
  if (test_dfd >= 0)
    close(test_dfd);
  test_dfd = -1;

  if (test_dir)
    {
      rm_rf(test_dir);
      free(test_dir);
      test_dir = NULL;
    }
}

/* ---------- Tests ---------- */

/* Hard link success: should return 0 and create a hard link with same inode/dev. */
static void test_atomic_link_hard_success(void)
{
  char *src = test_join(test_dir, "src1");
  char *dst_full = test_join(test_dir, "dst1");
  TEST_ASSERT_NOT_NULL(src);
  TEST_ASSERT_NOT_NULL(dst_full);

  TEST_ASSERT_EQUAL_INT(0, create_file(src, "hello"));

  symbolic_link = false;
  beware_hard_dir_link = false;
  logical = false;

  int ret = atomic_link(src, test_dfd, "dst1");
  TEST_ASSERT_EQUAL_INT(0, ret);

  struct stat s1, s2;
  TEST_ASSERT_EQUAL_INT(0, stat(src, &s1));
  TEST_ASSERT_EQUAL_INT(0, stat(dst_full, &s2));

  TEST_ASSERT_EQUAL_INT(s1.st_dev, s2.st_dev);
  TEST_ASSERT_EQUAL_INT(s1.st_ino, s2.st_ino);

  free(src);
  free(dst_full);
}

/* Hard link failure when destination exists: expect positive errno EEXIST and no change. */
static void test_atomic_link_hard_eexist(void)
{
  char *src = test_join(test_dir, "src2");
  char *dst_full = test_join(test_dir, "dst2");
  TEST_ASSERT_NOT_NULL(src);
  TEST_ASSERT_NOT_NULL(dst_full);

  TEST_ASSERT_EQUAL_INT(0, create_file(src, "hello"));
  TEST_ASSERT_EQUAL_INT(0, create_file(dst_full, "preexisting"));

  struct stat before;
  TEST_ASSERT_EQUAL_INT(0, lstat(dst_full, &before));

  symbolic_link = false;
  beware_hard_dir_link = false;
  logical = false;

  int ret = atomic_link(src, test_dfd, "dst2");
  TEST_ASSERT_EQUAL_INT(EEXIST, ret);

  struct stat after;
  TEST_ASSERT_EQUAL_INT(0, lstat(dst_full, &after));
  TEST_ASSERT_EQUAL_INT(before.st_dev, after.st_dev);
  TEST_ASSERT_EQUAL_INT(before.st_ino, after.st_ino);

  free(src);
  free(dst_full);
}

/* Hard link when beware_hard_dir_link is true: should return -1 and not create destination. */
static void test_atomic_link_beware_returns_minus1(void)
{
  char *src = test_join(test_dir, "src3");
  char *dst_full = test_join(test_dir, "dst3");
  TEST_ASSERT_NOT_NULL(src);
  TEST_ASSERT_NOT_NULL(dst_full);

  TEST_ASSERT_EQUAL_INT(0, create_file(src, "hello"));

  symbolic_link = false;
  beware_hard_dir_link = true; /* triggers -1 path */
  logical = false;

  int ret = atomic_link(src, test_dfd, "dst3");
  TEST_ASSERT_EQUAL_INT(-1, ret);

  assert_path_not_exists(dst_full);

  free(src);
  free(dst_full);
}

/* Symbolic link with relative=true: should return -1 and not create destination. */
static void test_atomic_link_symbolic_relative_returns_minus1(void)
{
  char *src = test_join(test_dir, "src4");
  char *dst_full = test_join(test_dir, "symrel");
  TEST_ASSERT_NOT_NULL(src);
  TEST_ASSERT_NOT_NULL(dst_full);

  /* Source need not exist for symlinkat, but create it anyway. */
  TEST_ASSERT_EQUAL_INT(0, create_file(src, "hello"));

  symbolic_link = true;
  relative = true; /* triggers -1 for atomic_link */

  int ret = atomic_link(src, test_dfd, "symrel");
  TEST_ASSERT_EQUAL_INT(-1, ret);

  assert_path_not_exists(dst_full);

  free(src);
  free(dst_full);
}

/* Symbolic link success: should return 0 and create a symlink pointing to the given source string. */
static void test_atomic_link_symbolic_success(void)
{
  char *src = test_join(test_dir, "src5");
  char *dst_full = test_join(test_dir, "sym1");
  TEST_ASSERT_NOT_NULL(src);
  TEST_ASSERT_NOT_NULL(dst_full);

  TEST_ASSERT_EQUAL_INT(0, create_file(src, "world"));

  symbolic_link = true;
  relative = false;

  int ret = atomic_link(src, test_dfd, "sym1");
  TEST_ASSERT_EQUAL_INT(0, ret);

  struct stat lst;
  TEST_ASSERT_EQUAL_INT(0, lstat(dst_full, &lst));
  TEST_ASSERT_TRUE_MESSAGE(S_ISLNK(lst.st_mode), "Destination is not a symlink");

  char buf[PATH_MAX];
  ssize_t n = readlink(dst_full, buf, sizeof(buf) - 1);
  TEST_ASSERT_TRUE_MESSAGE(n >= 0, "readlink failed");
  buf[n] = '\0';
  TEST_ASSERT_EQUAL_STRING(src, buf);

  free(src);
  free(dst_full);
}

/* Symbolic link failure when destination exists: expect EEXIST and no change. */
static void test_atomic_link_symbolic_eexist(void)
{
  char *src = test_join(test_dir, "src6");
  char *dst_full = test_join(test_dir, "sym2");
  TEST_ASSERT_NOT_NULL(src);
  TEST_ASSERT_NOT_NULL(dst_full);

  TEST_ASSERT_EQUAL_INT(0, create_file(src, "data"));
  /* Precreate destination as a regular file. */
  TEST_ASSERT_EQUAL_INT(0, create_file(dst_full, "preexisting"));

  struct stat before;
  TEST_ASSERT_EQUAL_INT(0, lstat(dst_full, &before));

  symbolic_link = true;
  relative = false;

  int ret = atomic_link(src, test_dfd, "sym2");
  TEST_ASSERT_EQUAL_INT(EEXIST, ret);

  struct stat after;
  TEST_ASSERT_EQUAL_INT(0, lstat(dst_full, &after));
  TEST_ASSERT_EQUAL_INT(before.st_dev, after.st_dev);
  TEST_ASSERT_EQUAL_INT(before.st_ino, after.st_ino);

  free(src);
  free(dst_full);
}

/* ---------- Unity main ---------- */

int main(void)
{
  UNITY_BEGIN();

  RUN_TEST(test_atomic_link_hard_success);
  RUN_TEST(test_atomic_link_hard_eexist);
  RUN_TEST(test_atomic_link_beware_returns_minus1);
  RUN_TEST(test_atomic_link_symbolic_relative_returns_minus1);
  RUN_TEST(test_atomic_link_symbolic_success);
  RUN_TEST(test_atomic_link_symbolic_eexist);

  return UNITY_END();
}