summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/anqp/TestDriver.java
blob: b6be0e49448ba1d6cbe25748438c3e07828e88e2 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.android.server.wifi.anqp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Test ANQP code by talking to an ANQP server of a socket.
 */
public class TestDriver {

    private static final Constants.ANQPElementType[] QueryElements = {
            Constants.ANQPElementType.ANQPCapabilityList,
            Constants.ANQPElementType.ANQPVenueName,
            Constants.ANQPElementType.ANQPEmergencyNumber,
            Constants.ANQPElementType.ANQPNwkAuthType,
            Constants.ANQPElementType.ANQPRoamingConsortium,
            Constants.ANQPElementType.ANQPIPAddrAvailability,
            Constants.ANQPElementType.ANQPNAIRealm,
            Constants.ANQPElementType.ANQP3GPPNetwork,
            Constants.ANQPElementType.ANQPGeoLoc,
            Constants.ANQPElementType.ANQPCivicLoc,
            Constants.ANQPElementType.ANQPLocURI,
            Constants.ANQPElementType.ANQPDomName,
            Constants.ANQPElementType.ANQPEmergencyAlert,
            Constants.ANQPElementType.ANQPTDLSCap,
            Constants.ANQPElementType.ANQPEmergencyNAI,
            Constants.ANQPElementType.ANQPNeighborReport,

            Constants.ANQPElementType.HSCapabilityList,
            Constants.ANQPElementType.HSFriendlyName,
            Constants.ANQPElementType.HSWANMetrics,
            Constants.ANQPElementType.HSConnCapability,
            Constants.ANQPElementType.HSNAIHomeRealmQuery,
            Constants.ANQPElementType.HSOperatingclass,
            Constants.ANQPElementType.HSOSUProviders
    };

    public static void runTest() throws IOException {

        Set<Constants.ANQPElementType> elements =
                new HashSet<Constants.ANQPElementType>(QueryElements.length);
        elements.addAll(Arrays.asList(QueryElements));

        ByteBuffer request = ByteBuffer.allocate(8192);
        request.order(ByteOrder.LITTLE_ENDIAN);
        int lenPos = request.position();
        request.putShort((short) 0);
        ANQPFactory.buildQueryRequest(elements, request);

        byte[] requestBytes = prepRequest(lenPos, request);

        System.out.println( "Connecting...");
        Socket sock = new Socket(InetAddress.getLoopbackAddress(), 6104);
        BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());
        System.out.println(" ### Querying for " + Arrays.toString(QueryElements));
        out.write(requestBytes);
        out.flush();

        BufferedInputStream in = new BufferedInputStream(sock.getInputStream());
        ByteBuffer payload = getResponse(in);

        HSOsuProvidersElement osuProvidersElement = null;
        List<ANQPElement> anqpResult = ANQPFactory.parsePayload(payload);
        for ( ANQPElement element : anqpResult ) {
            System.out.println( element );
            if (element.getID() == Constants.ANQPElementType.HSOSUProviders) {
                osuProvidersElement = (HSOsuProvidersElement)element;
            }
        }

        if ( osuProvidersElement != null ) {
            for (OSUProvider provider : osuProvidersElement.getProviders()) {
                for (IconInfo iconInfo : provider.getIcons()) {
                    sendIconRequest(iconInfo.getFileName());
                }
            }
        }
        sendIconRequest("doesNotExist.noimg");

        sendHomeRealmQuery("nxdomain.abc", "jan.com");
    }

    private static void sendIconRequest(String fileName) throws IOException {
        ByteBuffer iconRequest = ByteBuffer.allocate(fileName.length()*2)
                .order(ByteOrder.LITTLE_ENDIAN);
        int iconPos = iconRequest.position();
        iconRequest.putShort((short) 0);
        ANQPFactory.buildIconRequest(fileName, iconRequest);
        byte[] iconBytes = prepRequest(iconPos, iconRequest);

        System.out.println( "Connecting...");
        Socket sock = new Socket(InetAddress.getLoopbackAddress(), 6104);
        BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());

        System.out.println(" ### Requesting icon '" + fileName + "'");
        out.write(iconBytes);
        out.flush();

        BufferedInputStream in = new BufferedInputStream(sock.getInputStream());
        ByteBuffer payload = getResponse(in);
        List<ANQPElement> anqpResult = ANQPFactory.parsePayload(payload);
        System.out.println("Icon: " + anqpResult );
    }

    private static void sendHomeRealmQuery(String ... realms) throws IOException{
        ByteBuffer request = ByteBuffer.allocate(1024).order(ByteOrder.LITTLE_ENDIAN);
        int iconPos = request.position();
        request.putShort((short) 0);
        ANQPFactory.buildHomeRealmRequest(Arrays.asList(realms), request);
        byte[] iconBytes = prepRequest(iconPos, request);

        System.out.println( "Connecting...");
        Socket sock = new Socket(InetAddress.getLoopbackAddress(), 6104);
        BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());

        System.out.println(" ### Home realm query for " + Arrays.toString(realms));
        out.write(iconBytes);
        out.flush();

        BufferedInputStream in = new BufferedInputStream(sock.getInputStream());
        ByteBuffer payload = getResponse(in);
        List<ANQPElement> anqpResult = ANQPFactory.parsePayload(payload);
        System.out.println("Home realm query: " + anqpResult );
    }

    private static ByteBuffer getResponse(InputStream in) throws IOException {
        ByteBuffer lengthBuffer = read( in, 2 );
        int length = lengthBuffer.getShort() & Constants.SHORT_MASK;
        System.out.println("Length " + length);

        return read(in, length);
    }

    private static byte[] prepRequest(int pos0, ByteBuffer request) {
        request.putShort(pos0, (short)( request.limit() - pos0 - Constants.BYTES_IN_SHORT ));
        byte[] octets = new byte[request.remaining()];
        request.get(octets);
        return octets;
    }

    private static ByteBuffer read(InputStream in, int length) throws IOException {
        byte[] payload = new byte[length];
        int position = 0;
        while ( position < length ) {
            int amount = in.read(payload, position, length - position);
            if ( amount <= 0 ) {
                throw new EOFException("Got " + amount);
            }
            position += amount;
        }
        return ByteBuffer.wrap(payload).order(ByteOrder.LITTLE_ENDIAN);
    }

    public static void main(String[] args) throws IOException {
        runTest();
    }
}