Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: daf3a5020cff2c932cc0969918b6c474430c7fdd (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009, 2017 Cloudsmith Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.core;

import java.io.*;
import org.eclipse.equinox.internal.p2.touchpoint.natives.BackupStore;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class BackupTest extends AbstractProvisioningTest {
	private static final String BUPREFIX = "BackupTest";
	private File sourceDir;
	private File aDir;
	private File aaDir;
	private File bDir;
	private File aTxt;
	private File bTxt;
	private File abDir;
	private File cTxt;
	private File cTxtRelative;

	/**
	 * Sets up directories and files under user.home
	 * <ul><li>P2BUTEST/</li>
	 *     <ul><li>A/</li>
	 *         <ul><li>AA/</li>
	 *             <ul><li>a.txt</li>
	 *                 <li>b.txt</li>
	 *             </ul>
	 *         </ul>
	 *         <li>B/</li>
	 *     </ul>
	 * </ul>
	 */
	@Override
	public void setUp() {
		// create some test files under user.home
		// do not want them under /tmp as it may be on its own file system (and even
		// be an in-memory file system).
		//
		String userHome = System.getProperty("user.home");
		sourceDir = new File(new File(userHome), "P2BUTEST");
		aDir = new File(sourceDir, "A");
		aDir.mkdirs();
		aaDir = new File(aDir, "AA");
		aaDir.mkdir();
		abDir = new File(aDir, "AB");
		abDir.mkdir();

		bDir = new File(sourceDir, "B");
		bDir.mkdirs();
		aTxt = new File(aaDir, "a.txt");
		bTxt = new File(aaDir, "b.txt");
		cTxt = new File(abDir, "c.txt");
		cTxtRelative = new File(aaDir, "../AB/c.txt");
		try {
			writeToFile(aTxt, "A\nA file with an A");
			writeToFile(bTxt, "B\nA file with a B");
			writeToFile(cTxt, "C\nA file with a C");
		} catch (IOException e) {
			fail();
		}

	}

	private void writeToFile(File file, String content) throws IOException {
		file.getParentFile().mkdirs();
		file.createNewFile();

		try (Writer writer = new BufferedWriter(new FileWriter(file))) {
			writer.write(content);
		}
	}

	@Override
	public void tearDown() {
		fullyDelete(sourceDir);
	}

	/**
	 * Deletes a file, or a directory with all of it's children.
	 * @param file the file or directory to fully delete
	 * @return true if, and only if the file is deleted
	 */
	private boolean fullyDelete(File file) {
		if (!file.exists())
			return true;
		if (file.isDirectory()) {
			File[] children = file.listFiles();
			for (File child : children) {
				if (!fullyDelete(new File(file, child.getName()))) {
					return false;
				}
			}
		}
		return file.delete();
	}

	/**
	 * Test that a path containing ".." can be backed up and restored.
	 */
	public void testBackupRelative() {
		BackupStore store = new BackupStore(null, BUPREFIX);
		// backup and overwrite a.txt
		try {
			store.backup(cTxtRelative);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up cTxtRelative");
		}
		if (cTxt.exists())
			fail("File not moved to backup - still exists");
		try {
			writeToFile(cTxt, "XXXX\n- This file should be restored with C");
		} catch (IOException e) {
			e.printStackTrace();
			fail("Could not write a file for testing purposes.");
		}

		// restore
		try {
			store.restore();
		} catch (IOException e) {
			e.printStackTrace();
			fail("Restore operation failed with IOException");
		}
		// assert restore
		assertFileContent("Restore of C failed - not original content", cTxt, "C");
		assertNoGarbage(store);
	}

	public void testBackupRestore() {
		BackupStore store = new BackupStore(null, BUPREFIX);
		// backup and overwrite a.txt
		try {
			store.backup(aTxt);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up aTxt");
		}
		if (aTxt.exists())
			fail("File not moved to backup - still exists");
		try {
			writeToFile(aTxt, "XXXX\n- This file should be restored with A");
		} catch (IOException e) {
			e.printStackTrace();
			fail("Could not write a file for testing purposes.");
		}

		// backup the empty B directory
		try {
			store.backup(bDir);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up bDir");
		}
		if (bDir.exists())
			fail("Backed up directory was not moved");

		// backup b as a copy
		try {
			store.backupCopy(bTxt);
			assertFileContent("File should have been copied", bTxt, "B");
		} catch (IOException e) {
			fail("Could not backupCopy bTxt");
		}

		// restore
		try {
			store.restore();
		} catch (IOException e) {
			e.printStackTrace();
			fail("Restore operation failed with IOException");
		}

		// assert restore
		assertFileContent("Restore of A failed - not original content", aTxt, "A");
		if (!bDir.isDirectory() && bDir.listFiles().length != 0)
			fail("Empty directory not restored ok");

		assertNoGarbage(store);
	}

	public void testBackupDiscard() {
		BackupStore store = new BackupStore(null, BUPREFIX);
		// backup and overwrite a.txt
		try {
			store.backup(aTxt);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up aTxt");
		}
		if (aTxt.exists())
			fail("File not moved to backup - still exists");
		try {
			writeToFile(aTxt, "XXXX\n- This file should be restored with A");
		} catch (IOException e) {
			e.printStackTrace();
			fail("Could not write a file for testing purposes.");
		}

		// backup the empty B directory
		try {
			store.backup(bDir);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up bDir");
		}
		if (bDir.exists())
			fail("Backed up directory was not moved");

		// restore
		store.discard();

		// assert discard
		assertFileContent("Discard of A failed - not new content", aTxt, "XXXX");
		if (bDir.isDirectory())
			fail("Remove of empty directory not discarded ok");

		assertNoGarbage(store);
	}

	public void testBackupAll() {
		BackupStore store = new BackupStore(null, BUPREFIX);
		// backup and overwrite a.txt
		try {
			store.backupAll(aDir);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up aDir");
		}
		if (aTxt.exists())
			fail("File not moved to backup - still exists");
		if (bTxt.exists())
			fail("File bTxt not moved to backup - still exists");

		try {
			writeToFile(aTxt, "XXXX\n- This file should be restored with A");
		} catch (IOException e) {
			e.printStackTrace();
			fail("Could not write a file for testing purposes.");
		}
		try {
			store.restore();
		} catch (IOException e) {
			fail("Restore failed");
		}
		assertFileContent("A not restored", aTxt, "A");
		assertFileContent("B not restored", bTxt, "B");
		assertNoGarbage(store);
	}

	public void testBackupCopyAll() {
		BackupStore store = new BackupStore(null, BUPREFIX);
		// backup and overwrite a.txt
		try {
			store.backupCopyAll(aDir);
		} catch (IOException e) {
			e.printStackTrace();
			fail("IO Exception when backing up aDir");
		}
		if (!aTxt.exists())
			fail("File not copied to backup - does not exist");
		if (!bTxt.exists())
			fail("File bTxt not copied to backup - does not exists");

		try {
			writeToFile(aTxt, "XXXX\n- This file should be restored with A");
			writeToFile(bTxt, "XXXX\n- This file should be restored with B");
		} catch (IOException e) {
			e.printStackTrace();
			fail("Could not write a file for testing purposes.");
		}
		try {
			store.restore();
		} catch (IOException e) {
			fail("Restore failed");
		}
		assertFileContent("A not restored", aTxt, "A");
		assertFileContent("B not restored", bTxt, "B");
		assertNoGarbage(store);
	}

	private void assertNoGarbage(BackupStore store) {
		File buDir = new File(store.getBackupRoot(), BUPREFIX);
		if (buDir.exists())
			fail("Backup directory not cleaned up");

		//		Set roots = store.getBackupRoots();
		//		if (roots.size() == 0)
		//			assertTrue("Root set is empty", true);
		//		for (Iterator itor = roots.iterator(); itor.hasNext();) {
		//			File root = (File) itor.next();
		//			File buDir = new File(root, BUPREFIX);
		//			if (buDir.exists())
		//				fail("Backup directory not cleaned up");
		//		}
	}
}

Back to the top