View | Details | Raw Unified | Return to bug 328975
Collapse All | Expand All

(-)core/framework/org/eclipse/osgi/framework/internal/core/AbstractBundle.java (-2 / +38 lines)
Lines 1383-1393 Link Here
1383
		if (filePattern != null)
1383
		if (filePattern != null)
1384
			try {
1384
			try {
1385
				// create a file pattern filter with 'filename' as the key
1385
				// create a file pattern filter with 'filename' as the key
1386
				patternFilter = FilterImpl.newInstance("(filename=" + filePattern + ")"); //$NON-NLS-1$ //$NON-NLS-2$
1386
				patternFilter = FilterImpl.newInstance("(filename=" + sanitizeFilterInput(filePattern) + ")"); //$NON-NLS-1$ //$NON-NLS-2$
1387
				// create a single hashtable to be shared during the recursive search
1387
				// create a single hashtable to be shared during the recursive search
1388
				patternProps = new Hashtable(2);
1388
				patternProps = new Hashtable(2);
1389
			} catch (InvalidSyntaxException e) {
1389
			} catch (InvalidSyntaxException e) {
1390
				// cannot happen
1390
				// something unexpected happened; log error and return nothing
1391
				Bundle b = framework.systemBundle;
1392
				framework.publishFrameworkEvent(FrameworkEvent.ERROR, b, e);
1393
				return null;
1391
			}
1394
			}
1392
		// find the local entries of this bundle
1395
		// find the local entries of this bundle
1393
		findLocalEntryPaths(path, patternFilter, patternProps, recurse, pathList);
1396
		findLocalEntryPaths(path, patternFilter, patternProps, recurse, pathList);
Lines 1450-1455 Link Here
1450
		};
1453
		};
1451
	}
1454
	}
1452
1455
1456
	private String sanitizeFilterInput(String filePattern) throws InvalidSyntaxException {
1457
		if (filePattern.indexOf('\\') < 0 && filePattern.indexOf('(') < 0 && filePattern.indexOf(')') < 0)
1458
			return filePattern;
1459
		StringBuffer buffer = new StringBuffer(filePattern);
1460
		boolean foundEscape = false;
1461
		for (int i = 0; i < buffer.length(); i++) {
1462
			char c = buffer.charAt(i);
1463
			switch (c) {
1464
				case '\\' :
1465
					// we either used the escape found or found a new escape.
1466
					foundEscape = foundEscape ? false : true;
1467
					break;
1468
				case '(' :
1469
				case ')' :
1470
					if (!foundEscape) {
1471
						// must escape with '\'
1472
						buffer.insert(i, '\\');
1473
						i++;
1474
					} else {
1475
						foundEscape = false; // used the escape found
1476
					}
1477
					break;
1478
				default :
1479
					// if we found an escape it has been used
1480
					foundEscape = false;
1481
					break;
1482
			}
1483
		}
1484
		if (foundEscape)
1485
			throw new InvalidSyntaxException("Trailing escape characters must be escaped.", filePattern); //$NON-NLS-1$
1486
		return buffer.toString();
1487
	}
