-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathXmppBitsOfBinaryTests.cs
More file actions
124 lines (104 loc) · 2.99 KB
/
XmppBitsOfBinaryTests.cs
File metadata and controls
124 lines (104 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Waher.Content.Text;
using Waher.Networking.XMPP.BitsOfBinary;
namespace Waher.Networking.XMPP.Test
{
[TestClass]
public class XmppBitsOfBinaryTests : CommunicationTests
{
private BobClient bobClient1;
private BobClient bobClient2;
[ClassInitialize]
public static void ClassInitialize(TestContext _)
{
SetupSnifferAndLog();
}
[ClassCleanup]
public static async Task ClassCleanup()
{
await DisposeSnifferAndLog();
}
public override async Task ConnectClients()
{
await base.ConnectClients();
Assert.AreEqual(XmppState.Connected, this.client1.State);
Assert.AreEqual(XmppState.Connected, this.client2.State);
this.bobClient1 = new BobClient(this.client1, "Bob1");
this.bobClient2 = new BobClient(this.client2, "Bob2");
}
public override Task DisposeClients()
{
if (this.bobClient1 is not null)
{
this.bobClient1.Dispose();
this.bobClient1 = null;
}
if (this.bobClient2 is not null)
{
this.bobClient2.Dispose();
this.bobClient2 = null;
}
return base.DisposeClients();
}
[TestMethod]
public async Task BitsOfBinary_Test_01_GetData()
{
await this.ConnectClients();
try
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
string s = "Hello world.";
byte[] Bin = Encoding.UTF8.GetBytes(s);
string ContentId = await this.bobClient2.StoreData(Bin, PlainTextCodec.DefaultContentType);
await this.bobClient1.GetData(this.client2.FullJID, ContentId, (Sender, e) =>
{
if (e.Ok && e.ContentId == ContentId && e.ContentType == PlainTextCodec.DefaultContentType && Encoding.UTF8.GetString(e.Data) == s)
Done.Set();
else
Error.Set();
return Task.CompletedTask;
}, null);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 5000));
}
finally
{
await this.DisposeClients();
}
}
[TestMethod]
public async Task BitsOfBinary_Test_02_GetData_Expires()
{
await this.ConnectClients();
try
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
string s = "Hello world.";
byte[] Bin = Encoding.UTF8.GetBytes(s);
string ContentId = await this.bobClient2.StoreData(Bin, PlainTextCodec.DefaultContentType, DateTime.Now.AddMinutes(1));
await this.bobClient1.GetData(this.client2.FullJID, ContentId, (Sender, e) =>
{
double d;
if (e.Ok && e.ContentId == ContentId && e.ContentType == PlainTextCodec.DefaultContentType && Encoding.UTF8.GetString(e.Data) == s &&
e.Expires.HasValue && (d = (e.Expires.Value - DateTime.Now).TotalSeconds) >= 50 && d <= 60)
{
Done.Set();
}
else
Error.Set();
return Task.CompletedTask;
}, null);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 5000));
}
finally
{
await this.DisposeClients();
}
}
}
}