1 package com.android.tv.mdnsoffloadmanager;
2 
3 import static org.junit.Assert.assertEquals;
4 
5 import android.content.Intent;
6 import android.net.LinkProperties;
7 import android.os.PowerManager;
8 
9 import device.google.atv.mdns_offload.IMdnsOffloadManager.OffloadServiceInfo;
10 import java.util.Arrays;
11 import java.util.List;
12 import java.util.Set;
13 import java.util.stream.Collectors;
14 
15 public class TestHelpers {
16 
17     static final OffloadServiceInfo SERVICE_ATV
18         = makeOffloadServiceInfo("", "atv", "somedevice", new byte[]{
19             0, 0, 0, 0,             // Id, Flags
20             0, 0, 0, 1, 0, 0, 0, 0, // Header section, 1 answer
21 
22             // Data 1:
23             3, 'a', 't', 'v', 0x00, // "atv."
24             0x00, 0x01,             // Type A
25             (byte) 0x80, 0x01,      // Cache flush: True, class: in
26             0, 0, 0, 5,             // TTL 5sec
27             0, 4,                   // Data with size 4
28             100, 80, 40, 20         // IP: 100.80.40.20
29         });
30 
31     static final OffloadServiceInfo SERVICE_AIRPLAY
32         = makeOffloadServiceInfo("", "airplay", "somedevice", new byte[]{
33             0, 0, 0, 0,             // Id, Flags
34             0, 0, 0, 1, 0, 0, 0, 0, // Header section, 1 answer
35 
36             // Data 1:
37             7, 'a', 'i', 'r', 'p', 'l', 'a', 'y', 0x00, // "airplay."
38             0x00, 0x01,             // Type A
39             (byte) 0x80, 0x01,      // Cache flush: True, class: in
40             0, 0, 0, 5,             // TTL 5sec
41             0, 4,                   // Data with size 4
42             100, 80, 40, 20         // IP: 100.80.40.20
43         });
44 
45     static final OffloadServiceInfo SERVICE_GTV
46         = makeOffloadServiceInfo("gtv", "atv", "somedevice", new byte[]{
47             0, 0, 0, 0,             // Id, Flags
48             0, 0, 0, 2, 0, 0, 0, 0, // Header section, 2 answers
49 
50             // Data 1:
51             3, 'a', 't', 'v', 0x00, // "atv."
52             0x00, 0x01,             // Type A
53             (byte) 0x80, 0x01,      // Cache flush: True, class: in
54             0, 0, 0, 5,             // TTL 5sec
55             0, 4,                   // Data with size 4
56             100, 80, 40, 20,        // IP: 100.80.40.20
57 
58             // Data 2:
59             3, 'g', 't', 'v',       // "gtv."
60             (byte) 0b11000000, 12,  // [ptr->] "atv."
61             0x00, 16,               // Type TXT
62             (byte) 0x80, 0x01,      // Cache flush: True, class: in
63             0, 0, 0, 5,             // TTL 5sec
64             0, 3,                   // Data with size 3
65             'i', 's', 'o'           // "iso"
66         });
67 
68     static final OffloadServiceInfo SERVICE_GOOGLECAST
69         = makeOffloadServiceInfo("_googlecast", "_tcp", "tv-abc", new byte[]{
70             0, 0, 0, 0,             // Id, Flags
71             0, 0, 0, 2, 0, 0, 0, 0, // Header section, 2 answers
72 
73             // Data 1:
74             11, '_', 'g', 'o', 'o', 'g', 'l', 'e', 'c', 'a', 's', 't', // "_googlecast."
75             4, '_', 't', 'c', 'p',  // "_tcp."
76             5, 'l', 'o', 'c', 'a', 'l', 0x00,  // "local."
77             0x00, 0x0c,             // Type PTR
78             (byte) 0x80, 0x01,      // Cache flush: True, class: in
79             0, 0, 0, 5,             // TTL 5sec
80             0, 9,                   // Data with size 9
81             6, 't', 'v', '-', 'a', 'b', 'c', // "tv-abc."
82             (byte) 0b11000000, 29,  // [ptr->] "local."
83 
84             // Data 2:
85             (byte) 0b11000000, 46,  // [ptr->] "tv-abc.local."
86             0x00, 0x01,             // Type A
87             (byte) 0x80, 0x01,      // Cache flush: True, class: in
88             0, 0, 0, 5,             // TTL 5sec
89             0, 4,                   // Data with size 4
90             100, 80, 40, 20,        // IP: 100.80.40.20
91         });
92 
makeOffloadServiceInfo(String serviceName, String serviceType, String deviceHostName, byte[] rawOffloadPacket)93     static OffloadServiceInfo makeOffloadServiceInfo(String serviceName, String serviceType,
94         String deviceHostName, byte[] rawOffloadPacket) {
95         OffloadServiceInfo serviceInfo = new OffloadServiceInfo();
96         serviceInfo.serviceName = serviceName;
97         serviceInfo.serviceType = serviceType;
98         serviceInfo.deviceHostName = deviceHostName;
99         serviceInfo.rawOffloadPacket = rawOffloadPacket;
100         return serviceInfo;
101     }
102 
makeLinkProperties(String interfaceName)103     static LinkProperties makeLinkProperties(String interfaceName) {
104         LinkProperties linkProperties = new LinkProperties();
105         linkProperties.setInterfaceName(interfaceName);
106         return linkProperties;
107     }
108 
makeIntent(String action)109     static Intent makeIntent(String action) {
110         Intent intent = new Intent();
111         if (action != null) {
112             intent.setAction(action);
113         }
114         return intent;
115     }
116 
makeLowPowerStandbyPolicy(String... exemptPackages)117     static PowerManager.LowPowerStandbyPolicy makeLowPowerStandbyPolicy(String... exemptPackages) {
118         return new PowerManager.LowPowerStandbyPolicy(
119                 "placeholder", Set.of(exemptPackages), 0, Set.of());
120     }
121 
verifyOffloadedServices( FakeMdnsOffloadService offloadService, String networkInterface, OffloadServiceInfo... expectedServices)122     static void verifyOffloadedServices(
123             FakeMdnsOffloadService offloadService,
124             String networkInterface,
125             OffloadServiceInfo... expectedServices) {
126         List<byte[]> expectedPackets = Arrays.stream(expectedServices)
127                 .map(service -> service.rawOffloadPacket)
128                 .collect(Collectors.toList());
129         List<byte[]> offloadedPackets = offloadService
130                 .getOffloadData(networkInterface)
131                 .offloadedRecords
132                 .stream()
133                 .map(protocolData -> protocolData.rawOffloadPacket)
134                 .collect(Collectors.toList());
135         assertEquals(expectedPackets, offloadedPackets);
136     }
137 
verifyPassthroughQNames( FakeMdnsOffloadService offloadService, String networkInterface, String... qNames)138     static void verifyPassthroughQNames(
139             FakeMdnsOffloadService offloadService,
140             String networkInterface,
141             String... qNames) {
142         assertEquals(
143                 List.of(qNames),
144                 offloadService.getOffloadData(networkInterface).passthroughQNames);
145     }
146 }
147