root / trunk / Pithos.Core.Test / NetworkAgentTest.cs @ c53aa229
History | View | Annotate | Download (3.2 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.IO; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
using NUnit.Framework; |
8 |
using Pithos.Core.Agents; |
9 |
using Pithos.Network; |
10 |
|
11 |
namespace Pithos.Core.Test |
12 |
{ |
13 |
[TestFixture] |
14 |
class NetworkAgentTest |
15 |
{ |
16 |
[Test] |
17 |
public void TestUpload() |
18 |
{ |
19 |
var agent = new NetworkAgent(); |
20 |
|
21 |
var account = "890329@vho.grnet.gr"; |
22 |
var apiKey = "24989dce4e0fcb072f8cb60c8922be19"; |
23 |
|
24 |
var client = new CloudFilesClient(account,apiKey) |
25 |
{ |
26 |
AuthenticationUrl = @"https://pithos.dev.grnet.gr", |
27 |
UsePithos = true |
28 |
}; |
29 |
|
30 |
|
31 |
var accountInfo=client.Authenticate(); |
32 |
|
33 |
var fileName = "012345.dump"; |
34 |
var filePath = Path.Combine(@"e:\pithos\", fileName); |
35 |
|
36 |
var random = new Random(); |
37 |
var buffer = new byte[4096]; |
38 |
random.NextBytes(buffer); |
39 |
|
40 |
if (File.Exists(filePath)) |
41 |
File.Delete(filePath); |
42 |
|
43 |
using (var stream = File.OpenWrite(filePath)) |
44 |
{ |
45 |
for (var i = 0; i < 2* 1024;i++ ) |
46 |
stream.Write(buffer, 0, buffer.Length); |
47 |
} |
48 |
|
49 |
|
50 |
client.DeleteObject(null, FolderConstants.PithosContainer, fileName); |
51 |
|
52 |
var task = Signature.CalculateTreeHashAsync(filePath, accountInfo.BlockSize, accountInfo.BlockHash); |
53 |
var tasks=agent.UploadWithHashMap(accountInfo,account,"pithos",new FileInfo(filePath),fileName,task); |
54 |
Task.Factory.Iterate(tasks).Wait(); |
55 |
|
56 |
var newHash = client.GetHashMap(null, FolderConstants.PithosContainer, fileName).Result; |
57 |
|
58 |
|
59 |
var treeHash = task.Result; |
60 |
Assert.AreEqual(treeHash.TopHash, newHash.TopHash); |
61 |
|
62 |
Assert.AreEqual(treeHash.Hashes, newHash.Hashes); |
63 |
|
64 |
} |
65 |
|
66 |
[Test] |
67 |
public void TestDownload() |
68 |
{ |
69 |
var fileAgent = new FileAgent {FragmentsPath = @"e:\pithos\Fragments"}; |
70 |
|
71 |
var agent = new NetworkAgent(); |
72 |
|
73 |
var account = "890329@vho.grnet.gr"; |
74 |
var apiKey = "24989dce4e0fcb072f8cb60c8922be19"; |
75 |
var client = new CloudFilesClient(account,apiKey) |
76 |
{ |
77 |
AuthenticationUrl = @"https://pithos.dev.grnet.gr", |
78 |
UsePithos = true |
79 |
}; |
80 |
|
81 |
var accountInfo=client.Authenticate(); |
82 |
|
83 |
var fileName = @"AccessDatabaseEngine_x64.exe"; |
84 |
|
85 |
var filePath = Path.Combine(@"e:\pithos\", fileName); |
86 |
if (File.Exists(filePath)) |
87 |
File.Delete(filePath); |
88 |
|
89 |
var newHash = client.GetHashMap(null, FolderConstants.PithosContainer, fileName).Result; |
90 |
agent.DownloadWithBlocks(client, account, FolderConstants.PithosContainer, new Uri(fileName, UriKind.Relative), filePath, newHash) |
91 |
.Wait(); |
92 |
|
93 |
Assert.IsTrue(File.Exists(filePath)); |
94 |
var treeHash = Signature.CalculateTreeHashAsync(filePath, accountInfo.BlockSize, accountInfo.BlockHash).Result; |
95 |
|
96 |
Assert.AreEqual(treeHash.TopHash, newHash.TopHash); |
97 |
|
98 |
Assert.AreEqual(treeHash.Hashes, newHash.Hashes); |
99 |
|
100 |
} |
101 |
|
102 |
} |
103 |
} |