Skip to content

Commit 4879602

Browse files
bagajjalmanojampalam
authored andcommitted
Docker#666 keyscan#731 posixcompatnewunittests (#152)
docker ssh issue PowerShell/Win32-OpenSSH#666 a) fdopen changes to accept the /dev/null device b) fix the select (using same fdset as readfdset, exceptfdset) issue with the unix opensssh code. changed keyscan pester test to refer to localhost (127.0.0.1) instead of GitHub.com PowerShell/Win32-OpenSSH#731 Fix the ASSERT_HANDLE issue.. ASSERT_HANDLE should fail if handle is either NULL or INVALID_HANDLE. Added new testcases for the null device.
1 parent 5989efc commit 4879602

File tree

8 files changed

+56
-29
lines changed

8 files changed

+56
-29
lines changed

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ on_finish:
3838
- ps: |
3939
Import-Module $env:APPVEYOR_BUILD_FOLDER\contrib\win32\openssh\AppveyorHelper.psm1 -DisableNameChecking
4040
Publish-Artifact
41-
41+

contrib/win32/win32compat/fileio.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,6 @@ createFile_flags_setup(int flags, u_short mode, struct createFile_flags* cf_flag
413413
return ret;
414414
}
415415

416-
417-
#define NULL_DEVICE "/dev/null"
418416
/* open() implementation. Uses CreateFile to open file, console, device, etc */
419417
struct w32_io*
420418
fileio_open(const char *path_utf8, int flags, u_short mode)
@@ -433,7 +431,7 @@ fileio_open(const char *path_utf8, int flags, u_short mode)
433431
}
434432

435433
/* if opening null device, point to Windows equivalent */
436-
if (strncmp(path_utf8, NULL_DEVICE, strlen(NULL_DEVICE)) == 0)
434+
if (strncmp(path_utf8, NULL_DEVICE, strlen(NULL_DEVICE)+1) == 0)
437435
path_utf8 = "NUL";
438436

439437
if ((path_utf16 = utf8_to_utf16(path_utf8)) == NULL) {

contrib/win32/win32compat/misc.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,27 @@ w32_fopen_utf8(const char *path, const char *mode)
240240
FILE* f;
241241
char utf8_bom[] = { 0xEF,0xBB,0xBF };
242242
char first3_bytes[3];
243+
int status = 1;
243244

244245
if (mode[1] != '\0') {
245246
errno = ENOTSUP;
246247
return NULL;
247248
}
248249

249-
if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, PATH_MAX) == 0 ||
250-
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, 5) == 0) {
250+
if(NULL == path) {
251+
errno = EINVAL;
252+
debug3("fopen - ERROR:%d", errno);
253+
return NULL;
254+
}
255+
256+
/* if opening null device, point to Windows equivalent */
257+
if (0 == strncmp(path, NULL_DEVICE, strlen(NULL_DEVICE)+1))
258+
wcsncpy_s(wpath, PATH_MAX, L"NUL", 3);
259+
else
260+
status = MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, PATH_MAX);
261+
262+
if ((0 == status) ||
263+
(0 == MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, 5))) {
251264
errno = EFAULT;
252265
debug3("WideCharToMultiByte failed for %c - ERROR:%d", path, GetLastError());
253266
return NULL;

contrib/win32/win32compat/misc_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#define SSH_ASYNC_STDOUT "SSH_ASYNC_STDOUT"
55
#define SSH_ASYNC_STDERR "SSH_ASYNC_STDERR"
66

7+
#define NULL_DEVICE "/dev/null"
8+
79
#define IS_INVALID_HANDLE(h) ( ((NULL == h) || (INVALID_HANDLE_VALUE == h)) ? 1 : 0 )
810

911
/* removes first '/' for Windows paths that are unix styled. Ex: /c:/ab.cd */

regress/pesterTests/KeyUtils.Tests.ps1

+11-10
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ Describe "E2E scenarios for ssh key management" -Tags "CI" {
313313
Context "$tC - ssh-keyscan test cases" {
314314
BeforeAll {
315315
$tI=1
316+
$port = $OpenSSHTestInfo["Port"]
316317
Remove-item (join-path $testDir "$tC.$tI.out.txt") -force -ErrorAction Ignore
317318
}
318319
BeforeEach {
@@ -321,25 +322,25 @@ Describe "E2E scenarios for ssh key management" -Tags "CI" {
321322
AfterAll{$tC++}
322323

323324
It "$tC.$tI - ssh-keyscan with default arguments" {
324-
cmd /c "ssh-keyscan -p 22 github.com 2>&1 > $outputFile"
325-
$outputFile | Should Contain 'github.com ssh-rsa.*'
325+
cmd /c "ssh-keyscan -p $port 127.0.0.1 2>&1 > $outputFile"
326+
$outputFile | Should Contain '.*ssh-rsa.*'
326327
}
327328

328329
It "$tC.$tI - ssh-keyscan with -p" {
329-
cmd /c "ssh-keyscan -p 22 github.com 2>&1 > $outputFile"
330-
$outputFile | Should Contain 'github.com ssh-rsa.*'
330+
cmd /c "ssh-keyscan -p $port 127.0.0.1 2>&1 > $outputFile"
331+
$outputFile | Should Contain '.*ssh-rsa.*'
331332
}
332333

333334
It "$tC.$tI - ssh-keyscan with -f" {
334-
Set-Content -Path tmp.txt -Value "github.com"
335-
cmd /c "ssh-keyscan -f tmp.txt 2>&1 > $outputFile"
336-
$outputFile | Should Contain 'github.com ssh-rsa.*'
335+
Set-Content -Path tmp.txt -Value "127.0.0.1"
336+
cmd /c "ssh-keyscan -p $port -f tmp.txt 2>&1 > $outputFile"
337+
$outputFile | Should Contain '.*ssh-rsa.*'
337338
}
338339

339340
It "$tC.$tI - ssh-keyscan with -f -t" {
340-
Set-Content -Path tmp.txt -Value "github.com"
341-
cmd /c "ssh-keyscan -f tmp.txt -t rsa,dsa 2>&1 > $outputFile"
342-
$outputFile | Should Contain 'github.com ssh-rsa.*'
341+
Set-Content -Path tmp.txt -Value "127.0.0.1"
342+
cmd /c "ssh-keyscan -p $port -f tmp.txt -t rsa,dsa 2>&1 > $outputFile"
343+
$outputFile | Should Contain '.*ssh-rsa.*'
343344
}
344345
}
345346
}

regress/unittests/win32compat/file_tests.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ void file_simple_fileio()
204204
ASSERT_INT_EQ(retValue, 0);
205205
}
206206

207+
{
208+
// test null device
209+
FILE *fp = fopen("/dev/null", "r");
210+
ASSERT_PTR_NE(fp, NULL);
211+
212+
f = open("/dev/null", O_RDONLY);
213+
ASSERT_INT_NE(f, -1);
214+
}
215+
207216
TEST_DONE();
208217
}
209218

@@ -429,7 +438,7 @@ file_miscellaneous_tests()
429438
int f1 = dup(f);
430439
ASSERT_INT_EQ(f1, -1);
431440
HANDLE h = w32_fd_to_handle(f);
432-
ASSERT_HANDLE(h, retValue);
441+
ASSERT_HANDLE(h);
433442
close(f);
434443

435444
char *tmp_filename_1 = "tmp_1.txt";
@@ -443,16 +452,16 @@ file_miscellaneous_tests()
443452
free(tmp);
444453

445454
h = w32_fd_to_handle(STDIN_FILENO);
446-
ASSERT_HANDLE(h, retValue);
455+
ASSERT_HANDLE(h);
447456

448457
h = w32_fd_to_handle(STDOUT_FILENO);
449-
ASSERT_HANDLE(h, retValue);
458+
ASSERT_HANDLE(h);
450459

451460
h = w32_fd_to_handle(STDERR_FILENO);
452-
ASSERT_HANDLE(h, retValue);
461+
ASSERT_HANDLE(h);
453462

454463
retValue = w32_allocate_fd_for_handle(h, FALSE);
455-
ASSERT_HANDLE(h, retValue);
464+
ASSERT_HANDLE(h);
456465

457466
TEST_DONE();
458467
}

