AssetServerPostHandlerTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. using System.Net;
  30. using System.Text;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using Nini.Config;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Server.Handlers.Asset;
  38. using OpenSim.Services.AssetService;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Tests.Common;
  41. using OpenSim.Tests.Common.Mock;
  42. namespace OpenSim.Server.Handlers.Asset.Test
  43. {
  44. [TestFixture]
  45. public class AssetServerPostHandlerTests : OpenSimTestCase
  46. {
  47. [Test]
  48. public void TestGoodAssetStoreRequest()
  49. {
  50. TestHelpers.InMethod();
  51. UUID assetId = TestHelpers.ParseTail(0x1);
  52. IConfigSource config = new IniConfigSource();
  53. config.AddConfig("AssetService");
  54. config.Configs["AssetService"].Set("StorageProvider", "OpenSim.Tests.Common.dll");
  55. AssetService assetService = new AssetService(config);
  56. AssetServerPostHandler asph = new AssetServerPostHandler(assetService);
  57. AssetBase asset = AssetHelpers.CreateNotecardAsset(assetId, "Hello World");
  58. MemoryStream buffer = new MemoryStream();
  59. XmlWriterSettings settings = new XmlWriterSettings();
  60. settings.Encoding = Encoding.UTF8;
  61. using (XmlWriter writer = XmlWriter.Create(buffer, settings))
  62. {
  63. XmlSerializer serializer = new XmlSerializer(typeof(AssetBase));
  64. serializer.Serialize(writer, asset);
  65. writer.Flush();
  66. }
  67. buffer.Position = 0;
  68. asph.Handle(null, buffer, null, null);
  69. AssetBase retrievedAsset = assetService.Get(assetId.ToString());
  70. Assert.That(retrievedAsset, Is.Not.Null);
  71. }
  72. [Test]
  73. public void TestBadXmlAssetStoreRequest()
  74. {
  75. TestHelpers.InMethod();
  76. IConfigSource config = new IniConfigSource();
  77. config.AddConfig("AssetService");
  78. config.Configs["AssetService"].Set("StorageProvider", "OpenSim.Tests.Common.dll");
  79. AssetService assetService = new AssetService(config);
  80. AssetServerPostHandler asph = new AssetServerPostHandler(assetService);
  81. MemoryStream buffer = new MemoryStream();
  82. byte[] badData = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f };
  83. buffer.Write(badData, 0, badData.Length);
  84. buffer.Position = 0;
  85. TestOSHttpResponse response = new TestOSHttpResponse();
  86. asph.Handle(null, buffer, null, response);
  87. Assert.That(response.StatusCode, Is.EqualTo((int)HttpStatusCode.BadRequest));
  88. }
  89. }
  90. }