forked from java-native-access/jna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreFoundationTest.java
377 lines (320 loc) · 14.3 KB
/
CoreFoundationTest.java
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (c) 2019 Daniel Widdis
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.mac;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import com.sun.jna.platform.mac.CoreFoundation.CFDictionaryRef;
import org.junit.Assert;
import org.junit.Test;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.mac.CoreFoundation.CFAllocatorRef;
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
import com.sun.jna.platform.mac.CoreFoundation.CFDataRef;
import com.sun.jna.platform.mac.CoreFoundation.CFIndex;
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
import com.sun.jna.platform.mac.CoreFoundation.CFNumberRef;
import com.sun.jna.platform.mac.CoreFoundation.CFNumberType;
import com.sun.jna.platform.mac.CoreFoundation.CFStringRef;
import com.sun.jna.platform.mac.CoreFoundation.CFTypeRef;
import com.sun.jna.ptr.DoubleByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;
public class CoreFoundationTest {
private static final CoreFoundation CF = CoreFoundation.INSTANCE;
@Test
public void testCFStringRef() throws UnsupportedEncodingException {
// Generate strings with different UTF8 byte lengths
byte[] pound = { (byte) 0xc2, (byte) 0xa3 };
byte[] euro = { (byte) 0xe2, (byte) 0x82, (byte) 0xac };
byte[] smileEmoji = { (byte) 0xf0, (byte) 0x9f, (byte) 0x98, (byte) 0x83 };
String[] testStrings = new String[4];
testStrings[0] = "ascii";
testStrings[1] = new String(pound, StandardCharsets.UTF_8);
testStrings[2] = new String(euro, StandardCharsets.UTF_8);
testStrings[3] = new String(smileEmoji, StandardCharsets.UTF_8);
for (String utf8Str : testStrings) {
CFStringRef cfStr = CFStringRef.createCFString(utf8Str);
// Length matches length of char array
// 2 for code points > 0xffff, 1 otherwise
assertEquals(utf8Str.length(), CF.CFStringGetLength(cfStr).intValue());
assertEquals(utf8Str, cfStr.stringValue());
assertEquals(CoreFoundation.STRING_TYPE_ID, cfStr.getTypeID());
byte[] utf8Arr = utf8Str.getBytes("UTF-8");
Memory mem = new Memory(utf8Arr.length + 1);
mem.clear();
assertNotEquals(0,
CF.CFStringGetCString(cfStr, mem, new CFIndex(mem.size()), CoreFoundation.kCFStringEncodingUTF8));
byte[] utf8Bytes = mem.getByteArray(0, (int) mem.size() - 1);
assertArrayEquals(utf8Arr, utf8Bytes);
// Essentially a toString, can't rely on format but should contain the string
CFStringRef desc = CF.CFCopyDescription(cfStr);
assertTrue(desc.stringValue().contains(utf8Str));
desc.release();
cfStr.release();
}
CFStringRef cfEmpty = CFStringRef.createCFString("");
assertTrue(cfEmpty.stringValue().equals(""));
cfEmpty.release();
}
@Test
public void testCFNumberRef() {
LongByReference max = new LongByReference(Long.MAX_VALUE);
CFNumberRef cfMax = CF.CFNumberCreate(null, CFNumberType.kCFNumberLongLongType.typeIndex(), max);
assertEquals(Long.MAX_VALUE, cfMax.longValue());
assertEquals(CoreFoundation.NUMBER_TYPE_ID, cfMax.getTypeID());
cfMax.release();
IntByReference zero = new IntByReference(0);
IntByReference one = new IntByReference(1);
CFNumberRef cfZero = CF.CFNumberCreate(null, CFNumberType.kCFNumberIntType.typeIndex(), zero);
CFNumberRef cfOne = CF.CFNumberCreate(null, CFNumberType.kCFNumberIntType.typeIndex(), one);
assertEquals(0, cfZero.intValue());
assertEquals(1, cfOne.intValue());
cfZero.release();
cfOne.release();
}
@Test
public void testCFRetainCount() {
DoubleByReference pi = new DoubleByReference(Math.PI);
DoubleByReference e = new DoubleByReference(Math.E);
CFNumberRef cfE = CF.CFNumberCreate(null, CFNumberType.kCFNumberDoubleType.typeIndex(), e);
CFNumberRef cfPi = CF.CFNumberCreate(null, CFNumberType.kCFNumberDoubleType.typeIndex(), pi);
assertEquals(1, CF.CFGetRetainCount(cfE).intValue());
assertEquals(1, CF.CFGetRetainCount(cfPi).intValue());
cfE.retain();
cfPi.retain();
cfPi.retain();
assertEquals(2, CF.CFGetRetainCount(cfE).intValue());
assertEquals(3, CF.CFGetRetainCount(cfPi).intValue());
List<? extends CFTypeRef> irrationalReferences = Arrays.asList(cfE, cfPi);
for (CFTypeRef value : irrationalReferences) {
value.release();
}
assertEquals(1, CF.CFGetRetainCount(cfE).intValue());
assertEquals(2, CF.CFGetRetainCount(cfPi).intValue());
cfPi.release();
assertEquals(1, CF.CFGetRetainCount(cfPi).intValue());
cfE.release();
cfPi.release();
}
@Test
public void testCFArray() {
CFNumberRef[] refArray = new CFNumberRef[3];
int size = Native.getNativeSize(CFNumberRef.class);
Memory contiguousArray = new Memory(size * refArray.length);
for (int i = 0; i < refArray.length; i++) {
refArray[i] = CF.CFNumberCreate(null, CoreFoundation.CFNumberType.kCFNumberIntType.typeIndex(),
new IntByReference(i));
contiguousArray.setPointer(i * size, refArray[i].getPointer());
}
CFArrayRef cfPtrArray = CF.CFArrayCreate(null, contiguousArray, new CFIndex(refArray.length), null);
assertEquals(CoreFoundation.ARRAY_TYPE_ID, cfPtrArray.getTypeID());
assertEquals(refArray.length, cfPtrArray.getCount());
for (int i = 0; i < refArray.length; i++) {
Pointer result = cfPtrArray.getValueAtIndex(i);
try {
new CFStringRef(result);
fail("Should have thrown a ClassCastExcpetion.");
} catch (ClassCastException expected) {
assertEquals("Unable to cast to CFString. Type ID: CFNumber", expected.getMessage());
}
CFNumberRef numRef = new CFNumberRef(result);
assertEquals(i, numRef.intValue());
}
for (int i = 0; i < refArray.length; i++) {
refArray[i].release();
}
cfPtrArray.release();
}
@Test
public void testCFData() {
int size = 128;
// Create some random bytes
byte[] randomBytes = new byte[size];
new Random().nextBytes(randomBytes);
// Fill native memory with them
Memory nativeBytes = new Memory(size);
nativeBytes.write(0, randomBytes, 0, randomBytes.length);
// Create a CF reference to the data
CFDataRef cfData = CF.CFDataCreate(null, nativeBytes, new CFIndex(size));
assertEquals(CoreFoundation.DATA_TYPE_ID, cfData.getTypeID());
int dataSize = cfData.getLength();
assertEquals(size, dataSize);
// Read it back out and convert to an array
Pointer bytes = cfData.getBytePtr();
byte[] dataBytes = bytes.getByteArray(0, dataSize);
assertArrayEquals(randomBytes, dataBytes);
cfData.release();
}
@Test
public void testCFDictionary() {
CFAllocatorRef alloc = CF.CFAllocatorGetDefault();
CFMutableDictionaryRef dict = CF.CFDictionaryCreateMutable(alloc, new CFIndex(2), null, null);
assertEquals(CoreFoundation.DICTIONARY_TYPE_ID, dict.getTypeID());
CFStringRef oneStr = CFStringRef.createCFString("one");
// Key does not exist, returns null
assertEquals(0L, dict.getCount());
assertFalse(dict.getValueIfPresent(oneStr, null));
Pointer cfNull = dict.getValue(oneStr);
assertNull(cfNull);
// Store and retrieve null value
dict.setValue(oneStr, null);
assertTrue(dict.getValueIfPresent(oneStr, null));
Pointer cfNullValue = dict.getValue(oneStr);
assertNull(cfNullValue);
// Store (replace the null) and retrieve integer value
IntByReference one = new IntByReference(1);
CFNumberRef cfOne = CF.CFNumberCreate(null, CFNumberType.kCFNumberIntType.typeIndex(), one);
dict.setValue(oneStr, cfOne);
assertEquals(1L, dict.getCount());
assertTrue(dict.getValueIfPresent(oneStr, null));
Pointer result = dict.getValue(oneStr);
CFNumberRef numRef = new CFNumberRef(result);
assertEquals(1, numRef.intValue());
PointerByReference resultPtr = new PointerByReference();
assertTrue(dict.getValueIfPresent(oneStr, resultPtr));
numRef = new CFNumberRef(resultPtr.getValue());
assertEquals(1, numRef.intValue());
// Test non-CF type as key
IntByReference onePtr = new IntByReference(1);
dict.setValue(onePtr, oneStr);
result = dict.getValue(onePtr);
CFStringRef strRef = new CFStringRef(result);
assertEquals("one", strRef.stringValue());
assertEquals(2L, dict.getCount());
oneStr.release();
cfOne.release();
dict.release();
}
@Test
public void testCFStringRefByReference() {
CFStringRef key = CFStringRef.createCFString("key");
CFStringRef value = CFStringRef.createCFString("value");
CFMutableDictionaryRef dict = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
dict.setValue(key, value);
// test getStringRefValue()
CFStringRef.ByReference byRef = new CFStringRef.ByReference();
assertTrue(dict.getValueIfPresent(key, byRef));
assertTrue(CF.CFEqual(value, byRef.getStringRefValue()));
// test constructor()
assertNull(new CFStringRef.ByReference().getValue());
// test constructor(null)
assertNull(new CFStringRef.ByReference(null).getValue());
// test setValue(null)
assertNotNull(byRef.getStringRefValue());
byRef.setValue(null);
assertNull(byRef.getStringRefValue());
// test setValue(CFStringRef), getValue()
byRef.setValue(value.getPointer());
assertTrue(CF.CFEqual(value, byRef.getStringRefValue()));
assertEquals(value.getPointer(), byRef.getValue());
// test setValue(CFDictionaryRef)
try {
byRef.setValue(dict.getPointer());
Assert.fail("must fail");
} catch (ClassCastException cce) {
// as it should be
}
CF.CFRelease(key);
CF.CFRelease(value);
CF.CFRelease(dict);
}
@Test
public void testCFDictionaryRefByReference() {
CFStringRef key = CFStringRef.createCFString("key");
CFMutableDictionaryRef value = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
value.setValue(key, key);
CFMutableDictionaryRef dict = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
dict.setValue(key, value);
// test getDictionaryRefValue()
CFDictionaryRef.ByReference byRef = new CFDictionaryRef.ByReference();
assertTrue(dict.getValueIfPresent(key, byRef));
assertTrue(CF.CFEqual(value, byRef.getDictionaryRefValue()));
// test constructor()
assertNull(new CFDictionaryRef.ByReference().getValue());
// test constructor(null)
assertNull(new CFDictionaryRef.ByReference(null).getValue());
// test setValue(null)
assertNotNull(byRef.getDictionaryRefValue());
byRef.setValue(null);
assertNull(byRef.getDictionaryRefValue());
// test setValue(CFDictionaryRef), getValue()
byRef.setValue(value.getPointer());
assertTrue(CF.CFEqual(value, byRef.getDictionaryRefValue()));
assertEquals(value.getPointer(), byRef.getValue());
// test setValue(CFStringRef)
try {
byRef.setValue(key.getPointer());
Assert.fail("must fail");
} catch (ClassCastException cce) {
// as it should be
}
CF.CFRelease(key);
CF.CFRelease(value);
CF.CFRelease(dict);
}
@Test
public void testCFGetTypeID() {
CFStringRef s1 = CFStringRef.createCFString("s1");
assertEquals(CF.CFStringGetTypeID(), CF.CFGetTypeID(s1.getPointer()));
assertEquals(CF.CFStringGetTypeID(), CF.CFGetTypeID(s1));
s1.release();
}
@Test
public void testCFEqual() {
CFStringRef s1 = CFStringRef.createCFString("s1");
CFStringRef s1_the_same = CFStringRef.createCFString("s1");
CFStringRef s2 = CFStringRef.createCFString("s2");
assertTrue(CF.CFEqual(s1, s1));
assertTrue(CF.CFEqual(s1, s1_the_same));
assertFalse(CF.CFEqual(s1, s2));
CFMutableDictionaryRef dict1 = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
dict1.setValue(s1, s1);
CFMutableDictionaryRef dict2 = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
dict2.setValue(s1, s1);
assertNotEquals(dict1.getPointer(), dict2.getPointer());
assertTrue(CF.CFEqual(dict1, dict2));
dict2.setValue(s1, s2);
assertFalse(CF.CFEqual(dict1, dict2));
CF.CFRelease(dict1);
CF.CFRelease(dict2);
CF.CFRelease(s1);
CF.CFRelease(s1_the_same);
CF.CFRelease(s2);
}
}