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 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
//
// MemoryMappedFileTest.cs
//
// Author:
// Zoltan Varga (vargaz@gmail.com)
//
// (C) 2009 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_0
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using NUnit.Framework;
namespace MonoTests.System.IO.MemoryMappedFiles {
[TestFixture]
public class MemoryMappedFileTest {
void AssertThrows<ExType> (Action del) where ExType : Exception {
bool thrown = false;
try {
del ();
} catch (ExType) {
thrown = true;
}
Assert.IsTrue (thrown);
}
static int named_index;
static String MkNamedMapping ()
{
return "test-" + named_index++;
}
static string tempDir = Path.Combine (Path.GetTempPath (), typeof (MemoryMappedFileTest).FullName);
string fname;
[SetUp]
protected void SetUp () {
if (Directory.Exists (tempDir))
Directory.Delete (tempDir, true);
Directory.CreateDirectory (tempDir);
fname = Path.Combine (tempDir, "basic.txt");
using (StreamWriter sw = new StreamWriter (fname)) {
sw.WriteLine ("Hello!");
sw.WriteLine ("World!");
}
}
[TearDown]
protected void TearDown () {
if (Directory.Exists (tempDir))
Directory.Delete (tempDir, true);
}
[Test]
public void Basic () {
var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
using (var stream = file.CreateViewStream ()) {
TextReader r = new StreamReader (stream);
string s;
s = r.ReadLine ();
Assert.AreEqual ("Hello!", s);
s = r.ReadLine ();
Assert.AreEqual ("World!", s);
}
}
[Test]
public void CreateNew ()
{
// This must succeed
MemoryMappedFile.CreateNew (Path.Combine (tempDir, "createNew.test"), 8192);
}
[Test]
[ExpectedException (typeof (IOException))]
public void CreateNew_OnExistingFile ()
{
// This must succeed
MemoryMappedFile.CreateNew (Path.Combine (tempDir, "createNew.test"), 8192);
// This should fail, the file exists
MemoryMappedFile.CreateNew (Path.Combine (tempDir, "createNew.test"), 8192);
}
// Call this twice, it should always work
[Test]
public void CreateOrOpen_Multiple ()
{
MemoryMappedFile.CreateOrOpen (Path.Combine (tempDir, "createOrOpen.test"), 8192);
MemoryMappedFile.CreateOrOpen (Path.Combine (tempDir, "createOrOpen.test"), 8192);
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CreateFromFileWithSmallerCapacityThanFile ()
{
var f = Path.Combine (tempDir, "8192-file");
File.WriteAllBytes (f, new byte [8192]);
// We are requesting fewer bytes to map.
MemoryMappedFile.CreateFromFile (f, FileMode.Open, "myMap", 4192);
}
[Test]
public void CreateFromFile_Null () {
AssertThrows<ArgumentNullException> (delegate () {
MemoryMappedFile.CreateFromFile (null);
});
}
[Test]
public void CreateViewStream_Offsets () {
var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
using (var stream = file.CreateViewStream (2, 3)) {
byte[] arr = new byte [128];
int len = stream.Read (arr, 0, 128);
Assert.AreEqual (3, len);
Assert.AreEqual ('l', (char)arr [0]);
Assert.AreEqual ('l', (char)arr [1]);
Assert.AreEqual ('o', (char)arr [2]);
}
}
[Test]
public void CreateViewStream_Rights () {
var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
using (var stream = file.CreateViewStream (0, 0, MemoryMappedFileAccess.Read)) {
AssertThrows<NotSupportedException> (delegate () {
stream.WriteByte (0);
});
}
using (var stream = file.CreateViewStream (0, 0, MemoryMappedFileAccess.Write)) {
AssertThrows<NotSupportedException> (delegate () {
stream.ReadByte ();
});
}
}
[Test]
public unsafe void CreateViewBasic () {
var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
using (var v = file.CreateViewAccessor ()) {
string s = "";
// FIXME: Use using
var handle = v.SafeMemoryMappedViewHandle;
byte *b = null;
try {
handle.AcquirePointer (ref b);
for (int i = 0; i < 5; ++i)
s += (char)b [i];
} finally {
handle.ReleasePointer ();
}
Assert.AreEqual ("Hello", s);
}
}
[Test]
public unsafe void ViewReadArray () {
var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
using (var v = file.CreateViewAccessor ()) {
var a = new byte [5];
var n = v.ReadArray (0, a, 0, 5);
Assert.AreEqual (5, n);
var s = new string (Array.ConvertAll (a, b => (char)b));
Assert.AreEqual ("Hello", s);
}
}
[Test]
public void NamedMappingToInvalidFile ()
{
var fileName = Path.Combine (tempDir, "temp_file_123");
if (File.Exists (fileName))
File.Delete (fileName);
var memoryMappedFile90 = MemoryMappedFile.CreateNew (fileName, 4194304, MemoryMappedFileAccess.ReadWrite);
memoryMappedFile90.CreateViewStream (4186112, 3222, MemoryMappedFileAccess.Write);
}
[Test]
public void CreateTheSameAreaTwiceShouldFail ()
{
var name = MkNamedMapping ();
using (var m0 = MemoryMappedFile.CreateNew(name, 4096, MemoryMappedFileAccess.ReadWrite)) {
try {
using (var m1 = MemoryMappedFile.CreateNew (name, 4096, MemoryMappedFileAccess.ReadWrite)) {
Assert.Fail ("Must fail");
}
} catch (IOException) {}
}
}
[Test]
public void MapAFileToAMemoryAreaShouldFail ()
{
var name = MkNamedMapping ();
using (var m0 = MemoryMappedFile.CreateNew(name, 4096, MemoryMappedFileAccess.ReadWrite)) {
try {
using (var m1 = MemoryMappedFile.CreateFromFile (fname, FileMode.OpenOrCreate, name)) {
Assert.Fail ("Must fail");
}
} catch (IOException) {}
}
}
[Test]
public void NamedMappingsShareMemoryArea ()
{
var name = MkNamedMapping ();
using (var m0 = MemoryMappedFile.CreateNew(name, 4096, MemoryMappedFileAccess.ReadWrite)) {
using (var m1 = MemoryMappedFile.CreateOrOpen (name, 4096, MemoryMappedFileAccess.ReadWrite)) {
using (MemoryMappedViewAccessor v0 = m0.CreateViewAccessor (), v1 = m1.CreateViewAccessor ()) {
v0.Write (10, 0x12345);
Assert.AreEqual (0x12345, v1.ReadInt32 (10));
}
}
}
}
[Test]
public void NamedFileCanBeOpen ()
{
var name = MkNamedMapping ();
using (var sw = new FileStream (fname, FileMode.Open)) {
byte[] b = new byte[20];
for (int i = 0; i < 20; ++i)
b[i] = 0xFF;
sw.Write (b, 0, 20);
}
using (var m0 = MemoryMappedFile.CreateFromFile (fname, FileMode.Open, name)) {
using (var m1 = MemoryMappedFile.CreateOrOpen (name, 4096)) {
using (MemoryMappedViewAccessor v0 = m0.CreateViewAccessor (), v1 = m1.CreateViewAccessor ()) {
v0.Write (10, 0x11223344);
Assert.AreEqual (0x11223344, v1.ReadInt32 (10));
}
}
}
}
[Test]
public void MapAtEdgeOfPage ()
{
using (var f = new FileStream (fname, FileMode.Open)) {
var b = new byte [4096];
for (int i = 0; i < 4096; ++i)
b[i] = 0xAA;
for (int i = 0; i < 2; ++i)
f.Write (b, 0, 4096);
}
var m0 = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
var v0 = m0.CreateViewAccessor (500, 4096);
var v1 = m0.CreateViewAccessor (0, 4096 * 2);
for (int i = 0; i < 4096; ++i) {
Assert.AreEqual (0xAA, v1.ReadByte (i + 500));
v0.Write (i, (byte)0xFF);
Assert.AreEqual (0xFF, v1.ReadByte (i + 500));
}
}
[Test]
public void DoubleAccountingInOffsetCalculation ()
{
var memoryMappedFile90 = MemoryMappedFile.CreateNew (MkNamedMapping (), 4194304, MemoryMappedFileAccess.ReadWrite);
var stream = memoryMappedFile90.CreateViewStream (4186112, 3222, MemoryMappedFileAccess.Write);
using (var tw = new StreamWriter(stream))
{
tw.WriteLine ("Hello World!");
}
}
}
}
#endif
|