summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs
blob: dbcadabf29136cf91c2b70dcb67f8c90ed2b891a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//-----------------------------------------------------------------------
// <copyright file="MessagePartTests.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Messaging.Reflection {
	using System;
	using System.Linq;
	using System.Reflection;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Reflection;
	using DotNetOpenAuth.Test.Mocks;
	using NUnit.Framework;

	[TestFixture]
	public class MessagePartTests : MessagingTestBase {
		[Test, ExpectedException(typeof(ArgumentException))]
		public void OptionalNonNullableStruct() {
			this.ParameterizedMessageTypeTest(typeof(MessageWithNonNullableOptionalStruct));
		}

		[Test]
		public void RequiredNonNullableStruct() {
			this.ParameterizedMessageTypeTest(typeof(MessageWithNonNullableRequiredStruct));
		}

		[Test]
		public void OptionalNullableStruct() {
			var message = new MessageWithNullableOptionalStruct();
			var part = this.ParameterizedMessageTypeTest(message.GetType());

			Assert.IsNull(part.GetValue(message));
			part.SetValue(message, "3");
			Assert.AreEqual("3", part.GetValue(message));
		}

		[Test]
		public void RequiredNullableStruct() {
			this.ParameterizedMessageTypeTest(typeof(MessageWithNullableRequiredStruct));
		}

		[Test, ExpectedException(typeof(ArgumentNullException))]
		public void CtorNullMember() {
			new MessagePart(null, new MessagePartAttribute());
		}

		[Test, ExpectedException(typeof(ArgumentNullException))]
		public void CtorNullAttribute() {
			PropertyInfo field = typeof(MessageWithNullableOptionalStruct).GetProperty("OptionalInt", BindingFlags.NonPublic | BindingFlags.Instance);
			new MessagePart(field, null);
		}

		[Test]
		public void SetValue() {
			var message = new MessageWithNonNullableRequiredStruct();
			MessagePart part = this.ParameterizedMessageTypeTest(message.GetType());
			part.SetValue(message, "5");
			Assert.AreEqual(5, message.OptionalInt);
		}

		[Test]
		public void GetValue() {
			var message = new MessageWithNonNullableRequiredStruct();
			message.OptionalInt = 8;
			MessagePart part = this.ParameterizedMessageTypeTest(message.GetType());
			Assert.AreEqual("8", part.GetValue(message));
		}

		[Test]
		public void Base64Member() {
			var message = new MessageWithBase64EncodedString();
			message.LastName = "andrew";
			MessagePart part = GetMessagePart(message.GetType(), "nameBytes");
			Assert.AreEqual("YW5kcmV3", part.GetValue(message));
			part.SetValue(message, "YXJub3R0");
			Assert.AreEqual("arnott", message.LastName);
		}

		[Test]
		public void ConstantFieldMemberValidValues() {
			var message = new MessageWithConstantField();
			MessagePart part = GetMessagePart(message.GetType(), "ConstantField");
			Assert.AreEqual("abc", part.GetValue(message));
			part.SetValue(message, "abc");
			Assert.AreEqual("abc", part.GetValue(message));
		}

		[Test, ExpectedException(typeof(ProtocolException))]
		public void ConstantFieldMemberInvalidValues() {
			var message = new MessageWithConstantField();
			MessagePart part = GetMessagePart(message.GetType(), "ConstantField");
			part.SetValue(message, "def");
		}

		[Test, ExpectedException(typeof(ArgumentException))]
		public void NonFieldOrPropertyMember() {
			MemberInfo method = typeof(MessageWithNullableOptionalStruct).GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance);
			new MessagePart(method, new MessagePartAttribute());
		}

		[Test]
		public void RequiredMinAndMaxVersions() {
			Type messageType = typeof(MessageWithMinAndMaxVersionParts);
			FieldInfo newIn2Field = messageType.GetField("NewIn2", BindingFlags.Public | BindingFlags.Instance);
			MessagePartAttribute newIn2Attribute = newIn2Field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single();

			FieldInfo removedIn3Field = messageType.GetField("RemovedIn3", BindingFlags.Public | BindingFlags.Instance);
			MessagePartAttribute removedIn3Attribute = removedIn3Field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single();

			Assert.AreEqual(new Version(2, 0), newIn2Attribute.MinVersionValue);
			Assert.AreEqual(new Version(2, 5), removedIn3Attribute.MaxVersionValue);
		}

		private static MessagePart GetMessagePart(Type messageType, string memberName) {
			FieldInfo field = messageType.GetField(memberName, BindingFlags.NonPublic | BindingFlags.Instance);
			MessagePartAttribute attribute = field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single();
			return new MessagePart(field, attribute);
		}

		private MessagePart ParameterizedMessageTypeTest(Type messageType) {
			PropertyInfo field = messageType.GetProperty("OptionalInt", BindingFlags.NonPublic | BindingFlags.Instance);
			MessagePartAttribute attribute = field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single();
			return new MessagePart(field, attribute);
		}

		private class MessageWithNonNullableOptionalStruct : TestMessage {
			// Optional structs like int must be nullable for Optional to make sense.
			[MessagePart(IsRequired = false)]
			internal int OptionalInt { get; set; }
		}

		private class MessageWithNonNullableRequiredStruct : TestMessage {
			// This should work because a required field will always have a value so it
			// need not be nullable.
			[MessagePart(IsRequired = true)]
			internal int OptionalInt { get; set; }
		}

		private class MessageWithNullableOptionalStruct : TestMessage {
			// Optional structs like int must be nullable for Optional to make sense.
			[MessagePart(IsRequired = false)]
			internal int? OptionalInt { get; set; }
		}

		private class MessageWithNullableRequiredStruct : TestMessage {
			[MessagePart(IsRequired = true)]
			private int? OptionalInt { get; set; }
		}

		private class MessageWithBase64EncodedString : TestMessage {
			[MessagePart]
			private byte[] nameBytes;

			public string LastName {
				get { return this.nameBytes != null ? Encoding.UTF8.GetString(this.nameBytes) : null; }
				set { this.nameBytes = value != null ? Encoding.UTF8.GetBytes(value) : null; }
			}
		}

		private class MessageWithConstantField : TestMessage {
			[MessagePart(IsRequired = true)]
#pragma warning disable 0414 // read by reflection
			private readonly string ConstantField = "abc";
#pragma warning restore 0414
		}

		private class MessageWithMinAndMaxVersionParts : TestMessage {
#pragma warning disable 0649 // written to by reflection
			[MessagePart(MinVersion = "2.0")]
			public string NewIn2;

			[MessagePart(MaxVersion = "2.5")]
			public string RemovedIn3;
#pragma warning restore 0649
		}
	}
}