Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 20 | import static org.mockito.Mockito.any; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 22 | import static org.mockito.Mockito.never; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 23 | import static org.mockito.Mockito.reset; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 24 | import static org.mockito.Mockito.timeout; |
| 25 | import static org.mockito.Mockito.times; |
| 26 | import static org.mockito.Mockito.verify; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.os.Handler; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 30 | import android.os.HandlerThread; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 31 | import android.os.Looper; |
| 32 | import android.os.Message; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.content.ContentResolver; |
| 35 | import android.net.nsd.NsdManager; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 36 | import android.net.nsd.NsdServiceInfo; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 37 | import com.android.server.NsdService.DaemonConnection; |
| 38 | import com.android.server.NsdService.DaemonConnectionSupplier; |
| 39 | import com.android.server.NsdService.NativeCallbackReceiver; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 40 | import android.support.test.filters.SmallTest; |
| 41 | import android.support.test.runner.AndroidJUnit4; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 42 | import org.junit.After; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 46 | import org.mockito.ArgumentCaptor; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
| 49 | |
| 50 | // TODOs: |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 51 | // - test client can send requests and receive replies |
| 52 | // - test NSD_ON ENABLE/DISABLED listening |
| 53 | @RunWith(AndroidJUnit4.class) |
| 54 | @SmallTest |
| 55 | public class NsdServiceTest { |
| 56 | |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 57 | static final int PROTOCOL = NsdManager.PROTOCOL_DNS_SD; |
| 58 | |
| 59 | long mTimeoutMs = 100; // non-final so that tests can adjust the value. |
| 60 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 61 | @Mock Context mContext; |
| 62 | @Mock ContentResolver mResolver; |
| 63 | @Mock NsdService.NsdSettings mSettings; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 64 | @Mock DaemonConnection mDaemon; |
| 65 | NativeCallbackReceiver mDaemonCallback; |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 66 | HandlerThread mThread; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 67 | TestHandler mHandler; |
| 68 | |
| 69 | @Before |
| 70 | public void setUp() throws Exception { |
| 71 | MockitoAnnotations.initMocks(this); |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 72 | mThread = new HandlerThread("mock-service-handler"); |
| 73 | mThread.start(); |
| 74 | mHandler = new TestHandler(mThread.getLooper()); |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 75 | when(mContext.getContentResolver()).thenReturn(mResolver); |
| 76 | } |
| 77 | |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 78 | @After |
| 79 | public void tearDown() throws Exception { |
| 80 | mThread.quit(); |
| 81 | } |
| 82 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 83 | @Test |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 84 | public void testClientsCanConnectAndDisconnect() { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 85 | when(mSettings.isEnabled()).thenReturn(true); |
| 86 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 87 | NsdService service = makeService(); |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 88 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 89 | NsdManager client1 = connectClient(service); |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 90 | verify(mDaemon, timeout(100).times(1)).start(); |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 91 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 92 | NsdManager client2 = connectClient(service); |
| 93 | |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 94 | client1.disconnect(); |
| 95 | client2.disconnect(); |
| 96 | |
| 97 | verify(mDaemon, timeout(mTimeoutMs).times(1)).stop(); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testClientRequestsAreGCedAtDisconnection() { |
| 102 | when(mSettings.isEnabled()).thenReturn(true); |
| 103 | when(mDaemon.execute(any())).thenReturn(true); |
| 104 | |
| 105 | NsdService service = makeService(); |
| 106 | NsdManager client = connectClient(service); |
| 107 | |
| 108 | verify(mDaemon, timeout(100).times(1)).start(); |
| 109 | |
| 110 | NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type"); |
| 111 | request.setPort(2201); |
| 112 | |
| 113 | // Client registration request |
| 114 | NsdManager.RegistrationListener listener1 = mock(NsdManager.RegistrationListener.class); |
| 115 | client.registerService(request, PROTOCOL, listener1); |
| 116 | verifyDaemonCommand("register 2 a_name a_type 2201"); |
| 117 | |
| 118 | // Client discovery request |
| 119 | NsdManager.DiscoveryListener listener2 = mock(NsdManager.DiscoveryListener.class); |
| 120 | client.discoverServices("a_type", PROTOCOL, listener2); |
| 121 | verifyDaemonCommand("discover 3 a_type"); |
| 122 | |
| 123 | // Client resolve request |
| 124 | NsdManager.ResolveListener listener3 = mock(NsdManager.ResolveListener.class); |
| 125 | client.resolveService(request, listener3); |
| 126 | verifyDaemonCommand("resolve 4 a_name a_type local."); |
| 127 | |
| 128 | // Client disconnects |
| 129 | client.disconnect(); |
| 130 | verify(mDaemon, timeout(mTimeoutMs).times(1)).stop(); |
| 131 | |
| 132 | // checks that request are cleaned |
| 133 | verifyDaemonCommands("stop-register 2", "stop-discover 3", "stop-resolve 4"); |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | NsdService makeService() { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 137 | DaemonConnectionSupplier supplier = (callback) -> { |
| 138 | mDaemonCallback = callback; |
| 139 | return mDaemon; |
| 140 | }; |
| 141 | NsdService service = new NsdService(mContext, mSettings, mHandler, supplier); |
| 142 | verify(mDaemon, never()).execute(any(String.class)); |
| 143 | return service; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | NsdManager connectClient(NsdService service) { |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 147 | return new NsdManager(mContext, service); |
| 148 | } |
| 149 | |
| 150 | void verifyDaemonCommands(String... wants) { |
| 151 | verifyDaemonCommand(String.join(" ", wants), wants.length); |
| 152 | } |
| 153 | |
| 154 | void verifyDaemonCommand(String want) { |
| 155 | verifyDaemonCommand(want, 1); |
| 156 | } |
| 157 | |
| 158 | void verifyDaemonCommand(String want, int n) { |
| 159 | ArgumentCaptor<Object> argumentsCaptor = ArgumentCaptor.forClass(Object.class); |
| 160 | verify(mDaemon, timeout(mTimeoutMs).times(n)).execute(argumentsCaptor.capture()); |
| 161 | String got = ""; |
| 162 | for (Object o : argumentsCaptor.getAllValues()) { |
| 163 | got += o + " "; |
| 164 | } |
| 165 | assertEquals(want, got.trim()); |
| 166 | // rearm deamon for next command verification |
| 167 | reset(mDaemon); |
| 168 | when(mDaemon.execute(any())).thenReturn(true); |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public static class TestHandler extends Handler { |
| 172 | public Message lastMessage; |
| 173 | |
| 174 | TestHandler(Looper looper) { |
| 175 | super(looper); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void handleMessage(Message msg) { |
| 180 | lastMessage = obtainMessage(); |
| 181 | lastMessage.copyFrom(msg); |
| 182 | } |
| 183 | } |
| 184 | } |