summaryrefslogtreecommitdiffhomepage
path: root/util/safediff/diff_test.go
blob: 4251d788b10fef9ce27b4e34731a0d1ce6f1e909 (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
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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package safediff

import (
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func init() { diffTest = true }

func TestLines(t *testing.T) {
	// The diffs shown below technically depend on the stability of cmp,
	// but that should be fine for sufficiently simple diffs like these.
	// If the output does change, that would suggest a significant regression
	// in the optimality of cmp's diffing algorithm.

	x := `{
	"firstName": "John",
	"lastName": "Smith",
	"isAlive": true,
	"age": 27,
	"address": {
		"streetAddress": "21 2nd Street",
		"city": "New York",
		"state": "NY",
		"postalCode": "10021-3100"
	},
	"phoneNumbers": [{
		"type": "home",
		"number": "212 555-1234"
	}, {
		"type": "office",
		"number": "646 555-4567"
	}],
	"children": [
		"Catherine",
		"Thomas",
		"Trevor"
	],
	"spouse": null
}`
	y := x
	y = strings.ReplaceAll(y, `"New York"`, `"Los Angeles"`)
	y = strings.ReplaceAll(y, `"NY"`, `"CA"`)
	y = strings.ReplaceAll(y, `"646 555-4567"`, `"315 252-8888"`)

	wantDiff := `
… 5 identical lines
  	"address": {
  		"streetAddress": "21 2nd Street",
- 		"city": "New York",
- 		"state": "NY",
+ 		"city": "Los Angeles",
+ 		"state": "CA",
  		"postalCode": "10021-3100"
  	},
… 3 identical lines
  	}, {
  		"type": "office",
- 		"number": "646 555-4567"
+ 		"number": "315 252-8888"
  	}],
… 7 identical lines
`[1:]
	gotDiff, gotTrunc := Lines(x, y, -1)
	if d := cmp.Diff(gotDiff, wantDiff); d != "" {
		t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff)
	} else if gotTrunc == true {
		t.Errorf("Lines: output unexpectedly truncated")
	}

	wantDiff = `
… 5 identical lines
  	"address": {
  		"streetAddress": "21 2nd Street",
- 		"city": "New York",
- 		"state": "NY",
+ 		"city": "Los Angeles",
… 15 identical, 1 removed, and 2 inserted lines
`[1:]
	gotDiff, gotTrunc = Lines(x, y, 200)
	if d := cmp.Diff(gotDiff, wantDiff); d != "" {
		t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff)
	} else if gotTrunc == false {
		t.Errorf("Lines: output unexpectedly not truncated")
	}

	wantDiff = "… 17 identical, 3 removed, and 3 inserted lines\n"
	gotDiff, gotTrunc = Lines(x, y, 0)
	if d := cmp.Diff(gotDiff, wantDiff); d != "" {
		t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff)
	} else if gotTrunc == false {
		t.Errorf("Lines: output unexpectedly not truncated")
	}

	x = `{
	"unrelated": [
		"unrelated",
	],
	"related": {
		"unrelated": [
			"unrelated",
		],
		"related": {
			"unrelated": [
				"unrelated",
			],
			"related": {
				"related": "changed",
			},
			"unrelated": [
				"unrelated",
			],
		},
		"unrelated": [
			"unrelated",
		],
	},
	"unrelated": [
		"unrelated",
	],
}`
	y = strings.ReplaceAll(x, "changed", "CHANGED")

	wantDiff = `
… 4 identical lines
  	"related": {
… 3 identical lines
  		"related": {
… 3 identical lines
  			"related": {
- 				"related": "changed",
+ 				"related": "CHANGED",
  			},
… 3 identical lines
  		},
… 3 identical lines
  	},
… 4 identical lines
`[1:]
	gotDiff, gotTrunc = Lines(x, y, -1)
	if d := cmp.Diff(gotDiff, wantDiff); d != "" {
		t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff)
	} else if gotTrunc == true {
		t.Errorf("Lines: output unexpectedly truncated")
	}

	x = `{
	"ACLs": [
		{
			"Action": "accept",
			"Users":  ["group:all"],
			"Ports":  ["tag:tmemes:80"],
		},
	],
}`
	y = strings.ReplaceAll(x, "tag:tmemes:80", "tag:tmemes:80,8383")
	wantDiff = `
  {
  	"ACLs": [
  		{
  			"Action": "accept",
  			"Users":  ["group:all"],
- 			"Ports":  ["tag:tmemes:80"],
+ 			"Ports":  ["tag:tmemes:80,8383"],
  		},
  	],
  }
`[1:]
	gotDiff, gotTrunc = Lines(x, y, -1)
	if d := cmp.Diff(gotDiff, wantDiff); d != "" {
		t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff)
	} else if gotTrunc == true {
		t.Errorf("Lines: output unexpectedly truncated")
	}
}

func FuzzDiff(f *testing.F) {
	f.Fuzz(func(t *testing.T, x, y string, maxSize int) {
		const maxInput = 1e3
		if len(x) > maxInput {
			x = x[:maxInput]
		}
		if len(y) > maxInput {
			y = y[:maxInput]
		}
		diff, _ := Lines(x, y, maxSize) // make sure this does not panic
		if strings.Count(diff, "\n") > 1 && maxSize >= 0 && len(diff) > maxSize {
			t.Fatal("maxSize exceeded")
		}
	})
}