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