-
Notifications
You must be signed in to change notification settings - Fork 0
/
RpmTest.java
217 lines (207 loc) · 8.09 KB
/
RpmTest.java
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 artipie.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.artipie.rpm;
import com.artipie.asto.Content;
import com.artipie.asto.Key;
import com.artipie.asto.Storage;
import com.artipie.asto.fs.FileStorage;
import com.artipie.asto.memory.InMemoryStorage;
import com.artipie.rpm.files.Gzip;
import com.artipie.rpm.meta.XmlPackage;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Stream;
import org.cactoos.Scalar;
import org.cactoos.list.ListOf;
import org.cactoos.list.Mapped;
import org.cactoos.scalar.AndInThreads;
import org.cactoos.scalar.Unchecked;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Unit tests for {@link Rpm}.
*
* @since 0.9
* @todo #110:30min Meaningful error on broken package.
* Rpm should throw an exception when trying to add an invalid package.
* The type of exception must be IllegalArgumentException and its message
* "Reading of RPM package 'package' failed, data corrupt or malformed.",
* like described in showMeaningfulErrorWhenInvalidPackageSent. Implement it
* and then enable the test.
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class RpmTest {
@Test
void updatesDifferentReposSimultaneouslyTwice() throws Exception {
final Storage storage = new InMemoryStorage();
final Rpm repo = new Rpm(
storage, StandardNamingPolicy.SHA1, Digest.SHA256, true
);
final List<String> keys = new ListOf<>("one", "two", "three");
final CountDownLatch latch = new CountDownLatch(keys.size());
final List<Scalar<Boolean>> tasks = new Mapped<>(
key -> new Unchecked<>(
() -> {
new TestRpm.Multiple(
new TestRpm.Abc(),
new TestRpm.Libdeflt()
).put(storage);
latch.countDown();
latch.await();
repo.batchUpdate(new Key.From(key)).blockingAwait();
return true;
}
),
keys
);
new AndInThreads(tasks).value();
new AndInThreads(tasks).value();
keys.forEach(
key -> {
final Key res = new Key.From(key, "repodata");
RpmTest.metadataArePresent(storage, res);
RpmTest.repomdIsPresent(storage, res);
}
);
}
@Test
void doesntBrakeMetadataWhenInvalidPackageSent(@TempDir final Path tmp) throws Exception {
final Storage storage = new FileStorage(tmp);
final Rpm repo = new Rpm(storage, StandardNamingPolicy.SHA1, Digest.SHA256, true);
new TestRpm.Abc().put(storage);
repo.batchUpdate(Key.ROOT).blockingAwait();
final byte[] broken = {0x00, 0x01, 0x02 };
storage.save(new Key.From("broken.rpm"), new Content.From(broken)).get();
new TestRpm.Libdeflt().put(storage);
repo.batchUpdate(Key.ROOT).blockingAwait();
MatcherAssert.assertThat(
countData(tmp),
new IsEqual<>(2)
);
}
@Test
void doesntBrakeMetadataWhenInvalidPackageSentOnIncrementalUpdate(@TempDir final Path tmp)
throws Exception {
final Storage storage = new FileStorage(tmp);
final Rpm repo = new Rpm(storage, StandardNamingPolicy.SHA1, Digest.SHA256, true);
new TestRpm.Libdeflt().put(storage);
repo.batchUpdate(Key.ROOT).blockingAwait();
final byte[] broken = {0x00, 0x01, 0x02 };
storage.save(new Key.From("broken-file.rpm"), new Content.From(broken)).get();
new TestRpm.Abc().put(storage);
repo.batchUpdateIncrementally(Key.ROOT).blockingAwait();
MatcherAssert.assertThat(
countData(tmp),
new IsEqual<>(2)
);
}
@Test
@Disabled
void showMeaningfulErrorWhenInvalidPackageSent() throws Exception {
final Storage storage = new InMemoryStorage();
final Rpm repo = new Rpm(
storage, StandardNamingPolicy.SHA1, Digest.SHA256, true
);
new TestRpm.Multiple(
new TestRpm.Abc(),
new TestRpm.Libdeflt()
).put(storage);
repo.batchUpdate(Key.ROOT).blockingAwait();
new TestRpm.Invalid().put(storage);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> repo.batchUpdate(Key.ROOT).blockingAwait(),
"Reading of RPM package \"brokentwo.rpm\" failed, data corrupt or malformed."
);
}
private static int countData(final Path path) throws IOException {
final Path primary = path.resolve("primary.xml");
new Gzip(path.resolve(meta(path))).unpack(primary);
return Integer.parseInt(
new XMLDocument(new String(Files.readAllBytes(primary), StandardCharsets.UTF_8))
.xpath("count(/*[local-name()='metadata']/*[local-name()='package'])")
.get(0)
);
}
/**
* Searches for the meta file by substring in folder.
* @param dir Where to look for the file
* @return Path to find
* @throws IOException On error
*/
private static Path meta(final Path dir) throws IOException {
try (Stream<Path> walk = Files.walk(dir)) {
final Optional<Path> res = walk
.filter(
path -> path.getFileName().toString().endsWith("primary.xml.gz")
).findFirst();
if (res.isPresent()) {
return res.get();
} else {
throw new IllegalStateException(
String.format("Metafile %s does not exists in %s", "primary", dir.toString())
);
}
}
}
/**
* Checks that metadata are present.
* @param storage Storage
* @param repo Repodata key
*/
private static void metadataArePresent(final Storage storage, final Key repo) {
new XmlPackage.Stream(true).get().forEach(
item ->
MatcherAssert.assertThat(
String.format("Metadata %s is present", item.filename()),
storage.list(repo).join().stream()
.map(Key::string).filter(str -> str.contains(item.filename())).count(),
new IsEqual<>(1L)
)
);
}
/**
* Checks that repomd is present.
* @param storage Storage
* @param key Key
*/
private static void repomdIsPresent(final Storage storage, final Key key) {
MatcherAssert.assertThat(
"Repomd is present",
storage.list(key).join().stream()
.map(Key::string).filter(str -> str.contains("repomd")).count(),
new IsEqual<>(1L)
);
}
}