|
| 1 | +#ifdef_WIN32 |
| 2 | + |
| 3 | +#include"uv.h" |
| 4 | +#include"task.h" |
| 5 | +#include<stdio.h> |
| 6 | +#include<stdlib.h> |
| 7 | +#include<string.h> |
| 8 | + |
| 9 | +staticcharget_current_drive(void) { |
| 10 | +charcwd[MAX_PATH]; |
| 11 | +size_tcwd_size=sizeof(cwd); |
| 12 | +if (uv_cwd(cwd,&cwd_size)!=0) |
| 13 | +return0; |
| 14 | +if (cwd[0] >='A'&&cwd[0] <='Z') |
| 15 | +returncwd[0]; |
| 16 | +if (cwd[0] >='a'&&cwd[0] <='z') |
| 17 | +returncwd[0]-'a'+'A'; |
| 18 | +return0; |
| 19 | +} |
| 20 | + |
| 21 | +staticintget_drive_env(chardrive,char*buf,size_tbuflen) { |
| 22 | +charenv_name[4]; |
| 23 | +env_name[0]='='; |
| 24 | +env_name[1]=drive; |
| 25 | +env_name[2]=':'; |
| 26 | +env_name[3]='\0'; |
| 27 | + |
| 28 | +DWORDresult=GetEnvironmentVariableA(env_name,buf, (DWORD)buflen); |
| 29 | +returnresult>0&&result<buflen; |
| 30 | +} |
| 31 | + |
| 32 | +TEST_IMPL(chdir_sets_drive_env_for_normal_path) { |
| 33 | +charoriginal_cwd[1024]; |
| 34 | +size_tsize=sizeof(original_cwd); |
| 35 | +charenv_value[1024]; |
| 36 | +chardrive; |
| 37 | +intr; |
| 38 | + |
| 39 | +r=uv_cwd(original_cwd,&size); |
| 40 | +ASSERT_EQ(r,0); |
| 41 | + |
| 42 | +drive=get_current_drive(); |
| 43 | +if (drive==0) |
| 44 | +return0; |
| 45 | + |
| 46 | +r=uv_chdir(original_cwd); |
| 47 | +ASSERT_EQ(r,0); |
| 48 | + |
| 49 | +ASSERT_EQ(get_drive_env(drive,env_value,sizeof(env_value)),1); |
| 50 | +ASSERT(strstr(env_value,original_cwd)!=NULL); |
| 51 | + |
| 52 | +return0; |
| 53 | +} |
| 54 | + |
| 55 | +TEST_IMPL(chdir_normalizes_drive_letter_case) { |
| 56 | +charoriginal_cwd[1024]; |
| 57 | +size_tsize=sizeof(original_cwd); |
| 58 | +charlowercase_path[1024]; |
| 59 | +charenv_value[1024]; |
| 60 | +chardrive; |
| 61 | +intr; |
| 62 | + |
| 63 | +r=uv_cwd(original_cwd,&size); |
| 64 | +ASSERT_EQ(r,0); |
| 65 | + |
| 66 | +drive=get_current_drive(); |
| 67 | +if (drive==0) |
| 68 | +return0; |
| 69 | + |
| 70 | +strcpy(lowercase_path,original_cwd); |
| 71 | +if (lowercase_path[0] >='A'&&lowercase_path[0] <='Z') |
| 72 | +lowercase_path[0]=lowercase_path[0]+32; |
| 73 | + |
| 74 | +r=uv_chdir(lowercase_path); |
| 75 | +ASSERT_EQ(r,0); |
| 76 | + |
| 77 | +ASSERT_EQ(get_drive_env(drive,env_value,sizeof(env_value)),1); |
| 78 | + |
| 79 | +return0; |
| 80 | +} |
| 81 | + |
| 82 | +TEST_IMPL(chdir_updates_env_on_path_change) { |
| 83 | +charoriginal_cwd[1024]; |
| 84 | +size_tsize=sizeof(original_cwd); |
| 85 | +charparent_dir[1024]; |
| 86 | +charenv_value_1[1024]; |
| 87 | +charenv_value_2[1024]; |
| 88 | +chardrive; |
| 89 | +char*last_slash; |
| 90 | +intr; |
| 91 | + |
| 92 | +r=uv_cwd(original_cwd,&size); |
| 93 | +ASSERT_EQ(r,0); |
| 94 | + |
| 95 | +drive=get_current_drive(); |
| 96 | +if (drive==0)return0; |
| 97 | + |
| 98 | +strcpy(parent_dir,original_cwd); |
| 99 | +last_slash=strrchr(parent_dir,'\\'); |
| 100 | +if (last_slash==NULL||last_slash==parent_dir+2) |
| 101 | +return0; |
| 102 | + |
| 103 | +*last_slash='\0'; |
| 104 | + |
| 105 | +r=uv_chdir(parent_dir); |
| 106 | +ASSERT_EQ(r,0); |
| 107 | +ASSERT_EQ(get_drive_env(drive,env_value_1,sizeof(env_value_1)),1); |
| 108 | + |
| 109 | +r=uv_chdir(original_cwd); |
| 110 | +ASSERT_EQ(r,0); |
| 111 | +ASSERT_EQ(get_drive_env(drive,env_value_2,sizeof(env_value_2)),1); |
| 112 | + |
| 113 | +ASSERT_STR_NE(env_value_1,env_value_2); |
| 114 | + |
| 115 | +return0; |
| 116 | +} |
| 117 | + |
| 118 | +TEST_IMPL(chdir_preserves_env_for_regular_unc) { |
| 119 | +charoriginal_cwd[1024]; |
| 120 | +size_tsize=sizeof(original_cwd); |
| 121 | +charunc_path[256]; |
| 122 | +charold_env[1024]; |
| 123 | +charnew_env[1024]; |
| 124 | +chardrive; |
| 125 | +inthad_env; |
| 126 | +inthas_env; |
| 127 | +intr; |
| 128 | + |
| 129 | +r=uv_cwd(original_cwd,&size); |
| 130 | +ASSERT_EQ(r,0); |
| 131 | + |
| 132 | +drive=get_current_drive(); |
| 133 | +if (drive==0) |
| 134 | +return0; |
| 135 | + |
| 136 | +had_env=get_drive_env(drive,old_env,sizeof(old_env)); |
| 137 | + |
| 138 | +snprintf(unc_path,sizeof(unc_path),"\\\\localhost\\%c$",drive); |
| 139 | +r=uv_chdir(unc_path); |
| 140 | + |
| 141 | +if (r!=0) |
| 142 | +return0; |
| 143 | + |
| 144 | +has_env=get_drive_env(drive,new_env,sizeof(new_env)); |
| 145 | + |
| 146 | +if (had_env&&has_env) { |
| 147 | +ASSERT_STR_EQ(old_env,new_env); |
| 148 | + }elseif (had_env&& !has_env) { |
| 149 | +ASSERT(0&&"Drive env should not be deleted"); |
| 150 | + }elseif (!had_env&&has_env) { |
| 151 | +ASSERT(0&&"Drive env should not be created"); |
| 152 | + } |
| 153 | + |
| 154 | +uv_chdir(original_cwd); |
| 155 | +return0; |
| 156 | +} |
| 157 | + |
| 158 | +TEST_IMPL(chdir_preserves_env_for_volume_guid) { |
| 159 | +charoriginal_cwd[1024]; |
| 160 | +size_tsize=sizeof(original_cwd); |
| 161 | +charold_env[1024]; |
| 162 | +charnew_env[1024]; |
| 163 | +chardrive; |
| 164 | +inthad_env; |
| 165 | +inthas_env; |
| 166 | +intr; |
| 167 | + |
| 168 | +r=uv_cwd(original_cwd,&size); |
| 169 | +ASSERT_EQ(r,0); |
| 170 | + |
| 171 | +drive=get_current_drive(); |
| 172 | +if (drive==0) |
| 173 | +return0; |
| 174 | + |
| 175 | +had_env=get_drive_env(drive,old_env,sizeof(old_env)); |
| 176 | + |
| 177 | +r=uv_chdir("\\\\?\\Volume{12345678-1234-1234-1234-123456789012}\\"); |
| 178 | + |
| 179 | +if (r!=0) |
| 180 | +return0; |
| 181 | + |
| 182 | +has_env=get_drive_env(drive,new_env,sizeof(new_env)); |
| 183 | + |
| 184 | +if (had_env&&has_env) { |
| 185 | +ASSERT_STR_EQ(old_env,new_env); |
| 186 | + }elseif (had_env&& !has_env) { |
| 187 | +ASSERT(0&&"Drive env should not be deleted for volume GUID"); |
| 188 | + }elseif (!had_env&&has_env) { |
| 189 | +ASSERT(0&&"Drive env should not be created for volume GUID"); |
| 190 | + } |
| 191 | + |
| 192 | +uv_chdir(original_cwd); |
| 193 | + |
| 194 | +return0; |
| 195 | +} |
| 196 | + |
| 197 | +TEST_IMPL(chdir_preserves_env_for_globalroot) { |
| 198 | +charoriginal_cwd[1024]; |
| 199 | +size_tsize=sizeof(original_cwd); |
| 200 | +charold_env[1024]; |
| 201 | +charnew_env[1024]; |
| 202 | +chardrive; |
| 203 | +inthad_env; |
| 204 | +inthas_env; |
| 205 | +intr; |
| 206 | + |
| 207 | +r=uv_cwd(original_cwd,&size); |
| 208 | +ASSERT_EQ(r,0); |
| 209 | + |
| 210 | +drive=get_current_drive(); |
| 211 | +if (drive==0)return0; |
| 212 | + |
| 213 | +had_env=get_drive_env(drive,old_env,sizeof(old_env)); |
| 214 | + |
| 215 | +r=uv_chdir("\\\\?\\GLOBALROOT\\Device\\HarddiskVolume1"); |
| 216 | + |
| 217 | +if (r!=0) |
| 218 | +return0; |
| 219 | + |
| 220 | +has_env=get_drive_env(drive,new_env,sizeof(new_env)); |
| 221 | + |
| 222 | +if (had_env&&has_env) { |
| 223 | +ASSERT_STR_EQ(old_env,new_env); |
| 224 | + }elseif (had_env&& !has_env) { |
| 225 | +ASSERT(0&&"Drive env should not be deleted for GLOBALROOT"); |
| 226 | + }elseif (!had_env&&has_env) { |
| 227 | +ASSERT(0&&"Drive env should not be created for GLOBALROOT"); |
| 228 | + } |
| 229 | + |
| 230 | +uv_chdir(original_cwd); |
| 231 | + |
| 232 | +return0; |
| 233 | +} |
| 234 | + |
| 235 | +TEST_IMPL(chdir_invalid_input_returns_error) { |
| 236 | +intr; |
| 237 | + |
| 238 | +r=uv_chdir(NULL); |
| 239 | +ASSERT_EQ(r,UV_EINVAL); |
| 240 | + |
| 241 | +r=uv_chdir(""); |
| 242 | +ASSERT_EQ(r,UV_EINVAL); |
| 243 | + |
| 244 | +return0; |
| 245 | +} |
| 246 | + |
| 247 | +TEST_IMPL(chdir_nonexistent_path_returns_error) { |
| 248 | +chardrive; |
| 249 | +charnonexistent[256]; |
| 250 | +intr; |
| 251 | + |
| 252 | +drive=get_current_drive(); |
| 253 | +if (drive==0)return0; |
| 254 | + |
| 255 | +snprintf(nonexistent,sizeof(nonexistent),"%c:\\NonExistentDirectory_xyz123456789",drive); |
| 256 | + |
| 257 | +r=uv_chdir(nonexistent); |
| 258 | + |
| 259 | +ASSERT(r!=0); |
| 260 | +ASSERT(r==UV_ENOENT||r==UV_EACCES); |
| 261 | + |
| 262 | +return0; |
| 263 | +} |
| 264 | + |
| 265 | +TEST_IMPL(chdir_win32_namespace_updates_env) { |
| 266 | +charoriginal_cwd[1024]; |
| 267 | +size_tsize=sizeof(original_cwd); |
| 268 | +charwin32_path[2048]; |
| 269 | +charenv_value[1024]; |
| 270 | +chardrive; |
| 271 | +intr; |
| 272 | + |
| 273 | +r=uv_cwd(original_cwd,&size); |
| 274 | +ASSERT_EQ(r,0); |
| 275 | + |
| 276 | +drive=get_current_drive(); |
| 277 | +if (drive==0)return0; |
| 278 | + |
| 279 | +if (strlen(original_cwd)>sizeof(win32_path)-5) { |
| 280 | +return0; |
| 281 | + } |
| 282 | + |
| 283 | +snprintf(win32_path,sizeof(win32_path),"\\\\?\\%s",original_cwd); |
| 284 | + |
| 285 | +r=uv_chdir(win32_path); |
| 286 | +ASSERT_EQ(r,0); |
| 287 | + |
| 288 | +ASSERT_EQ(get_drive_env(drive,env_value,sizeof(env_value)),1); |
| 289 | + |
| 290 | +return0; |
| 291 | +} |
| 292 | + |
| 293 | +TEST_IMPL(chdir_device_namespace_with_drive) { |
| 294 | +charoriginal_cwd[1024]; |
| 295 | +size_tsize=sizeof(original_cwd); |
| 296 | +chardevice_path[256]; |
| 297 | +charold_env[1024]; |
| 298 | +chardrive; |
| 299 | +intr; |
| 300 | + |
| 301 | +r=uv_cwd(original_cwd,&size); |
| 302 | +ASSERT_EQ(r,0); |
| 303 | + |
| 304 | +drive=get_current_drive(); |
| 305 | +if (drive==0)return0; |
| 306 | + |
| 307 | +get_drive_env(drive,old_env,sizeof(old_env)); |
| 308 | + |
| 309 | +snprintf(device_path,sizeof(device_path),"\\\\.\\%c:",drive); |
| 310 | +r=uv_chdir(device_path); |
| 311 | + |
| 312 | +if (r==0) { |
| 313 | +charnew_env[1024]; |
| 314 | +ASSERT_EQ(get_drive_env(drive,new_env,sizeof(new_env)),1); |
| 315 | + } |
| 316 | + |
| 317 | +uv_chdir(original_cwd); |
| 318 | +return0; |
| 319 | +} |
| 320 | + |
| 321 | +TEST_IMPL(chdir_nt_namespace_with_drive) { |
| 322 | +charoriginal_cwd[1024]; |
| 323 | +size_tsize=sizeof(original_cwd); |
| 324 | +charnt_path[1024]; |
| 325 | +charenv_value[1024]; |
| 326 | +chardrive; |
| 327 | +char*path_after_drive; |
| 328 | +intr; |
| 329 | + |
| 330 | +r=uv_cwd(original_cwd,&size); |
| 331 | +ASSERT_EQ(r,0); |
| 332 | + |
| 333 | +drive=get_current_drive(); |
| 334 | +if (drive==0)return0; |
| 335 | + |
| 336 | +path_after_drive=strchr(original_cwd+2,'\\'); |
| 337 | +if (path_after_drive==NULL) { |
| 338 | +return0; |
| 339 | + } |
| 340 | + |
| 341 | +snprintf(nt_path,sizeof(nt_path),"\\??\\%c:%s",drive,path_after_drive); |
| 342 | + |
| 343 | +r=uv_chdir(nt_path); |
| 344 | + |
| 345 | +if (r==0) { |
| 346 | +ASSERT_EQ(get_drive_env(drive,env_value,sizeof(env_value)),1); |
| 347 | + }else { |
| 348 | +ASSERT(r==UV_ENOENT||r==UV_EINVAL||r==UV_EACCES); |
| 349 | + } |
| 350 | + |
| 351 | +uv_chdir(original_cwd); |
| 352 | +return0; |
| 353 | +} |
| 354 | + |
| 355 | +#endif |