bagajjal 2017-05-22 22:50:51 -07:00 committed by Manoj Ampalam
parent 4dbee0d15a
commit e296463fc8
16 changed files with 1378 additions and 631 deletions

View File

@ -38,3 +38,4 @@ on_finish:
- ps: |
Import-Module $env:APPVEYOR_BUILD_FOLDER\contrib\win32\openssh\AppveyorHelper.psm1 -DisableNameChecking
Publish-Artifact

View File

@ -25,6 +25,12 @@
<ClCompile Include="$(OpenSSH-Src-Path)regress\unittests\win32compat\tests.c" />
<ClCompile Include="$(OpenSSH-Src-Path)regress\unittests\test_helper\test_helper.c" />
<ClCompile Include="$(OpenSSH-Src-Path)contrib\win32\win32compat\wmain_common.c" />
<ClCompile Include="$(OpenSSH-Src-Path)regress\unittests\win32compat\dir_tests.c" />
<ClCompile Include="$(OpenSSH-Src-Path)regress\unittests\win32compat\miscellaneous_tests.c" />
<ClCompile Include="$(OpenSSH-Src-Path)regress\unittests\win32compat\string_tests.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(OpenSSH-Src-Path)regress\unittests\win32compat\tests.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{BF295BA9-4BF8-43F8-8CBF-FAE84815466C}</ProjectGuid>

View File

@ -176,7 +176,6 @@
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\dlfcn.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\syslog.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\signal_internal.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\sys\param.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\utf.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\arpa\inet.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\arpa\nameser.h" />

View File

@ -53,9 +53,6 @@
<Filter>inc\sys</Filter>
</ClInclude>
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\signal_internal.h" />
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\sys\param.h">
<Filter>inc\sys</Filter>
</ClInclude>
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\inc\utf.h">
<Filter>inc</Filter>
</ClInclude>

View File

@ -20,7 +20,7 @@ int w32_fcntl(int fd, int cmd, ... /* arg */);
int w32_open(const char *pathname, int flags, ...);
void* w32_fd_to_handle(int fd);
int w32_allocate_fd_for_handle(void* h, int is_sock);
int w32_allocate_fd_for_handle(HANDLE, BOOL);
#define O_RDONLY _O_RDONLY
#define O_WRONLY _O_WRONLY

View File

@ -74,15 +74,11 @@ int w32_chdir(const char *dirname);
char *w32_getcwd(char *buffer, int maxlen);
#define getcwd w32_getcwd
int daemon(int nochdir, int noclose);
char *crypt(const char *key, const char *salt);
int link(const char *oldpath, const char *newpath);
int readlink(const char *path, char *link, int linklen);
int spawn_child(char* cmd, char** argv, int in, int out, int err, unsigned long flags);
int spawn_child(char*, char**, int, int, int, DWORD);
/*
* readpassphrase.h definitions

View File

@ -3,6 +3,7 @@
*
* UTF-16 <--> UTF-8 definitions
*/
#pragma once
#ifndef UTF_H
#define UTF_H 1

View File

