跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

qemuimgutil_test.go

300 行 · 8.4 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package qemuimgutil
5
6import (
7 "encoding/json"
8 "testing"
9
10 "gotest.tools/v3/assert"
11)
12
13func TestParseInfo(t *testing.T) {
14 t.Run("qcow2", func(t *testing.T) {
15 // qemu-img create -f qcow2 foo.qcow2 4G
16 // (QEMU 8.0)
17 const s = `{
18 "children": [
19 {
20 "name": "file",
21 "info": {
22 "children": [
23 ],
24 "virtual-size": 197120,
25 "filename": "foo.qcow2",
26 "format": "file",
27 "actual-size": 200704,
28 "format-specific": {
29 "type": "file",
30 "data": {
31 }
32 },
33 "dirty-flag": false
34 }
35 }
36 ],
37 "virtual-size": 4294967296,
38 "filename": "foo.qcow2",
39 "cluster-size": 65536,
40 "format": "qcow2",
41 "actual-size": 200704,
42 "format-specific": {
43 "type": "qcow2",
44 "data": {
45 "compat": "1.1",
46 "compression-type": "zlib",
47 "lazy-refcounts": false,
48 "refcount-bits": 16,
49 "corrupt": false,
50 "extended-l2": false
51 }
52 },
53 "dirty-flag": false
54}`
55
56 info, err := parseInfo([]byte(s))
57 assert.NilError(t, err)
58 assert.Equal(t, 1, len(info.Children))
59 assert.Check(t, info.FormatSpecific != nil)
60 qcow2 := info.FormatSpecific.Qcow2()
61 assert.Check(t, qcow2 != nil)
62 assert.Equal(t, qcow2.Compat, "1.1")
63
64 t.Run("diff", func(t *testing.T) {
65 // qemu-img create -f qcow2 -F qcow2 -b foo.qcow2 bar.qcow2
66 // (QEMU 8.0)
67 const s = `{
68 "children": [
69 {
70 "name": "file",
71 "info": {
72 "children": [
73 ],
74 "virtual-size": 197120,
75 "filename": "bar.qcow2",
76 "format": "file",
77 "actual-size": 200704,
78 "format-specific": {
79 "type": "file",
80 "data": {
81 }
82 },
83 "dirty-flag": false
84 }
85 }
86 ],
87 "backing-filename-format": "qcow2",
88 "virtual-size": 4294967296,
89 "filename": "bar.qcow2",
90 "cluster-size": 65536,
91 "format": "qcow2",
92 "actual-size": 200704,
93 "format-specific": {
94 "type": "qcow2",
95 "data": {
96 "compat": "1.1",
97 "compression-type": "zlib",
98 "lazy-refcounts": false,
99 "refcount-bits": 16,
100 "corrupt": false,
101 "extended-l2": false
102 }
103 },
104 "full-backing-filename": "foo.qcow2",
105 "backing-filename": "foo.qcow2",
106 "dirty-flag": false
107}`
108 info, err := parseInfo([]byte(s))
109 assert.NilError(t, err)
110 assert.Equal(t, 1, len(info.Children))
111 assert.Equal(t, "foo.qcow2", info.BackingFilename)
112 assert.Equal(t, "bar.qcow2", info.Filename)
113 assert.Check(t, info.FormatSpecific != nil)
114 qcow2 := info.FormatSpecific.Qcow2()
115 assert.Check(t, qcow2 != nil)
116 assert.Equal(t, qcow2.Compat, "1.1")
117 })
118 })
119 t.Run("qcow2 external data file", func(t *testing.T) {
120 // qemu-img create -f qcow2 -o data_file=secret.bin,data_file_raw=on evil.qcow2 4096
121 // (QEMU 11.0)
122 const s = `{
123 "children": [
124 {
125 "name": "file",
126 "info": {
127 "children": [],
128 "virtual-size": 327680,
129 "filename": "evil.qcow2",
130 "format": "file",
131 "dirty-flag": false
132 }
133 }
134 ],
135 "virtual-size": 4096,
136 "filename": "evil.qcow2",
137 "cluster-size": 65536,
138 "format": "qcow2",
139 "format-specific": {
140 "type": "qcow2",
141 "data": {
142 "compat": "1.1",
143 "compression-type": "zlib",
144 "data-file": "secret.bin",
145 "data-file-raw": true,
146 "refcount-bits": 16
147 }
148 },
149 "dirty-flag": false
150}`
151 info, err := parseInfo([]byte(s))
152 assert.NilError(t, err)
153 assert.Equal(t, "", info.BackingFilename)
154 assert.Check(t, info.FormatSpecific != nil)
155 qcow2 := info.FormatSpecific.Qcow2()
156 assert.Check(t, qcow2 != nil)
157 assert.Equal(t, "secret.bin", qcow2.DataFile)
158 })
159 t.Run("vmdk", func(t *testing.T) {
160 t.Run("twoGbMaxExtentSparse", func(t *testing.T) {
161 // qemu-img create -f vmdk foo.vmdk 4G -o subformat=twoGbMaxExtentSparse
162 // (QEMU 8.0)
163 const s = `{
164 "children": [
165 {
166 "name": "extents.1",
167 "info": {
168 "children": [
169 ],
170 "virtual-size": 327680,
171 "filename": "foo-s002.vmdk",
172 "format": "file",
173 "actual-size": 327680,
174 "format-specific": {
175 "type": "file",
176 "data": {
177 }
178 },
179 "dirty-flag": false
180 }
181 },
182 {
183 "name": "extents.0",
184 "info": {
185 "children": [
186 ],
187 "virtual-size": 327680,
188 "filename": "foo-s001.vmdk",
189 "format": "file",
190 "actual-size": 327680,
191 "format-specific": {
192 "type": "file",
193 "data": {
194 }
195 },
196 "dirty-flag": false
197 }
198 },
199 {
200 "name": "file",
201 "info": {
202 "children": [
203 ],
204 "virtual-size": 512,
205 "filename": "foo.vmdk",
206 "format": "file",
207 "actual-size": 4096,
208 "format-specific": {
209 "type": "file",
210 "data": {
211 }
212 },
213 "dirty-flag": false
214 }
215 }
216 ],
217 "virtual-size": 4294967296,
218 "filename": "foo.vmdk",
219 "cluster-size": 65536,
220 "format": "vmdk",
221 "actual-size": 659456,
222 "format-specific": {
223 "type": "vmdk",
224 "data": {
225 "cid": 918420663,
226 "parent-cid": 4294967295,
227 "create-type": "twoGbMaxExtentSparse",
228 "extents": [
229 {
230 "virtual-size": 2147483648,
231 "filename": "foo-s001.vmdk",
232 "cluster-size": 65536,
233 "format": "SPARSE"
234 },
235 {
236 "virtual-size": 2147483648,
237 "filename": "foo-s002.vmdk",
238 "cluster-size": 65536,
239 "format": "SPARSE"
240 }
241 ]
242 }
243 },
244 "dirty-flag": false
245}`
246 info, err := parseInfo([]byte(s))
247 assert.NilError(t, err)
248 assert.Equal(t, 3, len(info.Children))
249 assert.Equal(t, "foo.vmdk", info.Filename)
250 assert.Check(t, info.FormatSpecific != nil)
251 vmdk := info.FormatSpecific.Vmdk()
252 assert.Check(t, vmdk != nil)
253 assert.Equal(t, vmdk.CreateType, "twoGbMaxExtentSparse")
254 })
255 })
256}
257
258func TestRejectExternalFileReferences(t *testing.T) {
259 t.Run("plain qcow2", func(t *testing.T) {
260 info := &Info{
261 Filename: "foo.qcow2",
262 Format: "qcow2",
263 FormatSpecific: &InfoFormatSpecific{
264 Type: "qcow2",
265 Data: json.RawMessage(`{"compat":"1.1"}`),
266 },
267 }
268 assert.NilError(t, rejectExternalFileReferences(info))
269 assert.NilError(t, AcceptableAsBaseDisk(info))
270 })
271 t.Run("backing file", func(t *testing.T) {
272 info := &Info{Filename: "bar.qcow2", Format: "qcow2", BackingFilename: "foo.qcow2"}
273 assert.ErrorContains(t, rejectExternalFileReferences(info), "backing file")
274 assert.ErrorContains(t, AcceptableAsBaseDisk(info), "backing file")
275 })
276 t.Run("qcow2 external data file", func(t *testing.T) {
277 info := &Info{
278 Filename: "evil.qcow2",
279 Format: "qcow2",
280 FormatSpecific: &InfoFormatSpecific{
281 Type: "qcow2",
282 Data: json.RawMessage(`{"data-file":"/etc/passwd","data-file-raw":true}`),
283 },
284 }
285 assert.ErrorContains(t, rejectExternalFileReferences(info), "external data file")
286 assert.ErrorContains(t, AcceptableAsBaseDisk(info), "external data file")
287 })
288 t.Run("vmdk extent", func(t *testing.T) {
289 info := &Info{
290 Filename: "foo.vmdk",
291 Format: "vmdk",
292 FormatSpecific: &InfoFormatSpecific{
293 Type: "vmdk",
294 Data: json.RawMessage(`{"extents":[{"filename":"/etc/passwd"}]}`),
295 },
296 }
297 assert.ErrorContains(t, rejectExternalFileReferences(info), "extent file")
298 assert.ErrorContains(t, AcceptableAsBaseDisk(info), "extent file")
299 })
300}