2015-06-13 12:36:22 +05:30
|
|
|
/*
|
|
|
|
* Copyright 2015 Vineet Garg <grg.vineet@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.kde.kdeconnect;
|
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
2015-06-13 12:36:22 +05:30
|
|
|
import org.kde.kdeconnect.Backends.LanBackend.LanLink;
|
|
|
|
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
|
2019-02-11 21:44:30 +01:00
|
|
|
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
2015-06-13 12:36:22 +05:30
|
|
|
import org.mockito.Mockito;
|
2019-02-11 21:44:30 +01:00
|
|
|
import org.powermock.api.mockito.PowerMockito;
|
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner;
|
2015-06-13 12:36:22 +05:30
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2016-06-12 21:07:01 +02:00
|
|
|
import java.lang.reflect.Method;
|
2015-06-13 12:36:22 +05:30
|
|
|
import java.net.Socket;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
2015-06-13 12:36:22 +05:30
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
@RunWith(PowerMockRunner.class)
|
|
|
|
@PrepareForTest({DeviceHelper.class, Log.class})
|
|
|
|
public class LanLinkProviderTest {
|
2015-06-13 12:36:22 +05:30
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
PowerMockito.mockStatic(DeviceHelper.class);
|
|
|
|
PowerMockito.when(DeviceHelper.getDeviceId(any())).thenReturn("123");
|
2015-06-13 12:36:22 +05:30
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
PowerMockito.mockStatic(Log.class);
|
2015-06-13 12:36:22 +05:30
|
|
|
}
|
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
@Test
|
2018-03-04 11:31:37 +01:00
|
|
|
public void testIdentityPacketReceived() throws Exception {
|
2015-06-13 12:36:22 +05:30
|
|
|
|
2019-02-11 21:44:30 +01:00
|
|
|
LanLinkProvider linkProvider = new LanLinkProvider(null);
|
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket networkPacket = Mockito.mock(NetworkPacket.class);
|
|
|
|
Mockito.when(networkPacket.getType()).thenReturn("kdeconnect.identity");
|
|
|
|
Mockito.when(networkPacket.getString("deviceId")).thenReturn("testDevice");
|
|
|
|
Mockito.when(networkPacket.getString("deviceName")).thenReturn("Test Device");
|
|
|
|
Mockito.when(networkPacket.getInt("protocolVersion")).thenReturn(5);
|
|
|
|
Mockito.when(networkPacket.getString("deviceType")).thenReturn("phone");
|
2015-06-13 12:36:22 +05:30
|
|
|
|
|
|
|
String serialized = "{\"type\":\"kdeconnect.identity\",\"id\":12345,\"body\":{\"deviceName\":\"Test Device\",\"deviceType\":\"phone\",\"deviceId\":\"testDevice\",\"protocolVersion\":5}}";
|
2018-03-04 11:31:37 +01:00
|
|
|
Mockito.when(networkPacket.serialize()).thenReturn(serialized);
|
2015-06-13 12:36:22 +05:30
|
|
|
|
2016-06-21 16:45:18 +02:00
|
|
|
Socket channel = Mockito.mock(Socket.class);
|
2018-09-29 21:08:45 +02:00
|
|
|
Method method = LanLinkProvider.class.getDeclaredMethod("identityPacketReceived", NetworkPacket.class, Socket.class, LanLink.ConnectionStarted.class);
|
|
|
|
method.setAccessible(true);
|
|
|
|
method.invoke(linkProvider, networkPacket, channel, LanLink.ConnectionStarted.Locally);
|
2015-06-13 12:36:22 +05:30
|
|
|
|
|
|
|
HashMap<String, LanLink> visibleComputers;
|
2018-09-29 21:08:45 +02:00
|
|
|
Field field = LanLinkProvider.class.getDeclaredField("visibleComputers");
|
|
|
|
field.setAccessible(true);
|
|
|
|
visibleComputers = (HashMap<String, LanLink>) field.get(linkProvider);
|
2015-06-13 12:36:22 +05:30
|
|
|
assertNotNull(visibleComputers.get("testDevice"));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|