This repository was archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGuardedByteArrayTests.cs
102 lines (96 loc) · 3.68 KB
/
GuardedByteArrayTests.cs
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
/*
* ====================
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License("CDDL") (the "License"). You may not use this file
* except in compliance with the License.
*
* You can obtain a copy of the License at
* http://opensource.org/licenses/cddl1.php
* See the License for the specific language governing permissions and limitations
* under the License.
*
* When distributing the Covered Code, include this CDDL Header Notice in each file
* and include the License file at http://opensource.org/licenses/cddl1.php.
* If applicable, add the following below this CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
* Portions Copyrighted 2014 ForgeRock AS.
*/
using System;
using Org.IdentityConnectors.Common.Security;
using Org.IdentityConnectors.Framework.Common.Serializer;
using NUnit.Framework;
namespace FrameworkTests
{
[TestFixture]
public class GuardedByteArrayTests
{
[Test]
public void TestBasics()
{
GuardedByteArray ss = new GuardedByteArray();
ss.AppendByte(0x00);
ss.AppendByte(0x01);
ss.AppendByte(0x02);
byte[] decrypted = DecryptToByteArray(ss);
Assert.AreEqual(new byte[] { 0x00, 0x01, 0x02 }, decrypted);
String hash = ss.GetBase64SHA1Hash();
Assert.IsTrue(ss.VerifyBase64SHA1Hash(hash));
ss.AppendByte(0x03);
Assert.IsFalse(ss.VerifyBase64SHA1Hash(hash));
}
[Test]
public void TestRange()
{
for (byte i = 0; i < 0xFF; i++)
{
byte expected = i;
GuardedByteArray gba = new GuardedByteArray();
gba = (GuardedByteArray)SerializerUtil.CloneObject(gba);
gba.AppendByte(i);
gba.Access(new GuardedByteArray.LambdaAccessor(clearChars =>
{
int v = (byte)clearChars[0];
Assert.AreEqual(expected, v);
}));
}
}
[Test]
public void TestEquals()
{
GuardedByteArray arr1 = new GuardedByteArray();
GuardedByteArray arr2 = new GuardedByteArray();
Assert.AreEqual(arr1, arr2);
arr2.AppendByte(0x02);
Assert.AreNotEqual(arr1, arr2);
arr1.AppendByte(0x02);
Assert.AreEqual(arr1, arr2);
}
/// <summary>
/// Highly insecure method! Do not do this in production
/// code.
/// </summary>
/// <remarks>
/// This is only for test purposes
/// </remarks>
private byte[] DecryptToByteArray(GuardedByteArray str)
{
byte[] result = null;
str.Access(new GuardedByteArray.LambdaAccessor(
array =>
{
result = new byte[array.Length];
for (int i = 0; i < array.Length; i++)
{
result[i] = array[i];
}
}));
return result;
}
}
}