1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Antmicro.Renode.Storage;
10 using NUnit.Framework;
11 using System.IO;
12 using System.Linq;
13 using Antmicro.Renode.Utilities;
14 using Antmicro.Renode.Core;
15 
16 namespace Antmicro.Renode.UnitTests
17 {
18     [TestFixture]
19     public class StorageTests
20     {
StorageTests()21         public StorageTests()
22         {
23             random = EmulationManager.Instance.CurrentEmulation.RandomGenerator;
24         }
25 
26         [Test, Repeat(10)]
ShouldReadAndWriteLBABackend()27         public void ShouldReadAndWriteLBABackend()
28         {
29             var underlyingFile = TemporaryFilesManager.Instance.GetTemporaryFile();
30             try
31             {
32                 var blocksCount = random.Next(MaxBlocksCount + 1);
33                 using(var lbaBackend = new LBABackend(underlyingFile, blocksCount, BlockSize))
34                 {
35                     var testBlocksCount = Math.Min(DesiredTestBlocksCount, blocksCount);
36                     var blockPositions = Enumerable.Range(0, blocksCount).OrderBy(x => random.Next()).Take(testBlocksCount).ToArray();
37                     var blockContent = new byte[testBlocksCount][];
38                     for(var i = 0; i < testBlocksCount; i++)
39                     {
40                         blockContent[i] = new byte[BlockSize];
41                         random.NextBytes(blockContent[i]);
42                         lbaBackend.Write(blockPositions[i], blockContent[i], 1);
43                     }
44                     for(var i = 0; i < testBlocksCount; i++)
45                     {
46                         CollectionAssert.AreEqual(blockContent[i], lbaBackend.Read(blockPositions[i], 1));
47                     }
48                 }
49             }
50             finally
51             {
52                 File.Delete(underlyingFile);
53             }
54         }
55 
56         private readonly PseudorandomNumberGenerator random;
57         private const int BlockSize = 512;
58         private const int DesiredTestBlocksCount = 500;
59         private const int MaxBlocksCount = 1024 * 256;
60     }
61 }
62 
63