Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / Hammock.Tests / OAuth / OAuthToolsTests.cs @ 0eea575a

History | View | Annotate | Download (8.8 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Threading;
5
using Hammock.Authentication.OAuth;
6
using Hammock.Web;
7
using RestCore.Tests.Helpers;
8
using NUnit.Framework;
9

    
10
namespace Hammock.Tests.OAuth
11
{
12
    [TestFixture]
13
    public class OAuthToolsTests
14
    {
15
        [Test]
16
        public void Can_construct_http_request_url()
17
        {
18
            const string expected = "http://example.com/resource";
19
            var input = "HTTP://Example.com:80/resource?id=123".AsUri();
20
            var actual = OAuthTools.ConstructRequestUrl(input);
21

    
22
            Assert.AreEqual(expected, actual);
23
            Console.WriteLine(actual);
24
        }
25

    
26
        [Test]
27
        public void Can_construct_https_request_url()
28
        {
29
            const string expected = "https://example.com/resource";
30
            var input = "HTTPS://Example.com:443/resource?id=123".AsUri();
31
            var actual = OAuthTools.ConstructRequestUrl(input);
32

    
33
            Assert.AreEqual(expected, actual);
34
            Console.WriteLine(actual);
35
        }
36

    
37
        [Test]
38
        public void Can_construct_non_standard_port_http_request_url()
39
        {
40
            const string expected = "http://example.com:8080/resource";
41
            var input = "HTTP://Example.com:8080/resource?id=123".AsUri();
42
            var actual = OAuthTools.ConstructRequestUrl(input);
43

    
44
            Assert.AreEqual(expected, actual);
45
            Console.WriteLine(actual);
46
        }
47

    
48
        [Test]
49
        public void Can_construct_non_standard_port_https_request_url()
50
        {
51
            const string expected = "https://example.com:8080/resource";
52
            var input = "HTTPS://Example.com:8080/resource?id=123".AsUri();
53
            var actual = OAuthTools.ConstructRequestUrl(input);
54

    
55
            Assert.AreEqual(expected, actual);
56
            Console.WriteLine(actual);
57
        }
58

    
59
        [Test]
60
        public void Can_generate_nonce()
61
        {
62
            var nonce = OAuthTools.GetNonce();
63

    
64
            Assert.IsNotNull(nonce);
65
            Assert.IsTrue(nonce.Length == 16);
66
            Console.WriteLine(nonce);
67

    
68
            var next = OAuthTools.GetNonce();
69

    
70
            Assert.AreNotEqual(nonce, next);
71
            Console.WriteLine(next);
72
        }
73

    
74
        [Test]
75
        public void Can_generate_timestamp()
76
        {
77
            var timestamp = OAuthTools.GetTimestamp();
78
            Assert.IsNotNull(timestamp);
79
            Assert.IsTrue(timestamp.Length == 10, "What century is this?");
80
            Console.WriteLine(timestamp);
81
        }
82

    
83
        [Test]
84
        public void Can_guarantee_random_nonces_in_succession()
85
        {
86
            var nonces = new List<string>();
87
            for (var i = 0; i < 10000; i++)
88
            {
89
                var nonce = OAuthTools.GetNonce();
90
                var timestamp = DateTime.Now;
91

    
92
                Console.WriteLine(nonce + ":" + timestamp);
93

    
94
                if (nonces.Contains(nonce))
95
                {
96
                    Assert.Fail("non-unique nonce seed generated");
97
                }
98
                else
99
                {
100
                    nonces.Add(nonce);
101
                }
102
            }
103
        }
104
#if !Smartphone
105
        [Test]
106
        public void Can_guarantee_random_nonces_in_succession_multithreaded()
107
        {
108
            const int threads = 16;
109
            const int totalNonces = 30000;
110
            const int noncesPerThread = totalNonces / threads;
111
            var nonces = new List<string>();
112
            var noncesLock = new object();
113
            var dupes = new List<string>();
114
            var dupesLock = new object();
115
            var sem = new Semaphore(0, threads);
116
            var ts = new ThreadStart(() =>
117
                                         {
118
                                             sem.WaitOne();
119
                                             try
120
                                             {
121
                                                 var localNonces = new List<string>();
122
                                                 for (var i = 0; i < noncesPerThread; i++)
123
                                                 {
124
                                                     var nonce = OAuthTools.GetNonce();
125
                                                     localNonces.Add(nonce);
126
                                                 }
127
                                                 lock (nonces)
128
                                                 {
129
                                                     var localDupes = from s in nonces 
130
                                                                      where localNonces.Contains(s) 
131
                                                                      select s;
132
                                                     if (localDupes.Any())
133
                                                     {
134
                                                         lock(dupesLock)
135
                                                         {
136
                                                             dupes.AddRange(localDupes);
137
                                                         }
138
                                                     }
139
                                                     nonces.AddRange(localNonces.Except(localDupes));
140
                                                 }
141
                                             }
142
                                             finally
143
                                             {
144
                                                 sem.Release();
145
                                             }
146
                                         });
147
            var workerThreads = new Thread[threads];
148
            for (var i = 0; i < threads; i++)
149
            {
150
                workerThreads[i] = new Thread(ts) { IsBackground = false, Name = "thread" + i };
151
                workerThreads[i].Start();
152
            }
153

    
154
            sem.Release(threads);
155
            foreach (var t in workerThreads)
156
            {
157
                t.Join();
158
            }
159
            Assert.IsEmpty(dupes, "Found {0} duplicated nonces generated during test", dupes.Count);
160
            lock (noncesLock)
161
            {
162
                Assert.AreEqual(totalNonces, nonces.Count);
163
            }
164
        }
165
#endif
166

    
167
        [Test]
168
        public void Can_sort_and_normalize_parameters()
169
        {
170
            var input = new WebParameterCollection
171
                            {
172
                                {"a", "1"},
173
                                {"f", "50"},
174
                                {"f", "25"},
175
                                {"z", "t"},
176
                                {"f", "a"},
177
                                {"c", "hi there"},
178
                                {"z", "p"},
179
                            };
180

    
181
            const string expected = "a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t";
182
            var actual = OAuthTools.NormalizeRequestParameters(input);
183
            Console.WriteLine(actual);
184

    
185
            Assert.AreEqual(expected, actual);
186
        }
187

    
188
        [Test]
189
        public void Can_sort_and_normalize_parameters_excluding_signature()
190
        {
191
            var input = new WebParameterCollection
192
                            {
193
                                {"a", "1"},
194
                                {"f", "50"},
195
                                {"f", "25"},
196
                                {"z", "t"},
197
                                {"oauth_signature", "signature"},
198
                                {"f", "a"},
199
                                {"c", "hi there"},
200
                                {"z", "p"},
201
                            };
202

    
203
            const string expected = "a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t";
204
            var actual = OAuthTools.NormalizeRequestParameters(input);
205
            Console.WriteLine(actual);
206

    
207
            Assert.AreEqual(expected, actual);
208
        }
209

    
210
        [Test]
211
        public void Can_url_encode_with_uppercase_hexadecimals()
212
        {
213
            const string expected = "What%20century%20is%20this%3F";
214
            var actual = OAuthTools.UrlEncodeRelaxed("What century is this?");
215

    
216
            Assert.AreEqual(expected, actual);
217
            Console.WriteLine(actual);
218
        }
219

    
220
        [Test]
221
        public void Can_strict_url_encode_complex_string()
222
        {
223
            const string expected = "%21%3F%22%3B%3A%3C%3E%5C%5C%7C%60%23%24%25%5E%26%2A%2B-_%7B%7D%5B%5D";
224
            const string sequence = @"!?"";:<>\\|`#$%^&*+-_{}[]";
225

    
226
            var actual = OAuthTools.UrlEncodeStrict(sequence);
227
            Assert.AreEqual(expected, actual);
228
        }
229

    
230
        [Test]
231
        public void Can_relax_url_encode_complex_string()
232
        {
233
            // Doesn't URL encode ! or * in this sequence
234
            const string expected = "!%3F%22%3B%3A%3C%3E%5C%5C%7C%60%23%24%25%5E%26*%2B-_%7B%7D%5B%5D";
235
            const string sequence = @"!?"";:<>\\|`#$%^&*+-_{}[]";
236

    
237
            var actual = OAuthTools.UrlEncodeRelaxed(sequence);
238
            Assert.AreEqual(expected, actual);
239
        }
240
    }
241
}