NonEAPInnerAuth.java revision d8dddd9671750e6bfbcfa218db16ad096b9904ee
1/*
2 * Copyright (C) 2016 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
17package com.android.server.wifi.hotspot2.anqp.eap;
18
19import com.android.internal.annotations.VisibleForTesting;
20
21import java.net.ProtocolException;
22import java.nio.BufferUnderflowException;
23import java.nio.ByteBuffer;
24
25/**
26 * The Non-EAP Inner Authentication Type authentication parameter, IEEE802.11-2012, table 8-188.
27 *
28 * Format:
29 * | Type |
30 *    1
31 */
32public class NonEAPInnerAuth extends AuthParam {
33    public static final int AUTH_TYPE_PAP = 1;
34    public static final int AUTH_TYPE_CHAP = 2;
35    public static final int AUTH_TYPE_MSCHAP = 3;
36    public static final int AUTH_TYPE_MSCHAPV2 = 4;
37
38    @VisibleForTesting
39    public static final int EXPECTED_LENGTH_VALUE = 1;
40
41    private final int mAuthType;
42
43    public NonEAPInnerAuth(int authType) {
44        super(AuthParam.PARAM_TYPE_NON_EAP_INNER_AUTH_TYPE);
45        mAuthType = authType;
46    }
47
48    /**
49     * Parse a NonEAPInnerAuth from the given buffer.
50     *
51     * @param payload The byte buffer to read from
52     * @param length The length of the data
53     * @return {@link NonEAPInnerAuth}
54     * @throws BufferUnderflowException
55     */
56    public static NonEAPInnerAuth parse(ByteBuffer payload, int length) throws ProtocolException {
57        if (length != EXPECTED_LENGTH_VALUE) {
58            throw new ProtocolException("Invalid length: " + length);
59        }
60        int authType = payload.get() & 0xFF;
61        return new NonEAPInnerAuth(authType);
62    }
63
64    @Override
65    public boolean equals(Object thatObject) {
66        if (thatObject == this) {
67            return true;
68        }
69        if (!(thatObject instanceof NonEAPInnerAuth)) {
70            return false;
71        }
72        NonEAPInnerAuth that = (NonEAPInnerAuth) thatObject;
73        return mAuthType == that.mAuthType;
74    }
75
76    @Override
77    public int hashCode() {
78        return mAuthType;
79    }
80
81    @Override
82    public String toString() {
83        return "NonEAPInnerAuth{mAuthType=" + mAuthType + "}";
84    }
85}
86