/
PropertiesFileAwareClasspathResourceHasherTest.groovy
374 lines (307 loc) · 13.8 KB
/
PropertiesFileAwareClasspathResourceHasherTest.groovy
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
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.changedetection.state
import com.google.common.collect.ImmutableSet
import com.google.common.collect.Maps
import com.google.common.io.ByteStreams
import org.gradle.api.internal.file.archive.ZipEntry
import org.gradle.internal.hash.Hasher
import org.gradle.internal.hash.Hashing
import org.gradle.internal.hash.HashingOutputStream
import org.gradle.internal.snapshot.RegularFileSnapshot
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
import spock.lang.Unroll
import java.util.function.Supplier
@Unroll
class PropertiesFileAwareClasspathResourceHasherTest extends Specification {
@Rule TemporaryFolder tmpdir = new TemporaryFolder()
Map<String, ResourceEntryFilter> filters = Maps.newHashMap()
ResourceHasher delegate = new RuntimeClasspathResourceHasher()
ResourceHasher unfilteredHasher = new PropertiesFileAwareClasspathResourceHasher(delegate, PropertiesFileFilter.FILTER_NOTHING)
def getFilteredHasher() {
return new PropertiesFileAwareClasspathResourceHasher(delegate, filters)
}
def setup() {
filters = [
'**/*.properties': filter("created-by", "पशुपतिरपि")
]
}
def "properties are case sensitive (context: #context)"() {
given:
def propertiesEntry1 = contextFor(context, ["Created-By": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def propertiesEntry2 = contextFor(context, ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = filteredHasher.hash(propertiesEntry1)
def hash4 = filteredHasher.hash(propertiesEntry2)
expect:
hash1 != hash2
hash2 != hash4
hash3 != hash4
hash1 != hash3
where:
context << SnapshotContext.values()
}
def "properties are normalized and filtered out (context: #context)"() {
given:
def propertiesEntry1 = contextFor(context, ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)", "foo": "true"])
def propertiesEntry2 = contextFor(context, ["created-by": "1.8.0_232-b15 (Azul Systems, Inc.)", "foo": "true"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = filteredHasher.hash(propertiesEntry1)
def hash4 = filteredHasher.hash(propertiesEntry2)
expect:
hash1 != hash2
hash1 != hash3
hash2 != hash4
and:
hash3 == hash4
where:
context << SnapshotContext.values()
}
def "properties can have UTF-8 encoding (context: #context)"() {
def propertiesEntry1 = contextFor(context, ["Created-By": "1.8.0_232-b18 (Azul Systems, Inc.)", "पशुपतिरपि": "some sanskrit", "तान्यहानि": "more sanskrit"])
def propertiesEntry2 = contextFor(context, ["Created-By": "1.8.0_232-b18 (Azul Systems, Inc.)", "पशुपतिरपि": "changed sanskrit", "तान्यहानि": "more sanskrit"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = filteredHasher.hash(propertiesEntry1)
def hash4 = filteredHasher.hash(propertiesEntry2)
expect:
hash1 != hash2
hash1 != hash3
hash2 != hash4
and:
hash3 == hash4
where:
context << SnapshotContext.values()
}
def "properties are order insensitive (context: #context)"() {
given:
def propertiesEntry1 = contextFor(context, ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)", "foo": "true"])
def propertiesEntry2 = contextFor(context, ["foo": "true", "created-by": "1.8.0_232-b15 (Azul Systems, Inc.)"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = filteredHasher.hash(propertiesEntry1)
def hash4 = filteredHasher.hash(propertiesEntry2)
expect:
hash1 != hash2
hash1 != hash3
hash2 != hash4
and:
hash3 == hash4
where:
context << SnapshotContext.values()
}
def "comments are always filtered out when filters are applied (context: #context)"() {
def propertiesEntry1 = contextFor(context, ["foo": "true"], "Build information 1.0")
def propertiesEntry2 = contextFor(context, ["foo": "true"], "Build information 1.1")
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = filteredHasher.hash(propertiesEntry1)
def hash4 = filteredHasher.hash(propertiesEntry2)
expect:
hash3 == hash4
and:
hash1 != hash2
hash1 != hash3
where:
context << SnapshotContext.values()
}
def "can filter files selectively based on pattern (pattern: #fooPattern, context: #context)"() {
given:
filters = [
'**/*.properties': ResourceEntryFilter.FILTER_NOTHING,
(fooPattern): filter("created-by", "पशुपतिरपि")
]
def propertiesEntry1 = contextFor(context, 'some/path/to/foo.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def propertiesEntry2 = contextFor(context, 'some/path/to/bar.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = filteredHasher.hash(propertiesEntry1)
def hash4 = filteredHasher.hash(propertiesEntry2)
expect:
hash2 != hash4
hash3 != hash4
hash1 != hash3
and:
hash1 == hash2
where:
[context, fooPattern] << [SnapshotContext.values(), ['**/foo.properties', '**/f*.properties', 'some/**/f*.properties', 'some/path/to/foo.properties']].combinations()*.flatten()
}
def "can filter multiple files selectively based on pattern (pattern: #fPattern, context: #context)"() {
given:
filters = [
'**/*.properties': ResourceEntryFilter.FILTER_NOTHING,
(fPattern.toString()): filter("created-by", "पशुपतिरपि")
]
def propertiesEntry1 = contextFor(context, 'some/path/to/foo.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def propertiesEntry2 = contextFor(context, 'some/path/to/bar.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def propertiesEntry3 = contextFor(context, 'some/other/path/to/fuzz.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = unfilteredHasher.hash(propertiesEntry3)
def hash4 = filteredHasher.hash(propertiesEntry1)
def hash5 = filteredHasher.hash(propertiesEntry2)
def hash6 = filteredHasher.hash(propertiesEntry3)
expect:
hash1 != hash4
hash4 != hash5
and:
hash1 == hash2
hash1 == hash3
hash4 == hash6
where:
[context, fPattern] << [SnapshotContext.values(), ['**/f*.properties', 'some/**/f*.properties']].combinations()*.flatten()
}
def "multiple filters can be applied to the same file (context: #context)"() {
given:
filters = [
'**/*.properties': filter("created-by"),
'**/foo.properties': filter("पशुपतिरपि")
]
def propertiesEntry1 = contextFor(context, 'some/path/to/foo.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)", "पशुपतिरपि": "some sanskrit"])
def propertiesEntry2 = contextFor(context, 'some/path/to/bar.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def propertiesEntry3 = contextFor(context, 'some/path/to/foo.properties', ["created-by": "1.8.0_232-b18 (Azul Systems, Inc.)"])
def hash1 = unfilteredHasher.hash(propertiesEntry1)
def hash2 = unfilteredHasher.hash(propertiesEntry2)
def hash3 = unfilteredHasher.hash(propertiesEntry3)
def hash4 = filteredHasher.hash(propertiesEntry1)
def hash5 = filteredHasher.hash(propertiesEntry2)
def hash6 = filteredHasher.hash(propertiesEntry3)
expect:
hash1 != hash2
hash2 != hash4
and:
hash2 == hash3
hash4 == hash5
hash4 == hash6
where:
context << SnapshotContext.values()
}
def "delegates to file hasher when bad unicode escape sequences are present (error in: #location, context: #context)"() {
given:
filters = [ '**/*.properties': ResourceEntryFilter.FILTER_NOTHING ]
def properties = contextFor(context, 'some/path/to/foo.properties', property.bytes)
expect:
filteredHasher.hash(properties) == delegate.hash(properties)
where:
context | location | property
SnapshotContext.ZIP_ENTRY | "value" | 'someKey=a value with bad escape sequence \\uxxxx'
SnapshotContext.ZIP_ENTRY | "key" | 'keyWithBadEscapeSequence\\uxxxx=some value'
SnapshotContext.FILE_SNAPSHOT | "value" | 'someKey=a value with bad escape sequence \\uxxxx'
SnapshotContext.FILE_SNAPSHOT | "key" | 'keyWithBadEscapeSequence\\uxxxx=some value'
}
def "delegate configuration is added to hasher"() {
def configurationHasher = Mock(Hasher)
delegate = Mock(ResourceHasher)
when:
filteredHasher.appendConfigurationToHasher(configurationHasher)
then:
1 * delegate.appendConfigurationToHasher(configurationHasher)
}
enum SnapshotContext {
ZIP_ENTRY, FILE_SNAPSHOT
}
def contextFor(SnapshotContext context, String path, Map<String, String> attributes, String comments = "") {
switch(context) {
case SnapshotContext.ZIP_ENTRY:
return zipEntry(path, attributes, comments)
case SnapshotContext.FILE_SNAPSHOT:
return fileSnapshot(path, attributes, comments)
default:
throw new IllegalArgumentException()
}
}
def contextFor(SnapshotContext context, String path, byte[] bytes) {
switch(context) {
case SnapshotContext.ZIP_ENTRY:
return zipEntry(path, bytes)
case SnapshotContext.FILE_SNAPSHOT:
return fileSnapshot(path, bytes)
default:
throw new IllegalArgumentException()
}
}
def contextFor(SnapshotContext context, Map<String, String> attributes, String comments = "") {
contextFor(context, "META-INF/build-info.properties", attributes, comments)
}
static filter(String... properties) {
return new IgnoringResourceEntryFilter(ImmutableSet.copyOf(properties))
}
ZipEntryContext zipEntry(String path, Map<String, String> attributes, String comments = "") {
Properties properties = new Properties()
properties.putAll(attributes)
ByteArrayOutputStream bos = new ByteArrayOutputStream()
properties.store(bos, comments)
return zipEntry(path, bos.toByteArray())
}
ZipEntryContext zipEntry(String path, byte[] bytes) {
def zipEntry = new ZipEntry() {
@Override
boolean isDirectory() {
return false
}
@Override
String getName() {
return path
}
@Override
byte[] getContent() throws IOException {
return bytes
}
@Override
InputStream getInputStream() {
return new ByteArrayInputStream(bytes)
}
@Override
int size() {
return bytes.length
}
}
return new ZipEntryContext(zipEntry, path, "foo.zip")
}
RegularFileSnapshotContext fileSnapshot(String path, Map<String, String> attributes, String comments = "") {
Properties properties = new Properties()
properties.putAll(attributes)
ByteArrayOutputStream bos = new ByteArrayOutputStream()
properties.store(bos, comments)
return fileSnapshot(path, bos.toByteArray())
}
RegularFileSnapshotContext fileSnapshot(String path, byte[] bytes) {
def dir = tmpdir.newFolder()
def file = new File(dir, path)
file.parentFile.mkdirs()
file << bytes
return Mock(RegularFileSnapshotContext) {
_ * getSnapshot() >> Mock(RegularFileSnapshot) {
_ * getAbsolutePath() >> file.absolutePath
_ * getHash() >> {
HashingOutputStream hasher = Hashing.primitiveStreamHasher()
ByteStreams.copy(new ByteArrayInputStream(bytes), hasher)
return hasher.hash()
}
}
_ * getRelativePathSegments() >> new Supplier<String[]>() {
@Override
String[] get() {
return path.split('/')
}
}
}
}
}