Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Dev: fix txt record parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
andriydruk committed May 19, 2021
1 parent 0e1a0a1 commit c80e412
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dnssd/src/main/java/com/github/druk/dnssd/DNSSD.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ static Map<String, String> parseTXTRecords(TXTRecord record) {
Map<String, String> result = new HashMap<>(record.size());
for (int i = 0; i < record.size(); i++) {
try {
if (!TextUtils.isEmpty(record.getKey(i)) && TextUtils.isEmpty(record.getValueAsString(i))) {
if (!TextUtils.isEmpty(record.getKey(i))) {
result.put(record.getKey(i), record.getValueAsString(i));
}
} catch (Exception e) {
Expand Down
38 changes: 38 additions & 0 deletions dnssd/src/test/java/com/github/druk/dnssd/DnssdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.druk.dnssd;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -28,11 +29,14 @@
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
Expand Down Expand Up @@ -220,4 +224,38 @@ public void test_query_records_failure() throws DNSSDException {
verify(queryListener).operationFailed(any(DNSSDService.class), eq(0));
}

@Test
public void test_txt_record_parse_empty() {
TXTRecord record = new TXTRecord();
Map<String, String> map = DNSSD.parseTXTRecords(record);
Assert.assertTrue(map.isEmpty());
}

@Test
public void test_txt_record_parse_empty_value() {
TXTRecord record = new TXTRecord();
record.set("key", (String) null);
Map<String, String> map = DNSSD.parseTXTRecords(record);
Assert.assertEquals(map.size(), 1);
Assert.assertNull(map.get("key"));
}

@Test
public void test_txt_record_parse_with_string_value() {
TXTRecord record = new TXTRecord();
record.set("key", "value");
Map<String, String> map = DNSSD.parseTXTRecords(record);
Assert.assertEquals(map.size(), 1);
Assert.assertEquals(map.get("key"), "value");
}

@Test
public void test_txt_record_parse_with_byte_value() {
TXTRecord record = new TXTRecord();
record.set("key", "value".getBytes());
Map<String, String> map = DNSSD.parseTXTRecords(record);
Assert.assertEquals(map.size(), 1);
Assert.assertEquals(map.get("key"), "value");
}

}

0 comments on commit c80e412

Please sign in to comment.