~hristoast/wem

wem/dll/main_test.go -rw-r--r-- 7.7 KiB
e4496baaHristos N. Triantafillou Changelog for the DNS flag 14 days ago
                                                                                
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package dll_test

import (
	"os"
	"path/filepath"
	"testing"

	"git.sr.ht/~hristoast/wem/cfg"
	"git.sr.ht/~hristoast/wem/dll"
)

func TestDll(t *testing.T) {
	// Setup
	dir := t.TempDir()
	os.Setenv("HOME", dir)

	config, _ := cfg.GetConfig()
	dllFile := "wemTest.dll"
	targetVer := "1.2.3"
	targetCachePath := filepath.Join(config.CacheDir, "test"+"-"+targetVer)
	testPrefix := filepath.Join(os.Getenv("HOME"), "WinePrefix")
	targetDstDir32 := filepath.Join(testPrefix, "drive_c", "windows", "system32")
	targetDstDir64 := filepath.Join(testPrefix, "drive_c", "windows", "syswow64")
	targetDstPath32 := filepath.Join(targetDstDir32, dllFile)
	targetDstPath64 := filepath.Join(targetDstDir64, dllFile)
	targetBakPath32 := filepath.Join(targetDstDir32, dllFile+".orig-wem")
	targetBakPath64 := filepath.Join(targetDstDir64, dllFile+".orig-wem")
	targetSrcPath32 := filepath.Join(targetCachePath, "test"+"-"+targetVer, "x64", dllFile)
	targetSrcPath64 := filepath.Join(targetCachePath, "test"+"-"+targetVer, "x86", dllFile)

	// Create fake initial dirs
	err := os.MkdirAll(targetCachePath, 0755)
	if err != nil {
		t.Error(err)
	}
	err = os.MkdirAll(targetDstDir32, 0755)
	if err != nil {
		t.Error(err)
	}
	err = os.MkdirAll(targetDstDir64, 0755)
	if err != nil {
		t.Error(err)
	}

	// Create fake initial files
	for _, file := range []string{targetDstPath32, targetDstPath64} {
		f, err := os.Create(file)
		if err != nil {
			t.Error(err)
		}
		err = f.Close()
		if err != nil {
			t.Error(err)
		}
	}

	has64 := true
	wineArch := "win64"
	err = dll.EnableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
	if err != nil {
		t.Errorf("Got an error enabling: %v\n", err)
	}

	// Enabled tests
	t.Run("32-bit backup file should be a file", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath32)
		if !isFile {
			t.Error("got: file doesn't exist; want: file exists")
		}
	})

	t.Run("64-bit backup file should be a file", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath64)
		if !isFile {
			t.Error("got: file doesn't exist; want: file exists")
		}
	})

	t.Run("32-bit file should be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath32)
		if !isSym {
			t.Error("got: file isn't a symlink; want: file is a symlink")
		}
	})

	t.Run("64-bit file should be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath64)
		if !isSym {
			t.Error("got: file isn't a symlink; want: file is a symlink")
		}
	})

	t.Run("32-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
		valid := dll.ValidLink(targetDstPath32, targetSrcPath32)
		if !valid {
			t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
		}
	})

	t.Run("64-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
		valid := dll.ValidLink(targetDstPath64, targetSrcPath64)
		if !valid {
			t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
		}
	})

	err = dll.DisableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
	if err != nil {
		t.Errorf("Got an error disabling: %v\n", err)
	}

	// Disabled tests
	t.Run("32-bit backup file should not exist", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath32)
		if isFile {
			t.Error("got: the backup file exists; want: the backup file doesn't exist")
		}
	})

	t.Run("64-bit backup file should not exist", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath64)
		if isFile {
			t.Error("got: the backup file exists; want: the backup file doesn't exist")
		}
	})

	t.Run("32-bit file should not be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath32)
		if isSym {
			t.Error("got: file is a symlink; want: file isn't a symlink")
		}
	})

	t.Run("64-bit file should not be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath64)
		if isSym {
			t.Error("got: file is a symlink; want: file isn't a symlink")
		}
	})

	// Erasing the backup files shouldn't cause any issues;
	// re-enable, delete the backup files, then re-disable to confirm this.
	err = dll.EnableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
	if err != nil {
		t.Errorf("Got an error enabling: %v\n", err)
	}

	err = os.Remove(targetBakPath32)
	if err != nil {
		t.Errorf("Got an error removing the targetBakPath32: %v\n", err)
	}

	err = os.Remove(targetBakPath64)
	if err != nil {
		t.Errorf("Got an error removing the targetBakPath64: %v\n", err)
	}
	// Disabling with the backup files removed should not cause problems
	err = dll.DisableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
	if err != nil {
		t.Errorf("Got an error disabling: %v\n", err)
	}

	// Disabled tests
	t.Run("32-bit backup file should not exist", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath32)
		if isFile {
			t.Error("got: the backup file exists; want: the backup file doesn't exist")
		}
	})

	t.Run("64-bit backup file should not exist", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath64)
		if isFile {
			t.Error("got: the backup file exists; want: the backup file doesn't exist")
		}
	})

	t.Run("32-bit file should not be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath32)
		if isSym {
			t.Error("got: file is a symlink; want: file isn't a symlink")
		}
	})

	t.Run("64-bit file should not be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath64)
		if isSym {
			t.Error("got: file is a symlink; want: file isn't a symlink")
		}
	})

	// Enabling/disabling when the original files don't exist should be fine as well.
	// Delete the original files, then re-enable/re-disable to confirm this.
	err = dll.EnableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
	if err != nil {
		t.Errorf("Got an error enabling: %v\n", err)
	}

	t.Run("32-bit file should be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath32)
		if !isSym {
			t.Error("got: file isn't a symlink; want: file is a symlink")
		}
	})

	t.Run("64-bit file should be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath64)
		if !isSym {
			t.Error("got: file isn't a symlink; want: file is a symlink")
		}
	})

	t.Run("32-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
		valid := dll.ValidLink(targetDstPath32, targetSrcPath32)
		if !valid {
			t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
		}
	})

	t.Run("64-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
		valid := dll.ValidLink(targetDstPath64, targetSrcPath64)
		if !valid {
			t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
		}
	})

	err = dll.DisableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
	if err != nil {
		t.Errorf("Got an error disabling: %v\n", err)
	}

	// Disabled tests
	t.Run("32-bit backup file should not exist", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath32)
		if isFile {
			t.Error("got: the backup file exists; want: the backup file doesn't exist")
		}
	})

	t.Run("64-bit backup file should not exist", func(t *testing.T) {
		isFile := dll.IsFile(targetBakPath64)
		if isFile {
			t.Error("got: the backup file exists; want: the backup file doesn't exist")
		}
	})

	t.Run("32-bit file should not be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath32)
		if isSym {
			t.Error("got: file is a symlink; want: file isn't a symlink")
		}
	})

	t.Run("64-bit file should not be a symlink", func(t *testing.T) {
		isSym := dll.IsSymlink(targetDstPath64)
		if isSym {
			t.Error("got: file is a symlink; want: file isn't a symlink")
		}
	})

	os.RemoveAll(dir)
}