private static void ReceivedAttach(TinyPacket packet) { try { var path = packet["Path"]; var isReadOnly = packet["MountReadOnly"].Equals("True", StringComparison.OrdinalIgnoreCase); var shouldInitialize = packet["InitializeDisk"].Equals("True", StringComparison.OrdinalIgnoreCase); string diskPath = null; using (var disk = new Medo.IO.VirtualDisk(path)) { var access = Medo.IO.VirtualDiskAccessMask.All; var options = Medo.IO.VirtualDiskAttachOptions.PermanentLifetime; if (isReadOnly) { if (shouldInitialize == false) { access = Medo.IO.VirtualDiskAccessMask.AttachReadOnly; } options |= Medo.IO.VirtualDiskAttachOptions.ReadOnly; } disk.Open(access); disk.Attach(options); if (shouldInitialize) { diskPath = disk.GetAttachedPath(); } } if (shouldInitialize) { DiskIO.InitializeDisk(diskPath); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Virtual disk file \"{0}\" cannot be attached.", (new FileInfo(packet["Path"])).Name), ex); } }
public void TinyPacket_Decode_MissingData_02() //it is an error state, but we shall recognize it. { TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); }
public void TinyPacket_NullItems() { var target = new TinyPacket("Example", "Test"); Assert.AreEqual(null, target["Key1Text"]); Assert.IsNotNull(target.GetEnumerator()); }
public void TinyPacket_Decode_Null() { TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test null ")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); }
public void TinyPacket_EncryptedWithoutIdentifiers() { var packet = new TinyPacket("Example", "Test"); var key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var bytesE1 = packet.GetBytes(key, omitIdentifiers: true); var bytesE2 = packet.GetBytes(key, omitIdentifiers: true); Assert.AreEqual("Tiny128 ", Encoding.UTF8.GetString(bytesE1, 0, 8)); Assert.AreEqual("Tiny128 ", Encoding.UTF8.GetString(bytesE2, 0, 8)); Assert.AreNotEqual(BitConverter.ToString(bytesE1), BitConverter.ToString(bytesE2)); var packet1a = TinyPacket.Parse(bytesE1, key); var packet2a = TinyPacket.Parse(bytesE2, key); Assert.IsNull(packet1a[".Id"]); Assert.IsNull(packet1a[".Host"]); Assert.IsNull(packet2a[".Id"]); Assert.IsNull(packet2a[".Host"]); var packet1b = packet1a.Clone(); var packet2b = packet2a.Clone(); Assert.IsNull(packet1b[".Id"]); Assert.IsNull(packet1b[".Host"]); Assert.IsNull(packet2b[".Id"]); Assert.IsNull(packet2b[".Host"]); var bytesP1 = packet1b.GetBytes(null, omitIdentifiers: true); var bytesP2 = packet2b.GetBytes(null, omitIdentifiers: true); Assert.AreEqual("Tiny ", Encoding.UTF8.GetString(bytesP1, 0, 5)); Assert.AreEqual("Tiny ", Encoding.UTF8.GetString(bytesP2, 0, 5)); Assert.AreEqual(Encoding.UTF8.GetString(bytesP1), Encoding.UTF8.GetString(bytesP2)); }
public static TinyPacket GetResponse(TinyPacket packet) { var data = new Dictionary<string, string>(); data.Add("IsError", false.ToString(CultureInfo.InvariantCulture)); data.Add("Message", ""); return new TinyPacket(packet.Product, packet.Operation, data); }
private static void ReceivedDetachDrive(TinyPacket packet) { try { DetachDrive(packet["Path"]); } catch (Exception ex) { throw new InvalidOperationException(string.Format("Drive \"{0}\" cannot be detached.", packet["Path"]), ex); } }
public void TinyPacket_Encode_Empty() { var target = new TinyPacket("Example", "Test"); var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true)); Assert.AreEqual(@"Tiny Example Test {}", actual); }
public static TinyPacket GetResponse(TinyPacket packet) { var data = new Dictionary <string, string>(); data.Add("IsError", false.ToString(CultureInfo.InvariantCulture)); data.Add("Message", ""); return(new TinyPacket(packet.Product, packet.Operation, data)); }
public static void Reply(TinyPacket response) { var buffer = response.GetBytes(); Pipe.Write(buffer); Pipe.Flush(); Pipe.Disconnect(); }
private static TinyPacket CreateBeatPacket() { var packet = new TinyPacket("Attache", "Beat"); packet.Add("Uptime", (Environment.TickCount / 1000).ToString("0", CultureInfo.InvariantCulture)); var hostEntry = Dns.GetHostEntry(Dns.GetHostName()); var sbIPv4 = new StringBuilder(); foreach (var address in hostEntry.AddressList) { if (address.AddressFamily != AddressFamily.InterNetwork) { continue; } //skip all not IPv4 if (sbIPv4.Length > 0) { sbIPv4.Append(" "); } sbIPv4.Append(address.ToString()); } packet.Add("IPv4", sbIPv4.ToString()); var sbIPv6 = new StringBuilder(); foreach (var address in hostEntry.AddressList) { if ((address.AddressFamily != AddressFamily.InterNetworkV6) || address.IsIPv6LinkLocal) { continue; } //skip all not IPv6 (global) if (sbIPv6.Length > 0) { sbIPv6.Append(" "); } sbIPv6.Append(address.ToString()); } packet.Add("IPv6", sbIPv6.ToString()); var sbIPv6Local = new StringBuilder(); foreach (var address in hostEntry.AddressList) { if ((address.AddressFamily != AddressFamily.InterNetworkV6) || !address.IsIPv6LinkLocal) { continue; } //skip all not IPv6 (link local) if (sbIPv6Local.Length > 0) { sbIPv6Local.Append(" "); } sbIPv6Local.Append(address.ToString()); } packet.Add("IPv6_Local", sbIPv6Local.ToString()); return(packet); }
public void TinyPacket_Decode_01() { var actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""Key1Text"":""Value1Text"",""Key2Text"":""Value2Text""} ")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); Assert.AreEqual("Value1Text", actual["Key1Text"]); Assert.AreEqual("Value2Text", actual["Key2Text"]); }
public void TinyPacket_CannotAddSpecialProperties1() { var packet = new TinyPacket("Example", "Test") { { "A", "1" }, { "B", "2" } }; packet.Add(".X", "X"); }
public void TinyPacket_CannotAddSpecialProperties2() { var packet = new TinyPacket("Example", "Test") { { "A", "1" }, { "B", "2" } }; packet[".X"] = "X"; }
private static void ReceivedWriteAutoAttachSettings(TinyPacket packet) { try { ServiceSettings.AutoAttachVhdList = GetFwoArray(packet["AutoAttachList"]); } catch (Exception ex) { Medo.Diagnostics.ErrorReport.SaveToTemp(ex); throw new InvalidOperationException("Auto-attach list cannot be written.", ex); } }
public void TinyPacket_Decode_Indexer() { var actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""A"":""0"",""A"":""1"",""B"":""null"",""B"":null}")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); Assert.AreEqual("1", actual["A"]); Assert.AreEqual(null, actual["B"]); Assert.AreEqual(null, actual["C"]); }
public void TinyPacket_Decode_02() { TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""Key1"":""A\n\rB"",""Key2"":""\u0000"",""Key3"":""\\""}")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); Assert.AreEqual("A\n\rB", actual["Key1"]); Assert.AreEqual("\0", actual["Key2"]); Assert.AreEqual("\\", actual["Key3"]); }
public void TinyPacket_Decode_NullItem_01A() { TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""A"":""1"",""B"":null,""C"":""2""}")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); Assert.AreEqual("1", actual["A"]); Assert.AreEqual(null, actual["B"]); Assert.AreEqual("2", actual["C"]); }
public static TinyPacket GetResponse(TinyPacket packet, Exception ex) { var data = new Dictionary<string, string>(); data.Add("IsError", true.ToString(CultureInfo.InvariantCulture)); if (ex.InnerException != null) { data.Add("Message", ex.Message + "\r\n" + ex.InnerException.Message); } else { data.Add("Message", ex.Message); } return new TinyPacket(packet.Product, packet.Operation, data); }
private static void ReceivedWriteContextMenuIsoSettings(TinyPacket packet) { try { ServiceSettings.ContextMenuIsoOpen = bool.Parse(packet["Open"]); ServiceSettings.ContextMenuIsoAttachReadOnly = bool.Parse(packet["AttachReadOnly"]); ServiceSettings.ContextMenuIsoDetach = bool.Parse(packet["Detach"]); } catch (Exception ex) { Medo.Diagnostics.ErrorReport.SaveToTemp(ex); throw new InvalidOperationException("Settings cannot be written.", ex); } }
public void TinyPacket_Encode_Indexer() { var target = new TinyPacket("Example", "Test"); target["A"] = "0"; target["A"] = "1"; var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true)); Assert.AreEqual(@"Tiny Example Test {""A"":""1""}", actual); }
public void TinyPacket_Encode_01() { var target = new TinyPacket("Example", "Test"); target["Key1Text"] = "Value1Text"; target["Key2Text"] = "Value2Text"; var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true)); Assert.AreEqual(@"Tiny Example Test {""Key1Text"":""Value1Text"",""Key2Text"":""Value2Text""}", actual); }
public void TinyPacket_Encode_02() { var target = new TinyPacket("Example", "Test"); target["Key1"] = "A\n\rB"; target["Key2"] = "\0"; target["Key3"] = "\\"; var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true)); Assert.AreEqual(@"Tiny Example Test {""Key1"":""A\n\rB"",""Key2"":""\u0000"",""Key3"":""\\""}", actual); }
public void TinyPacket_Encode_NullItem_01() { var target = new TinyPacket("Example", "Test"); target["A"] = "1"; target["B"] = null; target["C"] = "2"; var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true)); Assert.AreEqual(@"Tiny Example Test {""A"":""1"",""B"":null,""C"":""2""}", actual); }
private static void ReceivedDetach(TinyPacket packet) { try { var path = packet["Path"]; using (var disk = new Medo.IO.VirtualDisk(path)) { disk.Open(Medo.IO.VirtualDiskAccessMask.Detach); disk.Detach(); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Virtual disk file \"{0}\" cannot be detached.", (new FileInfo(packet["Path"])).Name), ex); } }
public void TinyPacket_Decode_01() { var data = new Dictionary <string, string>(); data.Add("Key1Text", "Value1Text"); data.Add("Key2Text", "Value2Text"); TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""Key1Text"":""Value1Text"",""Key2Text"":""Value2Text""} ")); Assert.AreEqual("Example", actual.Product); Assert.AreEqual("Test", actual.Operation); Assert.AreEqual("Value1Text", actual["Key1Text"]); Assert.AreEqual("Value2Text", actual["Key2Text"]); }
public static TinyPacket GetResponse(TinyPacket packet, Exception ex) { var data = new Dictionary <string, string>(); data.Add("IsError", true.ToString(CultureInfo.InvariantCulture)); if (ex.InnerException != null) { data.Add("Message", ex.Message + "\r\n" + ex.InnerException.Message); } else { data.Add("Message", ex.Message); } return(new TinyPacket(packet.Product, packet.Operation, data)); }
private static TinyPacket CreateVolumeInfoPacket() { var packet = new TinyPacket("Attache", "VolumeInfo"); foreach (var drive in DriveInfo.GetDrives()) { var volumeLetter = drive.Name.Substring(0, 1); String volumeLabel = ""; try { volumeLabel = drive.VolumeLabel; } catch (IOException ex) { volumeLabel = "{" + ex.Message + "}"; } long volumeSize = -1; try { volumeSize = drive.TotalSize; } catch (IOException) { } long volumeFree = -1; try { volumeFree = drive.AvailableFreeSpace; } catch (IOException) { } String volumeFormat = null; try { volumeFormat = drive.DriveFormat; } catch (IOException) { } packet.Add(volumeLetter, volumeLabel); if (volumeSize >= 0) { packet.Add(volumeLetter + "_Size", volumeSize.ToString("0" + CultureInfo.InvariantCulture)); } if (volumeSize >= 0) { packet.Add(volumeLetter + "_Free", volumeFree.ToString("0" + CultureInfo.InvariantCulture)); } if (volumeFormat != null) { packet.Add(volumeLetter + "_Format", volumeFormat); } } return(packet); }
private static void ReceivedChangeDriveLetter(TinyPacket packet) { try { var volume = new Volume(packet["VolumeName"]); var newDriveLetter = packet["NewDriveLetter"]; if (string.IsNullOrEmpty(newDriveLetter)) { volume.RemoveLetter(); } else { volume.ChangeLetter(newDriveLetter); } } catch (Exception ex) { Medo.Diagnostics.ErrorReport.SaveToTemp(ex); throw new InvalidOperationException("Cannot change drive letter.", ex); } }
public void TinyPacket_Cloning() { var packet = new TinyPacket("Example", "Test") { { "A", "1" }, { "B", "2" } }; var key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var bytesE = packet.GetBytes(key, omitIdentifiers: false); var packetA = TinyPacket.Parse(bytesE, key); Assert.IsNull(packetA[".Id"]); Assert.IsNotNull(packetA[".Host"]); Assert.IsNotNull(packetA["A"]); Assert.IsNotNull(packetA["B"]); var packetB = packetA.Clone(); Assert.IsNull(packetB[".Id"]); Assert.IsNull(packetB[".Host"]); Assert.IsNotNull(packetA["A"]); Assert.IsNotNull(packetA["B"]); var packetC = TinyPacket.Parse(packetB.GetBytes()); Assert.IsNotNull(packetC[".Id"]); Assert.IsNotNull(packetC[".Host"]); Assert.IsNotNull(packetA["A"]); Assert.IsNotNull(packetA["B"]); var packetD = packetC.Clone(); var bytesB = packetB.GetBytes(null, omitIdentifiers: true); var bytesD = packetD.GetBytes(null, omitIdentifiers: true); Assert.AreEqual(Encoding.UTF8.GetString(bytesB), Encoding.UTF8.GetString(bytesD)); }
private static void ReceivedDetachDrive(TinyPacket packet) { try { DetachDrive(packet["Path"]); } catch (Exception ex) { throw new InvalidOperationException(string.Format("Drive \"{0}\" cannot be detached.", packet["Path"]), ex); } }
public void TinyPacket_EncodeDecode_SpeedTest() { TinyPacket target = null; byte[] bytes = null; var nEncodePlain = 0; var swEncodePlain = Stopwatch.StartNew(); while (swEncodePlain.ElapsedMilliseconds < 500) { target = new TinyPacket("Example", "Test") { { "Key1Text", "Value1Text" }, { "Key2Text", "Value2Text" } }; bytes = target.GetBytes(); nEncodePlain += 1; } swEncodePlain.Stop(); TestContext.WriteLine(string.Format("TinyPacket.Encode.Plain: {0:#,##0}K packets/second", (double)nEncodePlain / swEncodePlain.ElapsedMilliseconds)); var nDecodePlain = 0; var swDecodePlain = Stopwatch.StartNew(); while (swDecodePlain.ElapsedMilliseconds < 500) { var target2 = TinyPacket.Parse(bytes); nDecodePlain += 1; } swDecodePlain.Stop(); TestContext.WriteLine(string.Format("TinyPacket.Decode.Plain: {0:#,##0}K packets/second", (double)nDecodePlain / swDecodePlain.ElapsedMilliseconds)); var key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var nEncodeAes = 0; var swEncodeAes = Stopwatch.StartNew(); while (swEncodeAes.ElapsedMilliseconds < 500) { target = new TinyPacket("Example", "Test") { { "Key1Text", "Value1Text" }, { "Key2Text", "Value2Text" } }; bytes = target.GetBytes(key); nEncodeAes += 1; } swEncodeAes.Stop(); TestContext.WriteLine(string.Format("TinyPacket.Encode.AES: {0:#,##0}K packets/second", (double)nEncodeAes / swEncodeAes.ElapsedMilliseconds)); Assert.AreEqual("Tiny128 ", Encoding.UTF8.GetString(bytes, 0, 8)); var nDecodeAes = 0; var swDecodeAes = Stopwatch.StartNew(); while (swDecodeAes.ElapsedMilliseconds < 500) { var target2 = TinyPacket.Parse(bytes, key); nDecodeAes += 1; } swDecodeAes.Stop(); TestContext.WriteLine(string.Format("TinyPacket.Decode.AES: {0:#,##0}K packets/second", (double)nDecodeAes / swDecodeAes.ElapsedMilliseconds)); }
public static void Reply(TinyPacket response) { var buffer = response.GetBytes(); Pipe.Write(buffer); Pipe.Flush(); Pipe.Disconnect(); }
private static void ReceivedAttach(TinyPacket packet) { try { var path = packet["Path"]; var isReadOnly = packet["MountReadOnly"].Equals("True", StringComparison.OrdinalIgnoreCase); var shouldInitialize = packet["InitializeDisk"].Equals("True", StringComparison.OrdinalIgnoreCase); string diskPath = null; using (var disk = new Medo.IO.VirtualDisk(path)) { var access = Medo.IO.VirtualDiskAccessMask.All; var options = Medo.IO.VirtualDiskAttachOptions.PermanentLifetime; if (isReadOnly) { if (shouldInitialize == false) { access = Medo.IO.VirtualDiskAccessMask.AttachReadOnly; } options |= Medo.IO.VirtualDiskAttachOptions.ReadOnly; } disk.Open(access); disk.Attach(options); if (shouldInitialize) { diskPath = disk.GetAttachedPath(); } } if (shouldInitialize) { DiskIO.InitializeDisk(diskPath); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Virtual disk file \"{0}\" cannot be attached.", (new FileInfo(packet["Path"])).Name), ex); } }
public void TinyPacket_Decode_Error_03() { TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"")); }
private static void ReceivedChangeDriveLetter(TinyPacket packet) { try { var volume = new Volume(packet["VolumeName"]); var newDriveLetter = packet["NewDriveLetter"]; if (string.IsNullOrEmpty(newDriveLetter)) { volume.RemoveLetter(); } else { volume.ChangeLetter(newDriveLetter); } } catch (Exception ex) { Medo.Diagnostics.ErrorReport.SaveToTemp(ex); throw new InvalidOperationException("Cannot change drive letter.", ex); } }
private static void ReceivedDetach(TinyPacket packet) { try { var path = packet["Path"]; using (var disk = new Medo.IO.VirtualDisk(path)) { disk.Open(Medo.IO.VirtualDiskAccessMask.Detach); disk.Detach(); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Virtual disk file \"{0}\" cannot be detached.", (new FileInfo(packet["Path"])).Name), ex); } }
private static void ReceivedWriteAutoAttachSettings(TinyPacket packet) { try { ServiceSettings.AutoAttachVhdList = GetFwoArray(packet["AutoAttachList"]); } catch (Exception ex) { Medo.Diagnostics.ErrorReport.SaveToTemp(ex); throw new InvalidOperationException("Auto-attach list cannot be written.", ex); } }
private static void ReceivedWriteContextMenuVhdSettings(TinyPacket packet) { try { ServiceSettings.ContextMenuVhdOpen = bool.Parse(packet["Open"]); ServiceSettings.ContextMenuVhdAttach = bool.Parse(packet["Attach"]); ServiceSettings.ContextMenuVhdAttachReadOnly = bool.Parse(packet["AttachReadOnly"]); ServiceSettings.ContextMenuVhdDetach = bool.Parse(packet["Detach"]); ServiceSettings.ContextMenuVhdDetachDrive = bool.Parse(packet["DetachDrive"]); } catch (Exception ex) { Medo.Diagnostics.ErrorReport.SaveToTemp(ex); throw new InvalidOperationException("Settings cannot be written.", ex); } }