8
8
#include "test-common.h"
9
9
10
10
#define BUFFER_SIZE 1024
11
+ #define TEST_TMP_DIR "./out/tmp"
11
12
12
13
static uvwasi_t uvwasi ;
13
14
static uvwasi_options_t init_options ;
@@ -25,7 +26,7 @@ static void check_normalize(char* path, char* expected) {
25
26
assert (0 == strcmp (buffer , expected ));
26
27
}
27
28
28
- static uvwasi_errno_t check (char * fd_mp , char * fd_rp , char * path , char * * res ) {
29
+ static uvwasi_errno_t check (char * fd_mp , char * fd_rp , char * path , char * * res , uvwasi_lookupflags_t flags ) {
29
30
struct uvwasi_fd_wrap_t fd ;
30
31
uvwasi_errno_t err ;
31
32
uvwasi_size_t len ;
@@ -48,21 +49,51 @@ static uvwasi_errno_t check(char* fd_mp, char* fd_rp, char* path, char** res) {
48
49
strlen (fd_mp ));
49
50
if (err != UVWASI_ESUCCESS )
50
51
return err ;
51
- return uvwasi__resolve_path (& uvwasi , & fd , path , len , res , 0 );
52
+ return uvwasi__resolve_path (& uvwasi , & fd , path , len , res , flags );
52
53
}
53
54
54
55
static void pass (char * mp , char * rp , char * path , char * expected ) {
55
56
char * resolved ;
57
+ char * resolved_follow ;
56
58
size_t res_len ;
57
59
size_t i ;
58
60
59
- assert (UVWASI_ESUCCESS == check (mp , rp , path , & resolved ));
61
+ assert (UVWASI_ESUCCESS == check (mp , rp , path , & resolved , 0 ));
60
62
res_len = strlen (resolved );
61
63
assert (res_len == strlen (expected ));
62
64
65
+ assert (UVWASI_ESUCCESS == check (mp , rp , path , & resolved_follow , UVWASI_LOOKUP_SYMLINK_FOLLOW ));
66
+ assert (strlen (resolved_follow ) == res_len );
67
+
63
68
for (i = 0 ; i < res_len + 1 ; i ++ ) {
64
69
#ifdef _WIN32
65
70
if (resolved [i ] == '\\' ) {
71
+ assert (resolved_follow [i ] == '\\' );
72
+ assert (expected [i ] == '/' );
73
+ continue ;
74
+ }
75
+ #endif /* _WIN32 */
76
+
77
+ assert (resolved [i ] == resolved_follow [i ]);
78
+ assert (resolved [i ] == expected [i ]);
79
+ }
80
+
81
+ free (resolved );
82
+ }
83
+
84
+ static void pass_follow (char * mp , char * rp , char * path , char * expected ) {
85
+ char * resolved ;
86
+ size_t res_len ;
87
+ size_t i ;
88
+
89
+ assert (UVWASI_ESUCCESS == check (mp , rp , path , & resolved , UVWASI_LOOKUP_SYMLINK_FOLLOW ));
90
+ res_len = strlen (resolved );
91
+ assert (res_len == strlen (expected ));
92
+
93
+ for (i = 0 ; i < res_len + 1 ; i ++ ) {
94
+ #ifdef _WIN32
95
+ if (resolved [i ] == '\\' ) {
96
+ assert (resolved_follow [i ] == '\\' );
66
97
assert (expected [i ] == '/' );
67
98
continue ;
68
99
}
@@ -76,9 +107,39 @@ static void pass(char* mp, char* rp, char* path, char* expected) {
76
107
77
108
static void fail (char * mp , char * rp , char * path , uvwasi_errno_t expected ) {
78
109
char * resolved ;
110
+ char * resolved_follow ;
79
111
80
- assert (expected == check (mp , rp , path , & resolved ));
112
+ assert (expected == check (mp , rp , path , & resolved , 0 ));
81
113
assert (resolved == NULL );
114
+
115
+ assert (expected == check (mp , rp , path , & resolved_follow , UVWASI_LOOKUP_SYMLINK_FOLLOW ));
116
+ assert (resolved_follow == NULL );
117
+ }
118
+
119
+ static void fail_follow (char * mp , char * rp , char * path , uvwasi_errno_t expected )
120
+ {
121
+ uvwasi_errno_t err ;
122
+ char * resolved ;
123
+
124
+ assert (expected == check (mp , rp , path , & resolved , UVWASI_LOOKUP_SYMLINK_FOLLOW ));
125
+ assert (resolved == NULL );
126
+ }
127
+
128
+ static void create_symlink (char * src , char * real_dst ) {
129
+ uv_fs_t req ;
130
+ int r ;
131
+
132
+ r = uv_fs_mkdir (NULL , & req , TEST_TMP_DIR , 0777 , NULL );
133
+ uv_fs_req_cleanup (& req );
134
+ assert (r == 0 || r == UV_EEXIST );
135
+
136
+ r = uv_fs_mkdir (NULL , & req , TEST_TMP_DIR "/dir" , 0777 , NULL );
137
+ uv_fs_req_cleanup (& req );
138
+ assert (r == 0 || r == UV_EEXIST );
139
+
140
+ r = uv_fs_symlink (NULL , & req , src , real_dst , 0 , NULL );
141
+ uv_fs_req_cleanup (& req );
142
+ assert (r == 0 || r == UV_EEXIST );
82
143
}
83
144
84
145
int main (void ) {
@@ -149,6 +210,34 @@ int main(void) {
149
210
fail ("foo" , "/baz" , "../../foo/test_path" , UVWASI_ENOTCAPABLE );
150
211
fail ("../baz" , "/foo" , "../bak/test_path" , UVWASI_ENOTCAPABLE );
151
212
213
+ /* Arguments: source path, destination real path */
214
+ create_symlink ("foo" , TEST_TMP_DIR "/bar" );
215
+ create_symlink ("./foo" , TEST_TMP_DIR "/bar2" );
216
+ create_symlink ("/foo" , TEST_TMP_DIR "/bar3" );
217
+ create_symlink ("../foo" , TEST_TMP_DIR "/bar4" );
218
+ create_symlink ("/../foo" , TEST_TMP_DIR "/bar5" );
219
+ create_symlink ("bar" , TEST_TMP_DIR "/baz" );
220
+ create_symlink ("./bar" , TEST_TMP_DIR "/baz2" );
221
+ create_symlink ("/bar" , TEST_TMP_DIR "/baz3" );
222
+ create_symlink ("../foo" , TEST_TMP_DIR "/dir/qux" );
223
+ create_symlink ("./qux" , TEST_TMP_DIR "/dir/quux" );
224
+
225
+ /* Arguments: fd mapped path, fd real path, path to resolve, expected path */
226
+ pass_follow ("/" , TEST_TMP_DIR , "/bar" , TEST_TMP_DIR "/foo" );
227
+ pass_follow ("/" , TEST_TMP_DIR , "/bar2" , TEST_TMP_DIR "/foo" );
228
+ pass_follow ("/" , TEST_TMP_DIR , "/bar3" , TEST_TMP_DIR "/foo" );
229
+ pass_follow ("/" , TEST_TMP_DIR , "/bar4" , TEST_TMP_DIR "/foo" );
230
+ pass_follow ("/" , TEST_TMP_DIR , "/bar5" , TEST_TMP_DIR "/foo" );
231
+ pass_follow ("/" , TEST_TMP_DIR , "/baz" , TEST_TMP_DIR "/foo" );
232
+ pass_follow ("/" , TEST_TMP_DIR , "/baz2" , TEST_TMP_DIR "/foo" );
233
+ pass_follow ("/" , TEST_TMP_DIR , "/baz3" , TEST_TMP_DIR "/foo" );
234
+ pass_follow ("/" , TEST_TMP_DIR , "/dir/qux" , TEST_TMP_DIR "/foo" );
235
+ pass_follow ("/" , TEST_TMP_DIR , "/dir/quux" , TEST_TMP_DIR "/foo" );
236
+
237
+ /* Arguments: fd mapped path, fd real path, path to resolve, expected error */
238
+ fail_follow ("/dir" , TEST_TMP_DIR "/dir" , "/dir/qux" , UVWASI_ENOTCAPABLE );
239
+ fail_follow ("/dir" , TEST_TMP_DIR "/dir" , "/dir/quux" , UVWASI_ENOTCAPABLE );
240
+
152
241
uvwasi_destroy (& uvwasi );
153
242
return 0 ;
154
243
}
0 commit comments