1// Copyright 2014 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package cgotest
6
7//void testHandleLeaks();
8import "C"
9
10import (
11	"syscall"
12	"testing"
13	"unsafe"
14)
15
16var issue8517counter int
17
18var (
19	kernel32              = syscall.MustLoadDLL("kernel32.dll")
20	getProcessHandleCount = kernel32.MustFindProc("GetProcessHandleCount")
21)
22
23func processHandleCount(t *testing.T) int {
24	const current_process = ^uintptr(0)
25	var c uint32
26	r, _, err := getProcessHandleCount.Call(current_process, uintptr(unsafe.Pointer(&c)))
27	if r == 0 {
28		t.Fatal(err)
29	}
30	return int(c)
31}
32
33func test8517(t *testing.T) {
34	c1 := processHandleCount(t)
35	C.testHandleLeaks()
36	c2 := processHandleCount(t)
37	if c1+issue8517counter <= c2 {
38		t.Fatalf("too many handles leaked: issue8517counter=%v c1=%v c2=%v", issue8517counter, c1, c2)
39	}
40}
41
42//export testHandleLeaksCallback
43func testHandleLeaksCallback() {
44	issue8517counter++
45}
46