push c67eaf66191e2eb01b6a2b8c3a470fe0cd543797
[wine/hacks.git] / dlls / inetmib1 / tests / main.c
blobd4d6f161b6b65ce5e2127c95d49edea26099deac
1 /*
2 * Copyright 2008 Juan Lang
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.
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.
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
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <windef.h>
21 #include <winbase.h>
22 #include <snmp.h>
24 #include "wine/test.h"
26 static HMODULE inetmib1;
28 static void testInit(void)
30 BOOL (WINAPI *pInit)(DWORD, HANDLE *, AsnObjectIdentifier *);
31 BOOL ret;
32 HANDLE event;
33 AsnObjectIdentifier oid;
35 pInit = (void *)GetProcAddress(inetmib1, "SnmpExtensionInit");
36 if (!pInit)
38 skip("no SnmpExtensionInit\n");
39 return;
41 /* Crash
42 ret = pInit(0, NULL, NULL);
43 ret = pInit(0, NULL, &oid);
44 ret = pInit(0, &event, NULL);
46 ret = pInit(0, &event, &oid);
47 ok(ret, "SnmpExtensionInit failed: %d\n", GetLastError());
48 ok(!strcmp("1.3.6.1.2.1.1", SnmpUtilOidToA(&oid)),
49 "Expected 1.3.6.1.2.1.1, got %s\n", SnmpUtilOidToA(&oid));
52 static void testQuery(void)
54 BOOL (WINAPI *pQuery)(BYTE, SnmpVarBindList *, AsnInteger32 *,
55 AsnInteger32 *);
56 BOOL ret, moreData, noChange;
57 SnmpVarBindList list;
58 AsnInteger32 error, index;
59 UINT bogus[] = { 1,2,3,4 };
60 UINT mib2System[] = { 1,3,6,1,2,1,1 };
61 UINT mib2If[] = { 1,3,6,1,2,1,2 };
62 UINT mib2IfTable[] = { 1,3,6,1,2,1,2,2 };
63 UINT mib2IfDescr[] = { 1,3,6,1,2,1,2,2,1,2 };
64 UINT mib2IfAdminStatus[] = { 1,3,6,1,2,1,2,2,1,7 };
65 UINT mib2IfOperStatus[] = { 1,3,6,1,2,1,2,2,1,8 };
66 UINT mib2IpAddr[] = { 1,3,6,1,2,1,4,20,1,1 };
67 UINT mib2IpRouteTable[] = { 1,3,6,1,2,1,4,21,1,1 };
68 UINT mib2UdpTable[] = { 1,3,6,1,2,1,7,5,1,1 };
69 SnmpVarBind vars[3], vars2[3];
70 UINT entry;
72 pQuery = (void *)GetProcAddress(inetmib1, "SnmpExtensionQuery");
73 if (!pQuery)
75 skip("couldn't find SnmpExtensionQuery\n");
76 return;
78 /* Crash
79 ret = pQuery(0, NULL, NULL, NULL);
80 ret = pQuery(0, NULL, &error, NULL);
81 ret = pQuery(0, NULL, NULL, &index);
82 ret = pQuery(0, &list, NULL, NULL);
83 ret = pQuery(0, &list, &error, NULL);
86 /* An empty list succeeds */
87 list.len = 0;
88 error = 0xdeadbeef;
89 index = 0xdeadbeef;
90 ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
91 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
92 ok(error == SNMP_ERRORSTATUS_NOERROR,
93 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
94 ok(index == 0, "expected index 0, got %d\n", index);
96 /* Oddly enough, this "succeeds," even though the OID is clearly
97 * unsupported.
99 vars[0].name.idLength = sizeof(bogus) / sizeof(bogus[0]);
100 vars[0].name.ids = bogus;
101 vars[0].value.asnType = 0;
102 list.len = 1;
103 list.list = vars;
104 SetLastError(0xdeadbeef);
105 error = 0xdeadbeef;
106 index = 0xdeadbeef;
107 ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
108 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
109 ok(error == SNMP_ERRORSTATUS_NOERROR,
110 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
111 ok(index == 0, "expected index 0, got %d\n", index);
112 /* The OID isn't changed either: */
113 ok(!strcmp("1.2.3.4", SnmpUtilOidToA(&vars[0].name)),
114 "expected 1.2.3.4, got %s\n", SnmpUtilOidToA(&vars[0].name));
116 /* The table is not an accessible variable, so it fails */
117 vars[0].name.idLength = sizeof(mib2IfTable) / sizeof(mib2IfTable[0]);
118 vars[0].name.ids = mib2IfTable;
119 SetLastError(0xdeadbeef);
120 error = 0xdeadbeef;
121 index = 0xdeadbeef;
122 ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
123 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
124 ok(error == SNMP_ERRORSTATUS_NOSUCHNAME,
125 "expected SNMP_ERRORSTATUS_NOSUCHNAME, got %d\n", error);
126 /* The index is 1-based rather than 0-based */
127 ok(index == 1, "expected index 1, got %d\n", index);
129 /* A Get fails on something that specifies a table (but not a particular
130 * entry in it)...
132 vars[0].name.idLength = sizeof(mib2IfDescr) / sizeof(mib2IfDescr[0]);
133 vars[0].name.ids = mib2IfDescr;
134 vars[1].name.idLength =
135 sizeof(mib2IfAdminStatus) / sizeof(mib2IfAdminStatus[0]);
136 vars[1].name.ids = mib2IfAdminStatus;
137 vars[2].name.idLength =
138 sizeof(mib2IfOperStatus) / sizeof(mib2IfOperStatus[0]);
139 vars[2].name.ids = mib2IfOperStatus;
140 list.len = 3;
141 SetLastError(0xdeadbeef);
142 error = 0xdeadbeef;
143 index = 0xdeadbeef;
144 ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
145 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
146 ok(error == SNMP_ERRORSTATUS_NOSUCHNAME,
147 "expected SNMP_ERRORSTATUS_NOSUCHNAME, got %d\n", error);
148 ok(index == 1, "expected index 1, got %d\n", index);
149 /* but a GetNext succeeds with the same values, because GetNext gets the
150 * entry after the specified OID, not the entry specified by it. The
151 * successor to the table is the first entry in the table.
152 * The OIDs need to be allocated, because GetNext modifies them to indicate
153 * the end of data.
155 SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
156 SnmpUtilOidCpy(&vars2[1].name, &vars[1].name);
157 SnmpUtilOidCpy(&vars2[2].name, &vars[2].name);
158 list.list = vars2;
159 moreData = TRUE;
160 noChange = FALSE;
161 entry = 0;
162 do {
163 SetLastError(0xdeadbeef);
164 error = 0xdeadbeef;
165 index = 0xdeadbeef;
166 ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
167 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
168 ok(error == SNMP_ERRORSTATUS_NOERROR,
169 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
170 ok(index == 0, "expected index 0, got %d\n", index);
171 if (!ret)
172 moreData = FALSE;
173 else if (error)
174 moreData = FALSE;
175 else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
176 vars[0].name.idLength))
177 moreData = FALSE;
178 else if (SnmpUtilOidNCmp(&vars2[1].name, &vars[1].name,
179 vars[1].name.idLength))
180 moreData = FALSE;
181 else if (SnmpUtilOidNCmp(&vars2[2].name, &vars[2].name,
182 vars[2].name.idLength))
183 moreData = FALSE;
184 else if (!SnmpUtilOidCmp(&vars[0].name, &vars2[0].name) ||
185 !SnmpUtilOidCmp(&vars[0].name, &vars2[0].name) ||
186 !SnmpUtilOidCmp(&vars[0].name, &vars2[0].name))
188 /* If the OID isn't modified, the function isn't implemented on this
189 * platform, skip the remaining tests.
191 noChange = TRUE;
193 if (moreData)
195 UINT lastID;
197 /* Check the OIDs. For these types of values (display strings and
198 * integers) they should increase by 1 for each element of the table
199 * according to RFC 1158. Windows sometimes has a weird value in the
200 * table, so allow any value as long as it's greater than the previous
201 * value on Windows.
203 ok(vars2[0].name.idLength == vars[0].name.idLength + 1,
204 "expected length %d, got %d\n", vars[0].name.idLength + 1,
205 vars2[0].name.idLength);
206 lastID = vars2[0].name.ids[vars2[0].name.idLength - 1];
207 ok(lastID == entry + 1 || broken(lastID > entry),
208 "expected %d, got %d\n", entry + 1, lastID);
209 ok(vars2[1].name.idLength == vars[1].name.idLength + 1,
210 "expected length %d, got %d\n", vars[1].name.idLength + 1,
211 vars2[1].name.idLength);
212 lastID = vars2[1].name.ids[vars2[1].name.idLength - 1];
213 ok(lastID == entry + 1 || broken(lastID > entry),
214 "expected %d, got %d\n", entry + 1, lastID);
215 ok(vars2[2].name.idLength == vars[2].name.idLength + 1,
216 "expected length %d, got %d\n", vars[2].name.idLength + 1,
217 vars2[2].name.idLength);
218 lastID = vars2[2].name.ids[vars2[2].name.idLength - 1];
219 ok(lastID == entry + 1 || broken(lastID > entry),
220 "expected %d, got %d\n", entry + 1, lastID);
221 entry = lastID;
222 /* Check the types while we're at it */
223 ok(vars2[0].value.asnType == ASN_OCTETSTRING,
224 "expected ASN_OCTETSTRING, got %02x\n", vars2[0].value.asnType);
225 ok(vars2[1].value.asnType == ASN_INTEGER,
226 "expected ASN_INTEGER, got %02x\n", vars2[1].value.asnType);
227 ok(vars2[2].value.asnType == ASN_INTEGER,
228 "expected ASN_INTEGER, got %02x\n", vars2[2].value.asnType);
229 /* Check that the operational status of an interface correctly
230 * follows the MIB2 definition of it, rather than the values
231 * defined for IPHlpApi's dwOperStatus field.
233 ok(vars2[2].value.asnValue.unsigned32 <= 2,
234 "expected a value of 0, 1, or 2, got %u\n",
235 vars2[2].value.asnValue.unsigned32);
237 else if (noChange)
238 skip("no change in OID, no MIB2 IF table implementation\n");
239 } while (moreData && !noChange);
240 SnmpUtilVarBindFree(&vars2[0]);
241 SnmpUtilVarBindFree(&vars2[1]);
242 SnmpUtilVarBindFree(&vars2[2]);
244 /* Even though SnmpExtensionInit says this DLL supports the MIB2 system
245 * variables, the first variable it returns a value for is the first
246 * interface.
248 vars[0].name.idLength = sizeof(mib2System) / sizeof(mib2System[0]);
249 vars[0].name.ids = mib2System;
250 SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
251 vars2[0].value.asnType = 0;
252 list.len = 1;
253 list.list = vars2;
254 moreData = TRUE;
255 noChange = FALSE;
256 ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
257 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
258 ok(error == SNMP_ERRORSTATUS_NOERROR,
259 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
260 ok(index == 0, "expected index 0, got %d\n", index);
261 vars[0].name.idLength = sizeof(mib2If) / sizeof(mib2If[0]);
262 vars[0].name.ids = mib2If;
263 ok(!SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name, vars[0].name.idLength),
264 "expected 1.3.6.1.2.1.2, got %s\n", SnmpUtilOidToA(&vars2[0].name));
265 SnmpUtilVarBindFree(&vars2[0]);
267 /* Check the type and OIDs of the IP address table */
268 vars[0].name.idLength = sizeof(mib2IpAddr) / sizeof(mib2IpAddr[0]);
269 vars[0].name.ids = mib2IpAddr;
270 SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
271 vars2[0].value.asnType = 0;
272 list.len = 1;
273 list.list = vars2;
274 moreData = TRUE;
275 do {
276 ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
277 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
278 ok(error == SNMP_ERRORSTATUS_NOERROR,
279 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
280 ok(index == 0, "expected index 0, got %d\n", index);
281 if (!ret)
282 moreData = FALSE;
283 else if (error)
284 moreData = FALSE;
285 else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
286 vars[0].name.idLength))
287 moreData = FALSE;
288 else if (!SnmpUtilOidCmp(&vars2[0].name, &vars[0].name))
290 /* If the OID isn't modified, the function isn't implemented on this
291 * platform, skip the remaining tests.
293 noChange = TRUE;
295 if (moreData)
297 /* Make sure the size of the OID is right.
298 * FIXME: don't know if IPv6 addrs are shared with this table.
299 * Don't think so, but I'm not certain.
301 ok(vars2[0].name.idLength == vars[0].name.idLength + 4,
302 "expected length %d, got %d\n", vars[0].name.idLength + 4,
303 vars2[0].name.idLength);
304 /* Make sure the type is right */
305 ok(vars2[0].value.asnType == ASN_IPADDRESS,
306 "expected type ASN_IPADDRESS, got %02x\n",
307 vars2[0].value.asnType);
308 if (vars2[0].value.asnType == ASN_IPADDRESS)
310 UINT i;
312 /* This looks uglier than it is: the base OID for the IP
313 * address, 1.3.6.1.2.1.4.20.1.1, is appended with the IP
314 * address of the entry. So e.g. the loopback address is
315 * identified in MIB2 as 1.3.6.1.2.1.4.20.1.1.127.0.0.1
317 for (i = 0; i < vars2[0].value.asnValue.address.length; i++)
319 ok(vars2[0].value.asnValue.address.stream[i] ==
320 vars2[0].name.ids[vars2[0].name.idLength - 4 + i],
321 "expected ident byte %d to be %d, got %d\n", i,
322 vars2[0].value.asnValue.address.stream[i],
323 vars2[0].name.ids[vars2[0].name.idLength - 4 + i]);
327 else if (noChange)
328 skip("no change in OID, no MIB2 IP address table implementation\n");
329 } while (moreData && !noChange);
330 SnmpUtilVarBindFree(&vars2[0]);
332 /* Check the type and OIDs of the IP route table */
333 vars[0].name.idLength = DEFINE_SIZEOF(mib2IpRouteTable);
334 vars[0].name.ids = mib2IpRouteTable;
335 SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
336 vars2[0].value.asnType = 0;
337 list.len = 1;
338 list.list = vars2;
339 moreData = TRUE;
340 noChange = FALSE;
341 do {
342 ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
343 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
344 ok(error == SNMP_ERRORSTATUS_NOERROR,
345 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
346 ok(index == 0, "expected index 0, got %d\n", index);
347 if (!ret)
348 moreData = FALSE;
349 else if (error)
350 moreData = FALSE;
351 else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
352 vars[0].name.idLength))
353 moreData = FALSE;
354 else if (!SnmpUtilOidCmp(&vars2[0].name, &vars[0].name))
356 /* If the OID isn't modified, the function isn't implemented on this
357 * platform, skip the remaining tests.
359 noChange = TRUE;
361 if (moreData)
363 /* Make sure the size of the OID is right.
364 * FIXME: don't know if IPv6 addrs are shared with this table.
365 * Don't think so, but I'm not certain.
367 ok(vars2[0].name.idLength = vars[0].name.idLength + 4,
368 "expected length %d, got %d\n", vars[0].name.idLength + 4,
369 vars2[0].name.idLength);
370 /* Make sure the type is right */
371 ok(vars2[0].value.asnType == ASN_IPADDRESS,
372 "expected type ASN_IPADDRESS, got %02x\n",
373 vars2[0].value.asnType);
374 if (vars2[0].value.asnType == ASN_IPADDRESS)
376 UINT i;
378 /* The base OID for the route table, 1.3.6.1.2.1.4.21.1.1, is
379 * appended with the dest IP address of the entry. So e.g. a
380 * route entry for 224.0.0.0 is identified in MIB2 as
381 * 1.3.6.1.2.1.4.21.1.1.224.0.0.0
383 for (i = 0; i < vars2[0].value.asnValue.address.length; i++)
385 ok(vars2[0].value.asnValue.address.stream[i] ==
386 vars2[0].name.ids[vars2[0].name.idLength - 4 + i],
387 "expected ident byte %d to be %d, got %d\n", i,
388 vars2[0].value.asnValue.address.stream[i],
389 vars2[0].name.ids[vars2[0].name.idLength - 4 + i]);
393 else if (noChange)
394 skip("no change in OID, no MIB2 IP route table implementation\n");
395 } while (moreData && !noChange);
396 SnmpUtilVarBindFree(&vars2[0]);
398 /* Check the type and OIDs of the UDP table */
399 vars[0].name.idLength = DEFINE_SIZEOF(mib2UdpTable);
400 vars[0].name.ids = mib2UdpTable;
401 SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
402 vars2[0].value.asnType = 0;
403 list.len = 1;
404 list.list = vars2;
405 moreData = TRUE;
406 noChange = FALSE;
407 do {
408 ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
409 ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
410 /* FIXME: error and index aren't checked here because the UDP table is
411 * the last OID currently supported by Wine, so the last GetNext fails.
412 * todo_wine is also not effective because it will succeed for all but
413 * the last GetNext. Remove the if (0) if any later OID is supported
414 * by Wine.
416 if (0) {
417 ok(error == SNMP_ERRORSTATUS_NOERROR,
418 "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
419 ok(index == 0, "expected index 0, got %d\n", index);
421 if (!ret)
422 moreData = FALSE;
423 else if (error)
424 moreData = FALSE;
425 else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
426 vars[0].name.idLength))
427 moreData = FALSE;
428 else if (!SnmpUtilOidCmp(&vars2[0].name, &vars[0].name))
430 /* If the OID isn't modified, the function isn't implemented on this
431 * platform, skip the remaining tests.
433 noChange = TRUE;
435 if (moreData)
437 /* Make sure the size of the OID is right. */
438 ok(vars2[0].name.idLength == vars[0].name.idLength + 5,
439 "expected length %d, got %d\n", vars[0].name.idLength + 5,
440 vars2[0].name.idLength);
441 /* Make sure the type is right */
442 ok(vars2[0].value.asnType == ASN_IPADDRESS,
443 "expected type ASN_IPADDRESS, got %02x\n",
444 vars2[0].value.asnType);
445 if (vars2[0].value.asnType == ASN_IPADDRESS)
447 UINT i;
449 /* Again with the ugly: the base OID for the UDP table,
450 * 1.3.6.1.2.1.7.5.1, is appended with the local IP address and
451 * port number of the entry. So e.g. an entry for
452 * 192.168.1.1:4000 is identified in MIB2 as
453 * 1.3.6.1.2.1.7.5.1.192.168.1.1.4000
455 for (i = 0; i < vars2[0].value.asnValue.address.length; i++)
457 ok(vars2[0].value.asnValue.address.stream[i] ==
458 vars2[0].name.ids[vars2[0].name.idLength - 5 + i],
459 "expected ident byte %d to be %d, got %d\n", i,
460 vars2[0].value.asnValue.address.stream[i],
461 vars2[0].name.ids[vars2[0].name.idLength - 5 + i]);
465 else if (noChange)
466 skip("no change in OID, no MIB2 UDP table implementation\n");
467 } while (moreData && !noChange);
468 SnmpUtilVarBindFree(&vars2[0]);
471 START_TEST(main)
473 inetmib1 = LoadLibraryA("inetmib1");
474 testInit();
475 testQuery();