diff options
author | Jiyoung Yun <jy910.yun@samsung.com> | 2016-11-23 19:09:09 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2016-11-23 19:09:09 +0900 |
commit | 4b4aad7217d3292650e77eec2cf4c198ea9c3b4b (patch) | |
tree | 98110734c91668dfdbb126fcc0e15ddbd93738ca /src/pal/tests/palsuite/file_io/SetFileAttributesW/test1 | |
parent | fa45f57ed55137c75ac870356a1b8f76c84b229c (diff) | |
download | coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.gz coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.bz2 coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.zip |
Imported Upstream version 1.1.0upstream/1.1.0
Diffstat (limited to 'src/pal/tests/palsuite/file_io/SetFileAttributesW/test1')
4 files changed, 201 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/CMakeLists.txt new file mode 100644 index 0000000000..35927b4b1d --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFileAttributesW.c +) + +add_executable(paltest_setfileattributesw_test1 + ${SOURCES} +) + +add_dependencies(paltest_setfileattributesw_test1 coreclrpal) + +target_link_libraries(paltest_setfileattributesw_test1 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/SetFileAttributesW.c b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/SetFileAttributesW.c new file mode 100644 index 0000000000..753c396d1f --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/SetFileAttributesW.c @@ -0,0 +1,167 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*===================================================================== +** +** Source: SetFileAttributesW.c +** +** Purpose: Tests the PAL implementation of the SetFileAttributesW function +** Test that we can set a file READONLY, and then that we're unable to +** open that file with WRITE access. Then change it to NORMAL attributes, and +** try to open it again -- it should work now. +** +** Depends: +** CreateFile +** CloseHandle +** +** +**===================================================================*/ + +/* According to the spec, only READONLY attribute can be set + in FreeBSD. +*/ + +#define UNICODE + +#include <palsuite.h> + + + +int __cdecl main(int argc, char **argv) +{ + DWORD TheResult; + HANDLE TheFile; + CHAR *FileName_Multibyte = "test_file"; + WCHAR FileName[MAX_PATH]; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + // Create the test file + FILE *testFile = fopen(FileName_Multibyte, "w"); + if (testFile == NULL) + { + Fail("Unexpected error: Unable to open file %S with fopen. \n", FileName); + } + if (fputs("testing", testFile) == EOF) + { + Fail("Unexpected error: Unable to write to file %S with fputs. \n", FileName); + } + if (fclose(testFile) != 0) + { + Fail("Unexpected error: Unable to close file %S with fclose. \n", FileName); + } + testFile = NULL; + + /* Make a wide character string for the file name */ + + MultiByteToWideChar(CP_ACP, + 0, + FileName_Multibyte, + -1, + FileName, + MAX_PATH); + + + /* Try to set the file to Read-only */ + + TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_READONLY); + + if(TheResult == 0) + { + Fail("ERROR: SetFileAttributes returned 0, failure, when trying " + "to set the FILE_ATTRIBUTE_READONLY attribute."); + } + + /* Attempt to open this READONLY file with WRITE access, + The open should fail and the HANDLE should be invalid. + */ + + TheFile = CreateFile( + FileName, // file name + GENERIC_READ|GENERIC_WRITE, // access mode + 0, // share mode + NULL, // SD + OPEN_ALWAYS, // how to create + FILE_ATTRIBUTE_NORMAL, // file attributes + NULL // handle to template file + ); + + if(TheFile != INVALID_HANDLE_VALUE) + { + Fail("ERROR: Tried to open a file that was created as " + "READONLY with the GENERIC_WRITE access mode. This should" + " cause CreateFile to return an INVALID_HANDLE_VALUE."); + } + + /* Try to open the file with READ access, this should be ok. + The HANDLE will be valid. + */ + + TheFile = CreateFile( + FileName, // file name + GENERIC_READ, // access mode + 0, // share mode + NULL, // SD + OPEN_ALWAYS, // how to create + FILE_ATTRIBUTE_NORMAL, // file attributes + NULL // handle to template file + ); + + if(TheFile == INVALID_HANDLE_VALUE) + { + Fail("ERROR: Tried to open a file that was created as " + "READONLY with the GENERIC_READ access mode. This should" + " cause CreateFile to return an valid handle, but " + "INVALID_HANDLE_VALUE was returned!."); + } + + /* Close that HANDLE */ + + TheResult = CloseHandle(TheFile); + + if(TheResult == 0) + { + Fail("ERROR: CloseHandle failed. This tests relies upon it " + "working properly."); + } + + /* Set the file to NORMAL */ + + TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_NORMAL); + + if(TheResult == 0) + { + Fail("ERROR: SetFileAttributes returned 0, failure, when trying " + "to set the FILE_ATTRIBUTE_NORMAL attribute."); + } + + /* To ensure that the set worked correctly, try to open the file + with WRITE access again -- this time it should succeed. + */ + + TheFile = CreateFile( + FileName, // file name + GENERIC_READ|GENERIC_WRITE, // access mode + 0, // share mode + NULL, // SD + OPEN_ALWAYS, // how to create + FILE_ATTRIBUTE_NORMAL, // file attributes + NULL // handle to template file + ); + + if(TheFile == INVALID_HANDLE_VALUE) + { + Fail("ERROR: Tried to open a file that was created as " + "NORMAL with the GENERIC_WRITE access mode. This should" + " cause CreateFile to return an valid handle, but " + "INVALID_HANDLE_VALUE was returned!."); + } + + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/test_file b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/test_file new file mode 100644 index 0000000000..1b4bcfe1b5 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/test_file @@ -0,0 +1 @@ +Don't delete me. I'm needed. The File.
\ No newline at end of file diff --git a/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/testinfo.dat new file mode 100644 index 0000000000..75cdc3b719 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFileAttributesW/test1/testinfo.dat @@ -0,0 +1,14 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +Version = 1.0 +Section = file_io +Function = SetFileAttributesW +Name = Positive Test for SetFileAttributesW +TYPE = DEFAULT +EXE1 = setfileattributesw +Description += Test the SetFileAttributesW function. Set a file as READONLY and += attempt to open it with WRITE access, to ensure that this doesn't work. += Then switch to NORMAL attributes, and try again -- it should work. |