@ -284,6 +284,8 @@ w32_fopen_utf8(const char *path, const char *mode)
*/
char*
w32_fgets(char *str, int n, FILE *stream) {
if (!str || !n || !stream) return NULL;
HANDLE h = (HANDLE)_get_osfhandle(_fileno(stream));
wchar_t* str_w = NULL;
char *ret = NULL, *str_tmp = NULL, *cp = NULL;
@ -773,6 +775,8 @@ w32_chdir(const char *dirname_utf8)
char *
w32_getcwd(char *buffer, int maxlen)
{
if(!buffer) return NULL;
wchar_t wdirname[PATH_MAX];
char* putf8 = NULL;
@ -852,6 +856,8 @@ convertToForwardslash(char *str)
char *
realpath(const char *path, char resolved[PATH_MAX])
{
if (!path || !resolved) return NULL;
char tempPath[PATH_MAX];
if ((path[0] == '/') && path[1] && (path[2] == ':'))
@ -877,6 +883,8 @@ realpath(const char *path, char resolved[PATH_MAX])
char*
sanitized_path(const char *path)
{
if(!path) return NULL;
static char newPath[PATH_MAX] = { '\0', };
if (path[0] == '/' && path[1]) {
@ -968,8 +976,8 @@ statvfs(const char *path, struct statvfs *buf)
DWORD totalClusters;
wchar_t* path_utf16 = utf8_to_utf16(sanitized_path(path));
if (GetDiskFreeSpaceW(path_utf16, &sectorsPerCluster, &bytesPerSector,
&freeClusters, &totalClusters) == TRUE) {
if (path_utf16 && (GetDiskFreeSpaceW(path_utf16, &sectorsPerCluster, &bytesPerSector,
&freeClusters, &totalClusters) == TRUE)) {
debug5("path : [%s]", path);
debug5("sectorsPerCluster : [%lu]", sectorsPerCluster);
debug5("bytesPerSector : [%lu]", bytesPerSector);

View File

@ -88,6 +88,8 @@ opendir(const char *name)
int
closedir(DIR *dirp)
{
if(!dirp) return -1;
if (dirp && (dirp->hFile)) {
_findclose(dirp->hFile);
dirp->hFile = 0;
@ -104,6 +106,8 @@ closedir(DIR *dirp)
struct dirent *
readdir(void *avp)
{
if(!avp) return NULL;
static struct dirent pdirentry;
struct _wfinddata_t c_file;
DIR *dirp = (DIR *)avp;

View File

@ -0,0 +1,172 @@
#include "includes.h"
#include <sys/stat.h>
#include <unistd.h>
#include <inc/dirent.h>
#include <sys/statvfs.h>
#include <sys/time.h>
#include "../test_helper/test_helper.h"
#include "tests.h"
int retValue;
void
dir_tests_1()
{
TEST_START("directory testcases");
char *test_dirname_1 = "test_dir_1";
char *tes_dirname_2 = "test_dir_2";
char cwd[MAX_PATH];
char *p_ret;
struct stat st;
char *tmpfile = "tmp.txt";
char mode[12];
struct timeval tv[2];
DIR *dirp = NULL;
struct dirent *dp = NULL;
char dir_fullpath[MAX_PATH];
int f = -1;
p_ret = getcwd(NULL, MAX_PATH);
ASSERT_PTR_EQ(p_ret, NULL);
p_ret = getcwd(cwd, MAX_PATH);
ASSERT_PTR_NE(p_ret, NULL);
// delete test_dirname_1, if exits.
strcpy(dir_fullpath, cwd);
strcat(dir_fullpath, "\\");
strcat(dir_fullpath, test_dirname_1);
delete_dir_recursive(dir_fullpath);
// delete test_dirname_2, if exists
strcpy(dir_fullpath, cwd);
strcat(dir_fullpath, "\\");
strcat(dir_fullpath, tes_dirname_2);
delete_dir_recursive(dir_fullpath);
retValue = mkdir(NULL, 0);
ASSERT_INT_EQ(retValue, -1);
retValue = mkdir(test_dirname_1, S_IRUSR | S_IWUSR | S_IXUSR);
ASSERT_INT_EQ(retValue, 0);
retValue = stat(NULL, &st);
ASSERT_INT_EQ(retValue, -1);
retValue = stat(test_dirname_1, &st);
ASSERT_INT_EQ(retValue, 0);
ASSERT_INT_EQ(st.st_size, 0);
strmode(st.st_mode, mode);
ASSERT_CHAR_EQ(mode[0], 'd');
retValue = chdir(NULL);
ASSERT_INT_EQ(retValue, -1);
retValue = chdir(test_dirname_1);
ASSERT_INT_EQ(retValue, 0);
p_ret = getcwd(cwd, MAX_PATH);
ASSERT_PTR_NE(p_ret, NULL);
p_ret = NULL;
p_ret = strstr(cwd, test_dirname_1);
ASSERT_PTR_NE(p_ret, NULL);
retValue = chdir("..");
ASSERT_INT_EQ(retValue, 0);
retValue = rename(NULL, tes_dirname_2);
ASSERT_INT_EQ(retValue, -1);
retValue = rename(test_dirname_1, NULL);
ASSERT_INT_EQ(retValue, -1);
retValue = rename(NULL, NULL);
ASSERT_INT_EQ(retValue, -1);
retValue = rename(test_dirname_1, tes_dirname_2);
ASSERT_INT_EQ(retValue, 0);
retValue = stat(tes_dirname_2, &st);
ASSERT_INT_EQ(retValue, 0);
dirp = opendir(NULL);
ASSERT_PTR_EQ(dirp, NULL);
dirp = opendir(tes_dirname_2);
ASSERT_PTR_NE(dirp, NULL);
dp = readdir(NULL);
ASSERT_PTR_EQ(dp, NULL);
dp = readdir(dirp);
ASSERT_PTR_EQ(dp, NULL);
tv[0].tv_sec = st.st_atime + 1000;
tv[1].tv_sec = st.st_mtime + 1000;
tv[0].tv_usec = tv[1].tv_usec = 0;
retValue = utimes(tes_dirname_2, tv);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, ERROR_SHARING_VIOLATION);
retValue = closedir(NULL);
ASSERT_INT_EQ(retValue, -1);
retValue = closedir(dirp);
ASSERT_INT_EQ(retValue, 0);
retValue = utimes(tes_dirname_2, tv);
ASSERT_INT_EQ(retValue, 0);
retValue = chdir(tes_dirname_2);
ASSERT_INT_EQ(retValue, 0);
f = open(tmpfile, O_RDWR | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
close(f);
retValue = chdir("..");
ASSERT_INT_EQ(retValue, 0);
dirp = opendir(tes_dirname_2);
ASSERT_PTR_NE(dirp, NULL);
dp = readdir(dirp);
ASSERT_PTR_NE(dp, NULL);
retValue = closedir(dirp);
ASSERT_INT_EQ(retValue, 0);
retValue = rmdir(NULL);
ASSERT_INT_EQ(retValue, -1);
retValue = rmdir(tes_dirname_2);
ASSERT_INT_NE(retValue, 0);
retValue = chdir(tes_dirname_2);
ASSERT_INT_EQ(retValue, 0);
retValue = unlink(NULL);
ASSERT_INT_EQ(retValue, -1);
retValue = unlink(tmpfile);
ASSERT_INT_EQ(retValue, 0);
retValue = chdir("..");
ASSERT_INT_EQ(retValue, 0);
retValue = rmdir(tes_dirname_2);
ASSERT_INT_EQ(retValue, 0);
dirp = opendir(tes_dirname_2);
ASSERT_PTR_EQ(dirp, NULL);
TEST_DONE();
}
void
dir_tests()
{
dir_tests_1();
}

View File

@ -8,7 +8,10 @@
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include "../test_helper/test_helper.h"
#include "tests.h"
#define SMALL_RECV_BUF_SIZE 128
@ -17,12 +20,11 @@
fd_set read_set, write_set, except_set;
struct timeval time_val;
char *send_buf, *recv_buf;
int ret, r, w;
int retValue, r_pipe, w_pipe;
char *tmp_filename = "tmp.txt";
int unset_nonblock(int fd);
int set_nonblock(int fd);
void prep_input_buffer(char* buf, int size, int seed);
void
@ -30,88 +32,192 @@ file_blocking_io_tests()
{
char* small_send_buf = "sample payload";
char small_recv_buf[SMALL_RECV_BUF_SIZE];
TEST_START("Basic pipe()");
int pipeio[2];
ret = pipe(pipeio);
ASSERT_INT_EQ(ret, 0);
TEST_DONE();
TEST_START("pipe read and write");
r = pipeio[0];
w = pipeio[1];
ret = write(r, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(ret, -1);
ASSERT_INT_EQ(errno, EACCES);
ret = read(w, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, -1);
ASSERT_INT_EQ(errno, EACCES);
ret = write(w, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(ret, strlen(small_send_buf));
ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, strlen(small_send_buf));
small_recv_buf[ret] = '\0';
ASSERT_STRING_EQ(small_send_buf, small_recv_buf);
memset(small_recv_buf, 0, sizeof(small_recv_buf));
TEST_DONE();
{
TEST_START("Basic pipe()");
retValue = pipe(pipeio);
ASSERT_INT_EQ(retValue, 0);
TEST_DONE();
}
TEST_START("close pipe fds");
ret = close(w);
ASSERT_INT_EQ(ret, 0);
ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE); /* send on other side is closed*/
ASSERT_INT_EQ(ret, 0);
ret = close(r);
ASSERT_INT_EQ(ret, 0);
TEST_DONE();
{
TEST_START("pipe read and write");
r_pipe = pipeio[0];
w_pipe = pipeio[1];
retValue = write(r_pipe, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, EACCES);
retValue = read(w_pipe, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, EACCES);
retValue = write(w_pipe, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(retValue, strlen(small_send_buf));
retValue = read(r_pipe, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, strlen(small_send_buf));
small_recv_buf[retValue] = '\0';
ASSERT_STRING_EQ(small_send_buf, small_recv_buf);
memset(small_recv_buf, 0, sizeof(small_recv_buf));
TEST_DONE();
}
{
TEST_START("close pipe fds");
retValue = close(w_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = read(r_pipe, small_recv_buf, SMALL_RECV_BUF_SIZE); /* send on other side is closed*/
ASSERT_INT_EQ(retValue, 0);
retValue = close(r_pipe);
ASSERT_INT_EQ(retValue, 0);
TEST_DONE();
}
}
void file_simple_fileio()
{
TEST_START("file io");
char* small_write_buf = "sample payload";
char small_read_buf[SMALL_RECV_BUF_SIZE];
int f;
TEST_START("file io");
f = open("tmp.txt", O_WRONLY | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
close(f);
f = open("tmp.txt", O_RDONLY);
ASSERT_INT_NE(f, -1);
struct stat st;
ret = fstat(f, &st);
ASSERT_INT_EQ(ret, 0);
ASSERT_INT_EQ(st.st_size, 0);
ret = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, 0);
close(f);
f = open("tmp.txt", O_WRONLY | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
ret = write(f, small_write_buf, strlen(small_write_buf));
ASSERT_INT_EQ(ret, strlen(small_write_buf));
close(f);
f = open("tmp.txt", O_RDONLY);
ASSERT_INT_NE(f, -1);
ret = fstat(f, &st);
ASSERT_INT_EQ(ret, 0);
ASSERT_INT_EQ(st.st_size, strlen(small_write_buf));
ret = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, strlen(small_write_buf));
small_read_buf[ret] = '\0';
ASSERT_STRING_EQ(small_write_buf, small_read_buf);
ret = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, 0);
close(f);
{
f = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
close(f);
}
{
f = open(tmp_filename, O_RDONLY);
ASSERT_INT_NE(f, -1);
retValue = fstat(f, &st);
ASSERT_INT_EQ(retValue, 0);
ASSERT_INT_EQ(st.st_size, 0);
retValue = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, 0);
close(f);
}
{
f = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
retValue = write(f, small_write_buf, strlen(small_write_buf));
ASSERT_INT_EQ(retValue, strlen(small_write_buf));
close(f);
}
{
f = open(tmp_filename, O_RDONLY);
ASSERT_INT_NE(f, -1);
retValue = stat(tmp_filename, &st);
ASSERT_INT_EQ(retValue, 0);
ASSERT_INT_EQ(st.st_size, strlen(small_write_buf));
char mode[12];
strmode(st.st_mode, mode);
ASSERT_CHAR_EQ(mode[0], '-');
struct timeval tv[2];
tv[0].tv_sec = st.st_atime + 1000;
tv[1].tv_sec = st.st_mtime + 1000;
tv[0].tv_usec = tv[1].tv_usec = 0;
retValue = utimes(tmp_filename, tv);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, ERROR_SHARING_VIOLATION);
retValue = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, strlen(small_write_buf));
small_read_buf[retValue] = '\0';
ASSERT_STRING_EQ(small_write_buf, small_read_buf);
retValue = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, 0);
close(f);
retValue = utimes(tmp_filename, tv);
ASSERT_INT_EQ(retValue, 0);
}
{
/* test fopen, fgets, fclose*/
FILE *fp = fopen(tmp_filename, "r");
ASSERT_PTR_NE(fp, NULL);
char line[1024];
char *retp = fgets(line, sizeof(line), fp);
ASSERT_PTR_NE(retp, NULL);
retValue = strncmp(line, small_read_buf, strlen(line));
ASSERT_INT_EQ(retValue, 0);
fclose(fp);
}
{
/* test writev, ftruncate, isatty, lseek, fdopen */
f = open(tmp_filename, O_RDWR | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
struct iovec iov;
iov.iov_base = small_write_buf;
iov.iov_len = strlen(small_write_buf);
retValue = writev(f, &iov, 1);
ASSERT_INT_EQ(retValue, strlen(small_write_buf));
int truncate_len = 10;
int ret1 = ftruncate(f, truncate_len);
ASSERT_INT_EQ(ret1, 0);
explicit_bzero(small_read_buf, SMALL_RECV_BUF_SIZE);
retValue = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, truncate_len);
retValue = isatty(f);
ASSERT_INT_EQ(retValue, 0);
ASSERT_INT_EQ(errno, EINVAL);
int offset = 3;
retValue = lseek(f, offset, SEEK_SET);
ASSERT_INT_EQ(retValue, 0);
char *tmp = dup_str(small_read_buf);
retValue = read(f, small_read_buf, SMALL_RECV_BUF_SIZE);
small_read_buf[retValue] = '\0';
retValue = strcmp(tmp+offset, small_read_buf);
ASSERT_INT_EQ(retValue, 0);
FILE *f2 = fdopen(f, "r");
ASSERT_PTR_NE(f2, NULL);
fclose(f2);
retValue = unlink(tmp_filename);
ASSERT_INT_EQ(retValue, 0);
}
TEST_DONE();
}
void file_simple_fileio_mode()
{
TEST_START("file mode");
char * small_write_buf = "sample payload", *c, small_read_buf[SMALL_RECV_BUF_SIZE];
int ret;
FILE* f;
struct stat st;
TEST_START("file mode");
f = fopen(NULL, "w");
ASSERT_PTR_EQ(f, NULL);
c = fgets(NULL, 0, f);
ASSERT_PTR_EQ(c, NULL);
f = fopen("tmp.txt", "w");
ASSERT_PTR_NE(f, NULL);
fclose(f);
@ -120,11 +226,11 @@ void file_simple_fileio_mode()
c = fgets(small_read_buf, sizeof(small_read_buf), f);
ASSERT_PTR_EQ(c, NULL);
fclose(f);
ret = stat("tmp.txt", &st);
ASSERT_INT_EQ(ret, 0);
ASSERT_INT_EQ(st.st_size, 0);
f = fopen("tmp.txt", "w");
ASSERT_PTR_NE(f, NULL);
ret = fputs(small_write_buf, f);
@ -138,11 +244,11 @@ void file_simple_fileio_mode()
f = fopen("tmp.txt", "r");
ASSERT_PTR_NE(f, NULL);
c = fgets(small_read_buf, sizeof(small_read_buf), f);
ASSERT_PTR_NE(c, NULL);
ASSERT_PTR_NE(c, NULL);
ASSERT_STRING_EQ(small_write_buf, small_read_buf);
c = fgets(small_read_buf, sizeof(small_read_buf), f);
ASSERT_PTR_EQ(c, NULL);
ASSERT_PTR_EQ(c, NULL);
fclose(f);
TEST_DONE();
}
@ -150,106 +256,123 @@ void file_simple_fileio_mode()
void
file_nonblocking_io_tests()
{
char* small_send_buf = "sample payload";
char small_recv_buf[SMALL_RECV_BUF_SIZE];
TEST_START("non blocking file io");
char* small_send_buf = "sample payload";
char small_recv_buf[SMALL_RECV_BUF_SIZE];
int pipeio[2];
ret = pipe(pipeio);
ASSERT_INT_EQ(ret, 0);
r = pipeio[0];
w = pipeio[1];
ret = set_nonblock(r);
ASSERT_INT_EQ(ret, 0);
ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, -1);
retValue = pipe(pipeio);
ASSERT_INT_EQ(retValue, 0);
r_pipe = pipeio[0];
w_pipe = pipeio[1];
retValue = set_nonblock(r_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = read(r_pipe, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, EAGAIN);
ret = unset_nonblock(w);
ASSERT_INT_EQ(ret, 0);
ret = write(w, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(ret, strlen(small_send_buf));
ret = unset_nonblock(r);
ASSERT_INT_EQ(ret, 0);
ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(ret, strlen(small_send_buf));
small_recv_buf[ret] = '\0';
retValue = unset_nonblock(w_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = write(w_pipe, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(retValue, strlen(small_send_buf));
retValue = unset_nonblock(r_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = read(r_pipe, small_recv_buf, SMALL_RECV_BUF_SIZE);
ASSERT_INT_EQ(retValue, strlen(small_send_buf));
small_recv_buf[retValue] = '\0';
ASSERT_STRING_EQ(small_send_buf, small_recv_buf);
memset(small_recv_buf, 0, sizeof(small_recv_buf));
send_buf = malloc(10 * 1024);
ASSERT_PTR_NE(send_buf, NULL);
ret = set_nonblock(w);
ASSERT_INT_EQ(ret, 0);
ret = 1;
while (ret > 0) {
ret = write(w, send_buf, 10 * 1024);
retValue = set_nonblock(w_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = 1;
while (retValue > 0) {
retValue = write(w_pipe, send_buf, 10 * 1024);
}
ASSERT_INT_EQ(ret, -1);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, EAGAIN);
ret = close(r);
ASSERT_INT_EQ(ret, 0);
ret = close(w);
ASSERT_INT_EQ(ret, 0);
TEST_DONE();
retValue = close(r_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = close(w_pipe);
ASSERT_INT_EQ(retValue, 0);
free(send_buf);
TEST_DONE();
}
void
void
file_select_tests() {
TEST_START("select on file fds");
int num_bytes = 1024 * 700; //700KB
int bytes_sent = 0;
int bytes_received = 0;
int seed = 326;
int eagain_results = 0;
TEST_START("select on file fds");
int pipeio[2];
ret = pipe(pipeio);
ASSERT_INT_EQ(ret, 0);
r = pipeio[0];
w = pipeio[1];
ret = set_nonblock(w);
ASSERT_INT_EQ(ret, 0);
ret = set_nonblock(r);
ASSERT_INT_EQ(ret, 0);
retValue = pipe(pipeio);
ASSERT_INT_EQ(retValue, 0);
r_pipe = pipeio[0];
w_pipe = pipeio[1];
retValue = set_nonblock(w_pipe);
ASSERT_INT_EQ(retValue, 0);
retValue = set_nonblock(r_pipe);
ASSERT_INT_EQ(retValue, 0);
send_buf = malloc(num_bytes);
recv_buf = malloc(num_bytes + 1);
ASSERT_PTR_NE(send_buf, NULL);
ASSERT_PTR_NE(recv_buf, NULL);
prep_input_buffer(send_buf, num_bytes, 17);
FD_ZERO(&read_set);
FD_ZERO(&write_set);
FD_SET(w, &write_set);
FD_SET(r, &read_set);
while (-1 != select(max(r, w) + 1, &read_set, &write_set, NULL, &time_val)) {
if (FD_ISSET(w, &write_set)) {
while ((bytes_sent < num_bytes) && ((ret = write(w, send_buf + bytes_sent, num_bytes - bytes_sent)) > 0))
bytes_sent += ret;
FD_SET(w_pipe, &write_set);
FD_SET(r_pipe, &read_set);
while (-1 != select(max(r_pipe, w_pipe) + 1, &read_set, &write_set, NULL, &time_val)) {
if (FD_ISSET(w_pipe, &write_set)) {
while ((bytes_sent < num_bytes) && ((retValue = write(w_pipe, send_buf + bytes_sent, num_bytes - bytes_sent)) > 0))
bytes_sent += retValue;
if (bytes_sent < num_bytes) {
ASSERT_INT_EQ(ret, -1);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, EAGAIN);
eagain_results++;
}
}
if (FD_ISSET(r, &read_set)) {
while ((ret = read(r, recv_buf + bytes_received, num_bytes - bytes_received + 1)) > 0)
bytes_received += ret;
if (ret == 0)
if (FD_ISSET(r_pipe, &read_set)) {
while ((retValue = read(r_pipe, recv_buf + bytes_received, num_bytes - bytes_received + 1)) > 0)
bytes_received += retValue;
if (retValue == 0)
break;
ASSERT_INT_EQ(ret, -1);
ASSERT_INT_EQ(retValue, -1);
ASSERT_INT_EQ(errno, EAGAIN);
eagain_results++;
}
if (bytes_sent < num_bytes)
FD_SET(w, &write_set);
FD_SET(w_pipe, &write_set);
else {
FD_CLR(w, &write_set);
ret = close(w);
ASSERT_INT_EQ(ret, 0);
FD_CLR(w_pipe, &write_set);
retValue = close(w_pipe);
ASSERT_INT_EQ(retValue, 0);
}
FD_SET(r, &read_set);
FD_SET(r_pipe, &read_set);
}
/*ensure that we hit send and recv paths that returned EAGAIN. Else it would not have touched the async paths*/
@ -257,34 +380,86 @@ file_select_tests() {
ASSERT_INT_GT(eagain_results, 0);
ASSERT_INT_EQ(bytes_sent, bytes_received);
ASSERT_INT_EQ(memcmp(send_buf, recv_buf, num_bytes), 0);
ret = close(r);
ASSERT_INT_EQ(ret, 0);
retValue = close(r_pipe);
ASSERT_INT_EQ(retValue, 0);
free(send_buf);
free(recv_buf);
TEST_DONE();
TEST_DONE();
}
void console_io_test()
void
file_miscellaneous_tests()
{
char tmp[10];
TEST_START("console io test");
ret = read(STDIN_FILENO, tmp, 10);
ret = write(STDOUT_FILENO, "sample output", 13);
TEST_START("file miscellaneous");
char cwd[MAX_PATH];
char *pcwd = getcwd(cwd, MAX_PATH);
ASSERT_PTR_NE(pcwd, NULL);
char thishost[NI_MAXHOST];
retValue = gethostname(thishost, sizeof(thishost));
ASSERT_INT_NE(retValue, -1);
char *tmp = dup_str(thishost);
int len = strlen(tmp);
int f = dup(STDOUT_FILENO);
ASSERT_INT_NE(f, -1);
retValue = write(f, tmp, len);
ASSERT_INT_EQ(errno, 0);
ASSERT_INT_EQ(ret, 13);
ASSERT_INT_EQ(retValue, len);
close(f);
f = dup(STDIN_FILENO);
ASSERT_INT_NE(f, -1);
close(f);
f = dup(STDERR_FILENO);
ASSERT_INT_NE(f, -1);
close(f);
f = open(tmp_filename, O_RDWR | O_CREAT | O_TRUNC);
ASSERT_INT_NE(f, -1);
int f1 = dup(f);
ASSERT_INT_EQ(f1, -1);
HANDLE h = w32_fd_to_handle(f);
ASSERT_HANDLE(h, retValue);
close(f);
char *tmp_filename_1 = "tmp_1.txt";
retValue = rename(tmp_filename, tmp_filename_1);
ASSERT_INT_EQ(retValue, 0);
retValue = unlink(tmp_filename_1);
ASSERT_INT_EQ(retValue, 0);
if(tmp)
free(tmp);
h = w32_fd_to_handle(STDIN_FILENO);
ASSERT_HANDLE(h, retValue);
h = w32_fd_to_handle(STDOUT_FILENO);
ASSERT_HANDLE(h, retValue);
h = w32_fd_to_handle(STDERR_FILENO);
ASSERT_HANDLE(h, retValue);
retValue = w32_allocate_fd_for_handle(h, FALSE);
ASSERT_HANDLE(h, retValue);
TEST_DONE();
}
void
void
file_tests()
{
//console_io_test();
file_simple_fileio();
file_simple_fileio_mode();
file_blocking_io_tests();
file_nonblocking_io_tests();
file_select_tests();
file_miscellaneous_tests();
}

View File

@ -0,0 +1,195 @@
#include "includes.h"
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <misc_internal.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include "../test_helper/test_helper.h"
#include "tests.h"
int retValue;
// The ioctl() testcase is failing when ran from Run-OpenSSHUnitTest.
void
test_ioctl()
{
if(!isatty(fileno(stdin))) return;
TEST_START("ioctl");
struct winsize ws;
memset(&ws, 0, sizeof(ws));
retValue = ioctl(fileno(stdin), TIOCGWINSZ, &ws);
ASSERT_INT_EQ(retValue, 0);
ASSERT_INT_NE(ws.ws_col, 0);
ASSERT_INT_NE(ws.ws_row, 0);
ASSERT_INT_NE(ws.ws_xpixel, 0);
ASSERT_INT_NE(ws.ws_ypixel, 0);
TEST_DONE();
}
void
test_path_conversion_utilities()
{
TEST_START("path conversion utilities");
char *s = "c:\\testdir\\test";
char *windows_style_path = dup_str(s);
int len = strlen(windows_style_path);
char *backup = malloc(len + 1);
strncpy(backup, windows_style_path, len);
backup[len] = '\0';
convertToForwardslash(windows_style_path);
char *tmpStr = strstr(windows_style_path, "\\");
ASSERT_PTR_EQ(tmpStr, NULL);
convertToBackslash(windows_style_path);
tmpStr = strstr(windows_style_path, "/");
ASSERT_PTR_EQ(tmpStr, NULL);
retValue = strcmp(windows_style_path, backup);
ASSERT_INT_EQ(retValue, 0);
free(windows_style_path);
TEST_DONE();
}
void
test_sanitizedpath()
{
TEST_START("win32 program dir");
char *win32prgdir = w32_programdir();
ASSERT_PTR_NE(win32prgdir, NULL);
ASSERT_PTR_EQ(sanitized_path(NULL), NULL);
char *ret = sanitized_path(win32prgdir);
retValue = strcmp(win32prgdir, ret);
ASSERT_INT_EQ(retValue, 0);
char win32prgdir_len = strlen(win32prgdir);
char *tmp_path = malloc(win32prgdir_len + 2); /* 1-NULL and 1-adding "/" */
tmp_path[0] = '/';
strncpy(tmp_path+1, win32prgdir, win32prgdir_len);
tmp_path[win32prgdir_len+1] = '\0';
ret = sanitized_path(tmp_path);
retValue = strcmp(win32prgdir, ret);
ASSERT_INT_EQ(retValue, 0);
char *s1 = malloc(4), *s2 = malloc(4);
s1[0] = '/', s1[1] = win32prgdir[0], s1[2] = ':', s1[3] = '\0';
s2[0] = win32prgdir[0], s2[1] = ':', s2[2] = '\\', s2[3] = '\0';
ret = sanitized_path(s1);
retValue = strcmp(ret, s2);
ASSERT_INT_EQ(retValue, 0);
TEST_DONE();
}
void
test_pw()
{
TEST_START("pw tests");
struct passwd *pw = NULL;
pw = getpwuid(0);
ASSERT_PTR_NE(pw, NULL);
struct passwd *pw1 = NULL;
char *user = dup_str(pw->pw_name);
pw1 = getpwnam(user);
ASSERT_PTR_NE(pw1, NULL);
TEST_DONE();
}
void
test_statvfs()
{
TEST_START("test statvfs");
struct statvfs st;
char cwd[MAX_PATH];
char *tmp = getcwd(cwd, MAX_PATH);
ASSERT_PTR_NE(tmp, NULL);
retValue = statvfs(NULL, &st);
ASSERT_INT_EQ(retValue, -1);
explicit_bzero(&st, sizeof(st));
retValue = statvfs(cwd, &st);
ASSERT_INT_EQ(retValue, 0);
ASSERT_INT_NE(st.f_bavail, 0);
TEST_DONE();
}
void test_realpath()
{
TEST_START("test realpath");
char resolved_path[MAX_PATH];
char *ret = NULL;
char *expectedOutput1 = "/c:/windows/system32";
char *expectedOutput2 = "/c:/";
ret = realpath(NULL, NULL);
ASSERT_PTR_EQ(ret, NULL);
ret = realpath("c:\\windows\\system32", NULL);
ASSERT_PTR_EQ(ret, NULL);
ret = realpath(NULL, resolved_path);
ASSERT_PTR_EQ(ret, NULL);
ret = realpath("c:\\windows\\system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("/c:\\windows\\system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("/c:\\windows\\.\\system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("/c:\\windows\\.\\..\\windows\\system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("/c:\\windows/.\\..\\windows\\system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("c:/windows/system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("/c:/windows/system32", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput1);
ret = realpath("c:", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput2);
ret = realpath("c:\\", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput2);
ret = realpath("/c:", resolved_path);
ASSERT_STRING_EQ(ret, expectedOutput2);
TEST_DONE();
}
void
miscellaneous_tests()
{
//test_ioctl();
test_path_conversion_utilities();
test_sanitizedpath();
test_pw();
test_realpath();
test_statvfs();
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
#include "includes.h"
#include <string.h>
#include "../test_helper/test_helper.h"
#include "tests.h"
int retValue;
void
str_simple_tests()
{
TEST_START("string testcases");
char *s1 = "test_dir";
char *s2 = NULL;
s2 = strdup(NULL);
ASSERT_PTR_EQ(s2, NULL);
s2 = strdup(s1);
ASSERT_PTR_NE(s2, NULL);
retValue = strcasecmp(s1, s2);
ASSERT_INT_EQ(retValue, 0);
retValue = strncasecmp(s1, s2, strlen(s1));
ASSERT_INT_EQ(retValue, 0);
s2[0] = 'T';
retValue = strcasecmp(s1, s2);
ASSERT_INT_EQ(retValue, 0);
retValue = strncasecmp(s1, s2, strlen(s1));
ASSERT_INT_EQ(retValue, 0);
free(s2);
TEST_DONE();
}
void
str_tests()
{
str_simple_tests();
}

View File

@ -3,22 +3,70 @@
*/
/* disable inclusion of compatability defitnitions in CRT headers */
#define __STDC__ 1
#include "includes.h"
#include <inc/dirent.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
//#include <io.h>
#include "../test_helper/test_helper.h"
#include "tests.h"
extern void log_init(char *av0, int level, int facility, int on_stderr);
void socket_tests();
void file_tests();
void tests(void)
void
tests()
{
_set_abort_behavior(0, 1);
log_init(NULL, 7, 2, 0);
socket_tests();
file_tests();
return;
}
dir_tests();
str_tests();
miscellaneous_tests();
}
char *
dup_str(char *inStr)
{
if(NULL == inStr)
return NULL;
int len = strlen(inStr);
char *outStr = malloc(len + 1);
strncpy(outStr, inStr, len);
outStr[len] = '\0';
return outStr;
}
void
delete_dir_recursive(char *full_dir_path)
{
DIR *dirp = opendir(full_dir_path);
if (!dirp) return;
struct stat st;
struct dirent *dp;
char mode[12];
char *tmpFullPath = malloc(MAX_PATH + 1);
strcpy(tmpFullPath, full_dir_path);
int tmpStrLen = strlen(tmpFullPath);
tmpFullPath[tmpStrLen++] = '\\';
while (dp = readdir(dirp)) {
strcpy(tmpFullPath + tmpStrLen, dp->d_name);
tmpFullPath[tmpStrLen + strlen(dp->d_name)] = '\0';
stat(tmpFullPath, &st);
strmode(st.st_mode, mode);
if (mode[0] == '-') /* regular file */
unlink(tmpFullPath);
else if (mode[0] == 'd') /* directory */
delete_dir_recursive(tmpFullPath);
}
closedir(dirp);
rmdir(full_dir_path);
free(tmpFullPath);
}

View File

@ -0,0 +1,13 @@
#pragma once
void socket_tests();
void file_tests();
void miscellaneous_tests();
char *dup_str(char *inStr);
void delete_dir_recursive(char *full_dir_path);
#define ASSERT_HANDLE(handle,retValue) \
{ \
ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE); \
ASSERT_PTR_NE(handle, 0); \
}