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