Search in sources :

Example 11 with NsdServiceInfo

use of android.net.nsd.NsdServiceInfo in project platform_frameworks_base by android.

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"));
}
Also used : NsdServiceInfo(android.net.nsd.NsdServiceInfo)

Example 12 with NsdServiceInfo

use of android.net.nsd.NsdServiceInfo in project android_frameworks_base by DirtyUnicorns.

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);
}
Also used : NsdServiceInfo(android.net.nsd.NsdServiceInfo)

Example 13 with NsdServiceInfo

use of android.net.nsd.NsdServiceInfo in project android_frameworks_base by DirtyUnicorns.

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)));
    }
}
Also used : NsdServiceInfo(android.net.nsd.NsdServiceInfo) Parcel(android.os.Parcel) Bundle(android.os.Bundle)

Aggregations

NsdServiceInfo (android.net.nsd.NsdServiceInfo)13 Bundle (android.os.Bundle)4 Parcel (android.os.Parcel)4 TargetApi (android.annotation.TargetApi)1 NsdManager (android.net.nsd.NsdManager)1