2013-02-07 15:19:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2007-01-04 18:25:30 +01:00
|
|
|
package org.jivesoftware.smackx.jingle.nat;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
|
|
|
|
2007-02-23 17:07:23 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
public class BasicResolverTest extends SmackTestCase {
|
|
|
|
|
|
|
|
private int counter;
|
|
|
|
|
|
|
|
private final Object mutex = new Object();
|
|
|
|
|
|
|
|
public BasicResolverTest(String arg) {
|
|
|
|
super(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Counter management
|
|
|
|
|
|
|
|
private void resetCounter() {
|
|
|
|
synchronized (mutex) {
|
|
|
|
counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void incCounter() {
|
|
|
|
synchronized (mutex) {
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int valCounter() {
|
|
|
|
int val;
|
|
|
|
synchronized (mutex) {
|
|
|
|
val = counter;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testCheckValidHostname() {
|
|
|
|
String validHostname = new String("slashdot.org");
|
|
|
|
BasicResolver br = new BasicResolver();
|
|
|
|
TransportCandidate tc = new TransportCandidate.Fixed(validHostname, 0);
|
|
|
|
|
|
|
|
resetCounter();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
tc.addListener(new TransportResolverListener.Checker() {
|
|
|
|
public void candidateChecked(TransportCandidate cand, boolean result) {
|
|
|
|
if(result == true) {
|
|
|
|
System.out.println(cand.getIp() + " is reachable (as expected)");
|
2018-05-09 23:06:12 +02:00
|
|
|
incCounter();
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void candidateChecking(TransportCandidate cand) {
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
});
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-02-23 17:07:23 +01:00
|
|
|
tc.check(new ArrayList<TransportCandidate>());
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
try {
|
|
|
|
Thread.sleep(TransportResolver.CHECK_TIMEOUT);
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
assertTrue(valCounter() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testCheckInvalidHostname() {
|
|
|
|
String invalidHostname = new String("camupilosupino.org");
|
|
|
|
BasicResolver br = new BasicResolver();
|
|
|
|
TransportCandidate tc = new TransportCandidate.Fixed(invalidHostname, 0);
|
|
|
|
|
|
|
|
resetCounter();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
tc.addListener(new TransportResolverListener.Checker() {
|
|
|
|
public void candidateChecked(TransportCandidate cand, boolean result) {
|
|
|
|
if(result == false) {
|
|
|
|
System.out.println(cand.getIp() + " is _not_ reachable (as expected)");
|
2018-05-09 23:06:12 +02:00
|
|
|
incCounter();
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void candidateChecking(TransportCandidate cand) {
|
|
|
|
}
|
|
|
|
});
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-02-23 17:07:23 +01:00
|
|
|
tc.check(new ArrayList<TransportCandidate>());
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
try {
|
|
|
|
Thread.sleep(TransportResolver.CHECK_TIMEOUT);
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
assertTrue(valCounter() > 0);
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|