root / trunk / Pithos.Network.Test / SignatureTest.cs @ 1caef52e
History | View | Annotate | Download (2.9 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.IO; |
4 |
using System.Linq; |
5 |
using System.Security.Cryptography; |
6 |
using System.Text; |
7 |
using System.Threading; |
8 |
using System.Threading.Tasks; |
9 |
using NUnit.Framework; |
10 |
using Newtonsoft.Json; |
11 |
using Pithos.Network; |
12 |
|
13 |
namespace Pithos.Core.Test |
14 |
{ |
15 |
[TestFixture] |
16 |
public class SignatureTest |
17 |
{ |
18 |
|
19 |
[Test] |
20 |
public void TestCreate() |
21 |
{ |
22 |
var hasher = HashAlgorithm.Create("sha256"); |
23 |
Assert.IsNotNull(hasher); |
24 |
} |
25 |
|
26 |
[Test] |
27 |
public void TestHashmapCreation() |
28 |
{ |
29 |
var file = "e:\\pithos\\vlc-1.1.11-win32.exe"; |
30 |
|
31 |
decimal blockSize = 4*1024*1024; |
32 |
|
33 |
var fileSize = new FileInfo(file).Length; |
34 |
var numBlocks = decimal.Ceiling(fileSize/blockSize); |
35 |
|
36 |
var md5 = Signature.CalculateMD5(file); |
37 |
|
38 |
var hash1 = Signature.CalculateTreeHashAsync(file, (int) blockSize,"sha256").Result; |
39 |
Assert.IsNotNull(hash1.Hashes); |
40 |
Assert.AreEqual(numBlocks, hash1.Hashes.Count()); |
41 |
|
42 |
var topHash = hash1.TopHash; |
43 |
var hashString = BytesToStr(topHash); |
44 |
|
45 |
var stringHashes = (from hash in hash1.Hashes |
46 |
select BytesToStr(hash)).ToList(); |
47 |
var hashes = JsonConvert.SerializeObject(stringHashes); |
48 |
Assert.IsNotNull(topHash); |
49 |
} |
50 |
|
51 |
[Test] |
52 |
public void TestHashmapStorage() |
53 |
{ |
54 |
var file = "e:\\pithos\\vlc-1.1.11-win32.exe"; |
55 |
|
56 |
decimal blockSize = 4*1024*1024; |
57 |
|
58 |
var fileSize = new FileInfo(file).Length; |
59 |
var numBlocks = decimal.Ceiling(fileSize/blockSize); |
60 |
|
61 |
var md5 = Signature.CalculateMD5(file); |
62 |
|
63 |
var hash1 = Signature.CalculateTreeHashAsync(file, (int) blockSize, "sha256").Result; |
64 |
hash1.FileId = Guid.NewGuid(); |
65 |
var task = hash1.Save(@"e:\") |
66 |
.ContinueWith(_ => TreeHash.LoadTreeHash(@"e:\", hash1.FileId)).Unwrap(); |
67 |
task.ContinueWith(t => |
68 |
{ |
69 |
var hash = t.Result; |
70 |
Assert.AreEqual(hash1.Hashes, hash.Hashes.ToArray()); |
71 |
int i = 0; |
72 |
}).Wait(); |
73 |
} |
74 |
|
75 |
public static string BytesToStr(byte[] bytes) |
76 |
{ |
77 |
var str = new StringBuilder(); |
78 |
|
79 |
foreach (byte t in bytes) |
80 |
str.AppendFormat("{0:X2}", t); |
81 |
|
82 |
return str.ToString(); |
83 |
} |
84 |
|
85 |
|
86 |
[Test] |
87 |
public void TestTopHashEmpty() |
88 |
{ |
89 |
using (var hasher = HashAlgorithm.Create("sha256")) |
90 |
{ |
91 |
var hashEmpty = hasher.ComputeHash(new byte[] {}); |
92 |
|
93 |
var empty = new List<byte[]>(); |
94 |
var hash = Signature.CalculateTopHash(empty,"sha256"); |
95 |
Assert.IsNull(hash); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
} |