Last active
August 29, 2015 14:11
-
-
Save txdv/0bd5228bb50a53f2f8b0 to your computer and use it in GitHub Desktop.
Test example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using SharpTox.Core; | |
using SharpTox.Av; | |
using Xunit; | |
namespace SharpTox.Tests | |
{ | |
public class BaseFixture | |
{ | |
//check https://wiki.tox.im/Nodes for an up-to-date list of nodes | |
static ToxNode[] Nodes = new ToxNode[] | |
{ | |
new ToxNode("192.254.75.98", 33445, new ToxKey(ToxKeyType.Public, "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F")), | |
new ToxNode("144.76.60.215", 33445, new ToxKey(ToxKeyType.Public, "04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F")) | |
}; | |
public static void Start(out Tox client) | |
{ | |
client = new Tox(); | |
client.BootStrapFromNodes(Nodes); | |
} | |
public static void CanFriendRequest(Tox requester, Tox requestee) | |
{ | |
var message = "CanFriendRequest"; | |
bool friendRequest = false; | |
bool equivalentMessage = false; | |
requestee.OnFriendRequest += (sender, e) => { | |
friendRequest = true; | |
equivalentMessage = e.Message == message; | |
requestee.AddFriendNoRequest(new ToxKey(ToxKeyType.Secret, e.Id)); | |
}; | |
requester.AddFriend(requestee.Id, message); | |
Run(() => friendRequest, TimeSpan.FromSeconds(60), requester, requestee); | |
if (!equivalentMessage) | |
throw new Exception(); | |
} | |
public static void CanMessage(Tox sender, Tox receiver) | |
{ | |
string text = "Some message"; | |
bool message = false; | |
receiver.OnFriendMessage += (_, e) => { | |
if (sender.Id.PublicKey.GetString().StartsWith(e.Friend.PublicKey.GetString())) { | |
message = text == e.Message; | |
} | |
}; | |
sender.Friends.First().SendMessage(text); | |
Run(() => message, TimeSpan.FromSeconds(30), sender, receiver); | |
} | |
public static void ShowInfo(Tox client) | |
{ | |
Console.WriteLine("{0} ({1})", client.Name, client.Id); | |
} | |
public static void ShowInfo(params Tox[] clients) | |
{ | |
foreach (var client in clients) | |
ShowInfo(client); | |
} | |
public static void ShowInfo(ToxFriend friend) | |
{ | |
if (friend == null) | |
{ | |
Console.WriteLine("Friend(null)"); | |
return; | |
} | |
Console.WriteLine("Friend(Name={0}, PublicKey={1}, ConnectionStatus={2}, IsOnline={3}, LastOnline={4})", | |
friend.Name, | |
friend.PublicKey, | |
friend.ConnectionStatus, | |
friend.IsOnline, | |
friend.LastOnline); | |
} | |
public static string GetFilename(Tox client) | |
{ | |
return string.Format("{0}.tox", client.Id.ToString().Substring(10)); | |
} | |
public static void CanConnect(out Tox client1, out Tox client2) | |
{ | |
var clients = Directory.GetFiles("./", "*.tox"); | |
Start(out client1); | |
client1.Name = "Client 1"; | |
Start(out client2); | |
client2.Name = "Client 2"; | |
ShowInfo(client1); | |
ShowInfo(client2); | |
int connects = 0; | |
client1.OnConnected += (sender, e) => connects++; | |
client2.OnConnected += (sender, e) => connects++; | |
Run(() => connects == 2, TimeSpan.FromSeconds(2 * 60), client1, client2); | |
} | |
public static void CanOnline(Tox client1, Tox client2) | |
{ | |
int online = 0; | |
client1.OnConnectionStatusChanged += (sender, e) => online++; | |
client2.OnConnectionStatusChanged += (sender, e) => online++; | |
client1.OnNameChange += (sender, e) => online++; | |
client2.OnNameChange += (sender, e) => online++; | |
Run(() => online == 4, TimeSpan.FromSeconds(60), client1, client2); | |
} | |
public static void Run(Func<bool> state, TimeSpan timeout, params IToxItertable[] clients) | |
{ | |
var start = DateTime.Now; | |
int i = 1; | |
//Console.Write("Tick: 0"); | |
while (!state()) | |
{ | |
/* | |
for (int j = 0; j < (i - 1).ToString().Length; j++) | |
Console.Write("\b"); | |
Console.Write(i); | |
*/ | |
i++; | |
if (timeout != TimeSpan.Zero && DateTime.Now - start > timeout) | |
throw new TimeoutException(); | |
var delay = clients.Select((tox) => tox.Iterate()).Min(); | |
Thread.Sleep(delay); | |
} | |
//Console.WriteLine(); | |
Console.WriteLine("Time: {0}", DateTime.Now - start); | |
} | |
public static void Run(Func<bool> state, params IToxItertable[] clients) | |
{ | |
Run(state, TimeSpan.Zero, clients); | |
} | |
public static void Run(TimeSpan timeout, params IToxItertable[] clients) | |
{ | |
var start = DateTime.Now; | |
Run(() => (DateTime.Now - start) >= timeout, TimeSpan.Zero, clients); | |
} | |
public static ToxGroup CanGroupChat(Tox inviter, Tox invitee) | |
{ | |
invitee.OnGroupInvite += (sender, e) => e.GroupInvite.Accept(); | |
bool invited = false; | |
inviter.OnGroupNamelistChange += (sender, e) => { | |
if (e.Change == ToxChatChange.PeerAdd && !e.Peer.IsMe && e.Peer.Friend != null) { | |
invited = true; | |
} | |
}; | |
var group = inviter.CreateGroup(); | |
group.InviteFriend(inviter.Friends.First()); | |
Run(() => invited, TimeSpan.FromSeconds(30), inviter, invitee); | |
return group; | |
} | |
public static void CanGroupChatMessage(Tox sender, Tox receiver) | |
{ | |
var text = "some group chat"; | |
var message = false; | |
receiver.OnGroupMessage += (_, e) => { | |
if (!e.Peer.IsMe && sender.Id.PublicKey.GetString().StartsWith(e.Peer.Friend.PublicKey.GetString())) | |
message = text == e.Message; | |
}; | |
sender.Groups.First().SendGroupMessage(text); | |
Run(() => message, TimeSpan.FromSeconds(30), sender, receiver); | |
} | |
public static void CanSendFile(Tox sender, Tox receiver) | |
{ | |
bool done = false; | |
bool equal = false; | |
var filename = "test.txt"; | |
var file = File.ReadAllBytes(filename); | |
var fileSender = sender.Friends.First().SendFile(filename, file.Length); | |
receiver.OnFileSendRequest += (_, e) => { | |
var fs = e.FileSender; | |
fs.Control(ToxFileControl.Accept, null); | |
}; | |
receiver.OnFileData += (_, e) => { | |
Console.WriteLine ("Receive: " + e.Data.Length); | |
done = true; | |
equal = e.Data.SequenceEqual(file); | |
}; | |
receiver.OnFileControl += (_, e) => { | |
Console.WriteLine("Received: " + e.Control); | |
}; | |
sender.OnFileControl += (_, e) => { | |
if (e.Control == ToxFileControl.Accept) { | |
if (!fileSender.SendData(file)) | |
throw new Exception("Could not send data"); | |
fileSender.Control(ToxFileControl.Finished, null); | |
} | |
}; | |
Run(() => done, TimeSpan.FromSeconds(30), sender, receiver); | |
if (!equal) | |
throw new Exception("Data sent is not equal"); | |
} | |
public static void CanCall(Tox caller, Tox callee) | |
{ | |
bool done = false; | |
var call = caller.Friends.First().Call(ToxAv.DefaultCodecSettings, 30); | |
Console.WriteLine("Calling a friend: " + call.Friend.Name); | |
callee.ToxAv.OnInvite += (sender, e) => { | |
e.Call.Answer(); | |
Console.WriteLine ("Friend is calling: " + e.Call.Friend.Name); | |
}; | |
caller.ToxAv.OnStart += (sender, e) => { | |
done = true; | |
}; | |
Run(() => done, TimeSpan.FromSeconds(30), caller, callee); | |
} | |
public static void Main(string[] args) | |
{ | |
Tox client1; | |
Tox client2; | |
CanConnect(out client1, out client2); | |
Console.WriteLine("Connected"); | |
CanFriendRequest(client1, client2); | |
Console.WriteLine("Befriended"); | |
Console.WriteLine("Friends {0} {1}", client1.Friends.Length, client2.Friends.Length); | |
ShowInfo(client1.Friends.FirstOrDefault()); | |
ShowInfo(client2.Friends.FirstOrDefault()); | |
CanOnline(client1, client2); | |
Console.WriteLine("Online"); | |
ShowInfo(client1.Friends.FirstOrDefault()); | |
ShowInfo(client2.Friends.FirstOrDefault()); | |
CanMessage(client1, client2); | |
Console.WriteLine("Messaged"); | |
CanMessage(client2, client1); | |
Console.WriteLine("Messaged"); | |
CanGroupChat(client1, client2); | |
Console.WriteLine("GroupChat"); | |
CanGroupChat(client2, client1); | |
Console.WriteLine("GroupChat"); | |
CanGroupChatMessage(client1, client2); | |
Console.WriteLine("GroupChatMessage"); | |
CanGroupChatMessage(client2, client1); | |
Console.WriteLine("GroupChatMessage"); | |
CanSendFile(client1, client2); | |
Console.WriteLine("File sent"); | |
CanCall(client1, client2); | |
Console.WriteLine("CanCAll"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment