00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.Xml;
00005 using System.Xml.Serialization;
00006 using System.IO;
00007 using System.Collections;
00008 using System.Threading;
00009 using NUnit.Framework;
00010
00011 using ThexCS;
00012 using ICSharpCode.SharpZipLib;
00013
00014
00015 namespace DCPlusPlus
00016 {
00022 [Serializable ,TestFixture]
00023 public class Sharing: ICollection<Sharing.SharingEntry>
00024 {
00029 public class SharingEntry
00030 {
00031 protected long filesize = 0;
00035 public long Filesize
00036 {
00037 get
00038 {
00039 return (filesize);
00040 }
00041 set
00042 {
00043 filesize = value;
00044 }
00045 }
00049 [XmlIgnoreAttribute]
00050 public bool HasTTH
00051 {
00052 get
00053 {
00054 if (!string.IsNullOrEmpty(tth)) return (true);
00055 else return (false);
00056
00057 }
00058 }
00059 protected string tth = "";
00063 public string TTH
00064 {
00065 get
00066 {
00067 return (tth);
00068 }
00069 set
00070 {
00071 tth = value;
00072 }
00073 }
00074 protected string filename = "";
00078 public string Filename
00079 {
00080 get
00081 {
00082 return (filename);
00083 }
00084 set
00085 {
00086 filename = value;
00087 }
00088 }
00089 }
00090 protected List<SharingEntry> items = new List<SharingEntry>();
00091
00096 public List<SharingEntry> Items
00097 {
00098 get
00099 {
00100 return (items);
00101 }
00102 set
00103 {
00104 items = value;
00105 }
00106 }
00107 protected long total_bytes_shared = 0;
00112 [XmlIgnoreAttribute]
00113 public long TotalBytesShared
00114 {
00115 get
00116 {
00117
00118 return (total_bytes_shared);
00119 }
00120 }
00124 public int Count
00125 {
00126 get
00127 {
00128 return (items.Count);
00129 }
00130 }
00135 [XmlIgnoreAttribute]
00136 protected Object share_lock = new Object();
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00154 public delegate void DirectoryFinishedEventHandler(string directory);
00159 public event DirectoryFinishedEventHandler DirectoryFinished;
00164 public delegate void EntryAddedEventHandler(SharingEntry entry);
00169 public event EntryAddedEventHandler EntryAdded;
00174 public delegate void EntryRemovedEventHandler(SharingEntry entry);
00179 public event EntryRemovedEventHandler EntryRemoved;
00183 public delegate void EntriesChangedEventHandler();
00189 public event EntriesChangedEventHandler EntriesChanged;
00193 public delegate void EntriesClearedEventHandler();
00198 public event EntriesClearedEventHandler EntriesCleared;
00204 private delegate SharingEntry ShareFileHandler(string filename);
00212 private SharingEntry ShareFileAsync(string filename)
00213 {
00214 if (!System.IO.File.Exists(filename)) return (null);
00215 SharingEntry entry = new SharingEntry();
00216 entry.Filename = filename;
00217 try
00218 {
00219 System.IO.FileInfo fi = new FileInfo(filename);
00220 entry.Filesize = fi.Length;
00221
00222
00223
00224
00225
00226
00227
00228
00229 ThexThreaded TTH = new ThexThreaded();
00230 entry.TTH = Base32.ToBase32String(TTH.GetTTH_Value(filename));
00231
00232 }
00233 catch (Exception ex)
00234 {
00235 Console.WriteLine("exception during hashing:" + ex.Message);
00236 }
00237 return (entry);
00238 }
00247 private delegate string ShareDirectoryHandler(string directory);
00253 private string ShareDirectoryAsync(string directory)
00254 {
00255 RecurseShareDirectoryAsync(directory);
00256 return (directory);
00257 }
00262 private void RecurseShareDirectoryAsync(string directory)
00263 {
00264 if (!Directory.Exists(directory)) return;
00265 string[] files = Directory.GetFiles(directory);
00266 string[] dirs = Directory.GetDirectories(directory);
00267 foreach (string dir in dirs)
00268 {
00269 if (System.IO.Directory.Exists(dir))
00270 {
00271 RecurseShareDirectoryAsync(dir);
00272 }
00273 }
00274 foreach (string file in files)
00275 {
00276 if (System.IO.File.Exists(file))
00277 {
00278 SharingEntry entry = ShareFileAsync(file);
00279 if (entry != null)
00280 {
00281 total_bytes_shared += entry.Filesize;
00282 Add(entry);
00283 }
00284 }
00285 }
00286 }
00291 private void ShareFileFinished(IAsyncResult result)
00292 {
00293 ShareFileHandler sfh = (ShareFileHandler)result.AsyncState;
00294 SharingEntry entry = sfh.EndInvoke(result);
00295 if (entry != null)
00296 {
00297 total_bytes_shared += entry.Filesize;
00298 Add(entry);
00299 }
00300 file_list_needs_update = true;
00301 }
00306 private void ShareDirectoryFinished(IAsyncResult result)
00307 {
00308 ShareDirectoryHandler sdh = (ShareDirectoryHandler)result.AsyncState;
00309 string directory = sdh.EndInvoke(result);
00310 if (DirectoryFinished != null)
00311 DirectoryFinished(directory);
00312 file_list_needs_update = true;
00313 }
00314
00320 public void ShareFile(string filename)
00321 {
00322 ShareFileHandler sfh = new ShareFileHandler(ShareFileAsync);
00323 IAsyncResult result = sfh.BeginInvoke(filename, new AsyncCallback(ShareFileFinished), sfh);
00324 }
00330 public void ShareDirectory(string directory)
00331 {
00332 ShareDirectoryHandler sdh = new ShareDirectoryHandler(ShareDirectoryAsync);
00333 IAsyncResult result = sdh.BeginInvoke(directory, new AsyncCallback(ShareDirectoryFinished), sdh);
00334 }
00342 public SharingEntry GetShareByFileRequest(string file_request)
00343 {
00344 if(file_request.StartsWith("TTH/"))
00345 return(GetShareByTTH(file_request.Substring(4)));
00346 return (GetShareByFilename(file_request));
00347 }
00348
00354 public SharingEntry GetShareByFilename(string filename)
00355 {
00356 SharingEntry ret = null;
00357 lock (share_lock)
00358 {
00359 foreach (SharingEntry entry in items)
00360 {
00361 if (entry.Filename.EndsWith(filename))
00362 {
00363
00364 ret = entry;
00365 break;
00366 }
00367 }
00368 }
00369 return (ret);
00370 }
00376 public SharingEntry GetShareByTTH(string tth)
00377 {
00378 SharingEntry ret = null;
00379 lock (share_lock)
00380 {
00381 foreach (SharingEntry entry in items)
00382 {
00383 if (entry.HasTTH && entry.TTH == tth)
00384 {
00385 ret = entry;
00386 break;
00387 }
00388 }
00389 }
00390 return (ret);
00391 }
00397 public void Add(SharingEntry item)
00398 {
00399 lock (share_lock)
00400 {
00401 items.Add(item);
00402 }
00403 try
00404 {
00405 if (EntryAdded != null)
00406 EntryAdded(item);
00407 if (EntriesChanged != null)
00408 EntriesChanged();
00409 }
00410 catch (Exception ex)
00411 {
00412 Console.WriteLine("Exception occured in added event callback: " + ex.Message);
00413 }
00414 file_list_needs_update = true;
00415 }
00419 public void Clear()
00420 {
00421 lock (share_lock)
00422 {
00423 items.Clear();
00424 }
00425 try
00426 {
00427
00428 if (EntriesCleared != null)
00429 EntriesCleared();
00430 if (EntriesChanged != null)
00431 EntriesChanged();
00432 }
00433 catch (Exception ex)
00434 {
00435 Console.WriteLine("Exception occured in clear event callback: " + ex.Message);
00436 }
00437 file_list_needs_update = true;
00438 }
00446 public bool Remove(SharingEntry item)
00447 {
00448 total_bytes_shared -= item.Filesize;
00449 bool ret = false;
00450 lock (share_lock)
00451 {
00452 ret = items.Remove(item);
00453 }
00454 if (ret == false) return (false);
00455 try
00456 {
00457 if (EntryRemoved != null)
00458 EntryRemoved(item);
00459 if (EntriesChanged != null)
00460 EntriesChanged();
00461 }
00462 catch (Exception ex)
00463 {
00464 Console.WriteLine("Exception occured in remove event callback: " + ex.Message);
00465 }
00466 file_list_needs_update = true;
00467 return (ret);
00468 }
00476 public bool Remove(string filename)
00477 {
00478 SharingEntry ret = null;
00479 lock (share_lock)
00480 {
00481 foreach (SharingEntry entry in items)
00482 {
00483 if (entry.Filename == filename)
00484 {
00485 items.Remove(entry);
00486 ret = entry;
00487 break;
00488 }
00489 }
00490 }
00491 if (ret != null)
00492 {
00493 total_bytes_shared -= ret.Filesize;
00494 try
00495 {
00496 if (EntryRemoved != null)
00497 EntryRemoved(ret);
00498 if (EntriesChanged != null)
00499 EntriesChanged();
00500 }
00501 catch (Exception ex)
00502 {
00503 Console.WriteLine("Exception occured in remove event callback: " + ex.Message);
00504 }
00505 file_list_needs_update = true;
00506 return (true);
00507 }
00508 return (false);
00509 }
00514 public void LoadSharesFromXml(string xml)
00515 {
00516 lock (share_lock)
00517 {
00518 items.Clear();
00519 }
00520 if (EntriesCleared != null)
00521 EntriesCleared();
00522 Sharing s = new Sharing();
00523 try
00524 {
00525 XmlSerializer serializer = new XmlSerializer(typeof(Sharing));
00526 MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(xml));
00527 s = (Sharing)serializer.Deserialize(ms);
00528 }
00529 catch (Exception ex)
00530 {
00531 Console.WriteLine("Error deserializing queue: " + ex.Message);
00532 }
00533 if (s != null)
00534 {
00535 lock (share_lock)
00536 {
00537 items = s.Items;
00538 }
00539 }
00540 if (EntryAdded != null)
00541 {
00542 lock (share_lock)
00543 {
00544 foreach (SharingEntry entry in items)
00545 {
00546 total_bytes_shared += entry.Filesize;
00547 EntryAdded(entry);
00548 }
00549 }
00550 }
00551 if (EntriesChanged != null)
00552 {
00553 EntriesChanged();
00554 }
00555 file_list_needs_update = true;
00556 }
00561 public string SaveSharesToXml()
00562 {
00563
00564 lock (share_lock)
00565 {
00566 string ret = "";
00567 try
00568 {
00569 XmlSerializer serializer = new XmlSerializer(typeof(Sharing));
00570 MemoryStream ms = new MemoryStream();
00571 serializer.Serialize(ms, this);
00572 ms.Flush();
00573 ret = System.Text.Encoding.Default.GetString(ms.GetBuffer(), 0, (int)ms.Length);
00574
00575
00576 }
00577 catch (Exception ex)
00578 {
00579 Console.WriteLine("Error serializing queue: " + ex.Message);
00580 return (null);
00581 }
00582 return (ret);
00583 }
00584 }
00589 public void LoadSharesFromXmlFile(string filename)
00590 {
00591 try
00592 {
00593 if (!File.Exists(filename)) return;
00594 LoadSharesFromXml(System.IO.File.ReadAllText(filename));
00595 }
00596 catch (Exception ex)
00597 {
00598 Console.WriteLine("Error loading queue from: " + filename + " : " + ex.Message);
00599 }
00600 }
00605 public void SaveSharesToXmlFile(string filename)
00606 {
00607 try
00608 {
00609 if (File.Exists(filename + ".backup") && File.Exists(filename))
00610 File.Delete(filename + ".backup");
00611 if (File.Exists(filename))
00612 File.Move(filename, filename + ".backup");
00613
00614 System.IO.File.WriteAllText(filename, SaveSharesToXml());
00615 }
00616 catch (Exception ex)
00617 {
00618 Console.WriteLine("Error saving queue to: " + filename + " : " + ex.Message);
00619 }
00620 }
00626 private bool file_list_needs_update = true;
00630 private string file_list="";
00634 private byte[] file_list_bz2;
00638 public void UpdateFileLists()
00639 {
00640 string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n";
00641 xml += "<FileListing Version=\"1\" CID=\"" + cid + "\" Base=\"/\" Generator=\"" + generator + "\">\n";
00642
00643
00644
00645
00646
00647
00648 DirectoryContents root = new DirectoryContents();
00649 FillDirectories(root);
00650 bool empty_shares = CleanDirectories(root);
00651 xml += GetDirectoryContentsString(root);
00652 xml += "</FileListing>\n";
00653
00654
00655
00656 file_list = xml;
00657 try
00658 {
00659 MemoryStream input = new MemoryStream(System.Text.Encoding.Default.GetBytes(file_list));
00660 MemoryStream output = new MemoryStream();
00661 ICSharpCode.SharpZipLib.BZip2.BZip2.Compress(input, output, 1024);
00662 input.Flush();
00663 byte[] out_data = output.GetBuffer();
00664 file_list_bz2 = out_data;
00665 }
00666 catch (Exception ex)
00667 {
00668 Console.WriteLine("Error compressing file list: " + ex.Message);
00669 file_list_bz2 = null;
00670 }
00671 }
00672 protected string cid = "D2QLOGUYDX3QA";
00678 public string CID
00679 {
00680 get
00681 {
00682 return (cid);
00683 }
00684 set
00685 {
00686 cid = value;
00687 }
00688 }
00689 protected string generator = "vpDcPlusPlus 0.2";
00693 public string Generator
00694 {
00695 get
00696 {
00697 return (generator);
00698 }
00699 set
00700 {
00701 generator = value;
00702 }
00703 }
00707 public class DirectoryContents
00708 {
00712 public string directory_name = "";
00716 public List<SharingEntry> files = new List<SharingEntry>();
00720 public List<DirectoryContents> directories = new List<DirectoryContents>();
00721 }
00728 private static string ToXmlString(string org)
00729 {
00730 if (String.IsNullOrEmpty(org)) return ("");
00731
00732 string tmp = new string(org.ToCharArray());
00733
00734 int p = 0;
00735
00736 List<int> amps = new List<int>();
00737 while ((p = tmp.IndexOf("&", p)) != -1)
00738 {
00739
00740 amps.Add(p);
00741 if (p < tmp.Length - 1) p++;
00742 else break;
00743
00744
00745 }
00746
00747 for (int i = 0; i < amps.Count; i++)
00748 {
00749
00750 tmp = tmp.Remove(amps[i] + (i * 4), 1);
00751 tmp = tmp.Insert(amps[i] + (i * 4), "&");
00752
00753 }
00754
00755
00756 while (tmp.IndexOf("<") != -1) tmp = tmp.Replace("<", "<");
00757 while (tmp.IndexOf(">") != -1) tmp = tmp.Replace(">", ">");
00758 while (tmp.IndexOf("\"") != -1) tmp = tmp.Replace("\"", """);
00759 while (tmp.IndexOf("'") != -1) tmp = tmp.Replace("'", "'");
00760 for (int i = 0; i < 32; i++)
00761 {
00762 char c = Convert.ToChar(i);
00763 if (i != 0x09 && i != 0x0a && i != 0x0d)
00764 {
00765 int pos = -1;
00766 while ((pos = tmp.IndexOf(c)) != -1)
00767 {
00768 tmp = tmp.Remove(pos, 1);
00769 tmp = tmp.Insert(pos, "&#" + Convert.ToString(c, 16) + ";");
00770 }
00771 }
00772 }
00773
00774 return (tmp);
00775 }
00782 private static string FromXmlString(string org)
00783 {
00784 if (org == null) return ("");
00785 string tmp = new string(org.ToCharArray());
00786
00787 while (tmp.IndexOf("&") != -1) tmp = tmp.Replace("&", "&");
00788 while (tmp.IndexOf("<") != -1) tmp = tmp.Replace("<", "<");
00789 while (tmp.IndexOf(">") != -1) tmp = tmp.Replace(">", ">");
00790 while (tmp.IndexOf(""") != -1) tmp = tmp.Replace(""", "\"");
00791 while (tmp.IndexOf("'") != -1) tmp = tmp.Replace("'", "'");
00792 for (int i = 0; i < 32; i++)
00793 {
00794 char c = Convert.ToChar(i);
00795 if (i != 0x09 && i != 0x0a && i != 0x0d)
00796 {
00797 int pos = -1;
00798 string s = "&#" + Convert.ToString(c, 16) + ";";
00799 while ((pos = tmp.IndexOf(s)) != -1)
00800 {
00801 tmp = tmp.Remove(pos, s.Length);
00802 tmp = tmp.Insert(pos, c.ToString());
00803 }
00804 }
00805 }
00806
00807 return (tmp);
00808 }
00815 private DirectoryContents FindExistingDirectory(DirectoryContents dc,string directory_name)
00816 {
00817 foreach (DirectoryContents dir in dc.directories)
00818 {
00819 if (dir.directory_name == directory_name) return (dir);
00820 }
00821 return (null);
00822 }
00827 private void FillDirectories(DirectoryContents root)
00828 {
00829 lock (share_lock)
00830 {
00831 foreach (SharingEntry entry in items)
00832 {
00833 string path = Path.GetDirectoryName(entry.Filename);
00834 path = path.Substring(3);
00835 char[] seps = {'\\'};
00836 string[] paths = path.Split(seps);
00837 DirectoryContents actual = root;
00838 foreach (string path_part in paths)
00839 {
00840 DirectoryContents existing = FindExistingDirectory(actual,path_part);
00841 if (existing == null)
00842 {
00843 DirectoryContents add = new DirectoryContents();
00844 add.directory_name = path_part;
00845 actual.directories.Add(add);
00846 existing = add;
00847 }
00848 actual = existing;
00849 }
00850 if(!actual.files.Contains(entry))
00851 actual.files.Add(entry);
00852
00853
00854 }
00855 }
00856 }
00863 private bool CleanDirectories(DirectoryContents dc)
00864 {
00865
00866 foreach (DirectoryContents dir in dc.directories)
00867 {
00868 if (!CleanDirectories(dir))
00869 dc.directories.Remove(dir);
00870 }
00871 if (dc.directories.Count == 0 && dc.files.Count == 0)
00872 {
00873 return (false);
00874 }
00875 else if (dc.directories.Count == 1 && dc.files.Count == 0)
00876 {
00877 dc = dc.directories[0];
00878 }
00879 return (true);
00880 }
00886 private string GetDirectoryContentsString(DirectoryContents dc)
00887 {
00888 string dir_string = "";
00889 if (dc.directory_name != "")
00890 dir_string += "<Directory Name=\"" + ToXmlString(dc.directory_name) + "\">\n";
00891 foreach (DirectoryContents dir in dc.directories)
00892 {
00893 dir_string += GetDirectoryContentsString(dir);
00894 }
00895 if (dc.directory_name != "")
00896 {
00897 foreach (SharingEntry file in dc.files)
00898 {
00899 dir_string += "<File Name=\"" + ToXmlString(Path.GetFileName(file.Filename)) + "\" Size=\"" + file.Filesize + "\" TTH=\"" + file.TTH + "\"/>\n";
00900 }
00901 dir_string += "</Directory>\n";
00902 }
00903 return (dir_string);
00904 }
00909 public string GetFileListXml()
00910 {
00911 if (file_list_needs_update)
00912 UpdateFileLists();
00913 return (file_list);
00914 }
00919 public byte[] GetFileListXmlBZ2()
00920 {
00921 if (file_list_needs_update)
00922 UpdateFileLists();
00923 return(file_list_bz2);
00924 }
00925
00926 #region ICollection<SharingEntry> Members
00927 public bool Contains(SharingEntry item)
00928 {
00929 bool ret = false;
00930 lock (share_lock)
00931 {
00932 ret = items.Contains(item);
00933 }
00934 return (ret);
00935 }
00936
00937 public void CopyTo(SharingEntry[] array, int arrayIndex)
00938 {
00939 lock (share_lock)
00940 {
00941 foreach (SharingEntry entry in items)
00942 {
00943 array.SetValue(entry, arrayIndex);
00944 arrayIndex = arrayIndex + 1;
00945 }
00946 }
00947 }
00948
00949
00950 public bool IsReadOnly
00951 {
00952 get
00953 {
00954 return (false);
00955 }
00956 }
00957
00958
00959 #endregion
00960 #region IEnumerable<SharingEntry> Members
00961
00962 public class SharingEnumerator2 : IEnumerator<SharingEntry>
00963 {
00964 private SharingEntry[] sharing_array;
00965 private int Cursor;
00966 public SharingEnumerator2(SharingEntry[] my_array)
00967 {
00968 sharing_array = my_array;
00969 Cursor = -1;
00970 }
00971
00972 #region IEnumerator<SharingEntry> Members
00973
00974 public object Current
00975 {
00976 get
00977 {
00978 if ((Cursor < 0) || (Cursor == sharing_array.Length))
00979 throw new InvalidOperationException();
00980 return ((object)sharing_array[Cursor]);
00981 }
00982 }
00983
00984
00985 SharingEntry IEnumerator<SharingEntry>.Current
00986 {
00987 get
00988 {
00989 if ((Cursor < 0) || (Cursor == sharing_array.Length))
00990 throw new InvalidOperationException();
00991 return (sharing_array[Cursor]);
00992 }
00993 }
00994
00995 public bool MoveNext()
00996 {
00997 if (Cursor < sharing_array.Length)
00998 Cursor++;
00999 return (!(Cursor == sharing_array.Length));
01000 }
01001
01002 public void Reset()
01003 {
01004 Cursor = -1;
01005 }
01006
01007
01008
01009 #endregion
01010
01011 #region IDisposable Members
01012
01013 public void Dispose()
01014 {
01015
01016
01017 }
01018
01019 #endregion
01020 }
01021
01022 public IEnumerator<SharingEntry> GetEnumerator()
01023 {
01024 return (new SharingEnumerator2(items.ToArray()));
01025 }
01026
01027 #endregion
01028 #region IEnumerable Members
01029
01030 public class SharingEnumerator : IEnumerator
01031 {
01032 private SharingEntry[] sharing_array;
01033 private int Cursor;
01034 public SharingEnumerator(SharingEntry[] my_array)
01035 {
01036 sharing_array = my_array;
01037 Cursor = -1;
01038 }
01039
01040
01041 #region IEnumerator Members
01042
01043 public object Current
01044 {
01045 get
01046 {
01047 if ((Cursor < 0) || (Cursor == sharing_array.Length))
01048 throw new InvalidOperationException();
01049 return ((object)sharing_array[Cursor]);
01050 }
01051 }
01052
01053 public bool MoveNext()
01054 {
01055 if (Cursor < sharing_array.Length)
01056 Cursor++;
01057 return (!(Cursor == sharing_array.Length));
01058 }
01059
01060 public void Reset()
01061 {
01062 Cursor = -1;
01063 }
01064
01065 #endregion
01066 }
01067
01068 IEnumerator IEnumerable.GetEnumerator()
01069 {
01070 return (new SharingEnumerator(items.ToArray()));
01071 }
01072
01073 #endregion
01074
01075 #region Unit Testing
01079 [Test]
01080 public void TestShareFile()
01081 {
01082 Console.WriteLine("Test to share a file.");
01083 bool wait = true;
01084 Sharing s = new Sharing();
01085 s.EntryAdded = delegate(SharingEntry entry)
01086 {
01087 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01088 Assert.IsTrue(entry.Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01089 Assert.IsTrue(entry.Filesize == 6053888, "Filesize not correct(test.mp3).");
01090 wait = false;
01091 };
01092 s.ShareFile("..\\..\\..\\TestDateien\\test.mp3");
01093
01094 Console.WriteLine("Waiting for data");
01095 DateTime start = DateTime.Now;
01096 while (wait)
01097 {
01098 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01099 {
01100 Console.WriteLine("");
01101 Console.WriteLine("Operation took too long");
01102 wait = false;
01103 Assert.Fail("Operation took too long");
01104 }
01105 Console.Write(".");
01106 Thread.Sleep(250);
01107 }
01108 Console.WriteLine("Share File Test successful.");
01109 }
01113 [Test]
01114 public void TestShareDirectory()
01115 {
01116 Console.WriteLine("Test to share a directory.");
01117 bool wait = true;
01118 Sharing s = new Sharing();
01119 s.EntryAdded = delegate(SharingEntry entry)
01120 {
01121 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01122 };
01123 s.DirectoryFinished = delegate(string directory)
01124 {
01125 wait = false;
01126 };
01127
01128 s.ShareDirectory("..\\..\\..\\TestDateien");
01129
01130 Console.WriteLine("Waiting for data");
01131 DateTime start = DateTime.Now;
01132 while (wait)
01133 {
01134 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01135 {
01136 Console.WriteLine("");
01137 Console.WriteLine("Operation took too long");
01138 wait = false;
01139 Assert.Fail("Operation took too long");
01140 }
01141 Console.Write(".");
01142 Thread.Sleep(250);
01143 }
01144
01145
01146 Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01147 Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01148 Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01149 Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01150 Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01151 Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01152 Console.WriteLine("Share Directory Test successful.");
01153 }
01157 [Test]
01158 public void TestShareSaveLoad()
01159 {
01160 Console.WriteLine("Test to save and load shares.");
01161 bool wait = true;
01162 Sharing s = new Sharing();
01163 s.EntryAdded = delegate(SharingEntry entry)
01164 {
01165
01166 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01167 };
01168 s.DirectoryFinished = delegate(string directory)
01169 {
01170 wait = false;
01171 };
01172 s.ShareDirectory("..\\..\\..\\TestDateien");
01173
01174 Console.WriteLine("Waiting for data");
01175 DateTime start = DateTime.Now;
01176 while (wait)
01177 {
01178 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01179 {
01180 Console.WriteLine("");
01181 Console.WriteLine("Operation took too long");
01182 wait = false;
01183 Assert.Fail("Operation took too long");
01184 }
01185 Console.Write(".");
01186 Thread.Sleep(250);
01187 }
01188 Console.WriteLine("");
01189 Console.WriteLine("Sharing of files completed.");
01190
01191 Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01192 Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01193 Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01194 Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01195 Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01196 Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01197 s.SaveSharesToXmlFile("..\\..\\..\\TestDateien\\test_shares.xml");
01198 Console.WriteLine("Saved Shares.");
01199 s.Clear();
01200 Console.WriteLine("Cleared Shares");
01201 Assert.IsTrue(s.items.Count == 0, "Clearing Shares failed.");
01202 s.LoadSharesFromXmlFile("..\\..\\..\\TestDateien\\test_shares.xml");
01203 Console.WriteLine("Shares loaded.");
01204 Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01205 Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01206 Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01207 Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01208 Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01209 Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01210 Console.WriteLine("Save and Load Shares Test successful.");
01211 }
01215 [Test]
01216 public void TestShareSearchRemove()
01217 {
01218 Console.WriteLine("Test to search and remove a share.");
01219 bool wait = true;
01220 Sharing s = new Sharing();
01221 s.EntryAdded = delegate(SharingEntry entry)
01222 {
01223 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01224 };
01225 s.DirectoryFinished = delegate(string directory)
01226 {
01227 wait = false;
01228 };
01229 s.ShareDirectory("..\\..\\..\\TestDateien");
01230
01231 Console.WriteLine("Waiting for data");
01232 DateTime start = DateTime.Now;
01233 while (wait)
01234 {
01235 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01236 {
01237 Console.WriteLine("");
01238 Console.WriteLine("Operation took too long");
01239 wait = false;
01240 Assert.Fail("Operation took too long");
01241 }
01242 Console.Write(".");
01243 Thread.Sleep(250);
01244 }
01245 Console.WriteLine("Sharing of files completed.");
01246
01247 Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01248 Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01249 Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test2.mp3).");
01250 Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01251 Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01252 Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01253 int num = s.items.Count;
01254 SharingEntry found = s.GetShareByFilename("..\\..\\..\\TestDateien\\test.mp3");
01255 Assert.IsTrue(found != null, "Searching Share failed.");
01256 s.Remove(found);
01257 Console.WriteLine("Removed Share");
01258 Assert.IsTrue(s.items.Count == num - 1, "Removing Share failed(test.mp3).");
01259 found = s.GetShareByFilename("..\\..\\..\\TestDateien\\test.mp3");
01260 Assert.IsTrue(found == null, "Removing Share failed(test.mp3).");
01261 s.Remove("..\\..\\..\\TestDateien\\test2.mp3");
01262 Console.WriteLine("Removed another Share");
01263 Assert.IsTrue(s.items.Count == num - 2, "Removing Share failed(test2.mp3).");
01264 found = s.GetShareByFilename("..\\..\\..\\TestDateien\\test2.mp3");
01265 Assert.IsTrue(found == null, "Removing Share failed(test2.mp3).");
01266 Console.WriteLine("Search and Remove Test successful.");
01267 }
01271 [Test]
01272 public void TestTTHs()
01273 {
01274 Console.WriteLine("Test to see if correct TTHs were created.");
01275 bool wait = true;
01276 Sharing s = new Sharing();
01277 s.EntryAdded = delegate(SharingEntry entry)
01278 {
01279 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01280 };
01281 s.DirectoryFinished = delegate(string directory)
01282 {
01283 wait = false;
01284 };
01285 s.ShareDirectory("..\\..\\..\\TestDateien");
01286
01287 Console.WriteLine("Waiting for data");
01288 DateTime start = DateTime.Now;
01289 while (wait)
01290 {
01291 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01292 {
01293 Console.WriteLine("");
01294 Console.WriteLine("Operation took too long");
01295 wait = false;
01296 Assert.Fail("Operation took too long");
01297 }
01298 Console.Write(".");
01299 Thread.Sleep(250);
01300 }
01301
01302
01303 Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01304 Assert.IsTrue(s.items[0].TTH == "QNGNAPOTVVZRPGSQPOH5X4RWITB3OI27KWXGCEI", "TTH not correct(2sd.avi)(\"" + s.items[0].TTH + "\"!=\"QNGNAPOTVVZRPGSQPOH5X4RWITB3OI27KWXGCEI\").");
01305 Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01306 Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01307 Assert.IsTrue(s.items[1].TTH == "LODVHCUGIS5G534HRWG4LIPXT5TPIO4SS6D2KKI", "TTH not correct(test.mp3)(\"" + s.items[1].TTH + "\"!=\"LODVHCUGIS5G534HRWG4LIPXT5TPIO4SS6D2KKI\").");
01308 Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01309 Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01310 Assert.IsTrue(s.items[2].TTH == "6CFXRPW5GWT5NQGAU3DYZOCQBAYM63WST5J3HAY", "TTH not correct(test2.mp3)(\"" + s.items[2].TTH + "\"!=\"6CFXRPW5GWT5NQGAU3DYZOCQBAYM63WST5J3HAY\").");
01311 Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01312 Console.WriteLine("TTHs Creation Test successful.");
01313 }
01318 [Test]
01319 public void TestSharingNotExistingDirectory()
01320 {
01321 Console.WriteLine("Test to see if no files were shared.");
01322 bool wait = true;
01323 Sharing s = new Sharing();
01324 s.EntryAdded = delegate(SharingEntry entry)
01325 {
01326 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01327 };
01328 s.DirectoryFinished = delegate(string directory)
01329 {
01330 wait = false;
01331 };
01332 s.ShareDirectory("..\\..\\..\\NotExistingDirectory");
01333
01334 Console.WriteLine("Waiting for data");
01335 DateTime start = DateTime.Now;
01336 while (wait)
01337 {
01338 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01339 {
01340 Console.WriteLine("");
01341 Console.WriteLine("Operation took too long");
01342 wait = false;
01343 Assert.Fail("Operation took too long");
01344 }
01345 Console.Write(".");
01346 Thread.Sleep(250);
01347 }
01348
01349
01350 Assert.IsTrue(s.items.Count == 0, "Test failed : More than none files were shared.");
01351 Console.WriteLine("Sharing Empty Dir Test successful.");
01352 }
01356 [Test]
01357 public void TestEmptyGetFileListXml()
01358 {
01359 Console.WriteLine("Test to see if a correct empty filelist was created.");
01360 bool wait = true;
01361 Sharing s = new Sharing();
01362 s.EntryAdded = delegate(SharingEntry entry)
01363 {
01364 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01365 };
01366 s.DirectoryFinished = delegate(string directory)
01367 {
01368 wait = false;
01369 };
01370 s.ShareDirectory("..\\..\\..\\TestFileListEmpty");
01371
01372 Console.WriteLine("Waiting for data");
01373 DateTime start = DateTime.Now;
01374 while (wait)
01375 {
01376 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01377 {
01378 Console.WriteLine("");
01379 Console.WriteLine("Operation took too long");
01380 wait = false;
01381 Assert.Fail("Operation took too long");
01382 }
01383 Console.Write(".");
01384 Thread.Sleep(250);
01385 }
01386 string file_list = s.GetFileListXml();
01387 Console.WriteLine("\nfilelist:\n" + file_list);
01388
01389
01390 Assert.IsTrue(file_list == "", "Empty FileList expected.");
01391 Console.WriteLine("Empty GetFilesList Creation Test successful.");
01392 }
01396 [Test]
01397 public void TestGetFileListXml()
01398 {
01399 Console.WriteLine("Test to see if a correct filelist was created.");
01400 bool wait = true;
01401 Sharing s = new Sharing();
01402 s.EntryAdded = delegate(SharingEntry entry)
01403 {
01404 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01405 };
01406 s.DirectoryFinished = delegate(string directory)
01407 {
01408 wait = false;
01409 };
01410 s.ShareDirectory("..\\..\\..\\TestFileList");
01411
01412 Console.WriteLine("Waiting for data");
01413 DateTime start = DateTime.Now;
01414 while (wait)
01415 {
01416 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01417 {
01418 Console.WriteLine("");
01419 Console.WriteLine("Operation took too long");
01420 wait = false;
01421 Assert.Fail("Operation took too long");
01422 }
01423 Console.Write(".");
01424 Thread.Sleep(250);
01425 }
01426 string file_list = s.GetFileListXml();
01427 Console.WriteLine("\nfilelist:\n" + file_list);
01428
01429
01430 Assert.IsTrue(file_list == "", "Empty FileList expected.");
01431 Console.WriteLine("GetFilesList Creation Test successful.");
01432 }
01433 #endregion
01434 }
01435 }