regress/unittests/win32compat/tests.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ void miscellaneous_tests();
66
char *dup_str(char *inStr);
77
void delete_dir_recursive(char *full_dir_path);
88

9-
#define ASSERT_HANDLE(handle,retValue) \
10-
{ \
9+
#define ASSERT_HANDLE(handle) \
10+
{\
1111
ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE); \
1212
ASSERT_PTR_NE(handle, 0); \
1313
}

sshconnect.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,12 @@ ssh_exchange_identification(int timeout_ms)
564564
size_t len;
565565
int fdsetsz, remaining, rc;
566566
struct timeval t_start, t_remaining;
567-
fd_set *fdset;
567+
fd_set *readfds;
568+
fd_set *exceptfds;
568569

569570
fdsetsz = howmany(connection_in + 1, NFDBITS) * sizeof(fd_mask);
570-
fdset = xcalloc(1, fdsetsz);
571+
readfds = xcalloc(1, fdsetsz);
572+
exceptfds = xcalloc(1, fdsetsz);
571573

572574
send_client_banner(connection_out, 0);
573575

@@ -578,9 +580,10 @@ ssh_exchange_identification(int timeout_ms)
578580
if (timeout_ms > 0) {
579581
gettimeofday(&t_start, NULL);
580582
ms_to_timeval(&t_remaining, remaining);
581-
FD_SET(connection_in, fdset);
582-
rc = select(connection_in + 1, fdset, NULL,
583-
fdset, &t_remaining);
583+
FD_SET(connection_in, readfds);
584+
FD_SET(connection_in, exceptfds);
585+
rc = select(connection_in + 1, readfds, NULL,
586+
exceptfds, &t_remaining);
584587
ms_subtract_diff(&t_start, &remaining);
585588
if (rc == 0 || remaining <= 0)
586589
fatal("Connection timed out during "
@@ -620,7 +623,8 @@ ssh_exchange_identification(int timeout_ms)
620623
debug("ssh_exchange_identification: %s", buf);
621624
}
622625
server_version_string = xstrdup(buf);
623-
free(fdset);
626+
free(readfds);
627+
free(exceptfds);
624628

625629
/*
626630
* Check that the versions match. In future this might accept

0 commit comments

Comments
 (0)