Added environment tests.
[wine/wine64.git] / dlls / ntdll / tests / env.c
blob25b89031c1cda52e26aaf9c26d4c65f38116f36d
1 /*
2 * Unit test suite for ntdll path functions
4 * Copyright 2003 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdio.h>
22 #include "wine/test.h"
23 #include "winnt.h"
24 #include "winternl.h"
25 #include "wine/unicode.h"
27 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
28 LPCSTR src, DWORD srclen );
29 static NTSTATUS (WINAPI *pRtlCreateEnvironment)(BOOLEAN, PWSTR*);
30 static NTSTATUS (WINAPI *pRtlDestroyEnvironment)(PWSTR);
31 static NTSTATUS (WINAPI *pRtlQueryEnvironmentVariable_U)(PWSTR, PUNICODE_STRING, PUNICODE_STRING);
32 static void (WINAPI *pRtlSetCurrentEnvironment)(PWSTR, PWSTR*);
33 static NTSTATUS (WINAPI *pRtlSetEnvironmentVariable)(PWSTR*, PUNICODE_STRING, PUNICODE_STRING);
35 static WCHAR small_env[] = {'f','o','o','=','t','o','t','o',0,
36 'f','o','=','t','i','t','i',0,
37 'f','o','o','o','=','t','u','t','u',0,
38 's','r','=','a','n','=','o','u','o',0,
39 'g','=','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
40 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
41 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
42 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
43 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
44 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
45 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
46 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
47 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
48 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0,
49 '=','o','O','H','=','I','I','I',0,
50 0};
52 static void testQuery(void)
54 struct test
56 const char *var;
57 int len;
58 NTSTATUS status;
59 const char *val;
62 static const struct test tests[] =
64 {"foo", 256, STATUS_SUCCESS, "toto"},
65 {"FoO", 256, STATUS_SUCCESS, "toto"},
66 {"foo=", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
67 {"foo ", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
68 {"foo", 1, STATUS_BUFFER_TOO_SMALL, "toto"},
69 {"foo", 3, STATUS_BUFFER_TOO_SMALL, "toto"},
70 {"foo", 4, STATUS_SUCCESS, "toto"},
71 {"fooo", 256, STATUS_SUCCESS, "tutu"},
72 {"f", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
73 {"g", 256, STATUS_SUCCESS, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
74 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
75 {"sr=an", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
76 {"sr", 256, STATUS_SUCCESS, "an=ouo"},
77 {"=oOH", 256, STATUS_SUCCESS, "III"},
78 {"", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
79 {NULL, 0, 0, NULL}
82 WCHAR bn[256];
83 WCHAR bv[256];
84 UNICODE_STRING name;
85 UNICODE_STRING value;
86 const struct test* test;
87 NTSTATUS nts;
89 for (test = tests; test->var; test++)
91 name.Length = strlen(test->var) * 2;
92 name.MaximumLength = name.Length + 2;
93 name.Buffer = bn;
94 value.Length = 0;
95 value.MaximumLength = test->len * 2;
96 value.Buffer = bv;
98 pRtlMultiByteToUnicodeN( bn, sizeof(bn), NULL, test->var, strlen(test->var)+1 );
99 nts = pRtlQueryEnvironmentVariable_U(small_env, &name, &value);
100 ok( nts == test->status, "[%d]: Wrong status for '%s', expecting %lx got %lx",
101 test - tests, test->var, test->status, nts );
102 if (nts == test->status) switch (nts)
104 case STATUS_SUCCESS:
105 pRtlMultiByteToUnicodeN( bn, sizeof(bn), NULL, test->val, strlen(test->val)+1 );
106 ok( value.Length == strlen(test->val) * sizeof(WCHAR), "Wrong length %d/%d for %s",
107 value.Length, strlen(test->val) * sizeof(WCHAR), test->var );
108 ok( strcmpW(bv, bn) == 0, "Wrong result for %s", test->var );
109 break;
110 case STATUS_BUFFER_TOO_SMALL:
111 ok( value.Length == strlen(test->val) * sizeof(WCHAR),
112 "Wrong returned length %d/%d (too small buffer) for %s",
113 value.Length, strlen(test->val) * sizeof(WCHAR), test->var );
114 break;
119 static void testSetHelper(LPWSTR* env, const char* var, const char* val, NTSTATUS ret)
121 WCHAR bvar[256], bval1[256], bval2[256];
122 UNICODE_STRING uvar;
123 UNICODE_STRING uval;
124 NTSTATUS nts;
126 uvar.Length = strlen(var) * sizeof(WCHAR);
127 uvar.MaximumLength = uvar.Length + sizeof(WCHAR);
128 uvar.Buffer = bvar;
129 pRtlMultiByteToUnicodeN( bvar, sizeof(bvar), NULL, var, strlen(var)+1 );
130 if (val)
132 uval.Length = strlen(val) * sizeof(WCHAR);
133 uval.MaximumLength = uval.Length + sizeof(WCHAR);
134 uval.Buffer = bval1;
135 pRtlMultiByteToUnicodeN( bval1, sizeof(bval1), NULL, val, strlen(val)+1 );
137 nts = pRtlSetEnvironmentVariable(env, &uvar, val ? &uval : NULL);
138 ok(nts == ret, "Setting var %s=%s (%lx/%lx)", var, val, nts, ret);
139 if (nts == STATUS_SUCCESS)
141 uval.Length = 0;
142 uval.MaximumLength = sizeof(bval2);
143 uval.Buffer = bval2;
144 nts = pRtlQueryEnvironmentVariable_U(*env, &uvar, &uval);
145 switch (nts)
147 case STATUS_SUCCESS:
148 ok(strcmpW(bval1, bval2) == 0, "Cannot get value written to environment");
149 break;
150 case STATUS_VARIABLE_NOT_FOUND:
151 ok(val == NULL, "Couldn't find variable, but didn't delete it");
152 break;
153 default:
154 ok(0, "Wrong ret %lu for %s", nts, var);
155 break;
160 static void testSet(void)
162 LPWSTR env;
163 char tmp[16];
164 int i;
166 ok(pRtlCreateEnvironment(FALSE, &env) == STATUS_SUCCESS, "Creating environment");
167 memmove(env, small_env, sizeof(small_env));
169 testSetHelper(&env, "cat", "dog", STATUS_SUCCESS);
170 testSetHelper(&env, "cat", "horse", STATUS_SUCCESS);
171 testSetHelper(&env, "cat", "zz", STATUS_SUCCESS);
172 testSetHelper(&env, "cat", NULL, STATUS_SUCCESS);
173 testSetHelper(&env, "cat", NULL, STATUS_VARIABLE_NOT_FOUND);
174 testSetHelper(&env, "foo", "meouw", STATUS_SUCCESS);
175 testSetHelper(&env, "me=too", "also", STATUS_INVALID_PARAMETER);
176 testSetHelper(&env, "me", "too=also", STATUS_SUCCESS);
177 testSetHelper(&env, "=too", "also", STATUS_SUCCESS);
178 testSetHelper(&env, "=", "also", STATUS_SUCCESS);
180 for (i = 0; i < 128; i++)
182 sprintf(tmp, "zork%03d", i);
183 testSetHelper(&env, tmp, "is alive", STATUS_SUCCESS);
186 for (i = 0; i < 128; i++)
188 sprintf(tmp, "zork%03d", i);
189 testSetHelper(&env, tmp, NULL, STATUS_SUCCESS);
191 testSetHelper(&env, "fOo", NULL, STATUS_SUCCESS);
193 ok(pRtlDestroyEnvironment(env) == STATUS_SUCCESS, "Destroying environment");
196 START_TEST(env)
198 HMODULE mod = GetModuleHandleA("ntdll.dll");
200 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(mod,"RtlMultiByteToUnicodeN");
201 pRtlCreateEnvironment = (void*)GetProcAddress(mod, "RtlCreateEnvironment");
202 pRtlDestroyEnvironment = (void*)GetProcAddress(mod, "RtlDestroyEnvironment");
203 pRtlQueryEnvironmentVariable_U = (void*)GetProcAddress(mod, "RtlQueryEnvironmentVariable_U");
204 pRtlSetCurrentEnvironment = (void*)GetProcAddress(mod, "RtlSetCurrentEnvironment");
205 pRtlSetEnvironmentVariable = (void*)GetProcAddress(mod, "RtlSetEnvironmentVariable");
207 testQuery();
208 testSet();