|
| 1 | +using Lucene.Net.Attributes; |
| 2 | +using Lucene.Net.Util; |
| 3 | +using NUnit.Framework; |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using Directory = Lucene.Net.Store.Directory; |
| 7 | + |
| 8 | +namespace Lucene.Net.Replicator |
| 9 | +{ |
| 10 | + /* |
| 11 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 | + * contributor license agreements. See the NOTICE file distributed with |
| 13 | + * this work for additional information regarding copyright ownership. |
| 14 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 15 | + * (the "License"); you may not use this file except in compliance with |
| 16 | + * the License. You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, software |
| 21 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 22 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | + * See the License for the specific language governing permissions and |
| 24 | + * limitations under the License. |
| 25 | + */ |
| 26 | + |
| 27 | + // LUCENENET-specific: covers sessionId and source name validation in PerSessionDirectoryFactory |
| 28 | + // for the GetDirectory and CleanupSession entry points. |
| 29 | + [LuceneNetSpecific] |
| 30 | + public class PerSessionDirectoryFactoryTest : ReplicatorTestCase |
| 31 | + { |
| 32 | + private static readonly string[] InvalidPathComponents = |
| 33 | + { |
| 34 | + "../a", |
| 35 | + "..\\a", |
| 36 | + "/a", |
| 37 | + "C:\\folder", |
| 38 | + "subdir/segment", |
| 39 | + "subdir\\segment", |
| 40 | + "..", |
| 41 | + ".", |
| 42 | + "name\0extra", |
| 43 | + "", |
| 44 | + }; |
| 45 | + |
| 46 | + [Test] |
| 47 | + [TestCaseSource(nameof(InvalidPathComponents))] |
| 48 | + public void TestGetDirectoryRejectsInvalidSessionId(string sessionId) |
| 49 | + { |
| 50 | + DirectoryInfo workDir = CreateTempDir("perSessionFactoryGetSession"); |
| 51 | + PerSessionDirectoryFactory factory = new PerSessionDirectoryFactory(workDir.FullName); |
| 52 | + |
| 53 | + Assert.Throws<ArgumentException>(() => |
| 54 | + { |
| 55 | + using Directory _ = factory.GetDirectory(sessionId, "src"); |
| 56 | + }, $"GetDirectory should reject sessionId '{sessionId}'"); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + [TestCaseSource(nameof(InvalidPathComponents))] |
| 61 | + public void TestGetDirectoryRejectsInvalidSource(string source) |
| 62 | + { |
| 63 | + DirectoryInfo workDir = CreateTempDir("perSessionFactoryGetSource"); |
| 64 | + PerSessionDirectoryFactory factory = new PerSessionDirectoryFactory(workDir.FullName); |
| 65 | + |
| 66 | + Assert.Throws<ArgumentException>(() => |
| 67 | + { |
| 68 | + using Directory _ = factory.GetDirectory("session1", source); |
| 69 | + }, $"GetDirectory should reject source '{source}'"); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + [TestCaseSource(nameof(InvalidPathComponents))] |
| 74 | + public void TestCleanupSessionRejectsInvalidSessionId(string sessionId) |
| 75 | + { |
| 76 | + DirectoryInfo workDir = CreateTempDir("perSessionFactoryCleanup"); |
| 77 | + PerSessionDirectoryFactory factory = new PerSessionDirectoryFactory(workDir.FullName); |
| 78 | + |
| 79 | + Assert.Throws<ArgumentException>(() => |
| 80 | + { |
| 81 | + factory.CleanupSession(sessionId); |
| 82 | + }, $"CleanupSession should reject sessionId '{sessionId}'"); |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + public void TestCleanupSessionDoesNotTouchSiblingDirectory() |
| 87 | + { |
| 88 | + // A sessionId that resolves to a sibling of the workingDirectory must be rejected by |
| 89 | + // validation. The sibling directory and its contents must remain untouched. |
| 90 | + DirectoryInfo workDir = CreateTempDir("perSessionFactorySiblingWork"); |
| 91 | + DirectoryInfo sibling = CreateTempDir("perSessionFactorySibling"); |
| 92 | + string siblingFile = Path.Combine(sibling.FullName, "marker.txt"); |
| 93 | + File.WriteAllText(siblingFile, "untouched"); |
| 94 | + |
| 95 | + PerSessionDirectoryFactory factory = new PerSessionDirectoryFactory(workDir.FullName); |
| 96 | + // Path.GetRelativePath is not available on .NET Framework; compute manually. |
| 97 | + // Both temp dirs share the same parent, so the relative path is "../<siblingName>". |
| 98 | + string relative = Path.Combine("..", sibling.Name); |
| 99 | + |
| 100 | + Assert.Throws<ArgumentException>(() => factory.CleanupSession(relative)); |
| 101 | + assertTrue("sibling directory must still exist", System.IO.Directory.Exists(sibling.FullName)); |
| 102 | + assertTrue("sibling file must still exist", File.Exists(siblingFile)); |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void TestGetDirectoryAcceptsValidNames() |
| 107 | + { |
| 108 | + DirectoryInfo workDir = CreateTempDir("perSessionFactoryValid"); |
| 109 | + PerSessionDirectoryFactory factory = new PerSessionDirectoryFactory(workDir.FullName); |
| 110 | + |
| 111 | + using Directory dir = factory.GetDirectory("session-abc", "src"); |
| 112 | + assertNotNull(dir); |
| 113 | + |
| 114 | + string expected = Path.Combine(workDir.FullName, "session-abc", "src"); |
| 115 | + assertTrue("expected session directory to exist on disk", System.IO.Directory.Exists(expected)); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments