From f3db3d6a001abdca53d88def7a22a40d6aa30271 Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Thu, 7 May 2009 06:31:16 +0000 Subject: * Added some more GetAssetStreamHandlerTests * In the process, caught a potential bug where the handler would allow paths not starting with the registered prefix --- .../Servers/HttpServer/BaseRequestHandler.cs | 14 ++++++-- .../Servers/Tests/GetAssetStreamHandlerTests.cs | 42 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-) (limited to 'OpenSim/Framework/Servers') diff --git a/OpenSim/Framework/Servers/HttpServer/BaseRequestHandler.cs b/OpenSim/Framework/Servers/HttpServer/BaseRequestHandler.cs index 5ad4520..da4f9a8 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseRequestHandler.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseRequestHandler.cs @@ -58,14 +58,22 @@ namespace OpenSim.Framework.Servers.HttpServer public string GetParam(string path) { - try + if (CheckParam(path)) { return path.Substring(m_path.Length); } - catch (Exception) + + return String.Empty; + } + + protected bool CheckParam(string path) + { + if(String.IsNullOrEmpty(path)) { - return String.Empty; + return false; } + + return path.StartsWith(Path); } } } diff --git a/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs b/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs index d399274..0b70cb1 100644 --- a/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs +++ b/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs @@ -2,12 +2,13 @@ using System.Collections.Generic; using System.Text; using NUnit.Framework; +using OpenSim.Data; namespace OpenSim.Framework.Servers.Tests { [TestFixture] public class GetAssetStreamHandlerTests - { + { [Test] public void TestConstructor() { @@ -19,6 +20,11 @@ namespace OpenSim.Framework.Servers.Tests { GetAssetStreamHandler handler = new GetAssetStreamHandler(null); + Assert.AreEqual("", handler.GetParam(null), "Failed on null path."); + Assert.AreEqual("", handler.GetParam(""), "Failed on empty path."); + Assert.AreEqual("", handler.GetParam("s"), "Failed on short url."); + Assert.AreEqual("", handler.GetParam("corruptUrl"), "Failed on corruptUrl."); + Assert.AreEqual("", handler.GetParam("/assets")); Assert.AreEqual("/", handler.GetParam("/assets/")); Assert.AreEqual("/a", handler.GetParam("/assets/a")); @@ -32,11 +38,45 @@ namespace OpenSim.Framework.Servers.Tests { GetAssetStreamHandler handler = new GetAssetStreamHandler(null); + Assert.AreEqual(new string[] { }, handler.SplitParams(null), "Failed on null."); + Assert.AreEqual(new string[] { }, handler.SplitParams(""), "Failed on empty path."); + Assert.AreEqual(new string[] { }, handler.SplitParams("corruptUrl"), "Failed on corrupt url."); + Assert.AreEqual(new string[] { }, handler.SplitParams("/assets"), "Failed on empty params."); Assert.AreEqual(new string[] { }, handler.SplitParams("/assets/"), "Failed on single slash."); Assert.AreEqual(new string[] { "a" }, handler.SplitParams("/assets/a"), "Failed on first segment."); Assert.AreEqual(new string[] { "b" }, handler.SplitParams("/assets/b/"), "Failed on second slash."); + Assert.AreEqual(new string[] { "c", "d" }, handler.SplitParams("/assets/c/d"), "Failed on second segment."); + Assert.AreEqual(new string[] { "e", "f" }, handler.SplitParams("/assets/e/f/"), "Failed on trailing slash."); + } + + [Test] + public void TestHandleNoParams() + { + byte[] emptyResult = new byte[] {}; + GetAssetStreamHandler handler = new GetAssetStreamHandler(null); + + Assert.AreEqual(new string[] { }, handler.Handle("/assets", null, null, null), "Failed on empty params."); + Assert.AreEqual(new string[] { }, handler.Handle("/assets/", null, null, null ), "Failed on single slash."); } + [Test] + public void TestHandleMalformedGuid() + { + byte[] emptyResult = new byte[] {}; + GetAssetStreamHandler handler = new GetAssetStreamHandler(null); + + Assert.AreEqual(new string[] {}, handler.Handle("/assets/badGuid", null, null, null), "Failed on bad guid."); + } + + //[Test] + //public void TestHandleFetchMissingAsset() + //{ + + // byte[] emptyResult = new byte[] { }; + // GetAssetStreamHandler handler = new GetAssetStreamHandler(null); + + // Assert.AreEqual(new string[] { }, handler.Handle("/assets/badGuid", null, null, null), "Failed on bad guid."); + //} } } -- cgit v1.1