| 7 | #import <XCTest/XCTest.h> |
| 8 |
| 9 | #import "MockReflector.h" |
| 10 | #include "../VoIPController.h" |
| 11 | #include <openssl/rand.h> |
| 12 | #include "../webrtc_dsp/common_audio/wav_file.h" |
| 13 |
| 14 | @interface libtgvoipTests : XCTestCase |
| 15 |
| 16 | @end |
| 17 |
| 18 | using namespace tgvoip; |
| 19 |
| 20 | @implementation libtgvoipTests{ |
| 21 | VoIPController* controller1; |
| 22 | VoIPController* controller2; |
| 23 | std::string testWavFilePath; |
| 24 | } |
| 25 |
| 26 | - (void)setUp { |
| 27 | [super setUp]; |
| 28 | // this file must be mono 16-bit 48000hz |
| 29 | NSString* path=[NSString stringWithFormat:@"%@/Downloads/voip_test_input.wav", NSHomeDirectory()]; |
| 30 | testWavFilePath=[path UTF8String]; |
| 31 | } |
| 32 |
| 33 | - (void)tearDown { |
| 34 |
| 35 |
| 36 | [super tearDown]; |
| 37 | } |
| 38 |
| 39 | - (void)initControllers{ |
| 40 | controller1=new VoIPController(); |
| 41 | controller2=new VoIPController(); |
| 42 |
| 43 | std::array<std::array<uint8_t, 16>, 2> peerTags=test::MockReflector::GeneratePeerTags(); |
| 44 | std::vector<Endpoint> endpoints1; |
| 45 | IPv4Address localhost("127.0.0.1"); |
| 46 | IPv6Address emptyV6; |
| 47 | endpoints1.push_back(Endpoint(1, 1033, localhost, emptyV6, Endpoint::Type::UDP_RELAY, peerTags[0].data())); |
| 48 | controller1->SetRemoteEndpoints(endpoints1, false, 76); |
| 49 | std::vector<Endpoint> endpoints2; |
| 50 | endpoints2.push_back(Endpoint(1, 1033, localhost, emptyV6, Endpoint::Type::UDP_RELAY, peerTags[1].data())); |
| 51 | controller2->SetRemoteEndpoints(endpoints2, false, 76); |
| 52 |
| 53 | char encryptionKey[256]; |
| 54 | RAND_bytes((uint8_t*)encryptionKey, sizeof(encryptionKey)); |
| 55 | controller1->SetEncryptionKey(encryptionKey, true); |
| 56 | controller2->SetEncryptionKey(encryptionKey, false); |
| 57 | } |
| 58 |
| 59 | - (void)destroyControllers{ |
| 60 | controller1->Stop(); |
| 61 | delete controller1; |
| 62 | controller2->Stop(); |
| 63 | delete controller2; |
| 64 | } |
| 65 |
| 66 | - (void)testBasicOperation { |
| 67 | webrtc::WavReader wavReader1(testWavFilePath); |
| 68 | webrtc::WavReader wavReader2(testWavFilePath); |
| 69 | webrtc::WavWriter wavWriter("output.wav", 48000, 1); |
| 70 |
| 71 | test::MockReflector reflector("127.0.0.1", 1033); |
| 72 | reflector.Start(); |
| 73 |
| 74 | [self initControllers]; |
| 75 |
| 76 | controller1->SetAudioDataCallbacks([&wavReader1](int16_t* data, size_t len){ |
| 77 | wavReader1.ReadSamples(len, data); |
| 78 | }, [](int16_t* data, size_t len){ |
| 79 |
| 80 | }); |
| 81 |
| 82 | controller2->SetAudioDataCallbacks([&wavReader2](int16_t* data, size_t len){ |
| 83 | wavReader2.ReadSamples(len, data); |
| 84 | }, [&wavWriter](int16_t* data, size_t len){ |
| 85 | wavWriter.WriteSamples(data, len); |
| 86 | }); |
| 87 |
| 88 | controller1->Start(); |
| 89 | controller2->Start(); |
| 90 | controller1->Connect(); |
| 91 | controller2->Connect(); |
| 92 | [NSThread sleepForTimeInterval:10.0]; |
| 93 |
| 94 | [self destroyControllers]; |
| 95 |
| 96 | reflector.Stop(); |
| 97 | } |
| 98 |
| 99 | - (void)testAllocationAndDeallocation{ |
| 100 | test::MockReflector reflector("127.0.0.1", 1033); |
| 101 | reflector.Start(); |
| 102 |
| 103 | for(int i=0;i<10;i++){ |
| 104 | webrtc::WavReader wavReader(testWavFilePath); |
| 105 | [self initControllers]; |
| 106 |
| 107 | controller1->SetAudioDataCallbacks([&wavReader](int16_t* data, size_t len){ |
| 108 | wavReader.ReadSamples(len, data); |
| 109 | }, [](int16_t* data, size_t len){ |
| 110 |
| 111 | }); |
| 112 |
| 113 | controller2->SetAudioDataCallbacks([](int16_t* data, size_t len){ |
| 114 |
| 115 | }, [](int16_t* data, size_t len){ |
| 116 |
| 117 | }); |
| 118 |
| 119 | controller1->Start(); |
| 120 | controller2->Start(); |
| 121 | controller1->Connect(); |
| 122 | controller2->Connect(); |
| 123 | [NSThread sleepForTimeInterval:3.0]; |
| 124 |
| 125 | [self destroyControllers]; |
| 126 | } |
| 127 |
| 128 | reflector.Stop(); |
| 129 | } |
| 130 |
| 131 | - (void)testInitTimeout{ |
| 132 | [self initControllers]; |
| 133 | VoIPController::Config config; |
| 134 | config.enableNS=config.enableAEC=config.enableAGC=false; |
| 135 | config.enableCallUpgrade=false; |
| 136 | config.initTimeout=3.0; |
| 137 | controller1->SetConfig(config); |
| 138 | controller1->Start(); |
| 139 | controller1->Connect(); |
| 140 | [NSThread sleepForTimeInterval:1.5]; |
| 141 | XCTAssertEqual(controller1->GetConnectionState(), STATE_WAIT_INIT_ACK); |
| 142 | [NSThread sleepForTimeInterval:2.0]; |
| 143 | XCTAssertEqual(controller1->GetConnectionState(), STATE_FAILED); |
| 144 | XCTAssertEqual(controller1->GetLastError(), ERROR_TIMEOUT); |
| 145 | [self destroyControllers]; |
| 146 | } |
| 147 |
| 148 | - (void)testPacketTimeout{ |
| 149 | test::MockReflector reflector("127.0.0.1", 1033); |
| 150 | reflector.Start(); |
| 151 | [self initControllers]; |
| 152 |
| 153 | webrtc::WavReader wavReader(testWavFilePath); |
| 154 | controller1->SetAudioDataCallbacks([&wavReader](int16_t* data, size_t len){ |
| 155 | wavReader.ReadSamples(len, data); |
| 156 | }, [](int16_t* data, size_t len){ |
| 157 |
| 158 | }); |
| 159 |
| 160 | controller2->SetAudioDataCallbacks([](int16_t* data, size_t len){ |
| 161 |
| 162 | }, [](int16_t* data, size_t len){ |
| 163 |
| 164 | }); |
| 165 |
| 166 | VoIPController::Config config; |
| 167 | config.enableNS=config.enableAEC=config.enableAGC=false; |
| 168 | config.enableCallUpgrade=false; |
| 169 | config.initTimeout=3.0; |
| 170 | config.recvTimeout=1.5; |
| 171 | controller1->SetConfig(config); |
| 172 | config.recvTimeout=5.0; |
| 173 | controller2->SetConfig(config); |
| 174 |
| 175 | controller1->Start(); |
| 176 | controller2->Start(); |
| 177 | controller1->Connect(); |
| 178 | controller2->Connect(); |
| 179 | [NSThread sleepForTimeInterval:2.5]; |
| 180 | XCTAssertEqual(controller1->GetConnectionState(), STATE_ESTABLISHED); |
| 181 | XCTAssertEqual(controller2->GetConnectionState(), STATE_ESTABLISHED); |
| 182 | reflector.SetDropAllPackets(true); |
| 183 | [NSThread sleepForTimeInterval:2.5]; |
| 184 | XCTAssertEqual(controller1->GetConnectionState(), STATE_FAILED); |
| 185 | XCTAssertEqual(controller1->GetLastError(), ERROR_TIMEOUT); |
| 186 | XCTAssertEqual(controller2->GetConnectionState(), STATE_RECONNECTING); |
| 187 |
| 188 | [self destroyControllers]; |
| 189 | reflector.Stop(); |
| 190 | } |
| 191 |
| 192 | @end |
| 193 |