| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: GetTempPathW.c (test 1) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetTempPathW function. |
| 10 | ** |
| 11 | ** |
| 12 | **===================================================================*/ |
| 13 | |
| 14 | #include <palsuite.h> |
| 15 | |
| 16 | static void SetTmpDir(const WCHAR path[]) |
| 17 | { |
| 18 | DWORD result = SetEnvironmentVariableW(W("TMPDIR" ), path); |
| 19 | if (!result) |
| 20 | { |
| 21 | Fail("ERROR -> SetEnvironmentVariableW failed with result %d and error code %d.\n" , |
| 22 | result, GetLastError()); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | static void SetAndCompare(const WCHAR tmpDirPath[], const WCHAR expected[]) |
| 27 | { |
| 28 | DWORD dwBufferLength = _MAX_DIR; |
| 29 | WCHAR path[dwBufferLength]; |
| 30 | |
| 31 | SetTmpDir(tmpDirPath); |
| 32 | |
| 33 | DWORD dwResultLen = GetTempPathW(dwBufferLength, path); |
| 34 | if (dwResultLen <= 0) |
| 35 | { |
| 36 | Fail("ERROR: GetTempPathW returned %d with error code %d.\n" , dwResultLen, GetLastError()); |
| 37 | } |
| 38 | if (dwResultLen >= dwBufferLength) |
| 39 | { |
| 40 | Fail("ERROR: Buffer of length %d passed to GetTempPathA was too small to hold %d chars..\n" , dwBufferLength, dwResultLen); |
| 41 | } |
| 42 | if (wcscmp(expected, path) != 0) |
| 43 | { |
| 44 | Fail("ERROR: GetTempPathW expected to get '%S' but instead got '%S'.\n" , expected, path); |
| 45 | } |
| 46 | if (expected[dwResultLen - 1] != '/') |
| 47 | { |
| 48 | Fail("ERROR: GetTempPathW returned '%S', which should have ended in '/'.\n" , path); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static void SetAndCheckLength(const WCHAR tmpDirPath [], int bufferLength, int expectedResultLength) |
| 53 | { |
| 54 | WCHAR path[bufferLength]; |
| 55 | |
| 56 | SetTmpDir(tmpDirPath); |
| 57 | DWORD dwResultLen = GetTempPathW(bufferLength, path); |
| 58 | |
| 59 | if (dwResultLen != expectedResultLength) |
| 60 | { |
| 61 | Fail("GetTempPathW(%d, %S) expected to return %d but returned %d.\n" , |
| 62 | bufferLength, tmpDirPath?tmpDirPath:W("NULL" ), expectedResultLength, dwResultLen); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | int __cdecl main(int argc, char *argv[]) |
| 67 | { |
| 68 | if (0 != PAL_Initialize(argc, argv)) |
| 69 | { |
| 70 | return FAIL; |
| 71 | } |
| 72 | |
| 73 | SetAndCompare(W("/tmp" ), W("/tmp/" )); |
| 74 | SetAndCompare(W("/tmp/" ), W("/tmp/" )); |
| 75 | SetAndCompare(W("" ), W("/tmp/" )); |
| 76 | SetAndCompare(NULL, W("/tmp/" )); |
| 77 | SetAndCompare(W("/" ), W("/" )); |
| 78 | SetAndCompare(W("/var/tmp" ), W("/var/tmp/" )); |
| 79 | SetAndCompare(W("/var/tmp/" ), W("/var/tmp/" )); |
| 80 | SetAndCompare(W("~" ), W("~/" )); |
| 81 | SetAndCompare(W("~/" ), W("~/" )); |
| 82 | SetAndCompare(W(".tmp" ), W(".tmp/" )); |
| 83 | SetAndCompare(W("./tmp" ), W("./tmp/" )); |
| 84 | SetAndCompare(W("/home/someuser/sometempdir" ), W("/home/someuser/sometempdir/" )); |
| 85 | SetAndCompare(NULL, W("/tmp/" )); |
| 86 | |
| 87 | DWORD dwResultLen = GetTempPathA(0, NULL); |
| 88 | if (dwResultLen != 0 || GetLastError() != ERROR_INVALID_PARAMETER) |
| 89 | { |
| 90 | Fail("GetTempPathW(NULL, ...) returned %d with error code %d but " |
| 91 | "should have failed with ERROR_INVALID_PARAMETER (%d).\n" , |
| 92 | dwResultLen, GetLastError(), ERROR_INVALID_PARAMETER); |
| 93 | } |
| 94 | |
| 95 | SetAndCheckLength(W("abc/" ), 5, 4); |
| 96 | SetAndCheckLength(W("abcd" ), 5, 6); |
| 97 | SetAndCheckLength(W("abcde" ), 5, 7); |
| 98 | SetAndCheckLength(W("abcdef/" ), 5, 9); |
| 99 | SetAndCheckLength(NULL, 5, 6); |
| 100 | |
| 101 | PAL_Terminate(); |
| 102 | return PASS; |
| 103 | } |
| 104 | |