-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbody_test.go
More file actions
599 lines (523 loc) · 16.3 KB
/
Copy pathbody_test.go
File metadata and controls
599 lines (523 loc) · 16.3 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2025 Daniel Schmidt
package netconf
import (
"fmt"
"strings"
"testing"
)
func TestNewBody(t *testing.T) {
t.Run("create from existing XML", func(t *testing.T) {
existingXML := `<system><hostname>router1</hostname></system>`
body := NewBody(existingXML)
xml := body.Res()
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1', got: %s", xml)
}
})
t.Run("create and extend XML", func(t *testing.T) {
existingXML := `<config><system><hostname>router1</hostname></system></config>`
body := NewBody(existingXML).
Set("config.system.domain-name", "example.com")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1', got: %s", xml)
}
if !strings.Contains(xml, "example.com") {
t.Errorf("expected XML to contain 'example.com', got: %s", xml)
}
})
t.Run("create from empty string", func(t *testing.T) {
body := NewBody("")
// Empty body can be extended with Set
body = body.Set("config.hostname", "test")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "test") {
t.Errorf("expected XML to contain 'test', got: %s", xml)
}
})
t.Run("create and chain operations", func(t *testing.T) {
existingXML := `<config><interfaces></interfaces></config>`
body := NewBody(existingXML).
Set("config.interfaces.interface.name", "eth0").
SetAttr("config.interfaces.interface", "operation", "merge")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "eth0") {
t.Errorf("expected XML to contain 'eth0', got: %s", xml)
}
if !strings.Contains(xml, `operation="merge"`) {
t.Errorf("expected XML to contain operation attribute, got: %s", xml)
}
})
}
func TestBodySet(t *testing.T) {
t.Run("single set operation", func(t *testing.T) {
body := Body{}.
Set("config.hostname", "router1")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1', got: %s", xml)
}
if !strings.Contains(xml, "<hostname>") {
t.Errorf("expected XML to contain '<hostname>', got: %s", xml)
}
})
t.Run("multiple set operations", func(t *testing.T) {
body := Body{}.
Set("config.hostname", "router1").
Set("config.domain", "example.com")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1', got: %s", xml)
}
if !strings.Contains(xml, "example.com") {
t.Errorf("expected XML to contain 'example.com', got: %s", xml)
}
})
t.Run("set with different types", func(t *testing.T) {
body := Body{}.
Set("config.port", 8080).
Set("config.enabled", true)
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "8080") {
t.Errorf("expected XML to contain '8080', got: %s", xml)
}
if !strings.Contains(xml, "true") {
t.Errorf("expected XML to contain 'true', got: %s", xml)
}
})
}
func TestBodySetAttr(t *testing.T) {
t.Run("set attribute on element", func(t *testing.T) {
body := Body{}.
Set("config.interface.name", "eth0").
SetAttr("config.interface", "operation", "merge")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "eth0") {
t.Errorf("expected XML to contain 'eth0', got: %s", xml)
}
if !strings.Contains(xml, `operation="merge"`) {
t.Errorf("expected XML to contain operation attribute, got: %s", xml)
}
})
t.Run("set attribute with different types", func(t *testing.T) {
body := Body{}.
Set("config.interface.name", "eth0").
SetAttr("config.interface", "mtu", 1500).
SetAttr("config.interface", "enabled", true)
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, `mtu="1500"`) {
t.Errorf("expected XML to contain mtu attribute with int value, got: %s", xml)
}
if !strings.Contains(xml, `enabled="true"`) {
t.Errorf("expected XML to contain enabled attribute with bool value, got: %s", xml)
}
})
}
func TestBodySetRaw(t *testing.T) {
t.Run("insert raw XML", func(t *testing.T) {
rawXML := "<interface><name>eth0</name><enabled>true</enabled></interface>"
body := Body{}.
SetRaw("config.interfaces", rawXML)
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "<name>eth0</name>") {
t.Errorf("expected XML to contain raw content, got: %s", xml)
}
})
}
func TestBodyDelete(t *testing.T) {
t.Run("delete element", func(t *testing.T) {
body := Body{}.
Set("config.hostname", "router1").
Set("config.domain", "example.com").
Delete("config.domain")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1', got: %s", xml)
}
if strings.Contains(xml, "example.com") {
t.Errorf("did not expect XML to contain 'example.com', got: %s", xml)
}
})
}
func TestBodyRes(t *testing.T) {
t.Run("inspect built XML", func(t *testing.T) {
body := Body{}.
Set("config.hostname", "router1")
if body.Err() != nil {
t.Fatalf("unexpected error: %v", body.Err())
}
xml := body.Res()
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1', got: %s", xml)
}
})
}
func TestBodyMethodChaining(t *testing.T) {
t.Run("complex chained operations", func(t *testing.T) {
body := Body{}.
Set("config.system.hostname", "router1").
Set("config.system.domain", "example.com").
Set("config.interface.name", "eth0").
SetAttr("config.interface", "operation", "merge").
Set("config.interface.enabled", true)
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "router1") {
t.Errorf("expected XML to contain 'router1'")
}
if !strings.Contains(xml, "example.com") {
t.Errorf("expected XML to contain 'example.com'")
}
if !strings.Contains(xml, "eth0") {
t.Errorf("expected XML to contain 'eth0'")
}
if !strings.Contains(xml, "true") {
t.Errorf("expected XML to contain 'true'")
}
})
}
func TestBodyEmptyInitialization(t *testing.T) {
t.Run("start with empty body", func(t *testing.T) {
// Empty body should work fine
body := Body{}
body = body.Set("config.test", "value")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(xml, "value") {
t.Errorf("expected XML to contain 'value', got: %s", xml)
}
})
}
// Error path tests
func TestBodySetError(t *testing.T) {
t.Run("empty path", func(t *testing.T) {
body := Body{}.Set("", "value")
_, err := body.String()
if err == nil {
t.Error("expected error for empty path")
}
})
t.Run("invalid path", func(t *testing.T) {
// Empty path should error - xmldot rejects empty paths
body := Body{}.Set("", "value")
_, err := body.String()
if err == nil {
t.Error("expected error for invalid path")
}
})
t.Run("error propagation", func(t *testing.T) {
body := Body{}.
Set("valid", "works").
Set("", "fails"). // Should error
Set("after", "skipped") // Should skip
_, err := body.String()
if err == nil {
t.Error("expected error to propagate")
}
// Verify error message contains the failing operation
if !strings.Contains(err.Error(), "Set") {
t.Errorf("expected error message to mention 'Set', got: %v", err)
}
})
t.Run("check Err method", func(t *testing.T) {
body := Body{}.Set("", "invalid")
if body.Err() == nil {
t.Error("expected Err() to return error")
}
})
t.Run("Res returns empty on error", func(t *testing.T) {
body := Body{}.Set("", "invalid")
res := body.Res()
if res != "" {
t.Errorf("expected Res() to return empty string on error, got: %s", res)
}
})
}
func TestBodySetRawXXE(t *testing.T) {
t.Run("XXE injection attempt", func(t *testing.T) {
xxe := `<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><data>&xxe;</data>`
body := Body{}.SetRaw("config", xxe)
_, err := body.String()
if err == nil {
t.Error("expected error for XXE attempt")
}
// xmldot provides XXE protection by rejecting DOCTYPE declarations
if !strings.Contains(err.Error(), "XXE") && !strings.Contains(err.Error(), "ENTITY") && !strings.Contains(err.Error(), "DOCTYPE") {
t.Errorf("expected XXE-related error, got: %v", err)
}
})
t.Run("entity expansion attempt", func(t *testing.T) {
entity := `<!ENTITY a "test"><data>&a;</data>`
body := Body{}.SetRaw("config", entity)
_, err := body.String()
if err == nil {
t.Error("expected error for entity declaration")
}
})
t.Run("valid XML should work", func(t *testing.T) {
validXML := `<interface><name>eth0</name></interface>`
body := Body{}.SetRaw("config", validXML)
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error for valid XML: %v", err)
}
if !strings.Contains(xml, "eth0") {
t.Errorf("expected XML to contain 'eth0', got: %s", xml)
}
})
}
func TestBodySetAttrError(t *testing.T) {
t.Run("SetAttr on invalid path", func(t *testing.T) {
body := Body{}.
Set("config.interface", "test").
SetAttr("", "attr", "value") // Empty path should error
_, err := body.String()
if err == nil {
t.Error("expected error for invalid path in SetAttr")
}
})
t.Run("SetAttr error propagation", func(t *testing.T) {
body := Body{}.
Set("config.test", "value").
SetAttr("", "attr", "value"). // Should error
Set("after", "skipped") // Should skip
_, err := body.String()
if err == nil {
t.Error("expected error to propagate from SetAttr")
}
})
}
func TestBodyDeleteError(t *testing.T) {
t.Run("Delete nonexistent path", func(t *testing.T) {
// Note: xmldot.Delete() doesn't error for nonexistent paths
// It just returns the original XML unchanged
body := Body{}.
Set("config.test", "value").
Delete("config.nonexistent")
xml, err := body.String()
if err != nil {
t.Errorf("unexpected error for deleting nonexistent path: %v", err)
}
// Verify original content still exists
if !strings.Contains(xml, "value") {
t.Errorf("expected XML to still contain 'value', got: %s", xml)
}
})
t.Run("Delete error propagation", func(t *testing.T) {
body := Body{}.
Set("config.test", "value").
Delete(""). // Should error (empty path)
Set("after", "skipped") // Should skip
_, err := body.String()
if err == nil {
t.Error("expected error to propagate from Delete")
}
})
}
func TestBodyErrorImmutability(t *testing.T) {
t.Run("error state is immutable", func(t *testing.T) {
// Create body with error
body1 := Body{}.Set("", "invalid")
// Try to continue using it
body2 := body1.Set("valid", "value")
// Both should have error
if body1.Err() == nil {
t.Error("expected body1 to have error")
}
if body2.Err() == nil {
t.Error("expected body2 to have error")
}
// Verify error is the same (first error preserved)
if body1.Err().Error() != body2.Err().Error() {
t.Error("expected same error in both bodies")
}
})
}
// Benchmark tests for Body operations
func BenchmarkBodySet(b *testing.B) {
for i := 0; i < b.N; i++ {
body := Body{}.
Set("config.hostname", "router1").
Set("config.domain", "example.com").
Set("config.port", 8080)
_ = body.Res()
}
}
func BenchmarkBodySetComplex(b *testing.B) {
for i := 0; i < b.N; i++ {
body := Body{}.
Set("config.system.hostname", "router1").
Set("config.system.domain", "example.com").
Set("config.system.port", 8080).
Set("config.interface.name", "eth0").
Set("config.interface.mtu", 1500).
Set("config.interface.enabled", true).
Set("config.interface.speed", "1000").
Set("config.interface.duplex", "full")
_ = body.Res()
}
}
func BenchmarkBodySetAttr(b *testing.B) {
for i := 0; i < b.N; i++ {
body := Body{}.
Set("config.interface.name", "eth0").
SetAttr("config.interface", "operation", "merge").
Set("config.interface.enabled", true)
_ = body.Res()
}
}
func BenchmarkBodySetRaw(b *testing.B) {
rawXML := "<interface><name>eth0</name><enabled>true</enabled><mtu>1500</mtu></interface>"
for i := 0; i < b.N; i++ {
body := Body{}.
SetRaw("config.interfaces", rawXML)
_ = body.Res()
}
}
func BenchmarkBodyDelete(b *testing.B) {
for i := 0; i < b.N; i++ {
body := Body{}.
Set("config.hostname", "router1").
Set("config.domain", "example.com").
Set("config.port", 8080).
Delete("config.domain")
_ = body.Res()
}
}
func BenchmarkBodyString(b *testing.B) {
body := Body{}.
Set("config.hostname", "router1").
Set("config.domain", "example.com").
Set("config.port", 8080)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = body.String() //nolint:errcheck // Benchmark only measures performance
}
}
func BenchmarkBodyChaining(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Body{}.
Set("config.a", "1").
Set("config.b", "2").
Set("config.c", "3").
Set("config.d", "4").
Set("config.e", "5").
Res()
}
}
// ExampleBody_Set demonstrates the fluent Body builder API for creating XML configurations.
func ExampleBody_Set() {
// Build configuration using fluent API
body := Body{}.
Set("config.system.hostname", "Router1").
Set("config.system.domain-name", "example.com").
Set("config.interfaces.interface.name", "GigabitEthernet1").
Set("config.interfaces.interface.enabled", true)
// Get the XML string
xml, err := body.String()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Configuration built successfully")
fmt.Printf("XML length: %d bytes\n", len(xml))
// Output: Configuration built successfully
// XML length: 200 bytes
}
// TestBodySetMultipleRoots tests that Body.Set() properly handles multiple root-level sibling elements
func TestBodySetMultipleRoots(t *testing.T) {
t.Run("ACL with deny and permit siblings", func(t *testing.T) {
// This is the real-world use case from terraform-provider-iosxe ACLs
// where we have both deny and permit at the same level
body := Body{}.
Set("sequence", "10").
Set("deny.std-ace.ipv4-address-prefix", "10.0.0.0").
Set("deny.std-ace.mask", "0.0.0.255").
Set("permit.std-ace.ipv4-address-prefix", "192.168.0.0").
Set("permit.std-ace.mask", "0.0.255.255")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
t.Logf("Generated XML:\n%s", xml)
// Verify we have <sequence>, <deny>, and <permit> as separate root-level siblings
if !strings.Contains(xml, "<sequence>10</sequence>") {
t.Errorf("expected separate <sequence> element, got: %s", xml)
}
if !strings.Contains(xml, "<deny>") {
t.Errorf("expected <deny> element, got: %s", xml)
}
if !strings.Contains(xml, "<permit>") {
t.Errorf("expected <permit> element, got: %s", xml)
}
if !strings.Contains(xml, "<ipv4-address-prefix>10.0.0.0</ipv4-address-prefix>") {
t.Errorf("expected deny prefix in XML, got: %s", xml)
}
if !strings.Contains(xml, "<ipv4-address-prefix>192.168.0.0</ipv4-address-prefix>") {
t.Errorf("expected permit prefix in XML, got: %s", xml)
}
// Verify it's NOT all nested inside <sequence> or duplicated
// The XML should have the structure:
// <sequence>10</sequence><deny>...</deny><permit>...</permit>
// NOT: <sequence>10<deny>...</deny><permit>...</permit></sequence>
if strings.Contains(xml, "<sequence>10<deny>") || strings.Contains(xml, "</deny></sequence>") {
t.Errorf("deny should not be nested inside sequence, got: %s", xml)
}
})
t.Run("multiple different root elements", func(t *testing.T) {
body := Body{}.
Set("hostname", "router1").
Set("domain", "example.com").
Set("port", "8080")
xml, err := body.String()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
t.Logf("Generated XML:\n%s", xml)
// All should be separate root elements
if !strings.Contains(xml, "<hostname>router1</hostname>") {
t.Errorf("expected hostname element, got: %s", xml)
}
if !strings.Contains(xml, "<domain>example.com</domain>") {
t.Errorf("expected domain element, got: %s", xml)
}
if !strings.Contains(xml, "<port>8080</port>") {
t.Errorf("expected port element, got: %s", xml)
}
})
}