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: gettemppatha.c (test 1) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the GetTempPathA function. |
10 | ** |
11 | ** |
12 | **===================================================================*/ |
13 | |
14 | #include <palsuite.h> |
15 | |
16 | static void SetTmpDir(CHAR path[]) |
17 | { |
18 | DWORD result = SetEnvironmentVariableA("TMPDIR" , path); |
19 | if (!result) |
20 | { |
21 | Fail("ERROR -> SetEnvironmentVariableA failed with result %d and error code %d.\n" , |
22 | result, GetLastError()); |
23 | } |
24 | } |
25 | |
26 | static void SetAndCompare(CHAR tmpDirPath[], CHAR expected[]) |
27 | { |
28 | DWORD dwBufferLength = _MAX_DIR; |
29 | CHAR path[dwBufferLength]; |
30 | |
31 | SetTmpDir(tmpDirPath); |
32 | |
33 | DWORD dwResultLen = GetTempPathA(dwBufferLength, path); |
34 | if (dwResultLen <= 0) |
35 | { |
36 | Fail("ERROR: GetTempPathA 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 (strcmp(expected, path) != 0) |
43 | { |
44 | Fail("ERROR: GetTempPathA expected to get '%s' but instead got '%s'.\n" , expected, path); |
45 | } |
46 | if (expected[dwResultLen - 1] != '/') |
47 | { |
48 | Fail("ERROR: GetTempPathA returned '%s', which should have ended in '/'.\n" , path); |
49 | } |
50 | } |
51 | |
52 | static void SetAndCheckLength(CHAR tmpDirPath[], int bufferLength, int expectedResultLength) |
53 | { |
54 | CHAR path[bufferLength]; |
55 | |
56 | SetTmpDir(tmpDirPath); |
57 | DWORD dwResultLen = GetTempPathA(bufferLength, path); |
58 | |
59 | if (dwResultLen != expectedResultLength) |
60 | { |
61 | Fail("GetTempPathA(%d, %s) expected to return %d but returned %d.\n" , |
62 | bufferLength, tmpDirPath?tmpDirPath:"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("/tmp" , "/tmp/" ); |
74 | SetAndCompare("/tmp/" , "/tmp/" ); |
75 | SetAndCompare("" , "/tmp/" ); |
76 | SetAndCompare(NULL, "/tmp/" ); |
77 | SetAndCompare("/" , "/" ); |
78 | SetAndCompare("/var/tmp" , "/var/tmp/" ); |
79 | SetAndCompare("/var/tmp/" , "/var/tmp/" ); |
80 | SetAndCompare("~" , "~/" ); |
81 | SetAndCompare("~/" , "~/" ); |
82 | SetAndCompare(".tmp" , ".tmp/" ); |
83 | SetAndCompare("./tmp" , "./tmp/" ); |
84 | SetAndCompare("/home/someuser/sometempdir" , "/home/someuser/sometempdir/" ); |
85 | SetAndCompare(NULL, "/tmp/" ); |
86 | |
87 | DWORD dwResultLen = GetTempPathA(0, NULL); |
88 | if (dwResultLen != 0 || GetLastError() != ERROR_INVALID_PARAMETER) |
89 | { |
90 | Fail("GetTempPath(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("abc/" , 5, 4); |
96 | SetAndCheckLength("abcd" , 5, 6); |
97 | SetAndCheckLength("abcde" , 5, 7); |
98 | SetAndCheckLength("abcdef/" , 5, 9); |
99 | SetAndCheckLength(NULL, 5, 6); |
100 | |
101 | PAL_Terminate(); |
102 | return PASS; |
103 | } |
104 | |