1488
1453
	protected void findLocalEntryPaths(String path, Filter patternFilter, Hashtable patternProps, boolean recurse, List pathList) {
1489
	protected void findLocalEntryPaths(String path, Filter patternFilter, Hashtable patternProps, boolean recurse, List pathList) {
1454
		Enumeration entryPaths = bundledata.getEntryPaths(path);
1490
		Enumeration entryPaths = bundledata.getEntryPaths(path);
1455
		if (entryPaths == null)
1491
		if (entryPaths == null)
(-)src/org/eclipse/osgi/tests/bundles/BundleResourceTests.java (+86 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.osgi.tests.bundles;
12
13
import java.util.Enumeration;
14
import junit.framework.Test;
15
import junit.framework.TestSuite;
16
import org.eclipse.core.tests.harness.CoreTest;
17
import org.eclipse.osgi.tests.OSGiTestsActivator;
18
import org.osgi.framework.*;
19
20
public class BundleResourceTests extends CoreTest {
21
	private BundleInstaller installer;
22
23
	protected void setUp() throws Exception {
24
		try {
25
			installer = new BundleInstaller(OSGiTestsActivator.TEST_FILES_ROOT + "resourcetests/bundles", OSGiTestsActivator.getContext()); //$NON-NLS-1$
26
		} catch (InvalidSyntaxException e) {
27
			fail("Failed to create bundle installer", e); //$NON-NLS-1$
28
		}
29
	}
30
31
	protected void tearDown() throws Exception {
32
		installer.shutdown();
33
	}
34
35
	public static Test suite() {
36
		return new TestSuite(BundleResourceTests.class);
37
	}
38
39
	public void testBug328795() throws BundleException {
40
		Bundle bundle = installer.installBundle("test"); //$NON-NLS-1$
41
		checkEntries(bundle, "notFound\\", 0); // this results in invalid syntax exception which is logged because of trailing escape
42
		checkEntries(bundle, "notFound\\\\", 0); // test escaped escape "notFound\"
43
		checkEntries(bundle, "notFound(", 0); // test unescaped trailing (
44
		checkEntries(bundle, "notFound\\(", 0); // test escaped trailing (
45
		checkEntries(bundle, "notFound)", 0); // test unescaped trailing )
46
		checkEntries(bundle, "notFound\\)", 0); // test escaped trailing )
47
		checkEntries(bundle, "notFound*", 0); // test trailing unescaped *
48
		checkEntries(bundle, "notFound\\*", 0); // test trailing escaped *
49
		checkEntries(bundle, "paren(.txt", 1); // test unescaped ( -> should find one
50
		checkEntries(bundle, "paren\\(.txt", 1); // test escaped ( -> should find one
51
		checkEntries(bundle, "paren\\\\(.txt", 0); // test escaped escape before unescaped ( -> should find none; looks for paren\(.txt file
52
		checkEntries(bundle, "paren).txt", 1); // test unescaped ) -> should find one
53
		checkEntries(bundle, "paren\\).txt", 1); // test escaped ) -> should find one
54
		checkEntries(bundle, "paren\\\\).txt", 0); // test escaped escape before unescaped ) -> should find none; looks for paren\).txt file
55
		checkEntries(bundle, "paren(", 1); // test unescaped trailing ( -> should find one
56
		checkEntries(bundle, "paren\\(", 1); // test escaped trailing ( -> should find one
57
		checkEntries(bundle, "paren\\\\(", 0); // test escaped escape before ( -> should find none; looks for paren\(
58
		checkEntries(bundle, "paren)", 1); // test unescaped trailing ( -> should find one
59
		checkEntries(bundle, "paren\\)", 1); // test escaped trailing ( -> should find one
60
		checkEntries(bundle, "paren\\\\)", 0); // test escaped escape before ) -> should find none; looks for paren\)
61
		checkEntries(bundle, "paren*", 4); // test trailing wild cards
62
		checkEntries(bundle, "paren*.txt", 2); // test middle wild cards
63
		checkEntries(bundle, "paren\\*", 0); // test escaped wild card -> should find none; looks for paren*
64
		checkEntries(bundle, "paren\\\\*", 0); // test escaped escape before wild card -> should find none; looks for paren\*
65
		checkEntries(bundle, "p*r*n*", 4); // test multiple wild cards
66
		checkEntries(bundle, "p*r*n*.txt", 2); // test multiple wild cards
67
		checkEntries(bundle, "*)*", 2);
68
		checkEntries(bundle, "*(*", 2);
69
		checkEntries(bundle, "*\\)*", 2);
70
		checkEntries(bundle, "*\\(*", 2);
71
	}
72
73
	private void checkEntries(Bundle bundle, String filePattern, int expectedNumber) {
74
		Enumeration entries = bundle.findEntries("folder", filePattern, false);
75
		if (expectedNumber == 0) {
76
			assertNull("Expected nothing here.", entries);
77
			return;
78
		}
79
		int i = 0;
80
		while (entries.hasMoreElements()) {
81
			entries.nextElement();
82
			i++;
83
		}
84
		assertEquals("Unexpected number of entries", expectedNumber, i);
85
	}
86
}
(-)src/org/eclipse/osgi/tests/bundles/BundleTests.java (+1 lines)
Lines 16-21 Link Here
16
public class BundleTests {
16
public class BundleTests {
17
	public static Test suite() {
17
	public static Test suite() {
18
		TestSuite suite = new TestSuite(BundleTests.class.getName());
18
		TestSuite suite = new TestSuite(BundleTests.class.getName());
19
		suite.addTest(BundleResourceTests.suite());
19
		suite.addTest(BundleInstallUpdateTests.suite());
20
		suite.addTest(BundleInstallUpdateTests.suite());
20
		suite.addTest(SystemBundleTests.suite());
21
		suite.addTest(SystemBundleTests.suite());
21
		suite.addTest(BundleExceptionTests.suite());
22
		suite.addTest(BundleExceptionTests.suite());

Return to bug 328975