This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
TestConfigUtil.java
336 lines (262 loc) · 12.1 KB
/
TestConfigUtil.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
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
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.tests.util;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.simpleframework.xml.core.Persister;
import org.syncany.chunk.Chunker;
import org.syncany.chunk.CipherTransformer;
import org.syncany.chunk.GzipTransformer;
import org.syncany.chunk.ZipMultiChunker;
import org.syncany.config.Config;
import org.syncany.config.UserConfig;
import org.syncany.config.to.ConfigTO;
import org.syncany.config.to.ConfigTO.ConnectionTO;
import org.syncany.config.to.RepoTO;
import org.syncany.config.to.RepoTO.ChunkerTO;
import org.syncany.config.to.RepoTO.MultiChunkerTO;
import org.syncany.config.to.RepoTO.TransformerTO;
import org.syncany.crypto.CipherSpecs;
import org.syncany.crypto.CipherUtil;
import org.syncany.crypto.SaltedSecretKey;
import org.syncany.operations.init.InitOperationOptions;
import org.syncany.plugins.Plugins;
import org.syncany.plugins.local.LocalConnection;
import org.syncany.plugins.transfer.TransferPlugin;
import org.syncany.plugins.transfer.TransferSettings;
import org.syncany.plugins.unreliable_local.UnreliableLocalConnection;
import org.syncany.plugins.unreliable_local.UnreliableLocalPlugin;
import org.syncany.util.StringUtil;
import com.google.common.collect.Lists;
public class TestConfigUtil {
private static final String RUNDATE = new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date());
private static boolean cryptoEnabled = false;
private static SaltedSecretKey masterKey = null;
static {
try {
UserConfig.init(); // Load userconfig (include system properties, e.g. org.syncany.test.tmpdir)
TestConfigUtil.cryptoEnabled = Boolean.parseBoolean(System.getProperty("crypto.enable"));
}
catch (Exception e) {
TestConfigUtil.cryptoEnabled = false;
}
}
public static Map<String, String> createTestLocalConnectionSettings() throws Exception {
Map<String, String> pluginSettings = new HashMap<String, String>();
File tempRepoDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("repo", new Random().nextFloat()));
pluginSettings.put("path", tempRepoDir.getAbsolutePath());
return pluginSettings;
}
public static Config createTestLocalConfig() throws Exception {
return createTestLocalConfig("syncanyclient");
}
public static Config createTestLocalConfig(String machineName) throws Exception {
return createTestLocalConfig(machineName, createTestLocalConnection());
}
public static MultiChunkerTO createZipMultiChunkerTO() {
Map<String, String> settings = new HashMap<String, String>();
settings.put(ZipMultiChunker.PROPERTY_SIZE, "4096");
MultiChunkerTO multiChunkerTO = new MultiChunkerTO();
multiChunkerTO.setType(ZipMultiChunker.TYPE);
multiChunkerTO.setSettings(settings);
return multiChunkerTO;
}
public static ChunkerTO createFixedChunkerTO() {
Map<String, String> settings = new HashMap<String, String>();
settings.put(Chunker.PROPERTY_SIZE, "32768");
ChunkerTO chunkerTO = new ChunkerTO();
chunkerTO.setType("fixed");
chunkerTO.setSettings(settings);
return chunkerTO;
}
public static RepoTO createRepoTO() {
// Create Repo TO
RepoTO repoTO = new RepoTO();
repoTO.setRepoId(new byte[] { 0x01, 0x02, 0x03 });
// Create ChunkerTO and MultiChunkerTO
MultiChunkerTO multiChunkerTO = createZipMultiChunkerTO();
ChunkerTO chunkerTO = createFixedChunkerTO();
repoTO.setChunkerTO(chunkerTO); // TODO [low] Chunker not configurable right now. Not used.
repoTO.setMultiChunker(multiChunkerTO);
// Create TransformerTO
List<TransformerTO> transformerTOs = createTransformerTOs();
repoTO.setTransformers(transformerTOs);
return repoTO;
}
public static List<TransformerTO> createTransformerTOs() {
if (!cryptoEnabled) {
return null;
}
else {
TransformerTO gzipTransformerTO = new TransformerTO();
gzipTransformerTO.setType(GzipTransformer.TYPE);
Map<String, String> cipherTransformerSettings = new HashMap<String, String>();
cipherTransformerSettings.put(CipherTransformer.PROPERTY_CIPHER_SPECS, "1,2");
TransformerTO cipherTransformerTO = new TransformerTO();
cipherTransformerTO.setType(CipherTransformer.TYPE);
cipherTransformerTO.setSettings(cipherTransformerSettings);
return Lists.newArrayList(gzipTransformerTO, cipherTransformerTO);
}
}
private static SaltedSecretKey getMasterKey() throws Exception {
if (!cryptoEnabled) {
return null;
}
else {
if (masterKey == null) {
masterKey = CipherUtil.createMasterKey("some password");
}
return masterKey;
}
}
public static Config createDummyConfig() throws Exception {
ConfigTO configTO = new ConfigTO();
configTO.setMachineName("dummymachine");
RepoTO repoTO = new RepoTO();
repoTO.setTransformers(null);
repoTO.setChunkerTO(createFixedChunkerTO());
repoTO.setMultiChunker(createZipMultiChunkerTO());
return new Config(new File("/dummy"), configTO, repoTO);
}
public static Config createTestLocalConfig(String machineName, TransferSettings connection) throws Exception {
File tempLocalDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("client-" + machineName, connection));
tempLocalDir.mkdirs();
RepoTO repoTO = createRepoTO();
// Create config TO
ConfigTO configTO = new ConfigTO();
configTO.setMachineName(machineName + CipherUtil.createRandomAlphabeticString(20));
// Get Masterkey
SaltedSecretKey masterKey = getMasterKey();
configTO.setMasterKey(masterKey);
LocalConnection localConnection = (LocalConnection) connection;
// Create connection TO
Map<String, String> localConnectionSettings = new HashMap<String, String>();
localConnectionSettings.put("path", localConnection.getRepositoryPath().getAbsolutePath());
ConnectionTO connectionTO = new ConnectionTO();
if (connection instanceof UnreliableLocalConnection) { // Dirty hack
UnreliableLocalConnection unreliableConnection = (UnreliableLocalConnection) connection;
String failingPatterns = StringUtil.join(unreliableConnection.getFailingOperationPatterns(), ",");
localConnectionSettings.put("patterns", failingPatterns);
connectionTO.setType("unreliable_local");
connectionTO.setSettings(localConnectionSettings);
}
else {
connectionTO.setType("local");
connectionTO.setSettings(localConnectionSettings);
}
configTO.setConnectionTO(connectionTO);
// Create
Config config = new Config(tempLocalDir, configTO, repoTO);
config.setConnection(connection);
config.getAppDir().mkdirs();
config.getCacheDir().mkdirs();
config.getDatabaseDir().mkdirs();
config.getLogDir().mkdirs();
config.getStateDir().mkdirs();
// Write to config folder (required for some tests)
new Persister().write(configTO, new File(config.getAppDir()+"/"+Config.FILE_CONFIG));
new Persister().write(repoTO, new File(config.getAppDir()+"/"+Config.FILE_REPO));
return config;
}
public static InitOperationOptions createTestInitOperationOptions(String machineName) throws Exception {
File tempLocalDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("client-" + machineName, machineName));
File tempRepoDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("repo", machineName));
tempLocalDir.mkdirs();
tempRepoDir.mkdirs();
RepoTO repoTO = createRepoTO();
// Create config TO
ConfigTO configTO = new ConfigTO();
configTO.setMachineName(machineName + Math.abs(new Random().nextInt()));
// Get Masterkey
SaltedSecretKey masterKey = getMasterKey();
configTO.setMasterKey(masterKey);
// Create connection TO
Map<String, String> localConnectionSettings = new HashMap<String, String>();
localConnectionSettings.put("path", tempRepoDir.getAbsolutePath());
ConnectionTO connectionTO = new ConnectionTO();
connectionTO.setType("local");
connectionTO.setSettings(localConnectionSettings);
configTO.setConnectionTO(connectionTO);
InitOperationOptions operationOptions = new InitOperationOptions();
operationOptions.setLocalDir(tempLocalDir);
operationOptions.setConfigTO(configTO);
operationOptions.setRepoTO(repoTO);
operationOptions.setEncryptionEnabled(cryptoEnabled);
operationOptions.setCipherSpecs(CipherSpecs.getDefaultCipherSpecs());
operationOptions.setPassword(cryptoEnabled ? "some password" : null);
return operationOptions;
}
public static InitOperationOptions createTestUnreliableInitOperationOptions(String machineName, String patterns) throws Exception {
InitOperationOptions initOperationOptions = createTestInitOperationOptions(machineName);
initOperationOptions.getConfigTO().getConnectionTO().setType("unreliable_local");
initOperationOptions.getConfigTO().getConnectionTO().getSettings().put("patterns", patterns);
return initOperationOptions;
}
public static TransferSettings createTestLocalConnection() throws Exception {
TransferPlugin plugin = Plugins.get("local", TransferPlugin.class);
TransferSettings conn = plugin.createSettings();
File tempRepoDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("repo", conn));
Map<String, String> pluginSettings = new HashMap<String, String>();
pluginSettings.put("path", tempRepoDir.getAbsolutePath());
conn.init(pluginSettings);
plugin.createTransferManager(conn).init(true);
return conn;
}
public static UnreliableLocalConnection createTestUnreliableLocalConnection(List<String> failingOperationPatterns) throws Exception {
UnreliableLocalPlugin unreliableLocalPlugin = new UnreliableLocalPlugin();
UnreliableLocalConnection unreliableLocalConnection = createTestUnreliableLocalConnectionWithoutInit(unreliableLocalPlugin, failingOperationPatterns);
unreliableLocalPlugin.createTransferManager(unreliableLocalConnection).init(true);
return unreliableLocalConnection;
}
public static UnreliableLocalConnection createTestUnreliableLocalConnectionWithoutInit(UnreliableLocalPlugin unreliableLocalPlugin, List<String> failingOperationPatterns) throws Exception {
UnreliableLocalConnection unreliableLocalConnection = (UnreliableLocalConnection) unreliableLocalPlugin.createSettings();
File tempRepoDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("repo", new Random().nextFloat()));
unreliableLocalConnection.setRepositoryPath(tempRepoDir);
unreliableLocalConnection.setFailingOperationPatterns(failingOperationPatterns);
return unreliableLocalConnection;
}
public static void deleteTestLocalConfigAndData(Config config) {
TestFileUtil.deleteDirectory(config.getLocalDir());
TestFileUtil.deleteDirectory(config.getCacheDir());
TestFileUtil.deleteDirectory(config.getDatabaseDir());
if (config.getAppDir() != null) {
TestFileUtil.deleteDirectory(config.getAppDir());
}
// TODO [low] workaround: delete empty parent folder of getAppDir() --> ROOT/app/.. --> ROOT/
config.getLocalDir().getParentFile().delete(); // if empty!
deleteTestLocalConnection(config);
}
private static void deleteTestLocalConnection(Config config) {
LocalConnection connection = (LocalConnection) config.getConnection();
TestFileUtil.deleteDirectory(connection.getRepositoryPath());
}
public static String createUniqueName(String name, Object uniqueHashObj) {
return String.format("syncany-%s-%d-%s", RUNDATE, 10000 + uniqueHashObj.hashCode() % 89999, name);
}
public static void setCrypto(boolean cryptoEnabled) {
TestConfigUtil.cryptoEnabled = cryptoEnabled;
}
public static boolean getCrypto() {
return cryptoEnabled;
}
}