summaryrefslogtreecommitdiffhomepage
path: root/tun/wintun/registry/registry_windows_test.go
blob: c56b51bb28ac6e59ef0ad23afee95584765ca7dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package registry

import (
	"testing"
	"time"

	"golang.org/x/sys/windows/registry"
)

const keyRoot = registry.CURRENT_USER
const pathRoot = "Software\\WireGuardRegistryTest"
const path = pathRoot + "\\foobar"
const pathFake = pathRoot + "\\raboof"

func Test_WaitForKey(t *testing.T) {
	registry.DeleteKey(keyRoot, path)
	registry.DeleteKey(keyRoot, pathRoot)
	go func() {
		time.Sleep(time.Second * 1)
		key, _, err := registry.CreateKey(keyRoot, pathFake, registry.QUERY_VALUE)
		if err != nil {
			t.Errorf("Error creating registry key: %v", err)
		}
		key.Close()
		registry.DeleteKey(keyRoot, pathFake)

		key, _, err = registry.CreateKey(keyRoot, path, registry.QUERY_VALUE)
		if err != nil {
			t.Errorf("Error creating registry key: %v", err)
		}
		key.Close()
	}()
	err := WaitForKey(keyRoot, path, time.Second*2)
	if err != nil {
		t.Errorf("Error waiting for registry key: %v", err)
	}
	registry.DeleteKey(keyRoot, path)
	registry.DeleteKey(keyRoot, pathRoot)

	err = WaitForKey(keyRoot, path, time.Second*1)
	if err == nil {
		t.Error("Registry key notification expected to timeout but it succeeded.")
	}
}

func Test_GetValueWait(t *testing.T) {
	registry.DeleteKey(keyRoot, path)
	registry.DeleteKey(keyRoot, pathRoot)
	go func() {
		time.Sleep(time.Second * 1)
		key, _, err := registry.CreateKey(keyRoot, path, registry.SET_VALUE)
		if err != nil {
			t.Errorf("Error creating registry key: %v", err)
		}
		time.Sleep(time.Second * 1)
		key.SetStringValue("name1", "eulav")
		key.SetExpandStringValue("name2", "value")
		time.Sleep(time.Second * 1)
		key.SetDWordValue("name3", ^uint32(123))
		key.SetDWordValue("name4", 123)
		key.Close()
	}()

	key, err := OpenKeyWait(keyRoot, path, registry.QUERY_VALUE|registry.NOTIFY, time.Second*2)
	if err != nil {
		t.Errorf("Error waiting for registry key: %v", err)
	}

	valueStr, err := GetStringValueWait(key, "name2", time.Second*2)
	if err != nil {
		t.Errorf("Error waiting for registry value: %v", err)
	}
	if valueStr != "value" {
		t.Errorf("Wrong value read: %v", valueStr)
	}

	_, err = GetStringValueWait(key, "nonexisting", time.Second*1)
	if err == nil {
		t.Error("Registry value notification expected to timeout but it succeeded.")
	}

	valueInt, err := GetIntegerValueWait(key, "name4", time.Second*2)
	if err != nil {
		t.Errorf("Error waiting for registry value: %v", err)
	}
	if valueInt != 123 {
		t.Errorf("Wrong value read: %v", valueInt)
	}

	_, err = GetIntegerValueWait(key, "nonexisting", time.Second*1)
	if err == nil {
		t.Error("Registry value notification expected to timeout but it succeeded.")
	}

	key.Close()
	registry.DeleteKey(keyRoot, path)
	registry.DeleteKey(keyRoot, pathRoot)
}