NAIRealmDataTest.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;
18
19import static org.junit.Assert.assertEquals;
20
21import android.test.suitebuilder.annotation.SmallTest;
22
23import org.junit.Test;
24
25import java.net.ProtocolException;
26import java.nio.BufferUnderflowException;
27import java.nio.ByteBuffer;
28
29/**
30 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.NAIRealmData}.
31 */
32@SmallTest
33public class NAIRealmDataTest {
34    /**
35     * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer.
36     *
37     * @throws Exception
38     */
39    @Test(expected = BufferUnderflowException.class)
40    public void parseEmptyBuffer() throws Exception {
41        NAIRealmData.parse(ByteBuffer.wrap(new byte[0]));
42    }
43
44    /**
45     * Verify that ProtocolException will be thrown when parsing a truncated buffer
46     * (missing a byte at the end).
47     *
48     * @throws Exception
49     */
50    @Test(expected = ProtocolException.class)
51    public void parseTruncatedBuffer() throws Exception {
52        ByteBuffer buffer = ByteBuffer.wrap(NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES);
53        buffer.limit(buffer.remaining() - 1);
54        NAIRealmData.parse(buffer);
55    }
56
57    /**
58     * Verify that an expected NAIRealmData will be returned when parsing a buffer contained
59     * the test data with realm string encoded using UTF8.
60     *
61     * @throws Exception
62     */
63    @Test
64    public void parseBufferWithUTF8EncodedNAIRealmData() throws Exception {
65        ByteBuffer buffer = ByteBuffer.wrap(NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES);
66        assertEquals(NAIRealmDataTestUtil.TEST_REALM_DATA, NAIRealmData.parse(buffer));
67    }
68
69    /**
70     * Verify that the expected NAIRealmData will be returned when parsing a buffer contained
71     * the test data with realm string encoded using non-UTF8.
72     *
73     * @throws Exception
74     */
75    @Test
76    public void parseBufferWithNonUTF8EncodedNAIRealmData() throws Exception {
77        ByteBuffer buffer = ByteBuffer.wrap(
78                NAIRealmDataTestUtil.TEST_REAML_WITH_NON_UTF8_DATA_BYTES);
79        assertEquals(NAIRealmDataTestUtil.TEST_REALM_DATA, NAIRealmData.parse(buffer));
80    }
81}
82