jscript: Get rid of BSTR in date.c.
[wine] / dlls / wbemprox / tests / query.c
1 /*
2  * Copyright 2012 Hans Leidekker for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20
21 #include <stdio.h>
22 #include "windows.h"
23 #include "ocidl.h"
24 #include "initguid.h"
25 #include "objidl.h"
26 #include "wbemcli.h"
27 #include "wine/test.h"
28
29 static const WCHAR wqlW[] = {'w','q','l',0};
30
31 static HRESULT exec_query( IWbemServices *services, const WCHAR *str, IEnumWbemClassObject **result )
32 {
33     static const WCHAR captionW[] = {'C','a','p','t','i','o','n',0};
34     static const WCHAR descriptionW[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
35     HRESULT hr;
36     IWbemClassObject *obj;
37     BSTR wql = SysAllocString( wqlW ), query = SysAllocString( str );
38     LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
39     ULONG count;
40
41     hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, result );
42     if (hr == S_OK)
43     {
44         trace("%s\n", wine_dbgstr_w(str));
45         for (;;)
46         {
47             VARIANT var;
48
49             IEnumWbemClassObject_Next( *result, WBEM_INFINITE, 1, &obj, &count );
50             if (!count) break;
51
52             if (IWbemClassObject_Get( obj, captionW, 0, &var, NULL, NULL ) == WBEM_S_NO_ERROR)
53             {
54                 trace("caption: %s\n", wine_dbgstr_w(V_BSTR(&var)));
55                 VariantClear( &var );
56             }
57             if (IWbemClassObject_Get( obj, descriptionW, 0, &var, NULL, NULL ) == WBEM_S_NO_ERROR)
58             {
59                 trace("description: %s\n", wine_dbgstr_w(V_BSTR(&var)));
60                 VariantClear( &var );
61             }
62             IWbemClassObject_Release( obj );
63         }
64     }
65     SysFreeString( wql );
66     SysFreeString( query );
67     return hr;
68 }
69
70 static void test_select(void)
71 {
72     static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
73     static const WCHAR sqlW[] = {'S','Q','L',0};
74     static const WCHAR query1[] =
75         {'S','E','L','E','C','T',' ','H','O','T','F','I','X','I','D',' ','F','R','O','M',' ',
76          'W','i','n','3','2','_','Q','u','i','c','k','F','i','x','E','n','g','i','n','e','e','r','i','n','g',0};
77     static const WCHAR query2[] =
78         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','W','i','n','3','2','_','B','I','O','S',0};
79     static const WCHAR query3[] =
80         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','W','i','n','3','2','_',
81          'L','o','g','i','c','a','l','D','i','s','k',' ','W','H','E','R','E',' ',
82          '\"','N','T','F','S','\"',' ','=',' ','F','i','l','e','S','y','s','t','e','m',0};
83     static const WCHAR query4[] =
84         {'S','E','L','E','C','T',' ','a',' ','F','R','O','M',' ','b',0};
85     static const WCHAR query5[] =
86         {'S','E','L','E','C','T',' ','a',' ','F','R','O','M',' ','W','i','n','3','2','_','B','i','o','s',0};
87     static const WCHAR query6[] =
88         {'S','E','L','E','C','T',' ','D','e','s','c','r','i','p','t','i','o','n',' ','F','R','O','M',' ',
89          'W','i','n','3','2','_','B','i','o','s',0};
90     static const WCHAR query7[] =
91         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','W','i','n','3','2','_',
92          'P','r','o','c','e','s','s',' ','W','H','E','R','E',' ','C','a','p','t','i','o','n',' ',
93          'L','I','K','E',' ','\'','%','%','R','E','G','E','D','I','T','%','\'',0};
94     static const WCHAR *test[] = { query1, query2, query3, query4, query5, query6, query7 };
95     HRESULT hr;
96     IWbemLocator *locator;
97     IWbemServices *services;
98     IEnumWbemClassObject *result;
99     BSTR path = SysAllocString( cimv2W );
100     BSTR wql = SysAllocString( wqlW );
101     BSTR sql = SysAllocString( sqlW );
102     BSTR query = SysAllocString( query1 );
103     UINT i;
104
105     hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
106                            (void **)&locator );
107     if (hr != S_OK)
108     {
109         win_skip("can't create instance of WbemLocator\n");
110         return;
111     }
112     ok( hr == S_OK, "failed to create IWbemLocator interface %08x\n", hr );
113
114     hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
115     ok( hr == S_OK, "failed to get IWbemServices interface %08x\n", hr );
116
117     hr = CoSetProxyBlanket( (IUnknown *)services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
118                             RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
119     ok( hr == S_OK, "failed to set proxy blanket %08x\n", hr );
120
121     hr = IWbemServices_ExecQuery( services, NULL, NULL, 0, NULL, &result );
122     ok( hr == WBEM_E_INVALID_PARAMETER, "query failed %08x\n", hr );
123
124     hr = IWbemServices_ExecQuery( services, NULL, query, 0, NULL, &result );
125     ok( hr == WBEM_E_INVALID_PARAMETER, "query failed %08x\n", hr );
126
127     hr = IWbemServices_ExecQuery( services, wql, NULL, 0, NULL, &result );
128     ok( hr == WBEM_E_INVALID_PARAMETER, "query failed %08x\n", hr );
129
130     hr = IWbemServices_ExecQuery( services, sql, query, 0, NULL, &result );
131     ok( hr == WBEM_E_INVALID_QUERY_TYPE, "query failed %08x\n", hr );
132
133     hr = IWbemServices_ExecQuery( services, sql, NULL, 0, NULL, &result );
134     ok( hr == WBEM_E_INVALID_PARAMETER, "query failed %08x\n", hr );
135
136     for (i = 0; i < sizeof(test)/sizeof(test[0]); i++)
137     {
138         hr = exec_query( services, test[i], &result );
139         ok( hr == S_OK, "query %u failed: %08x\n", i, hr );
140         if (result) IEnumWbemClassObject_Release( result );
141     }
142
143     IWbemServices_Release( services );
144     IWbemLocator_Release( locator );
145     SysFreeString( path );
146     SysFreeString( wql );
147     SysFreeString( sql );
148     SysFreeString( query );
149 }
150
151 START_TEST(query)
152 {
153     CoInitialize( NULL );
154     CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
155                           RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
156     test_select();
157     CoUninitialize();
158 }