From f2ca12db8fc9268ab636fc347f98b727936f9e57 Mon Sep 17 00:00:00 2001 From: lduchosal Date: Thu, 14 Mar 2019 16:21:52 +0100 Subject: [PATCH] version 2.3.0, supporting serialization --- appveyor.yml | 2 +- doc/Doxyfile | 2 +- nant/release.build | 5 +- .../Properties/AssemblyInfo.cs | 4 +- .../Properties/AssemblyInfo.cs | 4 +- ....IPNetwork.TestProject.NetFramework.csproj | 3 + .../packages.config | 1 + .../Properties/AssemblyInfo.cs | 4 +- .../SerializeBinaryFormatterTest.cs | 125 ++++++++++++++++ .../SerializeJsonTest.cs | 87 +++++++++++ .../SerializeXmlTest.cs | 138 ++++++++++++++++++ ...Net.IPNetwork.TestProject.Source.projitems | 3 + src/System.Net.IPNetwork/IPNetwork.cs | 61 +++++++- .../Properties/AssemblyInfo.cs | 4 +- .../System.Net.IPNetwork.csproj | 12 +- 15 files changed, 431 insertions(+), 24 deletions(-) create mode 100644 src/System.Net.IPNetwork.TestProject.Source/SerializeBinaryFormatterTest.cs create mode 100644 src/System.Net.IPNetwork.TestProject.Source/SerializeJsonTest.cs create mode 100644 src/System.Net.IPNetwork.TestProject.Source/SerializeXmlTest.cs diff --git a/appveyor.yml b/appveyor.yml index 39fa91a..4a75851 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: '2.2.0.{build}' +version: '2.3.0.{build}' image: Visual Studio 2017 assembly_info: diff --git a/doc/Doxyfile b/doc/Doxyfile index fb03801..847554f 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -38,7 +38,7 @@ PROJECT_NAME = "IPNetwork" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 2.2.0 +PROJECT_NUMBER = 2.3.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/nant/release.build b/nant/release.build index 001ec0a..5f7608a 100644 --- a/nant/release.build +++ b/nant/release.build @@ -4,7 +4,7 @@ - + @@ -14,7 +14,8 @@ - + + diff --git a/src/System.Net.IPNetwork.ConsoleApplication.NetFramework/Properties/AssemblyInfo.cs b/src/System.Net.IPNetwork.ConsoleApplication.NetFramework/Properties/AssemblyInfo.cs index 57a2b53..f2bd896 100644 --- a/src/System.Net.IPNetwork.ConsoleApplication.NetFramework/Properties/AssemblyInfo.cs +++ b/src/System.Net.IPNetwork.ConsoleApplication.NetFramework/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.2.0.0")] -[assembly: AssemblyFileVersion("2.2.0.0")] +[assembly: AssemblyVersion("2.3.0.0")] +[assembly: AssemblyFileVersion("2.3.0.0")] diff --git a/src/System.Net.IPNetwork.ConsoleApplication.Source/Properties/AssemblyInfo.cs b/src/System.Net.IPNetwork.ConsoleApplication.Source/Properties/AssemblyInfo.cs index 264aad5..03edc95 100644 --- a/src/System.Net.IPNetwork.ConsoleApplication.Source/Properties/AssemblyInfo.cs +++ b/src/System.Net.IPNetwork.ConsoleApplication.Source/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.2.0.0")] -[assembly: AssemblyFileVersion("2.2.0.0")] +[assembly: AssemblyVersion("2.3.0.0")] +[assembly: AssemblyFileVersion("2.3.0.0")] diff --git a/src/System.Net.IPNetwork.TestProject.NetFramework/System.Net.IPNetwork.TestProject.NetFramework.csproj b/src/System.Net.IPNetwork.TestProject.NetFramework/System.Net.IPNetwork.TestProject.NetFramework.csproj index 6556c36..a1092b3 100644 --- a/src/System.Net.IPNetwork.TestProject.NetFramework/System.Net.IPNetwork.TestProject.NetFramework.csproj +++ b/src/System.Net.IPNetwork.TestProject.NetFramework/System.Net.IPNetwork.TestProject.NetFramework.csproj @@ -38,6 +38,9 @@ + + ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + ..\packages\System.Console.4.0.0\lib\net46\System.Console.dll diff --git a/src/System.Net.IPNetwork.TestProject.NetFramework/packages.config b/src/System.Net.IPNetwork.TestProject.NetFramework/packages.config index 18f0862..0847c91 100644 --- a/src/System.Net.IPNetwork.TestProject.NetFramework/packages.config +++ b/src/System.Net.IPNetwork.TestProject.NetFramework/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/src/System.Net.IPNetwork.TestProject.Source/Properties/AssemblyInfo.cs b/src/System.Net.IPNetwork.TestProject.Source/Properties/AssemblyInfo.cs index 361b5b3..1574d87 100644 --- a/src/System.Net.IPNetwork.TestProject.Source/Properties/AssemblyInfo.cs +++ b/src/System.Net.IPNetwork.TestProject.Source/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("2.2.0.0")] -[assembly: AssemblyFileVersion("2.2.0.0")] +[assembly: AssemblyVersion("2.3.0.0")] +[assembly: AssemblyFileVersion("2.3.0.0")] diff --git a/src/System.Net.IPNetwork.TestProject.Source/SerializeBinaryFormatterTest.cs b/src/System.Net.IPNetwork.TestProject.Source/SerializeBinaryFormatterTest.cs new file mode 100644 index 0000000..be9705d --- /dev/null +++ b/src/System.Net.IPNetwork.TestProject.Source/SerializeBinaryFormatterTest.cs @@ -0,0 +1,125 @@ +using IPNetwork = System.Net.IPNetwork; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Xml.Serialization; +using System.IO; +using System.Xml; +using System.Text; +using Newtonsoft.Json; +using System.Runtime.Serialization.Formatters.Binary; + +namespace System.Net.TestSerialization +{ + [TestClass] + public class SerializeBinaryFormatterTest + { + + [TestMethod] + public void Test_Serialize_BinaryFormatter() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + BinaryFormatter serializer = new BinaryFormatter(); + var mem = new MemoryStream(); + + serializer.Serialize(mem, ipnetwork); + var result = Convert.ToBase64String(mem.ToArray()); + + string expected = "AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uTmV0LklQTmV0d29yaywgVmVyc2lvbj0yLjIuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTcxNzM0M2NjMmMyNWVkY2YFAQAAABRTeXN0ZW0uTmV0LklQTmV0d29yawEAAAAJSVBOZXR3b3JrAQIAAAAGAwAAAAoxMC4wLjAuMC84Cw=="; + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void Test_Deserialize_BinaryFormatter() + { + string base64 = "AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uTmV0LklQTmV0d29yaywgVmVyc2lvbj0yLjIuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTcxNzM0M2NjMmMyNWVkY2YFAQAAABRTeXN0ZW0uTmV0LklQTmV0d29yawEAAAAJSVBOZXR3b3JrAQIAAAAGAwAAAAoxMC4wLjAuMC84Cw=="; + var bytes = Convert.FromBase64String(base64); + var mem = new MemoryStream(bytes); + + BinaryFormatter serializer = new BinaryFormatter(); + var result = serializer.Deserialize(mem); + + var expected = IPNetwork.Parse("10.0.0.1/8"); + Assert.AreEqual(expected, result); + + } + + [TestMethod] + public void Test_Serialize_Deserialize_BinaryFormatter() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + BinaryFormatter serializer = new BinaryFormatter(); + var mem = new MemoryStream(); + + serializer.Serialize(mem, ipnetwork); + + mem.Position = 0; + var ipnetwork2 = serializer.Deserialize(mem); + + Assert.AreEqual(ipnetwork, ipnetwork2); + } + + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Serialize_BinaryFormatter() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var serializer = new BinaryFormatter(); + var mem = new MemoryStream(); + + for (int i = 0; i < 1000000; i++) + { + serializer.Serialize(mem, ipnetwork); + mem.SetLength(0); + } + + // 5.13 seconds(Ad hoc). + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Deserialize_BinaryFormatter() + { + string base64 = "AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uTmV0LklQTmV0d29yaywgVmVyc2lvbj0yLjIuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTcxNzM0M2NjMmMyNWVkY2YFAQAAABRTeXN0ZW0uTmV0LklQTmV0d29yawEAAAAJSVBOZXR3b3JrAQIAAAAGAwAAAAoxMC4wLjAuMC84Cw=="; + var bytes = Convert.FromBase64String(base64); + var mem = new MemoryStream(bytes); + + var serializer = new BinaryFormatter(); + + for (int i = 0; i < 1000000; i++) + { + var result = serializer.Deserialize(mem); + mem.Position = 0; + } + + // 11.949 seconds(Ad hoc). + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Serialize_Deserialize_BinaryFormatter() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + BinaryFormatter serializer = new BinaryFormatter(); + var mem = new MemoryStream(); + + for (int i = 0; i < 1000000; i++) + { + serializer.Serialize(mem, ipnetwork); + + mem.Position = 0; + var ipnetwork2 = serializer.Deserialize(mem); + + mem.SetLength(0); + + } + + // 17.48 seconds(Ad hoc). + + } + } +} diff --git a/src/System.Net.IPNetwork.TestProject.Source/SerializeJsonTest.cs b/src/System.Net.IPNetwork.TestProject.Source/SerializeJsonTest.cs new file mode 100644 index 0000000..10a3217 --- /dev/null +++ b/src/System.Net.IPNetwork.TestProject.Source/SerializeJsonTest.cs @@ -0,0 +1,87 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; + +namespace System.Net.TestSerialization.NetFramework +{ + [TestClass] + public class SerializeJsonTest + { + [TestMethod] + public void Test_Serialize_Json() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var result = JsonConvert.SerializeObject(ipnetwork); + + string expected = "{\"IPNetwork\":\"10.0.0.0/8\"}"; + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void Test_Deserialize_Json() + { + string json = "{\"IPNetwork\":\"10.0.0.0/8\"}"; + + var result = JsonConvert.DeserializeObject(json); + + IPNetwork expected = IPNetwork.Parse("10.0.0.1/8"); + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void Test_Serialize_Deserialize_Json() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var json = JsonConvert.SerializeObject(ipnetwork); + var result = JsonConvert.DeserializeObject(json); + + Assert.AreEqual(ipnetwork, result); + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Serialize_Json() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + for (int i = 0; i < 1000000; i++) + { + var json = JsonConvert.SerializeObject(ipnetwork); + } + + // 3.06 seconds(Ad hoc). + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Deserialize_Json() + { + string json = "{\"IPNetwork\":\"10.0.0.0/8\"}"; + + for (int i = 0; i < 1000000; i++) + { + var result = JsonConvert.DeserializeObject(json); + } + + // 10.20 seconds(Ad hoc). + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Serialize_Deserialize_Json() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + for (int i = 0; i < 1000000; i++) + { + var json = JsonConvert.SerializeObject(ipnetwork); + var result = JsonConvert.DeserializeObject(json); + } + + // 13.49 seconds(Ad hoc). + } + + } +} diff --git a/src/System.Net.IPNetwork.TestProject.Source/SerializeXmlTest.cs b/src/System.Net.IPNetwork.TestProject.Source/SerializeXmlTest.cs new file mode 100644 index 0000000..4eec76f --- /dev/null +++ b/src/System.Net.IPNetwork.TestProject.Source/SerializeXmlTest.cs @@ -0,0 +1,138 @@ +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.IO; +using System.Xml.Serialization; + +namespace System.Net.TestSerialization.NetFramework +{ + /// + /// Summary description for SerializeXmlTest + /// + [TestClass] + public class SerializeXmlTest + { + + [TestMethod] + public void Test_Serialize_Xml() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var mem = new MemoryStream(); + + var serializer = new XmlSerializer(typeof(IPNetwork)); + serializer.Serialize(mem, ipnetwork); + + var result = Encoding.UTF8.GetString(mem.ToArray()); + + string expected = @" + + 10.0.0.0/8 +"; + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void Test_Deserialize_Xml() + { + string xml = @" + + 10.0.0.0/8 +"; + var bytes = Encoding.UTF8.GetBytes(xml); + var mem = new MemoryStream(bytes); + + var serializer = new XmlSerializer(typeof(IPNetwork)); + var result = serializer.Deserialize(mem); + + IPNetwork expected = IPNetwork.Parse("10.0.0.1/8"); + Assert.AreEqual(expected, result); + } + + + [TestMethod] + public void Test_Serialize_Deserialize_Xml() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var mem = new MemoryStream(); + + var serializer = new XmlSerializer(typeof(IPNetwork)); + serializer.Serialize(mem, ipnetwork); + + string result = Encoding.UTF8.GetString(mem.ToArray()); + Console.WriteLine(result); + + mem.Position = 0; + var ipnetwork2 = serializer.Deserialize(mem); + + Assert.AreEqual(ipnetwork, ipnetwork2); + } + + + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Serialize_Xml() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var serializer = new XmlSerializer(typeof(IPNetwork)); + var mem = new MemoryStream(); + + for (int i = 0; i < 1000000; i++) + { + serializer.Serialize(mem, ipnetwork); + mem.SetLength(0); + } + + // 5.13 seconds(Ad hoc). + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Deserialize_Xml() + { + string xml = @" + + 10.0.0.0/8 +"; + var bytes = Encoding.UTF8.GetBytes(xml); + var mem = new MemoryStream(bytes); + + var serializer = new XmlSerializer(typeof(IPNetwork)); + + for (int i = 0; i < 1000000; i++) + { + var result = serializer.Deserialize(mem); + mem.Position = 0; + } + + // 17.98 seconds(Ad hoc). + } + + [TestMethod] + [TestCategory("LongRunning")] + public void Test_1_000_000_Serialize_Deserialize_Xml() + { + var ipnetwork = IPNetwork.Parse("10.0.0.1/8"); + + var serializer = new XmlSerializer(typeof(IPNetwork)); + var mem = new MemoryStream(); + + for (int i = 0; i < 1000000; i++) + { + serializer.Serialize(mem, ipnetwork); + + mem.Position = 0; + var ipnetwork2 = serializer.Deserialize(mem); + + mem.SetLength(0); + + } + + // 17.48 seconds(Ad hoc). + + } + + } +} diff --git a/src/System.Net.IPNetwork.TestProject.Source/System.Net.IPNetwork.TestProject.Source.projitems b/src/System.Net.IPNetwork.TestProject.Source/System.Net.IPNetwork.TestProject.Source.projitems index 926e774..91c4886 100644 --- a/src/System.Net.IPNetwork.TestProject.Source/System.Net.IPNetwork.TestProject.Source.projitems +++ b/src/System.Net.IPNetwork.TestProject.Source/System.Net.IPNetwork.TestProject.Source.projitems @@ -18,5 +18,8 @@ + + + \ No newline at end of file diff --git a/src/System.Net.IPNetwork/IPNetwork.cs b/src/System.Net.IPNetwork/IPNetwork.cs index 2ea502c..197a3aa 100644 --- a/src/System.Net.IPNetwork/IPNetwork.cs +++ b/src/System.Net.IPNetwork/IPNetwork.cs @@ -3,6 +3,8 @@ using System.Net.Sockets; using System.Numerics; using System.Text.RegularExpressions; +using System.Runtime.Serialization; +using System.Xml.Serialization; namespace System.Net { @@ -10,7 +12,9 @@ namespace System.Net /// IP Network utility class. /// Use IPNetwork.Parse to create instances. /// - public class IPNetwork : IComparable { + [DataContract] + [Serializable] + public sealed class IPNetwork : IComparable, ISerializable { #region properties @@ -24,9 +28,21 @@ public class IPNetwork : IComparable { //private uint _usable; private byte _cidr; + [DataMember(Name="IPNetwork", IsRequired=true)] + public string Value + { + get { return this.ToString(); } + set { + var ipnetwork = IPNetwork.Parse(value); + this._ipaddress = ipnetwork._ipaddress; + this._family = ipnetwork._family; + this._cidr = ipnetwork._cidr; + } + } + #endregion - #region accessors + #region accessors private BigInteger _network { get { @@ -166,7 +182,7 @@ public byte Cidr { internal #endif - IPNetwork(BigInteger ipaddress, AddressFamily family, byte cidr) { + IPNetwork(BigInteger ipaddress, AddressFamily family, byte cidr) { int maxCidr = family == Sockets.AddressFamily.InterNetwork ? 32 : 128; if (cidr > maxCidr) { @@ -1938,9 +1954,9 @@ public override Boolean Equals(Object obj) return Equals(this, obj as IPNetwork); } -#endregion + #endregion -#region Operators + #region Operators public static Boolean operator ==(IPNetwork left, IPNetwork right) { @@ -1962,7 +1978,40 @@ public override Boolean Equals(Object obj) return Compare(left, right) > 0; } -#endregion + #endregion + + #region XmlSerialization + + IPNetwork() { } + + #endregion + + #region ISerializable + internal struct IPNetworkInteral + { + public BigInteger IPAddress; + public byte Cidr; + public AddressFamily AddressFamily; + } + + + IPNetwork(SerializationInfo info, StreamingContext context) + { + var sipnetwork = (string)info.GetValue("IPNetwork", typeof(string)); + var ipnetwork = IPNetwork.Parse(sipnetwork); + + this._ipaddress = ipnetwork._ipaddress; + this._cidr = ipnetwork._cidr; + this._family = ipnetwork._family; + + } + + void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("IPNetwork", this.ToString()); + } + + #endregion } } diff --git a/src/System.Net.IPNetwork/Properties/AssemblyInfo.cs b/src/System.Net.IPNetwork/Properties/AssemblyInfo.cs index 45359f6..ba0e35d 100644 --- a/src/System.Net.IPNetwork/Properties/AssemblyInfo.cs +++ b/src/System.Net.IPNetwork/Properties/AssemblyInfo.cs @@ -32,8 +32,8 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.2.0.0")] -[assembly: AssemblyFileVersion("2.2.0.0")] +[assembly: AssemblyVersion("2.3.0.0")] +[assembly: AssemblyFileVersion("2.3.0.0")] #if !TRAVISCI [assembly: InternalsVisibleTo("System.Net.IPNetwork.TestProject.NetCore, PublicKey=00240000048000009400000006020000002400005253413100040000010001004d29ae79cfcf603de0200afc96f4d8304aa857341b78e706fedb3f0ac9c9d613443cea78a1ee687def573ad45b5cdc0abeeb1db304eec7c07331015d8aeeb3fd5e092273a2347e6cb54803a00484807c64bc3092f17619abfc5290133efad358a27747bfe71d1dc23b461d7cf91272844fc7a8390dc63b16236729dadb2c21bc")] [assembly: InternalsVisibleTo("System.Net.IPNetwork.TestProject.NetFramework, PublicKey=00240000048000009400000006020000002400005253413100040000010001004d29ae79cfcf603de0200afc96f4d8304aa857341b78e706fedb3f0ac9c9d613443cea78a1ee687def573ad45b5cdc0abeeb1db304eec7c07331015d8aeeb3fd5e092273a2347e6cb54803a00484807c64bc3092f17619abfc5290133efad358a27747bfe71d1dc23b461d7cf91272844fc7a8390dc63b16236729dadb2c21bc")] diff --git a/src/System.Net.IPNetwork/System.Net.IPNetwork.csproj b/src/System.Net.IPNetwork/System.Net.IPNetwork.csproj index 1cebfab..699940d 100644 --- a/src/System.Net.IPNetwork/System.Net.IPNetwork.csproj +++ b/src/System.Net.IPNetwork/System.Net.IPNetwork.csproj @@ -1,10 +1,10 @@  - netstandard1.3;net40;net45;net46 - netstandard1.3 + netstandard2.0;net40;net45;net46;net47 + netstandard2.0 IPNetwork2 - 2.2.0 + 2.3.0 false https://github.com/lduchosal/ipnetwork/blob/master/LICENSE https://github.com/lduchosal/ipnetwork @@ -17,7 +17,7 @@ IPNetwork utility classes for .Net Luc Dvchosal Luc Dvchosal - Copyright 2018 + Copyright 2019 True false System.Net.IPNetwork.snk @@ -26,7 +26,7 @@ - + full true Auto @@ -37,7 +37,7 @@ $(DefineConstants);TRAVISCI - +