1 // 2 // Copyright (c) 2010-2025 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 NUnit.Framework; 10 using Antmicro.Renode.UserInterface; 11 using System.Diagnostics; 12 using System.Linq; 13 using Antmicro.Renode.Core; 14 using Antmicro.Renode.Peripherals.Bus; 15 using Antmicro.Renode.Peripherals; 16 using System.Text; 17 using System.IO; 18 using System.Collections; 19 using Antmicro.Renode.Utilities; 20 using System.Text.RegularExpressions; 21 using Antmicro.Renode.Peripherals.Memory; 22 23 using Range = Antmicro.Renode.Core.Range; 24 25 namespace Antmicro.Renode.MonitorTests.CommandTests 26 { 27 [TestFixture] 28 public class PythonCommands 29 { 30 [Test] NextValueTest()31 public void NextValueTest() 32 { 33 monitor.Parse("next_value", commandEater); 34 var value = int.Parse(commandEater.GetContents().TrimEnd('\r', '\n')); 35 commandEater.Clear(); 36 monitor.Parse("next_value", commandEater); 37 var next_value = int.Parse(commandEater.GetContents().TrimEnd('\r', '\n')); 38 Assert.AreEqual(1, next_value - value); 39 } 40 41 [Test] SleepTest()42 public void SleepTest() 43 { 44 var stopwatch = new Stopwatch(); 45 //to ensure early loading of python commands 46 monitor.Parse("sleep 1", commandEater); 47 stopwatch.Start(); 48 monitor.Parse("sleep 5", commandEater); 49 stopwatch.Stop(); 50 Assert.GreaterOrEqual(stopwatch.Elapsed.Seconds, 4); 51 Assert.LessOrEqual(stopwatch.Elapsed.Seconds, 6); 52 } 53 54 [Test] EchoTest()55 public void EchoTest() 56 { 57 var text = "test text with stuff"; //it wont accept \n 58 monitor.Parse(String.Format("echo \"{0}\"", text), commandEater); 59 Assert.AreEqual(text, commandEater.GetContents().TrimEnd('\r', '\n')); 60 } 61 62 [Test] DumpTest()63 public void DumpTest() 64 { 65 66 BuildEmulation(); 67 const string message = "MAGIC MESSAGE"; 68 const uint address = MemoryOffset; 69 var bytes = Encoding.ASCII.GetBytes(message); 70 machine.SystemBus.ZeroRange(new Range(MemoryOffset, MemoryOffset)); 71 machine.SystemBus.WriteBytes(bytes, address); 72 monitor.Parse(String.Format("dump {0} {1}", address, bytes.Length), commandEater); 73 var result = commandEater.GetContents(); 74 var splitResult = result.Split(new[]{ '|' }, StringSplitOptions.RemoveEmptyEntries); 75 var index = -1; 76 var bytesIndex = 0; 77 foreach(var resultElement in splitResult) 78 { 79 index = (index + 1) % 3; 80 if(index == 0 || index == 2) 81 { 82 continue; 83 } 84 //we're in correct element of the output 85 var splitBytes = resultElement.Split(new[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries); 86 foreach(var resultByte in splitBytes.Select(x=>int.Parse(x,System.Globalization.NumberStyles.HexNumber))) 87 { 88 if(bytesIndex >= bytes.Length) 89 { 90 Assert.AreEqual(0, resultByte); 91 } 92 else 93 { 94 Assert.AreEqual(bytes[bytesIndex], resultByte); 95 } 96 bytesIndex++; 97 } 98 } 99 Assert.GreaterOrEqual(bytesIndex, bytes.Length); 100 101 } 102 103 [Test] DumpFileTest()104 public void DumpFileTest() 105 { 106 BuildEmulation(); 107 const string message = "MAGIC MESSAGE"; 108 const uint address = MemoryOffset; 109 var bytes = Encoding.ASCII.GetBytes(message); 110 machine.SystemBus.WriteBytes(bytes, address); 111 var file = TemporaryFilesManager.Instance.GetTemporaryFile(); 112 try 113 { 114 monitor.Parse(String.Format("dump_file {0} {1} @{2}", address, bytes.Length, file), commandEater); 115 using(var tmpFile = new StreamReader(file)) 116 { 117 var value = tmpFile.ReadToEnd(); 118 Assert.AreEqual(message, value); 119 } 120 } 121 catch(Exception e) 122 { 123 Assert.Fail(e.Message); 124 } 125 finally 126 { 127 File.Delete(file); 128 } 129 } 130 BuildEmulation()131 private void BuildEmulation() 132 { 133 monitor.Parse("mach create", commandEater); 134 machine = EmulationManager.Instance.CurrentEmulation.Machines.First() as Machine; 135 machine.SystemBus.Register(new MappedMemory(machine, 0x1000), new BusPointRegistration(MemoryOffset)); 136 } 137 138 [OneTimeSetUp] SetUp()139 public void SetUp() 140 { 141 monitor = new Monitor(); 142 commandEater = new CommandInteractionEater(); 143 } 144 145 [TearDown] TearDown()146 public void TearDown() 147 { 148 EmulationManager.Instance.Clear(); 149 commandEater.Clear(); 150 } 151 152 private const uint MemoryOffset = 0x1000; 153 private CommandInteractionEater commandEater; 154 private Monitor monitor; 155 private IMachine machine; 156 } 157 } 158 159