summaryrefslogtreecommitdiff
path: root/src/bin/evil/evil_test_link.c
blob: 868e9f23d2ca335cb2332bcd321cc01b597cc4a9 (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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <string.h>

#include <Evil.h>

#include "evil_suite.h"
#include "evil_test_link.h"

static int
test_link_test_file_create(const char *name, const char *data)
{
   FILE       *f;
   size_t      length;
   size_t      res;

   f = fopen(name, "wb");
   if (!f)
     return 0;

   length = strlen(data) + 1;
   res = fwrite(data, 1, length, f);
   if (res < length)
     {
        fclose(f);
        return 0;
     }

   fclose(f);

   return 1;
}

static int
test_link_test_symlink(void)
{
#ifdef _WIN32_WCE
   const char *old_name = "\\efl\\evil_test_link.dat";
   const char *new_name = "\\efl\\evil_test_link.lnk";
#else
   const char *old_name = "evil_test_link.dat";
   const char *new_name = "evil_test_link.lnk";
#endif

   if (!test_link_test_file_create(old_name,
                                   "evil_test_link symlink data\n"))
     return 0;

   if (symlink(old_name, new_name) < 0)
     {
        unlink(old_name);
        return 0;
     }

   if (unlink(new_name) < 0)
     {
        unlink(old_name);
        return 0;
     }

   if (unlink(old_name) < 0)
     return 0;

   return 1;
}

static int
test_link_test_readlink(void)
{
   char        buf[1024];
#ifdef _WIN32_WCE
   const char *old_name = "\\efl\\evil_test_link.dat";
   const char *new_name = "\\efl\\evil_test_link.lnk";
#else
   const char *old_name = "evil_test_link.dat";
   const char *new_name = "evil_test_link.lnk";
#endif
   const char *data = "evil_test_link symlink data\n";
   FILE       *f;
   ssize_t     s1;
   size_t      s2;
   int         l;

   if (!test_link_test_file_create(old_name, data))
     return 0;

   if (symlink(old_name, new_name) < 0)
     return 0;

   if ((s1 = readlink(new_name, buf, 1023)) < 0)
     {
        unlink(old_name);
        unlink(new_name);
        return 0;
     }

   buf[s1] = '\0';

   f = fopen(buf, "rb");
   if (!f)
     {
        unlink(old_name);
        unlink(new_name);
        return 0;
     }

   l = strlen(data);
   s2 = fread(buf, 1, l + 1, f);

   if ((int)s2 != (l + 1))
     {
        fclose(f);
        unlink(old_name);
        unlink(new_name);
        return 0;
     }

   if (strcmp(buf, data))
     {
        fclose(f);
        unlink(old_name);
        unlink(new_name);
        return 0;
     }

   fclose(f);

   if (unlink(new_name) < 0)
     {
        unlink(old_name);
        return 0;
     }

   if (unlink(old_name) < 0)
     return 0;

   return 1;
}

static int
test_link_tests_run(suite *s)
{
   int res;

   res  = test_link_test_symlink();
   res &= test_link_test_readlink();

   return res;
}

int
test_link(suite *s)
{

   return test_link_tests_run(s);
}