Repo created
This commit is contained in:
parent
81b91f4139
commit
f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions
22
TMessagesProj/jni/voip/libtgvoip/tests/Info.plist
Normal file
22
TMessagesProj/jni/voip/libtgvoip/tests/Info.plist
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>BNDL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
129
TMessagesProj/jni/voip/libtgvoip/tests/MockReflector.cpp
Normal file
129
TMessagesProj/jni/voip/libtgvoip/tests/MockReflector.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include "MockReflector.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace tgvoip;
|
||||
using namespace tgvoip::test;
|
||||
|
||||
struct UdpReflectorSelfInfo{
|
||||
uint8_t peerTag[16];
|
||||
uint64_t _id1=0xFFFFFFFFFFFFFFFFLL;
|
||||
uint32_t _id2=0xFFFFFFFF;
|
||||
uint32_t magic=0xc01572c7;
|
||||
int32_t date;
|
||||
uint64_t query_id;
|
||||
uint64_t my_ip_padding1;
|
||||
uint32_t my_ip_padding2;
|
||||
uint32_t my_ip;
|
||||
uint32_t my_port;
|
||||
} __attribute__((packed));
|
||||
|
||||
MockReflector::MockReflector(std::string bindAddress, uint16_t bindPort){
|
||||
sfd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
assert(sfd!=-1);
|
||||
sockaddr_in bindAddr={0};
|
||||
bindAddr.sin_family=AF_INET;
|
||||
bindAddr.sin_port=htons(bindPort);
|
||||
inet_aton(bindAddress.c_str(), &bindAddr.sin_addr);
|
||||
int res=bind(sfd, (struct sockaddr*)&bindAddr, sizeof(bindAddr));
|
||||
assert(res==0);
|
||||
}
|
||||
|
||||
MockReflector::~MockReflector(){
|
||||
|
||||
}
|
||||
|
||||
std::array<std::array<uint8_t, 16>, 2> MockReflector::GeneratePeerTags(){
|
||||
std::array<uint8_t, 16> tag1;
|
||||
for(int i=0;i<16;i++){
|
||||
tag1[i]=(uint8_t)rand();
|
||||
}
|
||||
tag1[15] &= 0xFE;
|
||||
std::array<std::array<uint8_t, 16>, 2> res;
|
||||
res[0]=tag1;
|
||||
std::copy(tag1.begin(), tag1.end(), res[1].begin());
|
||||
res[1][15] |= 1;
|
||||
return res;
|
||||
}
|
||||
|
||||
void MockReflector::Start(){
|
||||
if(running)
|
||||
return;
|
||||
running=true;
|
||||
pthread_create(&thread, NULL, [](void* arg) -> void* {
|
||||
reinterpret_cast<MockReflector*>(arg)->RunThread();
|
||||
return NULL;
|
||||
}, this);
|
||||
}
|
||||
|
||||
void MockReflector::Stop(){
|
||||
running=false;
|
||||
shutdown(sfd, SHUT_RDWR);
|
||||
close(sfd);
|
||||
pthread_join(thread, NULL);
|
||||
}
|
||||
|
||||
void MockReflector::SetDropAllPackets(bool drop){
|
||||
dropAllPackets=drop;
|
||||
}
|
||||
|
||||
void MockReflector::RunThread(){
|
||||
while(running){
|
||||
std::array<uint8_t, 1500> buf;
|
||||
sockaddr_in addr;
|
||||
socklen_t addrlen=sizeof(addr);
|
||||
ssize_t len=recvfrom(sfd, buf.data(), sizeof(buf), 0, (struct sockaddr*)&addr, &addrlen);
|
||||
if(len<=0)
|
||||
return;
|
||||
if(len>=32){
|
||||
std::array<uint8_t, 16> peerTag;
|
||||
int32_t specialID[4];
|
||||
std::copy(buf.begin(), buf.begin()+16, peerTag.begin());
|
||||
memcpy(specialID, buf.data()+16, 16);
|
||||
uint64_t tagID=*reinterpret_cast<uint64_t*>(peerTag.data());
|
||||
ClientPair c=clients[tagID];
|
||||
sockaddr_in* dest;
|
||||
if(peerTag[15] & 1){
|
||||
c.addr1=addr;
|
||||
dest=&c.addr0;
|
||||
}else{
|
||||
c.addr0=addr;
|
||||
dest=&c.addr1;
|
||||
}
|
||||
clients[tagID]=c;
|
||||
|
||||
if(specialID[0]==-1 && specialID[1]==-1 && specialID[2]==-1){
|
||||
if(specialID[3]==-1){
|
||||
continue;
|
||||
}else if(specialID[3]==-2){
|
||||
UdpReflectorSelfInfo response;
|
||||
memcpy(response.peerTag, peerTag.data(), 16);
|
||||
response.date=(int32_t)time(NULL);
|
||||
response.query_id=*reinterpret_cast<uint64_t*>(buf.data()+32);
|
||||
response.my_ip_padding1=0;
|
||||
response.my_ip_padding2=0xFFFF0000;
|
||||
response.my_ip=(uint32_t)addr.sin_addr.s_addr;
|
||||
response.my_port=ntohs(addr.sin_port);
|
||||
sendto(sfd, &response, sizeof(response), 0, (struct sockaddr*)&addr, sizeof(addr));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if(dest->sin_family==AF_INET && !dropAllPackets){
|
||||
if(peerTag[15] & 1)
|
||||
buf[15] &= 0xFE;
|
||||
else
|
||||
buf[15] |= 1;
|
||||
|
||||
sendto(sfd, buf.data(), len, 0, (struct sockaddr*)dest, sizeof(sockaddr_in));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
TMessagesProj/jni/voip/libtgvoip/tests/MockReflector.h
Normal file
50
TMessagesProj/jni/voip/libtgvoip/tests/MockReflector.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
#ifndef TGVOIP_MOCK_REFLECTOR
|
||||
#define TGVOIP_MOCK_REFLECTOR
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
namespace tgvoip{
|
||||
namespace test{
|
||||
class MockReflector{
|
||||
public:
|
||||
MockReflector(std::string bindAddress, uint16_t bindPort);
|
||||
~MockReflector();
|
||||
void Start();
|
||||
void Stop();
|
||||
void SetDropAllPackets(bool drop);
|
||||
static std::array<std::array<uint8_t, 16>, 2> GeneratePeerTags();
|
||||
|
||||
private:
|
||||
void RunThread();
|
||||
struct ClientPair{
|
||||
sockaddr_in addr0={0};
|
||||
sockaddr_in addr1={0};
|
||||
};
|
||||
std::unordered_map<uint64_t, ClientPair> clients; // clients are identified by the first half of their peer_tag
|
||||
int sfd;
|
||||
pthread_t thread;
|
||||
bool running=false;
|
||||
bool dropAllPackets=false;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif //TGVOIP_MOCK_REFLECTOR
|
||||
192
TMessagesProj/jni/voip/libtgvoip/tests/libtgvoipTests.mm
Normal file
192
TMessagesProj/jni/voip/libtgvoip/tests/libtgvoipTests.mm
Normal file
|
|
@ -0,0 +1,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
|
||||
Loading…
Add table
Add a link
Reference in a new issue