File: ProtectedDataTest.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (160 lines) | stat: -rw-r--r-- 4,758 bytes parent folder | download | duplicates (8)
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
//
// ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
//
// Author:
//	Sebastien Pouliot  <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//

#if NET_2_0

using NUnit.Framework;

using System;
using System.Security.Cryptography;

namespace MonoTests.System.Security.Cryptography {

	[TestFixture]
	public class ProtectedDataTest {

		private byte[] notMuchEntropy = new byte[16];

		private bool IsEmpty (byte[] array)
		{
			int total = 0;
			for (int i = 0; i < array.Length; i++)
				total += array[i];
			return (total == 0);
		}

		private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope) 
		{
			try {
				byte[] data = new byte [16];
				byte[] encdata = ProtectedData.Protect (data, entropy, scope);
				Assert.IsFalse (IsEmpty (encdata), "Protect");

				byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
				Assert.IsTrue (IsEmpty (decdata), "Unprotect");
			}
			catch (CryptographicException ce) {
				if (ce.InnerException is UnauthorizedAccessException)
					Assert.Ignore ("The machine key store hasn't yet been created (as root).");
			}
			catch (PlatformNotSupportedException) {
				Assert.Ignore ("Only supported under Windows 2000 and later");
			}
		}

		[Test]
		public void ProtectCurrentUser () 
		{
			// we're testing the DataProtectionScope definition but
			// not if it's really limited to the scope specified
			ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
		}

		[Test]
		public void ProtectLocalMachine () 
		{
			// we're testing the DataProtectionScope definition but
			// not if it's really limited to the scope specified
			ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
		}

		[Test]
		public void DataProtectionScope_All ()
		{
			byte[] data = new byte[16];
			try {
				foreach (DataProtectionScope dps in Enum.GetValues (typeof (DataProtectionScope))) {
					byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, dps);
					Assert.IsFalse (IsEmpty (encdata), "Protect");
					Assert.IsTrue (IsEmpty (data), "Protect(original unmodified)");
					byte[] decdata = ProtectedData.Unprotect (encdata, notMuchEntropy, dps);
					Assert.IsTrue (IsEmpty (decdata), "Unprotect");
				}
			}
			catch (CryptographicException ce) {
				if (ce.InnerException is UnauthorizedAccessException)
					Assert.Ignore ("The machine key store hasn't yet been created (as root).");
			}
			catch (PlatformNotSupportedException) {
				Assert.Ignore ("Only supported under Windows 2000 and later");
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		[Category ("NotDotNet")]
		public void Protect_InvalidDataProtectionScope ()
		{
			try {
				byte[] data = new byte[16];
				ProtectedData.Protect (data, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
				// MS doesn't throw an ArgumentException but returning from
				// this method will throw an UnhandledException in NUnit
			}
			catch (PlatformNotSupportedException) {
				Assert.Ignore ("Only supported under Windows 2000 and later");
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ProtectNull () 
		{
			ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
		}

		[Test]
		public void ProtectNullEntropy () 
		{
			// we're testing the DataProtectionScope definition but
			// not if it's really limited to the scope specified
			ProtectUnprotect (null, DataProtectionScope.CurrentUser);
		}

		[Test]
		[ExpectedException (typeof (CryptographicException))]
		public void UnprotectNotProtectedData () 
		{
			try {
				byte[] baddata = new byte [16];
				ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
			}
			catch (PlatformNotSupportedException) {
				Assert.Ignore ("Only supported under Windows 2000 and later");
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		[Category ("NotDotNet")]
		public void Unprotect_InvalidDataProtectionScope ()
		{
			try {
				byte[] data = new byte[16];
				byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, DataProtectionScope.CurrentUser);
				ProtectedData.Unprotect (encdata, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
				// MS doesn't throw an ArgumentException but returning from
				// this method will throw an UnhandledException in NUnit
			}
			catch (PlatformNotSupportedException) {
				Assert.Ignore ("Only supported under Windows 2000 and later");
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void UnprotectNull () 
		{
			ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
		}
	}
}

#endif