summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/MockKeyStore.java
blob: 4c30af35c32af9d1948e5f37883698ef217bb5a0 (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
package com.android.server.wifi;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.security.KeyStore;
import android.util.SparseArray;

import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.Arrays;
import java.util.HashMap;

class MockKeyStore {

    public static class KeyBlob {
        public byte[] blob;
        public int flag;

        public void update(byte[] blob, int flag) {
            this.blob = Arrays.copyOf(blob, blob.length);
            this.flag = flag;
        }
    }
    private SparseArray<HashMap<String, KeyBlob>> mStore;

    public MockKeyStore() {
        mStore = new SparseArray<HashMap<String, KeyBlob>>();
    }

    public KeyStore createMock() {
        KeyStore mock = mock(KeyStore.class);
        when(mock.state()).thenReturn(KeyStore.State.UNLOCKED);

        when(mock.put(anyString(), Matchers.any(byte[].class), anyInt(), anyInt()))
                .thenAnswer(new Answer<Boolean>() {

                    @Override
                    public Boolean answer(InvocationOnMock invocation) throws Throwable {
                        Object[] args = invocation.getArguments();
                        return put((String) args[0], (byte[]) args[1], (Integer) args[2],
                                (Integer) args[3]);
                    }
                });

        when(mock.importKey(anyString(), Matchers.any(byte[].class), anyInt(), anyInt()))
                .thenAnswer(new Answer<Boolean>() {

                    @Override
                    public Boolean answer(InvocationOnMock invocation) throws Throwable {
                        Object[] args = invocation.getArguments();
                        return importKey((String) args[0], (byte[]) args[1], (Integer) args[2],
                                (Integer) args[3]);
                    }
                });

        when(mock.delete(anyString(), anyInt())).thenAnswer(new Answer<Boolean>() {

            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                return delete((String) args[0], (Integer) args[1]);
            }
        });

        when(mock.contains(anyString(), anyInt())).thenAnswer(new Answer<Boolean>() {

            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                return contains((String) args[0], (Integer) args[1]);
            }
        });

        return mock;
    }

    private KeyBlob access(int uid, String key, boolean createIfNotExist) {
        if (mStore.get(uid) == null) {
            mStore.put(uid, new HashMap<String, KeyBlob>());
        }
        HashMap<String, KeyBlob> map = mStore.get(uid);
        if (map.containsKey(key)) {
            return map.get(key);
        } else {
            if (createIfNotExist) {
                KeyBlob blob = new KeyBlob();
                map.put(key, blob);
                return blob;
            } else {
                return null;
            }
        }
    }

    public KeyBlob getKeyBlob(int uid, String key) {
        return access(uid, key, false);
    }

    private boolean put(String key, byte[] value, int uid, int flags) {
        access(uid, key, true).update(value,  flags);
        return true;
    }

    private boolean importKey(String keyName, byte[] key, int uid, int flags) {
        return put(keyName, key, uid, flags);
    }

    private boolean delete(String key, int uid) {
        if (mStore.get(uid) != null) {
            mStore.get(uid).remove(key);
        }
        return true;
    }

    private boolean contains(String key, int uid) {
        return access(uid, key, false) != null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("KeyStore {");
        for (int i = 0; i < mStore.size(); i++) {
            int uid = mStore.keyAt(i);
            for (String keyName : mStore.get(uid).keySet()) {
                sb.append(String.format("%d:%s, ", uid, keyName));
            }
        }
        sb.append("}");
        return sb.toString();
    }
}