pdh: Remove or fix tests that fail on Windows.
[wine] / dlls / pdh / tests / pdh.c
1 /*
2  * Tests for pdh.dll (Performance Data Helper)
3  *
4  * Copyright 2007 Hans Leidekker
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22
23 #include "windows.h"
24
25 #include "pdh.h"
26 #include "pdhmsg.h"
27
28 #include "wine/test.h"
29
30 static HMODULE pdh;
31
32 static PDH_STATUS   (WINAPI *pPdhAddEnglishCounterA)(PDH_HQUERY, LPCSTR, DWORD_PTR, PDH_HCOUNTER *);
33 static PDH_STATUS   (WINAPI *pPdhAddEnglishCounterW)(PDH_HQUERY, LPCWSTR, DWORD_PTR, PDH_HCOUNTER *);
34 static PDH_STATUS   (WINAPI *pPdhCollectQueryDataWithTime)(PDH_HQUERY, LONGLONG *);
35 static PDH_STATUS   (WINAPI *pPdhValidatePathExA)(PDH_HLOG, LPCSTR);
36 static PDH_STATUS   (WINAPI *pPdhValidatePathExW)(PDH_HLOG, LPCWSTR);
37
38 #define GETFUNCPTR(func) p##func = (void *)GetProcAddress( pdh, #func );
39
40 static void init_function_ptrs( void )
41 {
42     pdh = GetModuleHandle( "pdh" );
43     GETFUNCPTR( PdhAddEnglishCounterA )
44     GETFUNCPTR( PdhAddEnglishCounterW )
45     GETFUNCPTR( PdhCollectQueryDataWithTime )
46     GETFUNCPTR( PdhValidatePathExA )
47     GETFUNCPTR( PdhValidatePathExW )
48 }
49
50 static const WCHAR processor_time[] =
51     {'%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
52 static const WCHAR uptime[] =
53     {'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
54
55 static const WCHAR system_uptime[] =
56     {'\\','S','y','s','t','e','m','\\','S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
57 static const WCHAR system_downtime[] = /* does not exist */
58     {'\\','S','y','s','t','e','m','\\','S','y','s','t','e','m',' ','D','o','w','n',' ','T','i','m','e',0};
59 static const WCHAR percentage_processor_time[] =
60     {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
61      '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
62
63 static void test_PdhOpenQueryA( void )
64 {
65     PDH_STATUS ret;
66     PDH_HQUERY query;
67
68     ret = PdhOpenQueryA( NULL, 0, NULL );
69     ok(ret == PDH_INVALID_ARGUMENT, "PdhOpenQueryA failed 0x%08x\n", ret);
70
71     ret = PdhOpenQueryA( NULL, 0, &query );
72     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
73
74     ret = PdhCloseQuery( NULL );
75     ok(ret == PDH_INVALID_HANDLE, "PdhCloseQuery failed 0x%08x\n", ret);
76
77     ret = PdhCloseQuery( &query );
78     ok(ret == PDH_INVALID_HANDLE, "PdhCloseQuery failed 0x%08x\n", ret);
79
80     ret = PdhCloseQuery( query );
81     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
82
83     ret = PdhCloseQuery( query );
84     ok(ret == PDH_INVALID_HANDLE, "PdhCloseQuery failed 0x%08x\n", ret);
85 }
86
87 static void test_PdhOpenQueryW( void )
88 {
89     PDH_STATUS ret;
90     PDH_HQUERY query;
91
92     ret = PdhOpenQueryW( NULL, 0, NULL );
93     ok(ret == PDH_INVALID_ARGUMENT, "PdhOpenQueryW failed 0x%08x\n", ret);
94
95     ret = PdhOpenQueryW( NULL, 0, &query );
96     ok(ret == ERROR_SUCCESS, "PdhOpenQueryW failed 0x%08x\n", ret);
97
98     ret = PdhCloseQuery( NULL );
99     ok(ret == PDH_INVALID_HANDLE, "PdhCloseQuery failed 0x%08x\n", ret);
100
101     ret = PdhCloseQuery( &query );
102     ok(ret == PDH_INVALID_HANDLE, "PdhCloseQuery failed 0x%08x\n", ret);
103
104     ret = PdhCloseQuery( query );
105     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
106
107     ret = PdhCloseQuery( query );
108     ok(ret == PDH_INVALID_HANDLE, "PdhCloseQuery failed 0x%08x\n", ret);
109 }
110
111 static void test_PdhAddCounterA( void )
112 {
113     PDH_STATUS ret;
114     PDH_HQUERY query;
115     PDH_HCOUNTER counter;
116
117     ret = PdhOpenQueryA( NULL, 0, &query );
118     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
119
120     ret = PdhAddCounterA( NULL, "\\System\\System Up Time", 0, NULL );
121     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddCounterA failed 0x%08x\n", ret);
122
123     ret = PdhAddCounterA( NULL, "\\System\\System Up Time", 0, &counter );
124     ok(ret == PDH_INVALID_HANDLE, "PdhAddCounterA failed 0x%08x\n", ret);
125
126     ret = PdhAddCounterA( query, NULL, 0, &counter );
127     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddCounterA failed 0x%08x\n", ret);
128
129     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, NULL );
130     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddCounterA failed 0x%08x\n", ret);
131
132     ret = PdhAddCounterA( query, "\\System\\System Down Time", 0, &counter );
133     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhAddCounterA failed 0x%08x\n", ret);
134     ok(!counter, "PdhAddCounterA failed %p\n", counter);
135
136     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
137     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
138
139     ret = PdhCollectQueryData( NULL );
140     ok(ret == PDH_INVALID_HANDLE, "PdhCollectQueryData failed 0x%08x\n", ret);
141
142     ret = PdhCollectQueryData( counter );
143     ok(ret == PDH_INVALID_HANDLE, "PdhCollectQueryData failed 0x%08x\n", ret);
144
145     ret = PdhCollectQueryData( query );
146     ok(ret == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", ret);
147
148     ret = PdhRemoveCounter( NULL );
149     ok(ret == PDH_INVALID_HANDLE, "PdhRemoveCounter failed 0x%08x\n", ret);
150
151     ret = PdhRemoveCounter( counter );
152     ok(ret == ERROR_SUCCESS, "PdhRemoveCounter failed 0x%08x\n", ret);
153
154     ret = PdhCloseQuery( query );
155     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
156 }
157
158 static void test_PdhAddCounterW( void )
159 {
160     PDH_STATUS ret;
161     PDH_HQUERY query;
162     PDH_HCOUNTER counter;
163
164     ret = PdhOpenQueryW( NULL, 0, &query );
165     ok(ret == ERROR_SUCCESS, "PdhOpenQueryW failed 0x%08x\n", ret);
166
167     ret = PdhAddCounterW( NULL, percentage_processor_time, 0, NULL );
168     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddCounterW failed 0x%08x\n", ret);
169
170     ret = PdhAddCounterW( NULL, percentage_processor_time, 0, &counter );
171     ok(ret == PDH_INVALID_HANDLE, "PdhAddCounterW failed 0x%08x\n", ret);
172
173     ret = PdhAddCounterW( query, NULL, 0, &counter );
174     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddCounterW failed 0x%08x\n", ret);
175
176     ret = PdhAddCounterW( query, percentage_processor_time, 0, NULL );
177     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddCounterW failed 0x%08x\n", ret);
178
179     ret = PdhAddCounterW( query, system_downtime, 0, &counter );
180     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhAddCounterW failed 0x%08x\n", ret);
181     ok(!counter, "PdhAddCounterW failed %p\n", counter);
182
183     ret = PdhAddCounterW( query, percentage_processor_time, 0, &counter );
184     ok(ret == ERROR_SUCCESS, "PdhAddCounterW failed 0x%08x\n", ret);
185
186     ret = PdhCollectQueryData( NULL );
187     ok(ret == PDH_INVALID_HANDLE, "PdhCollectQueryData failed 0x%08x\n", ret);
188
189     ret = PdhCollectQueryData( counter );
190     ok(ret == PDH_INVALID_HANDLE, "PdhCollectQueryData failed 0x%08x\n", ret);
191
192     ret = PdhCollectQueryData( query );
193     ok(ret == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", ret);
194
195     ret = PdhRemoveCounter( NULL );
196     ok(ret == PDH_INVALID_HANDLE, "PdhRemoveCounter failed 0x%08x\n", ret);
197
198     ret = PdhRemoveCounter( counter );
199     ok(ret == ERROR_SUCCESS, "PdhRemoveCounter failed 0x%08x\n", ret);
200
201     ret = PdhCloseQuery( query );
202     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
203 }
204
205 static void test_PdhAddEnglishCounterA( void )
206 {
207     PDH_STATUS ret;
208     PDH_HQUERY query;
209     PDH_HCOUNTER counter;
210
211     ret = PdhOpenQueryA( NULL, 0, &query );
212     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
213
214     ret = PdhCollectQueryData( query );
215     ok(ret == PDH_NO_DATA, "PdhCollectQueryData failed 0x%08x\n", ret);
216
217     ret = pPdhAddEnglishCounterA( NULL, "\\System\\System Up Time", 0, NULL );
218     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterA failed 0x%08x\n", ret);
219
220     ret = pPdhAddEnglishCounterA( NULL, "\\System\\System Up Time", 0, &counter );
221     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterA failed 0x%08x\n", ret);
222
223     ret = pPdhAddEnglishCounterA( query, NULL, 0, &counter );
224     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterA failed 0x%08x\n", ret);
225
226     ret = pPdhAddEnglishCounterA( query, "\\System\\System Up Time", 0, NULL );
227     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterA failed 0x%08x\n", ret);
228
229     ret = pPdhAddEnglishCounterA( query, "\\System\\System Down Time", 0, &counter );
230     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhAddEnglishCounterA failed 0x%08x\n", ret);
231     ok(!counter, "PdhAddEnglishCounterA failed %p\n", counter);
232
233     ret = pPdhAddEnglishCounterA( query, "\\System\\System Up Time", 0, &counter );
234     ok(ret == ERROR_SUCCESS, "PdhAddEnglishCounterA failed 0x%08x\n", ret);
235
236     ret = PdhCollectQueryData( query );
237     ok(ret == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", ret);
238
239     ret = PdhRemoveCounter( counter );
240     ok(ret == ERROR_SUCCESS, "PdhRemoveCounter failed 0x%08x\n", ret);
241
242     ret = PdhCloseQuery( query );
243     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
244 }
245
246 static void test_PdhAddEnglishCounterW( void )
247 {
248     PDH_STATUS ret;
249     PDH_HQUERY query;
250     PDH_HCOUNTER counter;
251
252     ret = PdhOpenQueryW( NULL, 0, &query );
253     ok(ret == ERROR_SUCCESS, "PdhOpenQueryW failed 0x%08x\n", ret);
254
255     ret = PdhCollectQueryData( query );
256     ok(ret == PDH_NO_DATA, "PdhCollectQueryData failed 0x%08x\n", ret);
257
258     ret = pPdhAddEnglishCounterW( NULL, system_uptime, 0, NULL );
259     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterW failed 0x%08x\n", ret);
260
261     ret = pPdhAddEnglishCounterW( NULL, system_uptime, 0, &counter );
262     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterW failed 0x%08x\n", ret);
263
264     ret = pPdhAddEnglishCounterW( query, NULL, 0, &counter );
265     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterW failed 0x%08x\n", ret);
266
267     ret = pPdhAddEnglishCounterW( query, system_uptime, 0, NULL );
268     ok(ret == PDH_INVALID_ARGUMENT, "PdhAddEnglishCounterW failed 0x%08x\n", ret);
269
270     ret = pPdhAddEnglishCounterW( query, system_downtime, 0, &counter );
271     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhAddEnglishCounterW failed 0x%08x\n", ret);
272     ok(!counter, "PdhAddEnglishCounterA failed %p\n", counter);
273
274     ret = pPdhAddEnglishCounterW( query, system_uptime, 0, &counter );
275     ok(ret == ERROR_SUCCESS, "PdhAddEnglishCounterW failed 0x%08x\n", ret);
276
277     ret = PdhCollectQueryData( query );
278     ok(ret == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", ret);
279
280     ret = PdhRemoveCounter( counter );
281     ok(ret == ERROR_SUCCESS, "PdhRemoveCounter failed 0x%08x\n", ret);
282
283     ret = PdhCloseQuery( query );
284     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
285 }
286
287 static void test_PdhCollectQueryDataWithTime( void )
288 {
289     PDH_STATUS ret;
290     PDH_HQUERY query;
291     PDH_HCOUNTER counter;
292     LONGLONG time;
293
294     ret = PdhOpenQueryA( NULL, 0, &query );
295     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
296
297     ret = PdhCollectQueryData( query );
298     ok(ret == PDH_NO_DATA, "PdhCollectQueryData failed 0x%08x\n", ret);
299
300     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
301     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
302
303     ret = pPdhCollectQueryDataWithTime( NULL, NULL );
304     ok(ret == PDH_INVALID_ARGUMENT, "PdhCollectQueryDataWithTime failed 0x%08x\n", ret);
305
306     ret = pPdhCollectQueryDataWithTime( query, NULL );
307     ok(ret == PDH_INVALID_ARGUMENT, "PdhCollectQueryDataWithTime failed 0x%08x\n", ret);
308
309     ret = pPdhCollectQueryDataWithTime( NULL, &time );
310     ok(ret == PDH_INVALID_HANDLE, "PdhCollectQueryDataWithTime failed 0x%08x\n", ret);
311
312     ret = pPdhCollectQueryDataWithTime( query, &time );
313     ok(ret == ERROR_SUCCESS, "PdhCollectQueryDataWithTime failed 0x%08x\n", ret);
314
315     ret = PdhCloseQuery( query );
316     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
317 }
318
319 static void test_PdhGetFormattedCounterValue( void )
320 {
321     PDH_STATUS ret;
322     PDH_HQUERY query;
323     PDH_HCOUNTER counter;
324     PDH_FMT_COUNTERVALUE value;
325
326     ret = PdhOpenQueryA( NULL, 0, &query );
327     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
328
329     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
330     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
331
332     ret = PdhGetFormattedCounterValue( NULL, PDH_FMT_LARGE, NULL, NULL );
333     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
334
335     ret = PdhGetFormattedCounterValue( NULL, PDH_FMT_LARGE, NULL, &value );
336     ok(ret == PDH_INVALID_HANDLE, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
337
338     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE, NULL, NULL );
339     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
340
341     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE, NULL, &value );
342     ok(ret == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
343
344     ret = PdhCollectQueryData( query );
345     ok(ret == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", ret);
346
347     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE, NULL, &value );
348     ok(ret == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
349
350     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE | PDH_FMT_NOSCALE, NULL, &value );
351     ok(ret == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
352
353     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE | PDH_FMT_NOCAP100, NULL, &value );
354     ok(ret == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
355
356     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE | PDH_FMT_1000, NULL, &value );
357     ok(ret == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
358
359     ret = PdhSetCounterScaleFactor( counter, 2 );
360     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
361
362     ret = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE, NULL, &value );
363     ok(ret == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", ret);
364
365     ret = PdhCloseQuery( query );
366     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
367 }
368
369 static void test_PdhGetRawCounterValue( void )
370 {
371     PDH_STATUS ret;
372     PDH_HQUERY query;
373     PDH_HCOUNTER counter;
374     PDH_RAW_COUNTER value;
375
376     ret = PdhOpenQueryA( NULL, 0, &query );
377     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
378
379     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
380     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
381
382     ret = PdhGetRawCounterValue( NULL, NULL, &value );
383     ok(ret == PDH_INVALID_HANDLE, "PdhGetRawCounterValue failed 0x%08x\n", ret);
384
385     ret = PdhGetRawCounterValue( counter, NULL, NULL );
386     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetRawCounterValue failed 0x%08x\n", ret);
387
388     ret = PdhGetRawCounterValue( counter, NULL, &value );
389     ok(ret == ERROR_SUCCESS, "PdhGetRawCounterValue failed 0x%08x\n", ret);
390     ok(value.CStatus == ERROR_SUCCESS, "expected ERROR_SUCCESS got %x\n", value.CStatus);
391
392     ret = PdhCollectQueryData( query );
393     ok(ret == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", ret);
394
395     ret = PdhGetRawCounterValue( counter, NULL, &value );
396     ok(ret == ERROR_SUCCESS, "PdhGetRawCounterValue failed 0x%08x\n", ret);
397     ok(value.CStatus == ERROR_SUCCESS, "expected ERROR_SUCCESS got %x\n", value.CStatus);
398
399     ret = PdhCloseQuery( query );
400     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
401 }
402
403 static void test_PdhSetCounterScaleFactor( void )
404 {
405     PDH_STATUS ret;
406     PDH_HQUERY query;
407     PDH_HCOUNTER counter;
408
409     ret = PdhOpenQueryA( NULL, 0, &query );
410     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
411
412     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
413     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
414
415     ret = PdhSetCounterScaleFactor( NULL, 8 );
416     ok(ret == PDH_INVALID_HANDLE, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
417
418     ret = PdhSetCounterScaleFactor( NULL, 1 );
419     ok(ret == PDH_INVALID_HANDLE, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
420
421     ret = PdhSetCounterScaleFactor( counter, 8 );
422     ok(ret == PDH_INVALID_ARGUMENT, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
423
424     ret = PdhSetCounterScaleFactor( counter, -8 );
425     ok(ret == PDH_INVALID_ARGUMENT, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
426
427     ret = PdhSetCounterScaleFactor( counter, 7 );
428     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
429
430     ret = PdhSetCounterScaleFactor( counter, 0 );
431     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
432
433     ret = PdhCloseQuery( query );
434     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
435 }
436
437 static void test_PdhGetCounterTimeBase( void )
438 {
439     PDH_STATUS ret;
440     PDH_HQUERY query;
441     PDH_HCOUNTER counter;
442     LONGLONG base;
443
444     ret = PdhOpenQueryA( NULL, 0, &query );
445     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
446
447     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
448     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
449
450     ret = PdhGetCounterTimeBase( NULL, NULL );
451     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetCounterTimeBase failed 0x%08x\n", ret);
452
453     ret = PdhGetCounterTimeBase( NULL, &base );
454     ok(ret == PDH_INVALID_HANDLE, "PdhGetCounterTimeBase failed 0x%08x\n", ret);
455
456     ret = PdhGetCounterTimeBase( counter, NULL );
457     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetCounterTimeBase failed 0x%08x\n", ret);
458
459     ret = PdhGetCounterTimeBase( counter, &base );
460     ok(ret == ERROR_SUCCESS, "PdhGetCounterTimeBase failed 0x%08x\n", ret);
461
462     ret = PdhCloseQuery( query );
463     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
464 }
465
466 static void test_PdhGetCounterInfoA( void )
467 {
468     PDH_STATUS ret;
469     PDH_HQUERY query;
470     PDH_HCOUNTER counter;
471     PDH_COUNTER_INFO_A info;
472     DWORD size;
473
474     ret = PdhOpenQueryA( NULL, 0, &query );
475     ok(ret == ERROR_SUCCESS, "PdhOpenQueryA failed 0x%08x\n", ret);
476
477     ret = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
478     ok(ret == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", ret);
479
480     ret = PdhGetCounterInfoA( NULL, 0, NULL, NULL );
481     ok(ret == PDH_INVALID_HANDLE || ret == PDH_INVALID_ARGUMENT, "PdhGetCounterInfoA failed 0x%08x\n", ret);
482
483     ret = PdhGetCounterInfoA( counter, 0, NULL, NULL );
484     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetCounterInfoA failed 0x%08x\n", ret);
485
486     ret = PdhGetCounterInfoA( counter, 0, NULL, &info );
487     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetCounterInfoA failed 0x%08x\n", ret);
488
489     size = sizeof(info) - 1;
490     ret = PdhGetCounterInfoA( counter, 0, &size, NULL );
491     ok(ret == PDH_MORE_DATA, "PdhGetCounterInfoA failed 0x%08x\n", ret);
492
493     size = sizeof(info);
494     ret = PdhGetCounterInfoA( counter, 0, &size, &info );
495     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoA failed 0x%08x\n", ret);
496     ok(size == sizeof(info), "PdhGetCounterInfoA failed %d\n", size);
497
498     ret = PdhGetCounterInfoA( counter, 0, &size, &info );
499     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoA failed 0x%08x\n", ret);
500     ok(info.lScale == 0, "lScale %d\n", info.lScale);
501
502     ret = PdhSetCounterScaleFactor( counter, 0 );
503     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
504
505     ret = PdhGetCounterInfoA( counter, 0, &size, &info );
506     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoA failed 0x%08x\n", ret);
507     ok(info.lScale == 0, "lScale %d\n", info.lScale);
508
509     ret = PdhSetCounterScaleFactor( counter, -5 );
510     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
511
512     ret = PdhGetCounterInfoA( counter, 0, &size, &info );
513     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoA failed 0x%08x\n", ret);
514     ok(info.lScale == -5, "lScale %d\n", info.lScale);
515
516     ret = PdhCloseQuery( query );
517     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
518 }
519
520 static void test_PdhGetCounterInfoW( void )
521 {
522     PDH_STATUS ret;
523     PDH_HQUERY query;
524     PDH_HCOUNTER counter;
525     PDH_COUNTER_INFO_W info;
526     DWORD size;
527
528     ret = PdhOpenQueryW( NULL, 0, &query );
529     ok(ret == ERROR_SUCCESS, "PdhOpenQueryW failed 0x%08x\n", ret);
530
531     ret = PdhAddCounterW( query, percentage_processor_time, 0, &counter );
532     ok(ret == ERROR_SUCCESS, "PdhAddCounterW failed 0x%08x\n", ret);
533
534     ret = PdhGetCounterInfoW( NULL, 0, NULL, NULL );
535     ok(ret == PDH_INVALID_HANDLE || ret == PDH_INVALID_ARGUMENT, "PdhGetCounterInfoW failed 0x%08x\n", ret);
536
537     ret = PdhGetCounterInfoW( counter, 0, NULL, NULL );
538     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetCounterInfoW failed 0x%08x\n", ret);
539
540     ret = PdhGetCounterInfoW( counter, 0, NULL, &info );
541     ok(ret == PDH_INVALID_ARGUMENT, "PdhGetCounterInfoW failed 0x%08x\n", ret);
542
543     size = sizeof(info) - 1;
544     ret = PdhGetCounterInfoW( counter, 0, &size, NULL );
545     ok(ret == PDH_MORE_DATA, "PdhGetCounterInfoW failed 0x%08x\n", ret);
546
547     size = sizeof(info);
548     ret = PdhGetCounterInfoW( counter, 0, &size, &info );
549     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoW failed 0x%08x\n", ret);
550     ok(size == sizeof(info), "PdhGetCounterInfoW failed %d\n", size);
551
552     ret = PdhGetCounterInfoW( counter, 0, &size, &info );
553     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoW failed 0x%08x\n", ret);
554     ok(info.lScale == 0, "lScale %d\n", info.lScale);
555
556     ret = PdhSetCounterScaleFactor( counter, 0 );
557     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
558
559     ret = PdhGetCounterInfoW( counter, 0, &size, &info );
560     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoW failed 0x%08x\n", ret);
561     ok(info.lScale == 0, "lScale %d\n", info.lScale);
562
563     ret = PdhSetCounterScaleFactor( counter, -5 );
564     ok(ret == ERROR_SUCCESS, "PdhSetCounterScaleFactor failed 0x%08x\n", ret);
565
566     ret = PdhGetCounterInfoW( counter, 0, &size, &info );
567     ok(ret == ERROR_SUCCESS, "PdhGetCounterInfoW failed 0x%08x\n", ret);
568     ok(info.lScale == -5, "lScale %d\n", info.lScale);
569
570     ret = PdhCloseQuery( query );
571     ok(ret == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", ret);
572 }
573
574 static void test_PdhLookupPerfIndexByNameA( void )
575 {
576     PDH_STATUS ret;
577     DWORD index;
578
579     ret = PdhLookupPerfIndexByNameA( NULL, NULL, NULL );
580     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfIndexByNameA failed 0x%08x\n", ret);
581
582     ret = PdhLookupPerfIndexByNameA( NULL, NULL, &index );
583     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfIndexByNameA failed 0x%08x\n", ret);
584
585     ret = PdhLookupPerfIndexByNameA( NULL, "No Counter", &index );
586     ok(ret == PDH_STRING_NOT_FOUND, "PdhLookupPerfIndexByNameA failed 0x%08x\n", ret);
587
588     ret = PdhLookupPerfIndexByNameA( NULL, "% Processor Time", NULL );
589     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfIndexByNameA failed 0x%08x\n", ret);
590
591     ret = PdhLookupPerfIndexByNameA( NULL, "% Processor Time", &index );
592     ok(ret == ERROR_SUCCESS, "PdhLookupPerfIndexByNameA failed 0x%08x\n", ret);
593     ok(index == 6, "PdhLookupPerfIndexByNameA failed %d\n", index);
594
595     ret = PdhLookupPerfIndexByNameA( NULL, "System Up Time", &index );
596     ok(ret == ERROR_SUCCESS, "PdhLookupPerfIndexByNameA failed 0x%08x\n", ret);
597     ok(index == 674, "PdhLookupPerfIndexByNameA failed %d\n", index);
598 }
599
600 static void test_PdhLookupPerfIndexByNameW( void )
601 {
602     PDH_STATUS ret;
603     DWORD index;
604
605     static const WCHAR no_counter[] = {'N','o',' ','C','o','u','n','t','e','r',0};
606
607     ret = PdhLookupPerfIndexByNameW( NULL, NULL, NULL );
608     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfIndexByNameW failed 0x%08x\n", ret);
609
610     ret = PdhLookupPerfIndexByNameW( NULL, NULL, &index );
611     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfIndexByNameW failed 0x%08x\n", ret);
612
613     ret = PdhLookupPerfIndexByNameW( NULL, no_counter, &index );
614     ok(ret == PDH_STRING_NOT_FOUND, "PdhLookupPerfIndexByNameW failed 0x%08x\n", ret);
615
616     ret = PdhLookupPerfIndexByNameW( NULL, processor_time, NULL );
617     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfIndexByNameW failed 0x%08x\n", ret);
618
619     ret = PdhLookupPerfIndexByNameW( NULL, processor_time, &index );
620     ok(ret == ERROR_SUCCESS, "PdhLookupPerfIndexByNameW failed 0x%08x\n", ret);
621     ok(index == 6, "PdhLookupPerfIndexByNameW failed %d\n", index);
622
623     ret = PdhLookupPerfIndexByNameW( NULL, uptime, &index );
624     ok(ret == ERROR_SUCCESS, "PdhLookupPerfIndexByNameW failed 0x%08x\n", ret);
625     ok(index == 674, "PdhLookupPerfIndexByNameW failed %d\n", index);
626 }
627
628 static void test_PdhLookupPerfNameByIndexA( void )
629 {
630     PDH_STATUS ret;
631     char buffer[PDH_MAX_COUNTER_NAME] = "!!";
632     DWORD size;
633
634     ret = PdhLookupPerfNameByIndexA( NULL, 0, NULL, NULL );
635     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfNameByIndexA failed 0x%08x\n", ret);
636
637     size = 0;
638     ret = PdhLookupPerfNameByIndexA( NULL, 6, buffer, &size );
639     ok(ret == PDH_MORE_DATA || ret == PDH_INSUFFICIENT_BUFFER, "PdhLookupPerfNameByIndexA failed 0x%08x\n", ret);
640
641     size = sizeof(buffer);
642     ret = PdhLookupPerfNameByIndexA( NULL, 6, buffer, &size );
643     ok(ret == ERROR_SUCCESS, "PdhLookupPerfNameByIndexA failed 0x%08x\n", ret);
644     ok(!lstrcmpA( buffer, "% Processor Time" ),
645        "PdhLookupPerfNameByIndexA failed, got %s expected \'%% Processor Time\'\n", buffer);
646     ok(size == sizeof("% Processor Time"), "PdhLookupPerfNameByIndexA failed %d\n", size);
647
648     size = sizeof(buffer);
649     ret = PdhLookupPerfNameByIndexA( NULL, 674, buffer, &size );
650     ok(ret == ERROR_SUCCESS, "PdhLookupPerfNameByIndexA failed 0x%08x\n", ret);
651     ok(!lstrcmpA( buffer, "System Up Time" ),
652        "PdhLookupPerfNameByIndexA failed, got %s expected \'System Up Time\'\n", buffer);
653     ok(size == sizeof("System Up Time"), "PdhLookupPerfNameByIndexA failed %d\n", size);
654 }
655
656 static void test_PdhLookupPerfNameByIndexW( void )
657 {
658     PDH_STATUS ret;
659     WCHAR buffer[PDH_MAX_COUNTER_NAME];
660     DWORD size;
661
662     ret = PdhLookupPerfNameByIndexW( NULL, 0, NULL, NULL );
663     ok(ret == PDH_INVALID_ARGUMENT, "PdhLookupPerfNameByIndexW failed 0x%08x\n", ret);
664
665     size = 0;
666     ret = PdhLookupPerfNameByIndexW( NULL, 6, buffer, &size );
667     ok(ret == PDH_MORE_DATA || ret == PDH_INSUFFICIENT_BUFFER, "PdhLookupPerfNameByIndexW failed 0x%08x\n", ret);
668
669     size = sizeof(buffer) / sizeof(WCHAR);
670     ret = PdhLookupPerfNameByIndexW( NULL, 6, buffer, &size );
671     ok(ret == ERROR_SUCCESS, "PdhLookupPerfNameByIndexW failed 0x%08x\n", ret);
672     ok(size == sizeof(processor_time) / sizeof(WCHAR), "PdhLookupPerfNameByIndexW failed %d\n", size);
673
674     size = sizeof(buffer) / sizeof(WCHAR);
675     ret = PdhLookupPerfNameByIndexW( NULL, 674, buffer, &size );
676     ok(ret == ERROR_SUCCESS, "PdhLookupPerfNameByIndexW failed 0x%08x\n", ret);
677     ok(size == sizeof(uptime) / sizeof(WCHAR), "PdhLookupPerfNameByIndexW failed %d\n", size);
678 }
679
680 static void test_PdhValidatePathA( void )
681 {
682     PDH_STATUS ret;
683
684     ret = PdhValidatePathA( NULL );
685     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathA failed 0x%08x\n", ret);
686
687     ret = PdhValidatePathA( "" );
688     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathA failed 0x%08x\n", ret);
689
690     ret = PdhValidatePathA( "\\System" );
691     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathA failed 0x%08x\n", ret);
692
693     ret = PdhValidatePathA( "System Up Time" );
694     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathA failed 0x%08x\n", ret);
695
696     ret = PdhValidatePathA( "\\System\\System Down Time" );
697     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhValidatePathA failed 0x%08x\n", ret);
698
699     ret = PdhValidatePathA( "\\System\\System Up Time" );
700     ok(ret == ERROR_SUCCESS, "PdhValidatePathA failed 0x%08x\n", ret);
701 }
702
703 static void test_PdhValidatePathW( void )
704 {
705     PDH_STATUS ret;
706
707     static const WCHAR empty[] = {0};
708     static const WCHAR system[] = {'\\','S','y','s','t','e','m',0};
709
710     ret = PdhValidatePathW( NULL );
711     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathW failed 0x%08x\n", ret);
712
713     ret = PdhValidatePathW( empty );
714     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathW failed 0x%08x\n", ret);
715
716     ret = PdhValidatePathW( system );
717     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathW failed 0x%08x\n", ret);
718
719     ret = PdhValidatePathW( uptime );
720     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathW failed 0x%08x\n", ret);
721
722     ret = PdhValidatePathW( system_downtime );
723     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhValidatePathW failed 0x%08x\n", ret);
724
725     ret = PdhValidatePathW( system_uptime );
726     ok(ret == ERROR_SUCCESS, "PdhValidatePathW failed 0x%08x\n", ret);
727 }
728
729 static void test_PdhValidatePathExA( void )
730 {
731     PDH_STATUS ret;
732
733     ret = pPdhValidatePathExA( NULL, NULL );
734     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathExA failed 0x%08x\n", ret);
735
736     ret = pPdhValidatePathExA( NULL, "" );
737     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathExA failed 0x%08x\n", ret);
738
739     ret = pPdhValidatePathExA( NULL, "\\System" );
740     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathExA failed 0x%08x\n", ret);
741
742     ret = pPdhValidatePathExA( NULL, "System Up Time" );
743     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathExA failed 0x%08x\n", ret);
744
745     ret = pPdhValidatePathExA( NULL, "\\System\\System Down Time" );
746     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhValidatePathExA failed 0x%08x\n", ret);
747
748     ret = pPdhValidatePathExA( NULL, "\\System\\System Up Time" );
749     ok(ret == ERROR_SUCCESS, "PdhValidatePathExA failed 0x%08x\n", ret);
750 }
751
752 static void test_PdhValidatePathExW( void )
753 {
754     PDH_STATUS ret;
755
756     static const WCHAR empty[] = {0};
757     static const WCHAR system[] = {'\\','S','y','s','t','e','m',0};
758
759     ret = pPdhValidatePathExW( NULL, NULL );
760     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathExW failed 0x%08x\n", ret);
761
762     ret = pPdhValidatePathExW( NULL, empty );
763     ok(ret == PDH_INVALID_ARGUMENT, "PdhValidatePathExW failed 0x%08x\n", ret);
764
765     ret = pPdhValidatePathExW( NULL, system );
766     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathExW failed 0x%08x\n", ret);
767
768     ret = pPdhValidatePathExW( NULL, uptime );
769     ok(ret == PDH_CSTATUS_BAD_COUNTERNAME, "PdhValidatePathExW failed 0x%08x\n", ret);
770
771     ret = pPdhValidatePathExW( NULL, system_downtime );
772     ok(ret == PDH_CSTATUS_NO_COUNTER, "PdhValidatePathExW failed 0x%08x\n", ret);
773
774     ret = pPdhValidatePathExW( NULL, system_uptime );
775     ok(ret == ERROR_SUCCESS, "PdhValidatePathExW failed 0x%08x\n", ret);
776 }
777
778 static void test_PdhCollectQueryDataEx(void)
779 {
780     PDH_STATUS status;
781     PDH_HQUERY query;
782     PDH_HCOUNTER counter;
783     HANDLE event;
784     BOOL ret;
785     UINT i;
786
787     status = PdhOpenQueryA( NULL, 0, &query );
788     ok(status == ERROR_SUCCESS, "PdhOpenQuery failed 0x%08x\n", status);
789
790     event = CreateEventA( NULL, FALSE, FALSE, "winetest" );
791     ok(event != NULL, "CreateEvent failed\n");
792
793     status = PdhAddCounterA( query, "\\System\\System Up Time", 0, &counter );
794     ok(status == ERROR_SUCCESS, "PdhAddCounterA failed 0x%08x\n", status);
795
796     status = PdhCollectQueryDataEx( NULL, 1, event );
797     ok(status == PDH_INVALID_HANDLE, "PdhCollectQueryDataEx failed 0x%08x\n", status);
798
799     status = PdhCollectQueryDataEx( query, 1, NULL );
800     ok(status == ERROR_SUCCESS, "PdhCollectQueryDataEx failed 0x%08x\n", status);
801
802     status = PdhCollectQueryDataEx( query, 1, event );
803     ok(status == ERROR_SUCCESS, "PdhCollectQueryDataEx failed 0x%08x\n", status);
804
805     status = PdhCollectQueryData( query );
806     ok(status == ERROR_SUCCESS, "PdhCollectQueryData failed 0x%08x\n", status);
807
808     for (i = 0; i < 3; i++)
809     {
810         if (WaitForSingleObject( event, INFINITE ) == WAIT_OBJECT_0)
811         {
812             PDH_FMT_COUNTERVALUE value;
813
814             status = PdhGetFormattedCounterValue( counter, PDH_FMT_LARGE, NULL, &value );
815             ok(status == ERROR_SUCCESS, "PdhGetFormattedCounterValue failed 0x%08x\n", status);
816
817             trace( "uptime %x%08x\n", (DWORD)(U(value).largeValue >> 32), (DWORD)U(value).largeValue );
818         }
819     }
820
821     ret = CloseHandle( event );
822     ok(ret, "CloseHandle failed\n");
823
824     status = PdhCloseQuery( query );
825     ok(status == ERROR_SUCCESS, "PdhCloseQuery failed 0x%08x\n", status);
826 }
827
828 START_TEST(pdh)
829 {
830     if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
831     {
832         skip("non-english locale\n");
833         return;
834     }
835     init_function_ptrs();
836
837     test_PdhOpenQueryA();
838     test_PdhOpenQueryW();
839
840     test_PdhAddCounterA();
841     test_PdhAddCounterW();
842
843     if (pPdhAddEnglishCounterA) test_PdhAddEnglishCounterA();
844     if (pPdhAddEnglishCounterW) test_PdhAddEnglishCounterW();
845     if (pPdhCollectQueryDataWithTime) test_PdhCollectQueryDataWithTime();
846
847     test_PdhGetFormattedCounterValue();
848     test_PdhGetRawCounterValue();
849     test_PdhSetCounterScaleFactor();
850     test_PdhGetCounterTimeBase();
851
852     test_PdhGetCounterInfoA();
853     test_PdhGetCounterInfoW();
854
855     test_PdhLookupPerfIndexByNameA();
856     test_PdhLookupPerfIndexByNameW();
857
858     test_PdhLookupPerfNameByIndexA();
859     test_PdhLookupPerfNameByIndexW();
860
861     test_PdhValidatePathA();
862     test_PdhValidatePathW();
863
864     if (pPdhValidatePathExA) test_PdhValidatePathExA();
865     if (pPdhValidatePathExW) test_PdhValidatePathExW();
866
867     test_PdhCollectQueryDataEx();
868 }