github.com/remoteit/go-ole@v1.2.7/idispatch_windows_test.go (about) 1 // +build windows 2 3 package ole 4 5 import "testing" 6 7 func wrapCOMExecute(t *testing.T, callback func(*testing.T)) { 8 defer func() { 9 if r := recover(); r != nil { 10 t.Error(r) 11 } 12 }() 13 14 err := CoInitialize(0) 15 if err != nil { 16 t.Fatal(err) 17 } 18 defer CoUninitialize() 19 20 callback(t) 21 } 22 23 func wrapDispatch(t *testing.T, ClassID, UnknownInterfaceID, DispatchInterfaceID *GUID, callback func(*testing.T, *IUnknown, *IDispatch)) { 24 var unknown *IUnknown 25 var dispatch *IDispatch 26 var err error 27 28 unknown, err = CreateInstance(ClassID, UnknownInterfaceID) 29 if err != nil { 30 t.Error(err) 31 return 32 } 33 defer unknown.Release() 34 35 dispatch, err = unknown.QueryInterface(DispatchInterfaceID) 36 if err != nil { 37 t.Error(err) 38 return 39 } 40 defer dispatch.Release() 41 42 callback(t, unknown, dispatch) 43 } 44 45 func wrapGoOLETestCOMServerEcho(t *testing.T, callback func(*testing.T, *IUnknown, *IDispatch)) { 46 wrapCOMExecute(t, func(t *testing.T) { 47 wrapDispatch(t, CLSID_COMEchoTestObject, IID_IUnknown, IID_ICOMEchoTestObject, callback) 48 }) 49 } 50 51 func wrapGoOLETestCOMServerScalar(t *testing.T, callback func(*testing.T, *IUnknown, *IDispatch)) { 52 wrapCOMExecute(t, func(t *testing.T) { 53 wrapDispatch(t, CLSID_COMTestScalarClass, IID_IUnknown, IID_ICOMTestTypes, callback) 54 }) 55 } 56 57 func TestIDispatch_goolecomserver_stringfield(t *testing.T) { 58 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 59 method := "StringField" 60 expected := "Test String" 61 _, err := idispatch.PutProperty(method, expected) 62 if err != nil { 63 t.Error(err) 64 return 65 } 66 variant, err := idispatch.GetProperty(method) 67 if err != nil { 68 t.Error(err) 69 return 70 } 71 defer variant.Clear() 72 actual, passed := variant.Value().(string) 73 if !passed { 74 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "string", variant.VT, variant.Val) 75 return 76 } 77 if actual != expected { 78 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 79 } 80 }) 81 } 82 83 func TestIDispatch_goolecomserver_int8field(t *testing.T) { 84 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 85 method := "Int8Field" 86 expected := int8(2) 87 _, err := idispatch.PutProperty(method, expected) 88 if err != nil { 89 t.Error(err) 90 return 91 } 92 variant, err := idispatch.GetProperty(method) 93 if err != nil { 94 t.Error(err) 95 return 96 } 97 defer variant.Clear() 98 actual, passed := variant.Value().(int8) 99 if !passed { 100 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int8", variant.VT, variant.Val) 101 return 102 } 103 if actual != expected { 104 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 105 } 106 }) 107 } 108 109 func TestIDispatch_goolecomserver_uint8field(t *testing.T) { 110 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 111 method := "UInt8Field" 112 expected := uint8(4) 113 _, err := idispatch.PutProperty(method, expected) 114 if err != nil { 115 t.Error(err) 116 return 117 } 118 variant, err := idispatch.GetProperty(method) 119 if err != nil { 120 t.Error(err) 121 return 122 } 123 defer variant.Clear() 124 actual, passed := variant.Value().(uint8) 125 if !passed { 126 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint8", variant.VT, variant.Val) 127 return 128 } 129 if actual != expected { 130 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 131 } 132 }) 133 } 134 135 func TestIDispatch_goolecomserver_int16field(t *testing.T) { 136 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 137 method := "Int16Field" 138 expected := int16(4) 139 _, err := idispatch.PutProperty(method, expected) 140 if err != nil { 141 t.Error(err) 142 return 143 } 144 variant, err := idispatch.GetProperty(method) 145 if err != nil { 146 t.Error(err) 147 return 148 } 149 defer variant.Clear() 150 actual, passed := variant.Value().(int16) 151 if !passed { 152 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int16", variant.VT, variant.Val) 153 return 154 } 155 if actual != expected { 156 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 157 } 158 }) 159 } 160 161 func TestIDispatch_goolecomserver_uint16field(t *testing.T) { 162 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 163 method := "UInt16Field" 164 expected := uint16(4) 165 _, err := idispatch.PutProperty(method, expected) 166 if err != nil { 167 t.Error(err) 168 return 169 } 170 variant, err := idispatch.GetProperty(method) 171 if err != nil { 172 t.Error(err) 173 return 174 } 175 defer variant.Clear() 176 actual, passed := variant.Value().(uint16) 177 if !passed { 178 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint16", variant.VT, variant.Val) 179 return 180 } 181 if actual != expected { 182 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 183 } 184 }) 185 } 186 187 func TestIDispatch_goolecomserver_int32field(t *testing.T) { 188 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 189 method := "Int32Field" 190 expected := int32(8) 191 _, err := idispatch.PutProperty(method, expected) 192 if err != nil { 193 t.Error(err) 194 return 195 } 196 variant, err := idispatch.GetProperty(method) 197 if err != nil { 198 t.Error(err) 199 return 200 } 201 defer variant.Clear() 202 actual, passed := variant.Value().(int32) 203 if !passed { 204 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int32", variant.VT, variant.Val) 205 return 206 } 207 if actual != expected { 208 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 209 } 210 }) 211 } 212 213 func TestIDispatch_goolecomserver_uint32field(t *testing.T) { 214 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 215 method := "UInt32Field" 216 expected := uint32(16) 217 _, err := idispatch.PutProperty(method, expected) 218 if err != nil { 219 t.Error(err) 220 return 221 } 222 variant, err := idispatch.GetProperty(method) 223 if err != nil { 224 t.Error(err) 225 return 226 } 227 defer variant.Clear() 228 actual, passed := variant.Value().(uint32) 229 if !passed { 230 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint32", variant.VT, variant.Val) 231 return 232 } 233 if actual != expected { 234 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 235 } 236 }) 237 } 238 239 func TestIDispatch_goolecomserver_int64field(t *testing.T) { 240 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 241 method := "Int64Field" 242 expected := int64(32) 243 _, err := idispatch.PutProperty(method, expected) 244 if err != nil { 245 t.Error(err) 246 return 247 } 248 variant, err := idispatch.GetProperty(method) 249 if err != nil { 250 t.Error(err) 251 return 252 } 253 defer variant.Clear() 254 actual, passed := variant.Value().(int64) 255 if !passed { 256 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int64", variant.VT, variant.Val) 257 return 258 } 259 if actual != expected { 260 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 261 } 262 }) 263 } 264 265 func TestIDispatch_goolecomserver_uint64field(t *testing.T) { 266 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 267 method := "UInt64Field" 268 expected := uint64(64) 269 _, err := idispatch.PutProperty(method, expected) 270 if err != nil { 271 t.Error(err) 272 return 273 } 274 variant, err := idispatch.GetProperty(method) 275 if err != nil { 276 t.Error(err) 277 return 278 } 279 defer variant.Clear() 280 actual, passed := variant.Value().(uint64) 281 if !passed { 282 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint64", variant.VT, variant.Val) 283 return 284 } 285 if actual != expected { 286 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 287 } 288 }) 289 } 290 291 func TestIDispatch_goolecomserver_booleanfield_true(t *testing.T) { 292 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 293 method := "BooleanField" 294 expected := true 295 _, err := idispatch.PutProperty(method, expected) 296 if err != nil { 297 t.Error(err) 298 return 299 } 300 variant, err := idispatch.GetProperty(method) 301 if err != nil { 302 t.Error(err) 303 return 304 } 305 defer variant.Clear() 306 actual, passed := variant.Value().(bool) 307 if !passed { 308 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "bool", variant.VT, variant.Val) 309 return 310 } 311 if actual != expected { 312 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 313 } 314 }) 315 } 316 317 func TestIDispatch_goolecomserver_booleanfield_false(t *testing.T) { 318 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 319 method := "BooleanField" 320 expected := false 321 _, err := idispatch.PutProperty(method, expected) 322 if err != nil { 323 t.Error(err) 324 return 325 } 326 variant, err := idispatch.GetProperty(method) 327 if err != nil { 328 t.Error(err) 329 return 330 } 331 defer variant.Clear() 332 actual, passed := variant.Value().(bool) 333 if !passed { 334 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "bool", variant.VT, variant.Val) 335 return 336 } 337 if actual != expected { 338 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 339 } 340 }) 341 } 342 343 func TestIDispatch_goolecomserver_float32field(t *testing.T) { 344 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 345 method := "Float32Field" 346 expected := float32(2.2) 347 _, err := idispatch.PutProperty(method, expected) 348 if err != nil { 349 t.Error(err) 350 } 351 variant, err := idispatch.GetProperty(method) 352 if err != nil { 353 t.Error(err) 354 return 355 } 356 defer variant.Clear() 357 actual, passed := variant.Value().(float32) 358 if !passed { 359 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "float32", variant.VT, variant.Val) 360 return 361 } 362 if actual != expected { 363 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 364 } 365 }) 366 } 367 368 func TestIDispatch_goolecomserver_float64field(t *testing.T) { 369 wrapGoOLETestCOMServerScalar(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 370 method := "Float64Field" 371 expected := float64(4.4) 372 _, err := idispatch.PutProperty(method, expected) 373 if err != nil { 374 t.Error(err) 375 } 376 variant, err := idispatch.GetProperty(method) 377 if err != nil { 378 t.Error(err) 379 return 380 } 381 defer variant.Clear() 382 actual, passed := variant.Value().(float64) 383 if !passed { 384 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "float64", variant.VT, variant.Val) 385 return 386 } 387 if actual != expected { 388 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 389 } 390 }) 391 } 392 393 func TestIDispatch_goolecomserver_echostring(t *testing.T) { 394 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 395 method := "EchoString" 396 expected := "Test String" 397 variant, err := idispatch.CallMethod(method, expected) 398 if err != nil { 399 t.Error(err) 400 return 401 } 402 defer variant.Clear() 403 actual, passed := variant.Value().(string) 404 if !passed { 405 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "string", variant.VT, variant.Val) 406 return 407 } 408 if actual != expected { 409 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 410 } 411 }) 412 } 413 414 func TestIDispatch_goolecomserver_echoboolean(t *testing.T) { 415 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 416 method := "EchoBoolean" 417 expected := true 418 variant, err := idispatch.CallMethod(method, expected) 419 if err != nil { 420 t.Error(err) 421 return 422 } 423 defer variant.Clear() 424 actual, passed := variant.Value().(bool) 425 if !passed { 426 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "bool", variant.VT, variant.Val) 427 return 428 } 429 if actual != expected { 430 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 431 } 432 }) 433 } 434 435 func TestIDispatch_goolecomserver_echoint8(t *testing.T) { 436 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 437 method := "EchoInt8" 438 expected := int8(1) 439 variant, err := idispatch.CallMethod(method, expected) 440 if err != nil { 441 t.Error(err) 442 return 443 } 444 defer variant.Clear() 445 actual, passed := variant.Value().(int8) 446 if !passed { 447 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int8", variant.VT, variant.Val) 448 return 449 } 450 if actual != expected { 451 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 452 } 453 }) 454 } 455 456 func TestIDispatch_goolecomserver_echouint8(t *testing.T) { 457 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 458 method := "EchoUInt8" 459 expected := uint8(1) 460 variant, err := idispatch.CallMethod(method, expected) 461 if err != nil { 462 t.Error(err) 463 return 464 } 465 defer variant.Clear() 466 actual, passed := variant.Value().(uint8) 467 if !passed { 468 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint8", variant.VT, variant.Val) 469 return 470 } 471 if actual != expected { 472 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 473 } 474 }) 475 } 476 477 func TestIDispatch_goolecomserver_echoint16(t *testing.T) { 478 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 479 method := "EchoInt16" 480 expected := int16(1) 481 variant, err := idispatch.CallMethod(method, expected) 482 if err != nil { 483 t.Error(err) 484 return 485 } 486 defer variant.Clear() 487 actual, passed := variant.Value().(int16) 488 if !passed { 489 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int16", variant.VT, variant.Val) 490 return 491 } 492 if actual != expected { 493 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 494 } 495 }) 496 } 497 498 func TestIDispatch_goolecomserver_echouint16(t *testing.T) { 499 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 500 method := "EchoUInt16" 501 expected := uint16(1) 502 variant, err := idispatch.CallMethod(method, expected) 503 if err != nil { 504 t.Error(err) 505 return 506 } 507 defer variant.Clear() 508 actual, passed := variant.Value().(uint16) 509 if !passed { 510 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint16", variant.VT, variant.Val) 511 return 512 } 513 if actual != expected { 514 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 515 } 516 }) 517 } 518 519 func TestIDispatch_goolecomserver_echoint32(t *testing.T) { 520 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 521 method := "EchoInt32" 522 expected := int32(2) 523 variant, err := idispatch.CallMethod(method, expected) 524 if err != nil { 525 t.Error(err) 526 return 527 } 528 defer variant.Clear() 529 actual, passed := variant.Value().(int32) 530 if !passed { 531 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int32", variant.VT, variant.Val) 532 return 533 } 534 if actual != expected { 535 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 536 } 537 }) 538 } 539 540 func TestIDispatch_goolecomserver_echouint32(t *testing.T) { 541 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 542 method := "EchoUInt32" 543 expected := uint32(4) 544 variant, err := idispatch.CallMethod(method, expected) 545 if err != nil { 546 t.Error(err) 547 return 548 } 549 defer variant.Clear() 550 actual, passed := variant.Value().(uint32) 551 if !passed { 552 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint32", variant.VT, variant.Val) 553 return 554 } 555 if actual != expected { 556 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 557 } 558 }) 559 } 560 561 func TestIDispatch_goolecomserver_echoint64(t *testing.T) { 562 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 563 method := "EchoInt64" 564 expected := int64(1) 565 variant, err := idispatch.CallMethod(method, expected) 566 if err != nil { 567 t.Error(err) 568 return 569 } 570 defer variant.Clear() 571 actual, passed := variant.Value().(int64) 572 if !passed { 573 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "int64", variant.VT, variant.Val) 574 return 575 } 576 if actual != expected { 577 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 578 } 579 }) 580 } 581 582 func TestIDispatch_goolecomserver_echouint64(t *testing.T) { 583 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 584 method := "EchoUInt64" 585 expected := uint64(1) 586 variant, err := idispatch.CallMethod(method, expected) 587 if err != nil { 588 t.Error(err) 589 return 590 } 591 defer variant.Clear() 592 actual, passed := variant.Value().(uint64) 593 if !passed { 594 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "uint64", variant.VT, variant.Val) 595 return 596 } 597 if actual != expected { 598 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 599 } 600 }) 601 } 602 603 func TestIDispatch_goolecomserver_echofloat32(t *testing.T) { 604 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 605 method := "EchoFloat32" 606 expected := float32(2.2) 607 variant, err := idispatch.CallMethod(method, expected) 608 if err != nil { 609 t.Error(err) 610 return 611 } 612 defer variant.Clear() 613 actual, passed := variant.Value().(float32) 614 if !passed { 615 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "float32", variant.VT, variant.Val) 616 return 617 } 618 if actual != expected { 619 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 620 } 621 }) 622 } 623 624 func TestIDispatch_goolecomserver_echofloat64(t *testing.T) { 625 wrapGoOLETestCOMServerEcho(t, func(t *testing.T, unknown *IUnknown, idispatch *IDispatch) { 626 method := "EchoFloat64" 627 expected := float64(2.2) 628 variant, err := idispatch.CallMethod(method, expected) 629 if err != nil { 630 t.Error(err) 631 return 632 } 633 defer variant.Clear() 634 actual, passed := variant.Value().(float64) 635 if !passed { 636 t.Errorf("%s() did not convert to %s, variant is %s with %v value", method, "float64", variant.VT, variant.Val) 637 return 638 } 639 if actual != expected { 640 t.Errorf("%s() expected %v did not match %v", method, expected, actual) 641 } 642 }) 643 }