summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DotNetOAuth.Test/MessageSerializerTest.cs85
-rw-r--r--src/DotNetOAuth.Test/Mocks/TestMessage.cs10
-rw-r--r--src/DotNetOAuth/DotNetOAuth.csproj1
-rw-r--r--src/DotNetOAuth/IProtocolMessage.cs13
-rw-r--r--src/DotNetOAuth/ProtocolException.cs17
-rw-r--r--src/DotNetOAuth/ProtocolMessageSerializer.cs11
6 files changed, 112 insertions, 25 deletions
diff --git a/src/DotNetOAuth.Test/MessageSerializerTest.cs b/src/DotNetOAuth.Test/MessageSerializerTest.cs
index e105ded..7f576aa 100644
--- a/src/DotNetOAuth.Test/MessageSerializerTest.cs
+++ b/src/DotNetOAuth.Test/MessageSerializerTest.cs
@@ -12,14 +12,62 @@ namespace DotNetOAuth.Test {
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SerializeNullFields() {
+ var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
+ serializer.Serialize(null, new Mocks.TestMessage());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SerializeNullMessage() {
+ var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
+ serializer.Serialize(new Dictionary<string, string>(), null);
+ }
+
+ [TestMethod, ExpectedException(typeof(ProtocolException))]
+ public void SerializeInvalidMessage() {
+ var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
+ Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
+ Mocks.TestMessage message = new DotNetOAuth.Test.Mocks.TestMessage();
+ message.EmptyMember = "invalidvalue";
+ serializer.Serialize(message);
+ }
+
+ [TestMethod()]
+ public void SerializeTest() {
+ var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
+ var message = new Mocks.TestMessage { Age = 15, Name = "Andrew" };
+ IDictionary<string, string> actual = serializer.Serialize(message);
+ Assert.AreEqual(2, actual.Count);
+
+ // Test case sensitivity of generated dictionary
+ Assert.IsFalse(actual.ContainsKey("Age"));
+ Assert.IsTrue(actual.ContainsKey("age"));
+
+ // Test contents of dictionary
+ Assert.AreEqual("15", actual["age"]);
+ Assert.AreEqual("Andrew", actual["Name"]);
+ Assert.IsFalse(actual.ContainsKey("EmptyMember"));
+ }
+
+ [TestMethod]
+ public void SerializeToExistingDictionary() {
+ var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
+ var message = new Mocks.TestMessage { Age = 15, Name = "Andrew" };
+ var fields = new Dictionary<string, string>();
+ fields["someExtraField"] = "someValue";
+ serializer.Serialize(fields, message);
+ Assert.AreEqual(3, fields.Count);
+ Assert.AreEqual("15", fields["age"]);
+ Assert.AreEqual("Andrew", fields["Name"]);
+ Assert.AreEqual("someValue", fields["someExtraField"]);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void DeserializeNull() {
var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
serializer.Deserialize(null);
}
- /// <summary>
- /// A test for Deserialize
- /// </summary>
[TestMethod()]
public void DeserializeSimple() {
var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
@@ -39,44 +87,33 @@ namespace DotNetOAuth.Test {
var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
fields["age"] = "15";
+ fields["Name"] = "Andrew";
// Add some field that is not recognized by the class. This simulates a querystring with
// more parameters than are actually interesting to the protocol message.
fields["someExtraField"] = "asdf";
var actual = serializer.Deserialize(fields);
Assert.AreEqual(15, actual.Age);
- Assert.IsNull(actual.Name);
+ Assert.AreEqual("Andrew", actual.Name);
Assert.IsNull(actual.EmptyMember);
}
- /// <summary>
- /// A test for Deserialize
- /// </summary>
[TestMethod()]
public void DeserializeEmpty() {
var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
var actual = serializer.Deserialize(fields);
Assert.AreEqual(0, actual.Age);
+ Assert.IsNull(actual.Name);
+ Assert.IsNull(actual.EmptyMember);
}
- /// <summary>
- /// A test for Serialize
- /// </summary>
- [TestMethod()]
- public void SerializeTest() {
+ [TestMethod, ExpectedException(typeof(ProtocolException))]
+ public void DeserializeInvalidMessage() {
var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
- var message = new Mocks.TestMessage { Age = 15, Name = "Andrew" };
- IDictionary<string, string> actual = serializer.Serialize(message);
- Assert.AreEqual(2, actual.Count);
-
- // Test case sensitivity of generated dictionary
- Assert.IsFalse(actual.ContainsKey("Age"));
- Assert.IsTrue(actual.ContainsKey("age"));
-
- // Test contents of dictionary
- Assert.AreEqual("15", actual["age"]);
- Assert.AreEqual("Andrew", actual["Name"]);
- Assert.IsFalse(actual.ContainsKey("EmptyMember"));
+ Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
+ // Put in a value where the field should be empty.
+ fields["EmptyMember"] = "15";
+ serializer.Deserialize(fields);
}
}
}
diff --git a/src/DotNetOAuth.Test/Mocks/TestMessage.cs b/src/DotNetOAuth.Test/Mocks/TestMessage.cs
index 1a8eba7..a81e2f9 100644
--- a/src/DotNetOAuth.Test/Mocks/TestMessage.cs
+++ b/src/DotNetOAuth.Test/Mocks/TestMessage.cs
@@ -9,5 +9,15 @@ namespace DotNetOAuth.Test.Mocks {
public string Name { get; set; }
[DataMember]
public string EmptyMember { get; set; }
+
+ #region IProtocolMessage Members
+
+ void IProtocolMessage.EnsureValidMessage() {
+ if (EmptyMember != null || Age < 0) {
+ throw new ProtocolException();
+ }
+ }
+
+ #endregion
}
}
diff --git a/src/DotNetOAuth/DotNetOAuth.csproj b/src/DotNetOAuth/DotNetOAuth.csproj
index 5e17d89..6f2e531 100644
--- a/src/DotNetOAuth/DotNetOAuth.csproj
+++ b/src/DotNetOAuth/DotNetOAuth.csproj
@@ -74,6 +74,7 @@
<Compile Include="IndirectMessageEncoder.cs" />
<Compile Include="IProtocolMessage.cs" />
<Compile Include="MessageScheme.cs" />
+ <Compile Include="ProtocolException.cs" />
<Compile Include="ProtocolMessageSerializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util.cs" />
diff --git a/src/DotNetOAuth/IProtocolMessage.cs b/src/DotNetOAuth/IProtocolMessage.cs
index ccf4e5a..4dd31ae 100644
--- a/src/DotNetOAuth/IProtocolMessage.cs
+++ b/src/DotNetOAuth/IProtocolMessage.cs
@@ -8,5 +8,18 @@ namespace DotNetOAuth {
/// as OAuth messages.
/// </summary>
interface IProtocolMessage {
+ /// <summary>
+ /// Checks the message state for conformity to the protocol specification
+ /// and throws an exception if the message is invalid.
+ /// </summary>
+ /// <remarks>
+ /// <para>Some messages have required fields, or combinations of fields that must relate to each other
+ /// in specialized ways. After deserializing a message, this method checks the state of the
+ /// message to see if it conforms to the protocol.</para>
+ /// <para>Note that this property should <i>not</i> check signatures or perform any state checks
+ /// outside this scope of this particular message.</para>
+ /// </remarks>
+ /// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
+ void EnsureValidMessage();
}
}
diff --git a/src/DotNetOAuth/ProtocolException.cs b/src/DotNetOAuth/ProtocolException.cs
new file mode 100644
index 0000000..b2bd93f
--- /dev/null
+++ b/src/DotNetOAuth/ProtocolException.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace DotNetOAuth {
+ [global::System.Serializable]
+ public class ProtocolException : Exception {
+ public ProtocolException() { }
+ public ProtocolException(string message) : base(message) { }
+ public ProtocolException(string message, Exception inner) : base(message, inner) { }
+ protected ProtocolException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context) { }
+ }
+}
diff --git a/src/DotNetOAuth/ProtocolMessageSerializer.cs b/src/DotNetOAuth/ProtocolMessageSerializer.cs
index 14422dd..14b6b2f 100644
--- a/src/DotNetOAuth/ProtocolMessageSerializer.cs
+++ b/src/DotNetOAuth/ProtocolMessageSerializer.cs
@@ -28,10 +28,18 @@ namespace DotNetOAuth {
if (message == null) throw new ArgumentNullException("message");
var fields = new Dictionary<string, string>();
+ Serialize(fields, message);
+ return fields;
+ }
+
+ internal void Serialize(IDictionary<string, string> fields, T message) {
+ if (fields == null) throw new ArgumentNullException("fields");
+ if (message == null) throw new ArgumentNullException("message");
+
+ message.EnsureValidMessage();
using (XmlWriter writer = DictionaryXmlWriter.Create(fields)) {
serializer.WriteObjectContent(writer, message);
}
- return fields;
}
/// <summary>
@@ -43,6 +51,7 @@ namespace DotNetOAuth {
var reader = DictionaryXmlReader.Create(rootElement, fields);
T result = (T)serializer.ReadObject(reader, false);
+ result.EnsureValidMessage();
return result;
}
}