Fixed unit tests, use uniqe file names

This commit is contained in:
Armin Novak 2018-12-03 17:23:55 +01:00
parent 6a75bc806b
commit d27cd1b19e
3 changed files with 73 additions and 34 deletions

View File

@ -4,6 +4,7 @@
#include <winpr/image.h> #include <winpr/image.h>
#include <winpr/print.h> #include <winpr/print.h>
#include <winpr/wlog.h> #include <winpr/wlog.h>
#include <winpr/sysinfo.h>
#include <freerdp/codec/region.h> #include <freerdp/codec/region.h>
@ -923,7 +924,14 @@ static int test_progressive_ms_sample(char* ms_sample_path)
int TestFreeRDPCodecProgressive(int argc, char* argv[]) int TestFreeRDPCodecProgressive(int argc, char* argv[])
{ {
char* ms_sample_path; char* ms_sample_path;
ms_sample_path = GetKnownSubPath(KNOWN_PATH_TEMP, "EGFX_PROGRESSIVE_MS_SAMPLE"); char name[8192];
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
sprintf_s(name, sizeof(name),
"EGFX_PROGRESSIVE_MS_SAMPLE-%04"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%04"PRIu16,
systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute,
systemTime.wSecond, systemTime.wMilliseconds);
ms_sample_path = GetKnownSubPath(KNOWN_PATH_TEMP, name);
if (!ms_sample_path) if (!ms_sample_path)
{ {

View File

@ -19,16 +19,20 @@
#include <winpr/path.h> #include <winpr/path.h>
#include <winpr/file.h> #include <winpr/file.h>
#include <winpr/sysinfo.h>
#include <freerdp/crypto/certificate.h> #include <freerdp/crypto/certificate.h>
static int prepare(const char* currentFileV2, const char* legacyFileV2, const char* legacyFile) static int prepare(const char* currentFileV2, const char* legacyFileV2, const char* legacyFile)
{ {
char* legacy[] = { char* legacy[] =
{
"someurl ff:11:22:dd\r\n", "someurl ff:11:22:dd\r\n",
"otherurl aa:bb:cc:dd\r", "otherurl aa:bb:cc:dd\r",
"legacyurl aa:bb:cc:dd\n" "legacyurl aa:bb:cc:dd\n"
}; };
char* hosts[] = { char* hosts[] =
{
"#somecomment\r\n" "#somecomment\r\n"
"someurl 3389 ff:11:22:dd subject issuer\r\n" "someurl 3389 ff:11:22:dd subject issuer\r\n"
" \t#anothercomment\r\n" " \t#anothercomment\r\n"
@ -37,16 +41,17 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
FILE* fl = NULL; FILE* fl = NULL;
FILE* fc = NULL; FILE* fc = NULL;
size_t i; size_t i;
fc = fopen(currentFileV2, "w+"); fc = fopen(currentFileV2, "w+");
if (!fc) if (!fc)
goto finish; goto finish;
fl = fopen(legacyFileV2, "w+"); fl = fopen(legacyFileV2, "w+");
if (!fl) if (!fl)
goto finish; goto finish;
for (i=0; i<sizeof(hosts)/sizeof(hosts[0]); i++) for (i = 0; i < sizeof(hosts) / sizeof(hosts[0]); i++)
{ {
if (fwrite(hosts[i], strlen(hosts[i]), 1, fl) != 1 || if (fwrite(hosts[i], strlen(hosts[i]), 1, fl) != 1 ||
fwrite(hosts[i], strlen(hosts[i]), 1, fc) != 1) fwrite(hosts[i], strlen(hosts[i]), 1, fc) != 1)
@ -55,15 +60,14 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
fclose(fc); fclose(fc);
fc = NULL; fc = NULL;
fclose(fl); fclose(fl);
fl = NULL; fl = NULL;
fl = fopen(legacyFile, "w+"); fl = fopen(legacyFile, "w+");
if (!fl) if (!fl)
goto finish; goto finish;
for (i=0; i<sizeof(legacy)/sizeof(legacy[0]); i++) for (i = 0; i < sizeof(legacy) / sizeof(legacy[0]); i++)
{ {
if (fwrite(legacy[i], strlen(legacy[i]), 1, fl) != 1) if (fwrite(legacy[i], strlen(legacy[i]), 1, fl) != 1)
goto finish; goto finish;
@ -71,10 +75,11 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
fclose(fl); fclose(fl);
return 0; return 0;
finish: finish:
if (fl) if (fl)
fclose(fl); fclose(fl);
if (fc) if (fc)
fclose(fc); fclose(fc);
@ -94,9 +99,22 @@ int TestKnownHosts(int argc, char* argv[])
char* subject = NULL; char* subject = NULL;
char* issuer = NULL; char* issuer = NULL;
char* fp = NULL; char* fp = NULL;
char sname[8192];
char dname[8192];
SYSTEMTIME systemTime;
current.ConfigPath = GetKnownSubPath(KNOWN_PATH_TEMP, "TestKnownHostsCurrent"); GetSystemTime(&systemTime);
legacy.ConfigPath = GetKnownSubPath(KNOWN_PATH_TEMP, "TestKnownHostsLegacy"); sprintf_s(sname, sizeof(sname),
"TestKnownHostsCurrent-%04"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%04"PRIu16,
systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute,
systemTime.wSecond, systemTime.wMilliseconds);
sprintf_s(dname, sizeof(dname),
"TestKnownHostsLegacy-%04"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%04"PRIu16,
systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute,
systemTime.wSecond, systemTime.wMilliseconds);
current.ConfigPath = GetKnownSubPath(KNOWN_PATH_TEMP, sname);
legacy.ConfigPath = GetKnownSubPath(KNOWN_PATH_TEMP, dname);
if (!PathFileExistsA(current.ConfigPath)) if (!PathFileExistsA(current.ConfigPath))
{ {
@ -117,6 +135,7 @@ int TestKnownHosts(int argc, char* argv[])
} }
currentFileV2 = GetCombinedPath(current.ConfigPath, "known_hosts2"); currentFileV2 = GetCombinedPath(current.ConfigPath, "known_hosts2");
if (!currentFileV2) if (!currentFileV2)
{ {
fprintf(stderr, "Could not get file path!\n"); fprintf(stderr, "Could not get file path!\n");
@ -124,6 +143,7 @@ int TestKnownHosts(int argc, char* argv[])
} }
legacyFileV2 = GetCombinedPath(legacy.ConfigPath, "known_hosts2"); legacyFileV2 = GetCombinedPath(legacy.ConfigPath, "known_hosts2");
if (!legacyFileV2) if (!legacyFileV2)
{ {
fprintf(stderr, "Could not get file path!\n"); fprintf(stderr, "Could not get file path!\n");
@ -131,6 +151,7 @@ int TestKnownHosts(int argc, char* argv[])
} }
legacyFile = GetCombinedPath(legacy.ConfigPath, "known_hosts"); legacyFile = GetCombinedPath(legacy.ConfigPath, "known_hosts");
if (!legacyFile) if (!legacyFile)
{ {
fprintf(stderr, "Could not get file path!\n"); fprintf(stderr, "Could not get file path!\n");
@ -138,6 +159,7 @@ int TestKnownHosts(int argc, char* argv[])
} }
store = certificate_store_new(&current); store = certificate_store_new(&current);
if (!store) if (!store)
{ {
fprintf(stderr, "Could not create certificate store!\n"); fprintf(stderr, "Could not create certificate store!\n");
@ -149,6 +171,7 @@ int TestKnownHosts(int argc, char* argv[])
/* Test if host is found in current file. */ /* Test if host is found in current file. */
data = certificate_data_new("someurl", 3389, "subject", "issuer", "ff:11:22:dd"); data = certificate_data_new("someurl", 3389, "subject", "issuer", "ff:11:22:dd");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -167,6 +190,7 @@ int TestKnownHosts(int argc, char* argv[])
fprintf(stderr, "Could not read old fingerprint!\n"); fprintf(stderr, "Could not read old fingerprint!\n");
goto finish; goto finish;
} }
printf("Got %s, %s '%s'\n", subject, issuer, fp); printf("Got %s, %s '%s'\n", subject, issuer, fp);
free(subject); free(subject);
free(issuer); free(issuer);
@ -175,9 +199,9 @@ int TestKnownHosts(int argc, char* argv[])
issuer = NULL; issuer = NULL;
fp = NULL; fp = NULL;
certificate_data_free(data); certificate_data_free(data);
/* Test if host not found in current file. */ /* Test if host not found in current file. */
data = certificate_data_new("somehost", 1234, "", "", "ff:aa:bb:cc"); data = certificate_data_new("somehost", 1234, "", "", "ff:aa:bb:cc");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -198,9 +222,9 @@ int TestKnownHosts(int argc, char* argv[])
} }
certificate_data_free(data); certificate_data_free(data);
/* Test host add current file. */ /* Test host add current file. */
data = certificate_data_new("somehost", 1234, "", "", "ff:aa:bb:cc"); data = certificate_data_new("somehost", 1234, "", "", "ff:aa:bb:cc");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -220,9 +244,9 @@ int TestKnownHosts(int argc, char* argv[])
} }
certificate_data_free(data); certificate_data_free(data);
/* Test host replace current file. */ /* Test host replace current file. */
data = certificate_data_new("somehost", 1234, "", "", "ff:aa:bb:dd:ee"); data = certificate_data_new("somehost", 1234, "", "", "ff:aa:bb:dd:ee");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -242,9 +266,9 @@ int TestKnownHosts(int argc, char* argv[])
} }
certificate_data_free(data); certificate_data_free(data);
/* Test host replace invalid entry in current file. */ /* Test host replace invalid entry in current file. */
data = certificate_data_new("somehostXXXX", 1234, "", "", "ff:aa:bb:dd:ee"); data = certificate_data_new("somehostXXXX", 1234, "", "", "ff:aa:bb:dd:ee");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -264,11 +288,9 @@ int TestKnownHosts(int argc, char* argv[])
} }
certificate_data_free(data); certificate_data_free(data);
certificate_store_free(store); certificate_store_free(store);
store = certificate_store_new(&legacy); store = certificate_store_new(&legacy);
if (!store) if (!store)
{ {
fprintf(stderr, "could not create certificate store!\n"); fprintf(stderr, "could not create certificate store!\n");
@ -277,6 +299,7 @@ int TestKnownHosts(int argc, char* argv[])
/* test if host found in legacy file. */ /* test if host found in legacy file. */
data = certificate_data_new("legacyurl", 1234, "", "", "aa:bb:cc:dd"); data = certificate_data_new("legacyurl", 1234, "", "", "aa:bb:cc:dd");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -290,9 +313,9 @@ int TestKnownHosts(int argc, char* argv[])
} }
certificate_data_free(data); certificate_data_free(data);
/* test if host not found. */ /* test if host not found. */
data = certificate_data_new("somehost-not-in-file", 1234, "", "", "ff:aa:bb:cc"); data = certificate_data_new("somehost-not-in-file", 1234, "", "", "ff:aa:bb:cc");
if (!data) if (!data)
{ {
fprintf(stderr, "Could not create certificate data!\n"); fprintf(stderr, "Could not create certificate data!\n");
@ -306,27 +329,26 @@ int TestKnownHosts(int argc, char* argv[])
} }
rc = 0; rc = 0;
finish: finish:
free(current.ConfigPath); free(current.ConfigPath);
free(legacy.ConfigPath); free(legacy.ConfigPath);
if (store) if (store)
certificate_store_free(store); certificate_store_free(store);
if (data) if (data)
certificate_data_free(data); certificate_data_free(data);
DeleteFileA(currentFileV2); DeleteFileA(currentFileV2);
//RemoveDirectoryA(current.ConfigPath); //RemoveDirectoryA(current.ConfigPath);
DeleteFileA(legacyFileV2); DeleteFileA(legacyFileV2);
DeleteFileA(legacyFile); DeleteFileA(legacyFile);
//RemoveDirectoryA(legacy.ConfigPath); //RemoveDirectoryA(legacy.ConfigPath);
free(currentFileV2);
free (currentFileV2); free(legacyFileV2);
free (legacyFileV2); free(legacyFile);
free (legacyFile);
free(subject); free(subject);
free(issuer); free(issuer);
free(fp); free(fp);
return rc; return rc;
} }

View File

@ -5,6 +5,7 @@
#include <winpr/path.h> #include <winpr/path.h>
#include <winpr/handle.h> #include <winpr/handle.h>
#include <winpr/windows.h> #include <winpr/windows.h>
#include <winpr/sysinfo.h>
int TestFileCreateFile(int argc, char* argv[]) int TestFileCreateFile(int argc, char* argv[])
{ {
@ -13,9 +14,16 @@ int TestFileCreateFile(int argc, char* argv[])
DWORD written; DWORD written;
const char buffer[] = "Some random text\r\njust want it done."; const char buffer[] = "Some random text\r\njust want it done.";
char cmp[sizeof(buffer)]; char cmp[sizeof(buffer)];
LPSTR name = GetKnownSubPath(KNOWN_PATH_TEMP, "CreateFile.testfile"); char sname[8192];
LPSTR name;
int rc = 0; int rc = 0;
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
sprintf_s(sname, sizeof(sname),
"CreateFile-%04"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%02"PRIu16"%04"PRIu16,
systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute,
systemTime.wSecond, systemTime.wMilliseconds);
name = GetKnownSubPath(KNOWN_PATH_TEMP, sname);
if (!name) if (!name)
return -1; return -1;
@ -23,11 +31,13 @@ int TestFileCreateFile(int argc, char* argv[])
/* On windows we would need '\\' or '/' as seperator. /* On windows we would need '\\' or '/' as seperator.
* Single '\' do not work. */ * Single '\' do not work. */
hr = PathCchConvertStyleA(name, strlen(name), PATH_STYLE_UNIX); hr = PathCchConvertStyleA(name, strlen(name), PATH_STYLE_UNIX);
if (FAILED(hr)) if (FAILED(hr))
rc = -1; rc = -1;
handle = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, handle = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (!handle) if (!handle)
{ {
free(name); free(name);
@ -77,6 +87,5 @@ int TestFileCreateFile(int argc, char* argv[])
rc = -1; rc = -1;
free(name); free(name);
return rc; return rc;
} }