use of android.net.nsd.NsdServiceInfo in project android_frameworks_base by ResurrectionRemix.
the class NsdServiceInfoTest method testLimits.
public void testLimits() throws Exception {
NsdServiceInfo info = new NsdServiceInfo();
// Non-ASCII keys.
boolean exceptionThrown = false;
try {
info.setAttribute("猫", "meow");
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEmptyServiceInfo(info);
// ASCII keys with '=' character.
exceptionThrown = false;
try {
info.setAttribute("kitten=", "meow");
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEmptyServiceInfo(info);
// Single key + value length too long.
exceptionThrown = false;
try {
String longValue = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + // 248 characters.
"ooooooooooooooooooooooooooooong";
// Key + value == 255 characters.
info.setAttribute("longcat", longValue);
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEmptyServiceInfo(info);
// Total TXT record length too long.
exceptionThrown = false;
int recordsAdded = 0;
try {
for (int i = 100; i < 300; ++i) {
// 6 char key + 5 char value + 2 bytes overhead = 13 byte record length.
String key = String.format("key%d", i);
info.setAttribute(key, "12345");
recordsAdded++;
}
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertTrue(100 == recordsAdded);
assertTrue(info.getTxtRecord().length == 1300);
}
use of android.net.nsd.NsdServiceInfo in project android_frameworks_base by crdroidandroid.
the class NsdServiceInfoTest method testLimits.
public void testLimits() throws Exception {
NsdServiceInfo info = new NsdServiceInfo();
// Non-ASCII keys.
boolean exceptionThrown = false;
try {
info.setAttribute("猫", "meow");
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEmptyServiceInfo(info);
// ASCII keys with '=' character.
exceptionThrown = false;
try {
info.setAttribute("kitten=", "meow");
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEmptyServiceInfo(info);
// Single key + value length too long.
exceptionThrown = false;
try {
String longValue = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + // 248 characters.
"ooooooooooooooooooooooooooooong";
// Key + value == 255 characters.
info.setAttribute("longcat", longValue);
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEmptyServiceInfo(info);
// Total TXT record length too long.
exceptionThrown = false;
int recordsAdded = 0;
try {
for (int i = 100; i < 300; ++i) {
// 6 char key + 5 char value + 2 bytes overhead = 13 byte record length.
String key = String.format("key%d", i);
info.setAttribute(key, "12345");
recordsAdded++;
}
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertTrue(100 == recordsAdded);
assertTrue(info.getTxtRecord().length == 1300);
}
use of android.net.nsd.NsdServiceInfo in project android_frameworks_base by crdroidandroid.
the class NsdServiceInfoTest method testParcel.
public void testParcel() throws Exception {
NsdServiceInfo emptyInfo = new NsdServiceInfo();
checkParcelable(emptyInfo);
NsdServiceInfo fullInfo = new NsdServiceInfo();
fullInfo.setServiceName("kitten");
fullInfo.setServiceType("_kitten._tcp");
fullInfo.setPort(4242);
fullInfo.setHost(LOCALHOST);
checkParcelable(fullInfo);
NsdServiceInfo noHostInfo = new NsdServiceInfo();
noHostInfo.setServiceName("kitten");
noHostInfo.setServiceType("_kitten._tcp");
noHostInfo.setPort(4242);
checkParcelable(noHostInfo);
NsdServiceInfo attributedInfo = new NsdServiceInfo();
attributedInfo.setServiceName("kitten");
attributedInfo.setServiceType("_kitten._tcp");
attributedInfo.setPort(4242);
attributedInfo.setHost(LOCALHOST);
attributedInfo.setAttribute("color", "pink");
attributedInfo.setAttribute("sound", (new String("にゃあ")).getBytes("UTF-8"));
attributedInfo.setAttribute("adorable", (String) null);
attributedInfo.setAttribute("sticky", "yes");
attributedInfo.setAttribute("siblings", new byte[] {});
attributedInfo.setAttribute("edge cases", new byte[] { 0, -1, 127, -128 });
attributedInfo.removeAttribute("sticky");
checkParcelable(attributedInfo);
// Sanity check that we actually wrote attributes to attributedInfo.
assertTrue(attributedInfo.getAttributes().keySet().contains("adorable"));
String sound = new String(attributedInfo.getAttributes().get("sound"), "UTF-8");
assertTrue(sound.equals("にゃあ"));
byte[] edgeCases = attributedInfo.getAttributes().get("edge cases");
assertTrue(Arrays.equals(edgeCases, new byte[] { 0, -1, 127, -128 }));
assertFalse(attributedInfo.getAttributes().keySet().contains("sticky"));
}
use of android.net.nsd.NsdServiceInfo in project android_frameworks_base by crdroidandroid.
the class NsdServiceInfoTest method checkParcelable.
public void checkParcelable(NsdServiceInfo original) {
// Write to parcel.
Parcel p = Parcel.obtain();
Bundle writer = new Bundle();
writer.putParcelable("test_info", original);
writer.writeToParcel(p, 0);
// Extract from parcel.
p.setDataPosition(0);
Bundle reader = p.readBundle();
reader.setClassLoader(NsdServiceInfo.class.getClassLoader());
NsdServiceInfo result = reader.getParcelable("test_info");
// Assert equality of base fields.
assertEquality(original.getServiceName(), result.getServiceName());
assertEquality(original.getServiceType(), result.getServiceType());
assertEquality(original.getHost(), result.getHost());
assertTrue(original.getPort() == result.getPort());
// Assert equality of attribute map.
Map<String, byte[]> originalMap = original.getAttributes();
Map<String, byte[]> resultMap = result.getAttributes();
assertEquality(originalMap.keySet(), resultMap.keySet());
for (String key : originalMap.keySet()) {
assertTrue(Arrays.equals(originalMap.get(key), resultMap.get(key)));
}
}
use of android.net.nsd.NsdServiceInfo in project facebook-android-sdk by facebook.
the class DeviceRequestsHelper method startAdvertisementServiceImpl.
@TargetApi(16)
private static boolean startAdvertisementServiceImpl(final String userCode) {
if (deviceRequestsListeners.containsKey(userCode)) {
return true;
}
// Dots in the version will mess up the Bonjour DNS record parsing
String sdkVersion = FacebookSdk.getSdkVersion().replace('.', '|');
// Other SDKs that adopt this feature should use different flavor name
// The whole name should not exceed 60 characters
final String nsdServiceName = String.format("%s_%s_%s", // static identifier
SDK_HEADER, // client app parses the string based on this version
String.format("%s-%s", SDK_FLAVOR, sdkVersion), // short code for the login flow
userCode);
NsdServiceInfo nsdLoginAdvertisementService = new NsdServiceInfo();
nsdLoginAdvertisementService.setServiceType(SERVICE_TYPE);
nsdLoginAdvertisementService.setServiceName(nsdServiceName);
nsdLoginAdvertisementService.setPort(80);
NsdManager nsdManager = (NsdManager) FacebookSdk.getApplicationContext().getSystemService(Context.NSD_SERVICE);
NsdManager.RegistrationListener nsdRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
// Android may have changed the service name in order to resolve a conflict
if (!nsdServiceName.equals(NsdServiceInfo.getServiceName())) {
cleanUpAdvertisementService(userCode);
}
}
@Override
public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
cleanUpAdvertisementService(userCode);
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
}
};
deviceRequestsListeners.put(userCode, nsdRegistrationListener);
nsdManager.registerService(nsdLoginAdvertisementService, NsdManager.PROTOCOL_DNS_SD, nsdRegistrationListener);
return true;
}
Aggregations