summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/hotspot2/AnqpCacheTest.java
blob: 3084e064a2b3d99fcc9aa462bb47719375605c51 (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi.hotspot2;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import androidx.test.filters.SmallTest;

import com.android.server.wifi.Clock;
import com.android.server.wifi.hotspot2.ANQPData;
import com.android.server.wifi.hotspot2.AnqpCache;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

/**
 * Unit tests for {@link com.android.server.wifi.hotspot2.AnqpCache}.
 *
 * TODO(b/33000864): add more test once the ANQP elements cleanup are completed, which will
 * allow easy construction of ANQP elements for testing.
 */
@SmallTest
public class AnqpCacheTest {
    private static final ANQPNetworkKey ENTRY_KEY = new ANQPNetworkKey("test", 0L, 0L, 1);

    @Mock Clock mClock;
    AnqpCache mCache;

    /**
     * Sets up test.
     */
    @Before
    public void setUp() throws Exception {
        initMocks(this);
        // Returning the initial timestamp.
        when(mClock.getElapsedSinceBootMillis()).thenReturn(0L);
        mCache = new AnqpCache(mClock);
    }

    /**
     * Verify expectation for addEntry and getEntry.
     *
     * @throws Exception
     */
    @Test
    public void addAndGetEntry() throws Exception {
        mCache.addEntry(ENTRY_KEY, null);
        ANQPData data = mCache.getEntry(ENTRY_KEY);
        assertNotNull(data);
        assertTrue(data.getElements().isEmpty());
    }

    /**
     * Verify that getting a non-existing entry will return a null.
     *
     * @throws Exception
     */
    @Test
    public void getNonExistEntry() throws Exception {
        assertNull(mCache.getEntry(ENTRY_KEY));
    }

    /**
     * Verify the expectation for the sweep function (expired entries will be removed).
     *
     * @throws Exception
     */
    @Test
    public void sweepRemoveExpiredEntry() throws Exception {
        mCache.addEntry(ENTRY_KEY, null);

        // Sweep the cache when the entry is not expired.
        when(mClock.getElapsedSinceBootMillis())
                .thenReturn(AnqpCache.CACHE_SWEEP_INTERVAL_MILLISECONDS);
        mCache.sweep();
        assertNotNull(mCache.getEntry(ENTRY_KEY));

        // Sweep the cache when the entry is expired.
        when(mClock.getElapsedSinceBootMillis()).thenReturn(ANQPData.DATA_LIFETIME_MILLISECONDS);
        mCache.sweep();
        assertNull(mCache.getEntry(ENTRY_KEY));
    }
}