aboutsummaryrefslogtreecommitdiff
path: root/app/src/test/java/invalid/lena/scrcpy/DevicesTest.java
blob: dcd04caf5d1ff50c17ab5923c592cd4642be6eb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package invalid.lena.scrcpy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DevicesTest {

    @Test
    public void parseEmptyOrNull() {
        assertTrue(Devices.parse(null).isEmpty());
        assertTrue(Devices.parse("").isEmpty());
        assertTrue(Devices.parse("   ").isEmpty());
    }

    @Test
    public void parseNonArrayReturnsEmpty() {
        assertTrue(Devices.parse("{not json").isEmpty());
        assertTrue(Devices.parse("{\"host\":\"x\"}").isEmpty());
    }

    @Test
    public void parseSkipsMalformedRowsKeepsValid() {
        // Middle row missing 'port' - must NOT nuke the other two.
        String json = "["
                + "{\"host\":\"a\",\"port\":1},"
                + "{\"host\":\"bad\"},"
                + "{\"host\":\"c\",\"port\":3}"
                + "]";
        List<Devices.Device> got = Devices.parse(json);
        assertEquals(2, got.size());
        assertEquals("a", got.get(0).host);
        assertEquals("c", got.get(1).host);
    }

    @Test
    public void roundTrip() {
        List<Devices.Device> in = new ArrayList<>(Arrays.asList(
                new Devices.Device("192.168.1.10", 5555),
                new Devices.Device("10.0.0.2",     43210)));
        String json = Devices.serialize(in);
        List<Devices.Device> out = Devices.parse(json);
        assertEquals(2, out.size());
        assertEquals("192.168.1.10", out.get(0).host);
        assertEquals(5555,           out.get(0).port);
        assertEquals("10.0.0.2",     out.get(1).host);
        assertEquals(43210,          out.get(1).port);
    }

    @Test
    public void parseAddressSplitsHostAndPort() {
        Devices.Device d = Devices.parseAddress("  192.168.1.42:41234 ");
        assertEquals("192.168.1.42", d.host);
        assertEquals(41234, d.port);

        Devices.Device v6 = Devices.parseAddress("[fe80::1]:5555");
        assertEquals("fe80::1", v6.host);
        assertEquals(5555, v6.port);
        // Round-trips through the display form.
        assertEquals("[fe80::1]:5555", v6.toString());
    }

    @Test
    public void parseAddressRejectsMalformed() {
        // No port, empty host, non-numeric and out-of-range ports.
        assertNull(Devices.parseAddress(null));
        assertNull(Devices.parseAddress(""));
        assertNull(Devices.parseAddress("192.168.1.42"));
        assertNull(Devices.parseAddress(":5555"));
        assertNull(Devices.parseAddress("192.168.1.42:"));
        assertNull(Devices.parseAddress("192.168.1.42:port"));
        assertNull(Devices.parseAddress("192.168.1.42:0"));
        assertNull(Devices.parseAddress("192.168.1.42:65536"));
        // Unbracketed IPv6 would otherwise split at the wrong colon.
        assertNull(Devices.parseAddress("fe80::1:5555"));
        assertNull(Devices.parseAddress("[fe80::1]5555"));
    }

    @Test
    public void parsePortRejectsNonDigits() {
        assertEquals(5555, Devices.parsePort(" 5555 "));
        assertEquals(1,     Devices.parsePort("1"));
        assertEquals(65535, Devices.parsePort("65535"));
        assertEquals(-1, Devices.parsePort(""));
        assertEquals(-1, Devices.parsePort("+5555"));
        assertEquals(-1, Devices.parsePort("-1"));
        assertEquals(-1, Devices.parsePort("55x5"));
        assertEquals(-1, Devices.parsePort("655360"));
    }

    @Test
    public void deviceEqualsByHostAndPort() {
        Devices.Device a = new Devices.Device("1.1.1.1", 5555);
        Devices.Device b = new Devices.Device("1.1.1.1", 5555);
        Devices.Device c = new Devices.Device("1.1.1.1", 5556);
        assertEquals(a, b);
        assertEquals(a.hashCode(), b.hashCode());
        assertEquals(false, a.equals(c));
    }
}