forked from grishka/libtgvoip
-
Notifications
You must be signed in to change notification settings - Fork 48
/
libtgvoipTests.mm
192 lines (148 loc) · 5.06 KB
/
libtgvoipTests.mm
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#import <XCTest/XCTest.h>
#import "MockReflector.h"
#include "../VoIPController.h"
#include <openssl/rand.h>
#include "../webrtc_dsp/common_audio/wav_file.h"
@interface libtgvoipTests : XCTestCase
@end
using namespace tgvoip;
@implementation libtgvoipTests{
VoIPController* controller1;
VoIPController* controller2;
std::string testWavFilePath;
}
- (void)setUp {
[super setUp];
// this file must be mono 16-bit 48000hz
NSString* path=[NSString stringWithFormat:@"%@/Downloads/voip_test_input.wav", NSHomeDirectory()];
testWavFilePath=[path UTF8String];
}
- (void)tearDown {
[super tearDown];
}
- (void)initControllers{
controller1=new VoIPController();
controller2=new VoIPController();
std::array<std::array<uint8_t, 16>, 2> peerTags=test::MockReflector::GeneratePeerTags();
std::vector<Endpoint> endpoints1;
IPv4Address localhost("127.0.0.1");
IPv6Address emptyV6;
endpoints1.push_back(Endpoint(1, 1033, localhost, emptyV6, Endpoint::Type::UDP_RELAY, peerTags[0].data()));
controller1->SetRemoteEndpoints(endpoints1, false, 76);
std::vector<Endpoint> endpoints2;
endpoints2.push_back(Endpoint(1, 1033, localhost, emptyV6, Endpoint::Type::UDP_RELAY, peerTags[1].data()));
controller2->SetRemoteEndpoints(endpoints2, false, 76);
char encryptionKey[256];
RAND_bytes((uint8_t*)encryptionKey, sizeof(encryptionKey));
controller1->SetEncryptionKey(encryptionKey, true);
controller2->SetEncryptionKey(encryptionKey, false);
}
- (void)destroyControllers{
controller1->Stop();
delete controller1;
controller2->Stop();
delete controller2;
}
- (void)testBasicOperation {
webrtc::WavReader wavReader1(testWavFilePath);
webrtc::WavReader wavReader2(testWavFilePath);
webrtc::WavWriter wavWriter("output.wav", 48000, 1);
test::MockReflector reflector("127.0.0.1", 1033);
reflector.Start();
[self initControllers];
controller1->SetAudioDataCallbacks([&wavReader1](int16_t* data, size_t len){
wavReader1.ReadSamples(len, data);
}, [](int16_t* data, size_t len){
});
controller2->SetAudioDataCallbacks([&wavReader2](int16_t* data, size_t len){
wavReader2.ReadSamples(len, data);
}, [&wavWriter](int16_t* data, size_t len){
wavWriter.WriteSamples(data, len);
});
controller1->Start();
controller2->Start();
controller1->Connect();
controller2->Connect();
[NSThread sleepForTimeInterval:10.0];
[self destroyControllers];
reflector.Stop();
}
- (void)testAllocationAndDeallocation{
test::MockReflector reflector("127.0.0.1", 1033);
reflector.Start();
for(int i=0;i<10;i++){
webrtc::WavReader wavReader(testWavFilePath);
[self initControllers];
controller1->SetAudioDataCallbacks([&wavReader](int16_t* data, size_t len){
wavReader.ReadSamples(len, data);
}, [](int16_t* data, size_t len){
});
controller2->SetAudioDataCallbacks([](int16_t* data, size_t len){
}, [](int16_t* data, size_t len){
});
controller1->Start();
controller2->Start();
controller1->Connect();
controller2->Connect();
[NSThread sleepForTimeInterval:3.0];
[self destroyControllers];
}
reflector.Stop();
}
- (void)testInitTimeout{
[self initControllers];
VoIPController::Config config;
config.enableNS=config.enableAEC=config.enableAGC=false;
config.enableCallUpgrade=false;
config.initTimeout=3.0;
controller1->SetConfig(config);
controller1->Start();
controller1->Connect();
[NSThread sleepForTimeInterval:1.5];
XCTAssertEqual(controller1->GetConnectionState(), STATE_WAIT_INIT_ACK);
[NSThread sleepForTimeInterval:2.0];
XCTAssertEqual(controller1->GetConnectionState(), STATE_FAILED);
XCTAssertEqual(controller1->GetLastError(), ERROR_TIMEOUT);
[self destroyControllers];
}
- (void)testPacketTimeout{
test::MockReflector reflector("127.0.0.1", 1033);
reflector.Start();
[self initControllers];
webrtc::WavReader wavReader(testWavFilePath);
controller1->SetAudioDataCallbacks([&wavReader](int16_t* data, size_t len){
wavReader.ReadSamples(len, data);
}, [](int16_t* data, size_t len){
});
controller2->SetAudioDataCallbacks([](int16_t* data, size_t len){
}, [](int16_t* data, size_t len){
});
VoIPController::Config config;
config.enableNS=config.enableAEC=config.enableAGC=false;
config.enableCallUpgrade=false;
config.initTimeout=3.0;
config.recvTimeout=1.5;
controller1->SetConfig(config);
config.recvTimeout=5.0;
controller2->SetConfig(config);
controller1->Start();
controller2->Start();
controller1->Connect();
controller2->Connect();
[NSThread sleepForTimeInterval:2.5];
XCTAssertEqual(controller1->GetConnectionState(), STATE_ESTABLISHED);
XCTAssertEqual(controller2->GetConnectionState(), STATE_ESTABLISHED);
reflector.SetDropAllPackets(true);
[NSThread sleepForTimeInterval:2.5];
XCTAssertEqual(controller1->GetConnectionState(), STATE_FAILED);
XCTAssertEqual(controller1->GetLastError(), ERROR_TIMEOUT);
XCTAssertEqual(controller2->GetConnectionState(), STATE_RECONNECTING);
[self destroyControllers];
reflector.Stop();
}
@end