/
StructureTest.java
1252 lines (1139 loc) · 47.3 KB
/
StructureTest.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2007-2009 Timothy Wall, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p/>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
/** TODO: need more alignment tests, especially platform-specific behavior
* @author twall@users.sf.net
*/
//@SuppressWarnings("unused")
public class StructureTest extends TestCase {
public static void main(java.lang.String[] argList) {
junit.textui.TestRunner.run(StructureTest.class);
}
public void testSimpleSize() throws Exception {
class TestStructure extends Structure {
public int field;
}
Structure s = new TestStructure();
assertEquals("Wrong size", 4, s.size());
}
// must be public to populate array
public static class TestAllocStructure extends Structure {
public int f0;
public int f1;
public int f2;
public int f3;
}
public void testFieldsAllocated() {
class TestStructure extends Structure {
public TestStructure() { }
public TestStructure(Pointer p) { super(p); }
public int field;
public int fieldCount() { return fields().size(); }
}
TestStructure s = new TestStructure();
assertEquals("Wrong number of fields (default)", 1, s.fieldCount());
s = new TestStructure(new Memory(4));
assertEquals("Wrong number of fields (preallocated)", 1, s.fieldCount());
}
public void testProvidedMemoryTooSmall() {
class TestStructure extends Structure {
public TestStructure() { }
public TestStructure(Pointer p) { super(p); }
public int field;
public int fieldCount() { return fields().size(); }
}
try {
TestStructure s = new TestStructure(new Memory(2));
fail("Expect exception if provided memory is insufficient");
}
catch(IllegalArgumentException e) {
}
}
public void testClearOnAllocate() {
TestAllocStructure s = new TestAllocStructure();
s.read();
assertEquals("Memory not cleared on structure init", 0, s.f0);
assertEquals("Memory not cleared on structure init", 0, s.f1);
assertEquals("Memory not cleared on structure init", 0, s.f2);
assertEquals("Memory not cleared on structure init", 0, s.f3);
s = (TestAllocStructure)s.toArray(2)[1];
assertEquals("Memory not cleared on array init", 0, s.f0);
assertEquals("Memory not cleared on array init", 0, s.f1);
assertEquals("Memory not cleared on array init", 0, s.f2);
assertEquals("Memory not cleared on array init", 0, s.f3);
}
// cross-platform smoke test
public void testGNUCAlignment() {
class TestStructure extends Structure {
public byte b;
public short s;
public int i;
public long l;
public float f;
public double d;
}
TestStructure s = new TestStructure();
s.setAlignType(Structure.ALIGN_GNUC);
final int SIZE = Structure.MAX_GNUC_ALIGNMENT == 8 ? 32 : 28;
assertEquals("Wrong structure size", SIZE, s.size());
}
// cross-platform smoke test
public void testMSVCAlignment() {
class TestStructure extends Structure {
public byte b;
public short s;
public int i;
public long l;
public float f;
public double d;
}
TestStructure s = new TestStructure();
s.setAlignType(Structure.ALIGN_MSVC);
assertEquals("Wrong structure size", 32, s.size());
}
public static class FilledStructure extends Structure {
public FilledStructure() {
for (int i=0;i < size();i++) {
getPointer().setByte(i, (byte)0xFF);
}
}
}
// Do NOT change the order of naming w/o changing testlib.c as well
public static class TestStructure0 extends FilledStructure {
public byte field0 = 0x01;
public short field1 = 0x0202;
}
public static class TestStructure1 extends FilledStructure {
public byte field0 = 0x01;
public int field1 = 0x02020202;
}
public static class TestStructure2 extends FilledStructure {
public short field0 = 0x0101;
public int field1 = 0x02020202;
}
public static class TestStructure3 extends FilledStructure {
public int field0 = 0x01010101;
public short field1 = 0x0202;
public int field2 = 0x03030303;
}
public static class TestStructure4 extends FilledStructure {
public int field0 = 0x01010101;
public long field1 = 0x0202020202020202L;
public int field2 = 0x03030303;
public long field3 = 0x0404040404040404L;
}
public static class TestStructure5 extends FilledStructure {
public long field0 = 0x0101010101010101L;
public byte field1 = 0x02;
}
public interface SizeTest extends Library {
int getStructureSize(int type);
}
private void testStructureSize(int index) {
try {
SizeTest lib = (SizeTest)Native.loadLibrary("testlib", SizeTest.class);
Class cls = Class.forName(getClass().getName() + "$TestStructure" + index);
Structure s = Structure.newInstance(cls);
assertEquals("Incorrect size for structure " + index + "=>" + s.toString(true), lib.getStructureSize(index), s.size());
}
catch(Exception e) {
throw new Error(e);
}
}
public void testStructureSize0() {
testStructureSize(0);
}
public void testStructureSize1() {
testStructureSize(1);
}
public void testStructureSize2() {
testStructureSize(2);
}
public void testStructureSize3() {
testStructureSize(3);
}
public void testStructureSize4() {
testStructureSize(4);
}
public void testStructureSize5() {
testStructureSize(5);
}
public interface AlignmentTest extends Library {
int testStructureAlignment(Structure s, int type,
IntByReference offsetp, LongByReference valuep);
}
private void testAlignStruct(int index) {
AlignmentTest lib = (AlignmentTest)Native.loadLibrary("testlib", AlignmentTest.class);
try {
IntByReference offset = new IntByReference();
LongByReference value = new LongByReference();
Class cls = Class.forName(getClass().getName() + "$TestStructure" + index);
Structure s = (Structure)cls.newInstance();
int result = lib.testStructureAlignment(s, index, offset, value);
assertEquals("Wrong native value at field " + result
+ "=0x" + Long.toHexString(value.getValue())
+ " (actual native field offset=" + offset.getValue()
+ ") in " + s, -2, result);
}
catch(Exception e) {
throw new Error(e);
}
}
public void testAlignStruct0() {
testAlignStruct(0);
}
public void testAlignStruct1() {
testAlignStruct(1);
}
public void testAlignStruct2() {
testAlignStruct(2);
}
public void testAlignStruct3() {
testAlignStruct(3);
}
public void testAlignStruct4() {
testAlignStruct(4);
}
public void testAlignStruct5() {
testAlignStruct(5);
}
// must be publicly accessible in order to create array elements
public static class PublicTestStructure extends Structure {
public static class ByReference extends PublicTestStructure implements Structure.ByReference { }
public int x, y;
}
public void testStructureField() {
class TestStructure extends Structure {
public PublicTestStructure s1, s2;
public int after;
}
TestStructure s = new TestStructure();
assertNotNull("Inner structure should be initialized", s.s1);
assertEquals("Wrong aggregate size",
s.s1.size() + s.s2.size() + 4, s.size());
s.write();
assertEquals("Wrong memory for structure field 1 after write",
s.getPointer(), s.s1.getPointer());
assertEquals("Wrong memory for structure field 2 after write",
s.getPointer().share(s.s1.size()),
s.s2.getPointer());
s.read();
assertEquals("Wrong memory for structure field 1 after read",
s.getPointer(), s.s1.getPointer());
assertEquals("Wrong memory for structure field 2 after read",
s.getPointer().share(s.s1.size()),
s.s2.getPointer());
}
public void testPrimitiveArrayField() {
class TestStructure extends Structure {
public byte[] buffer = new byte[1024];
}
TestStructure s = new TestStructure();
assertEquals("Wrong size for structure with nested array", 1024, s.size());
assertNotNull("Array should be initialized", s.buffer);
s.write();
s.read();
}
public void testStructureArrayField() {
class TestStructure extends Structure {
public PublicTestStructure[] inner = new PublicTestStructure[2];
public PublicTestStructure[] inner2 = (PublicTestStructure[])
new PublicTestStructure().toArray(2);
}
TestStructure s = new TestStructure();
int innerSize = new PublicTestStructure().size();
assertEquals("Wrong size for structure with nested array of struct",
s.inner.length * innerSize + s.inner2.length * innerSize,
s.size());
s.write();
assertNotNull("Inner array elements should auto-initialize", s.inner[0]);
s.inner[0].x = s.inner[0].y = -1;
s.inner[1].x = s.inner[1].y = -1;
s.read();
assertEquals("Inner structure array element 0 not properly read",
0, s.inner[0].x);
assertEquals("Inner structure array element 1 not properly read",
0, s.inner[1].x);
assertEquals("Wrong memory for uninitialized nested array",
s.getPointer(), s.inner[0].getPointer());
assertEquals("Wrong memory for initialized nested array",
s.getPointer().share(innerSize * s.inner.length),
s.inner2[0].getPointer());
}
public static class ToArrayTestStructure extends Structure {
public PublicTestStructure[] inner =
(PublicTestStructure[])new PublicTestStructure().toArray(2);
}
public void testToArrayWithStructureArrayField() {
ToArrayTestStructure[] array =
(ToArrayTestStructure[])new ToArrayTestStructure().toArray(2);
assertEquals("Wrong address for top-level array element",
array[0].getPointer().share(array[0].size()),
array[1].getPointer());
assertEquals("Wrong address for nested array element",
array[1].inner[0].getPointer().share(array[1].inner[0].size()),
array[1].inner[1].getPointer());
}
public void testUninitializedNestedArrayFails() {
class TestStructure extends Structure {
public Pointer[] buffer;
}
TestStructure s = new TestStructure();
try {
s.size();
fail("Size can't be calculated unless array fields are initialized");
}
catch(IllegalStateException e) {
}
}
public void testReadWriteStructure() {
class TestStructure extends Structure {
public TestStructure() {
// Have to do this due to inline primitive arrays
allocateMemory();
}
public boolean z; // native int
public byte b; // native char
public char c; // native wchar_t
public short s; // native short
public int i; // native int
public long l; // native long long
public float f; // native float
public double d; // native double
public byte[] ba = new byte[3];
public char[] ca = new char[3];
public short[] sa = new short[3];
public int[] ia = new int[3];
public long[] la = new long[3];
public float[] fa = new float[3];
public double[] da = new double[3];
public PublicTestStructure nested;
}
TestStructure s = new TestStructure();
// set content of the structure
s.z = true;
s.b = 1;
s.c = 2;
s.s = 3;
s.i = 4;
s.l = 5;
s.f = 6.0f;
s.d = 7.0;
s.nested.x = 1;
s.nested.y = 2;
for (int i = 0; i < 3; i++) {
s.ba[i] = (byte) (8 + i);
s.ca[i] = (char) (11 + i);
s.sa[i] = (short) (14 + i);
s.ia[i] = 17 + i;
s.la[i] = 23 + i;
s.fa[i] = (float) 26 + i;
s.da[i] = (double) 29 + i;
}
// write content to memory
s.write();
Pointer p = s.getPointer();
s = new TestStructure();
s.useMemory(p);
// read content from memory and compare field values
s.read();
assertEquals("Wrong boolean field value after write/read", s.z, true);
assertEquals("Wrong byte field value after write/read", s.b, 1);
assertEquals("Wrong char field value after write/read", s.c, 2);
assertEquals("Wrong short field value after write/read", s.s, 3);
assertEquals("Wrong int field value after write/read", s.i, 4);
assertEquals("Wrong long field value after write/read", s.l, 5);
assertEquals("Wrong float field value after write/read", s.f, 6.0f, 0f);
assertEquals("Wrong double field value after write/read", s.d, 7.0, 0d);
assertEquals("Wrong nested struct field value after write/read (x)", s.nested.x, 1);
assertEquals("Wrong nested struct field value after write/read (y)", s.nested.y, 2);
for (int i = 0; i < 3; i++) {
assertEquals("Wrong byte array field value after write/read", s.ba[i], (byte) (8 + i));
assertEquals("Wrong char array field value after write/read", s.ca[i], (char) (11 + i));
assertEquals("Wrong short array field value after write/read", s.sa[i], (short) (14 + i));
assertEquals("Wrong int array field value after write/read", s.ia[i], 17 + i);
assertEquals("Wrong long array field value after write/read", s.la[i], 23 + i);
assertEquals("Wrong float array field value after write/read", s.fa[i], (float) 26 + i, 0f);
assertEquals("Wrong double array field value after write/read", s.da[i], (double) 29 + i, 0d);
}
// test constancy of references after read
int[] ia = s.ia;
s.read();
assertTrue("Array field reference should be unchanged", ia == s.ia);
}
public void testNativeLongSize() throws Exception {
class TestStructure extends Structure {
public NativeLong l;
}
Structure s = new TestStructure();
assertEquals("Wrong size", NativeLong.SIZE, s.size());
}
public void testNativeLongRead() throws Exception {
class TestStructure extends Structure {
public int i;
public NativeLong l;
}
TestStructure s = new TestStructure();
if (NativeLong.SIZE == 8) {
final long MAGIC = 0x1234567887654321L;
s.getPointer().setLong(8, MAGIC);
s.read();
assertEquals("NativeLong field mismatch", MAGIC, s.l.longValue());
}
else {
final int MAGIC = 0xABEDCF23;
s.getPointer().setInt(4, MAGIC);
s.read();
assertEquals("NativeLong field mismatch", MAGIC, s.l.intValue());
}
}
public void testNativeLongWrite() throws Exception {
class TestStructure extends Structure {
public int i;
public NativeLong l;
}
TestStructure s = new TestStructure();
if (NativeLong.SIZE == 8) {
final long MAGIC = 0x1234567887654321L;
s.l = new NativeLong(MAGIC);
s.write();
long l = s.getPointer().getLong(8);
assertEquals("NativeLong field mismatch", MAGIC, l);
}
else {
final int MAGIC = 0xABEDCF23;
s.l = new NativeLong(MAGIC);
s.write();
int i = s.getPointer().getInt(4);
assertEquals("NativeLong field mismatch", MAGIC, i);
}
}
public void testDisallowFunctionPointerAsField() {
class BadFieldStructure extends Structure {
public Function cb;
}
try {
new BadFieldStructure().size();
fail("Function fields should not be allowed");
}
catch(IllegalArgumentException e) {
}
}
public static class BadFieldStructure extends Structure {
public Object badField;
}
public void testUnsupportedField() {
class BadNestedStructure extends Structure {
public BadFieldStructure badStruct = new BadFieldStructure();
}
try {
new BadFieldStructure().size();
fail("Should throw IllegalArgumentException on bad field");
}
catch(IllegalArgumentException e) {
assertTrue("Exception should include field name",
e.getMessage().indexOf("badField") != -1);
}
try {
new BadNestedStructure().size();
fail("Should throw IllegalArgumentException on bad field");
}
catch(IllegalArgumentException e) {
assertTrue("Exception should include enclosing type: " + e,
e.getMessage().indexOf(BadNestedStructure.class.getName()) != -1);
assertTrue("Exception should include nested field name: " + e,
e.getMessage().indexOf("badStruct") != -1);
assertTrue("Exception should include field name: " + e,
e.getMessage().indexOf("badField") != -1);
}
}
public void testToArray() {
PublicTestStructure s = new PublicTestStructure();
PublicTestStructure[] array = (PublicTestStructure[])s.toArray(1);
assertEquals("Array should consist of a single element",
1, array.length);
assertEquals("First element should be original", s, array[0]);
assertEquals("Structure memory should be expanded", 2, s.toArray(2).length);
}
public void testByReferenceArraySync() {
PublicTestStructure.ByReference s = new PublicTestStructure.ByReference();
PublicTestStructure.ByReference[] array =
(PublicTestStructure.ByReference[])s.toArray(2);
class TestStructure extends Structure {
public PublicTestStructure.ByReference ref;
}
TestStructure ts = new TestStructure();
ts.ref = s;
final int VALUE = 42;
array[0].x = VALUE;
array[1].x = VALUE;
ts.write();
assertEquals("Array element not written: " + array[0],
VALUE, array[0].getPointer().getInt(0));
assertEquals("Array element not written: " + array[1],
VALUE, array[1].getPointer().getInt(0));
array[0].getPointer().setInt(4, VALUE);
array[1].getPointer().setInt(4, VALUE);
ts.read();
assertEquals("Array element not read: " + array[0], VALUE, array[0].y);
assertEquals("Array element not read: " + array[1], VALUE, array[1].y);
}
static class CbStruct extends Structure {
public Callback cb;
}
public void testCallbackWrite() {
final CbStruct s = new CbStruct();
s.cb = new Callback() {
public void callback() {
}
};
s.write();
Pointer func = s.getPointer().getPointer(0);
assertNotNull("Callback trampoline not set", func);
Map refs = CallbackReference.callbackMap;
assertTrue("Callback not cached", refs.containsKey(s.cb));
CallbackReference ref = (CallbackReference)refs.get(s.cb);
assertEquals("Wrong trampoline", ref.getTrampoline(), func);
}
public void testUninitializedArrayField() {
class UninitializedArrayFieldStructure extends Structure {
public byte[] array;
}
try {
Structure s = new UninitializedArrayFieldStructure();
assertTrue("Invalid size: " + s.size(), s.size() > 0);
fail("Uninitialized array field should cause write failure");
}
catch(IllegalStateException e) {
}
}
public static class ArrayOfStructure extends Structure {
public Structure[] array;
}
public void testPlainStructureArrayField() {
try {
new ArrayOfStructure();
fail("Structure[] not allowed as a field of Structure");
}
catch(IllegalArgumentException e) {
}
catch(Exception e) {
fail("Wrong exception thrown on Structure[] field in Structure: " + e);
}
}
public void testPointerArrayField() {
class ArrayOfPointerStructure extends Structure {
final static int SIZE = 10;
public Pointer[] array = new Pointer[SIZE];
}
ArrayOfPointerStructure s = new ArrayOfPointerStructure();
int size = s.size();
assertEquals("Wrong size", ArrayOfPointerStructure.SIZE * Pointer.SIZE, size);
s.array[0] = s.getPointer();
s.write();
s.array[0] = null;
s.read();
assertEquals("Wrong first element", s.getPointer(), s.array[0]);
}
public void testVolatileStructureField() {
class VolatileStructure extends Structure {
public volatile int counter;
public int value;
}
VolatileStructure s = new VolatileStructure();
s.counter = 1;
s.value = 1;
s.write();
assertEquals("Volatile field should not be written", 0, s.getPointer().getInt(0));
assertEquals("Non-volatile field should be written", 1, s.getPointer().getInt(4));
s.writeField("counter");
assertEquals("Explicit volatile field write failed", 1, s.getPointer().getInt(0));
}
public static class StructureWithPointers extends Structure {
public PublicTestStructure.ByReference s1;
public PublicTestStructure.ByReference s2;
}
public void testStructureByReferenceField() {
StructureWithPointers s = new StructureWithPointers();
assertEquals("Wrong size for structure with structure references",
Pointer.SIZE * 2, s.size());
assertNull("Initial refs should be null", s.s1);
}
public void testRegenerateStructureByReferenceField() {
StructureWithPointers s = new StructureWithPointers();
PublicTestStructure.ByReference inner =
new PublicTestStructure.ByReference();
s.s1 = inner;
s.write();
s.s1 = null;
s.read();
assertEquals("Inner structure not regenerated on read", inner, s.s1);
}
public void testPreserveStructureByReferenceWithUnchangedPointerOnRead() {
StructureWithPointers s = new StructureWithPointers();
PublicTestStructure.ByReference inner =
new PublicTestStructure.ByReference();
s.s1 = inner;
s.write();
s.read();
assertSame("Read should preserve structure object", inner, s.s1);
assertTrue("Read should preserve structure memory",
inner.getPointer() instanceof Memory);
}
public static class TestPointer extends PointerType { }
public void testPreservePointerFields() {
class TestStructure extends Structure {
public Pointer p = new Memory(256);
public TestPointer p2 = new TestPointer() {
{ setPointer(new Memory(256)); }
};
}
TestStructure s = new TestStructure();
final Pointer p = s.p;
s.write();
s.read();
assertSame("Should preserve Pointer references if peer unchanged", p, s.p);
}
public void testOverwriteStructureByReferenceFieldOnRead() {
StructureWithPointers s = new StructureWithPointers();
PublicTestStructure.ByReference inner =
new PublicTestStructure.ByReference();
PublicTestStructure.ByReference inner2 =
new PublicTestStructure.ByReference();
s.s1 = inner2;
s.write();
s.s1 = inner;
s.read();
assertNotSame("Read should overwrite structure reference", inner, s.s1);
}
public void testAutoWriteStructureByReferenceField() {
StructureWithPointers s = new StructureWithPointers();
s.s1 = new StructureTest.PublicTestStructure.ByReference();
s.s1.x = -1;
s.write();
assertEquals("Structure.ByReference field not written automatically",
-1, s.s1.getPointer().getInt(0));
}
public void testStructureByReferenceArrayField() {
class TestStructure extends Structure {
public PublicTestStructure.ByReference[] array = new PublicTestStructure.ByReference[2];
}
TestStructure s = new TestStructure();
assertEquals("Wrong structure size", 2*Pointer.SIZE, s.size());
PublicTestStructure.ByReference ref = new PublicTestStructure.ByReference();
ref.x = 42;
Object aref = s.array;
s.array[0] = ref;
s.array[1] = new PublicTestStructure.ByReference();
s.write();
s.read();
assertSame("Array reference should not change", aref, s.array);
assertSame("Elements should not be overwritten when unchanged",
ref, s.array[0]);
s.array[0] = null;
s.read();
assertNotSame("Null should be overwritten with a new ref", ref, s.array[0]);
assertNotNull("New ref should not be null", s.array[0]);
assertEquals("New ref should be equivalent", ref, s.array[0]);
}
public void testAutoReadWriteStructureByReferenceArrayField() {
class TestStructure extends Structure {
public PublicTestStructure.ByReference field;
}
TestStructure s = new TestStructure();
s.field = new PublicTestStructure.ByReference();
PublicTestStructure.ByReference[] array =
(PublicTestStructure.ByReference[])s.field.toArray(2);
final int VALUE = -1;
array[1].x = VALUE;
s.write();
assertEquals("ByReference array member not auto-written",
VALUE, array[1].getPointer().getInt(0));
array[1].getPointer().setInt(0, VALUE*2);
s.read();
assertEquals("ByReference array member not auto-read",
VALUE*2, array[1].x);
}
static class NestedTypeInfoStructure extends Structure {
public static class Inner extends Structure {
public int dummy;
}
public Inner inner;
public int dummy;
}
public static class size_t extends IntegerType {
public size_t() { this(0); }
public size_t(long value) { super(Native.POINTER_SIZE, value); }
}
public void testNestedStructureTypeInfo() {
class FFIType extends Structure {
public FFIType(Pointer p) {
useMemory(p); read();
}
public size_t size;
public short alignment;
public short type;
public Pointer elements;
}
NestedTypeInfoStructure s = new NestedTypeInfoStructure();
Pointer p = s.getTypeInfo();
FFIType ffi_type = new FFIType(p);
assertNotNull("Type info should not be null", p);
Pointer els = ffi_type.elements;
Pointer inner = s.inner.getTypeInfo();
assertEquals("Wrong type information for 'inner' field",
inner, els.getPointer(0));
assertEquals("Wrong type information for integer field",
Structure.getTypeInfo(new Integer(0)),
els.getPointer(Pointer.SIZE));
assertNull("Type element list should be null-terminated",
els.getPointer(Pointer.SIZE*2));
}
public void testInnerArrayTypeInfo() {
class TestStructure extends Structure {
public int[] inner = new int[5];
}
TestStructure s = new TestStructure();
assertEquals("Wrong structure size", 20, s.size());
Pointer p = s.getTypeInfo();
assertNotNull("Type info should not be null", p);
}
public void testTypeInfoForNull() {
assertEquals("Wrong type information for 'null'",
Structure.getTypeInfo(new Pointer(0)),
Structure.getTypeInfo(null));
}
public void testToString() {
// wce missing String.matches() and regex support
if (Platform.isWindowsCE()) return;
class TestStructure extends Structure {
public int intField;
public PublicTestStructure inner;
}
TestStructure s = new TestStructure();
final String LS = System.getProperty("line.separator");
System.setProperty("jna.dump_memory", "true");
final String EXPECTED = "(?m).*" + s.size() + " bytes.*\\{" + LS
+ " int intField@0=0" + LS
+ " .* inner@4=.*\\{" + LS
+ " int x@0=0" + LS
+ " int y@4=0" + LS
+ " \\}" + LS
+ "\\}" + LS
+ "memory dump" + LS
+ "\\[00000000\\]" + LS
+ "\\[00000000\\]" + LS
+ "\\[00000000\\]";
String actual = s.toString();
assertTrue("Improperly formatted toString(): expected "
+ EXPECTED + "\n" + actual,
actual.matches(EXPECTED));
}
public void testNativeMappedWrite() {
class TestStructure extends Structure {
public ByteByReference ref;
}
TestStructure s = new TestStructure();
ByteByReference ref = s.ref = new ByteByReference();
s.write();
assertEquals("Value not properly written", ref.getPointer(), s.getPointer().getPointer(0));
s.ref = null;
s.write();
assertNull("Non-null value was written: " + s.getPointer().getPointer(0), s.getPointer().getPointer(0));
}
public void testNativeMappedRead() {
class TestStructure extends Structure {
public ByteByReference ref;
}
TestStructure s = new TestStructure();
s.read();
assertNull("Should read null for initial field value", s.ref);
ByteByReference ref = new ByteByReference();
s.getPointer().setPointer(0, ref.getPointer());
s.read();
assertEquals("Field incorrectly read", ref, s.ref);
s.getPointer().setPointer(0, null);
s.read();
assertNull("Null field incorrectly read", s.ref);
}
public static class ROStructure extends Structure {
public final int field;
{
// Initialize in ctor to avoid compiler replacing
// field references with a constant everywhere
field = 0;
}
}
private ROStructure avoidConstantFieldOptimization(ROStructure s) {
return s;
}
// wce/phoneME fails to write final field
public void testReadOnlyField() {
ROStructure s = new ROStructure();
s.getPointer().setInt(0, 42);
s.read();
s = avoidConstantFieldOptimization(s);
assertEquals("Field value should be set from native", 42, s.field);
s.getPointer().setInt(0, 0);
s.read();
s = avoidConstantFieldOptimization(s);
assertEquals("Field value not synched after native change", 0, s.field);
// Read-only fields should not be copied to native memory
s.getPointer().setInt(0, 42);
try { s.write(); } catch(UnsupportedOperationException e) { }
assertEquals("Field should not be written", 42, s.getPointer().getInt(0));
// Native changes should propagate to read-only fields
s.read();
s = avoidConstantFieldOptimization(s);
assertEquals("Field value not synched after native change (2)", 42, s.field);
}
public void testNativeMappedArrayField() {
final int SIZE = 24;
class TestStructure extends Structure {
public NativeLong[] longs = new NativeLong[SIZE];
}
TestStructure s = new TestStructure();
assertEquals("Wrong structure size", Native.LONG_SIZE * SIZE, s.size());
NativeLong[] aref = s.longs;
for (int i=0;i < s.longs.length;i++) {
s.longs[i] = new NativeLong(i);
}
s.write();
for (int i=0;i < s.longs.length;i++) {
assertEquals("Value not written to memory at index " + i,
i, s.getPointer().getNativeLong(i * NativeLong.SIZE).intValue());
}
s.read();
assertEquals("Array reference should remain unchanged on read",
aref, s.longs);
for (int i=0;i < s.longs.length;i++) {
assertEquals("Wrong value after read at index " + i,
i, s.longs[i].intValue());
}
}
public void testInitializeNativeMappedField() {
final long VALUE = 20;
final NativeLong INITIAL = new NativeLong(VALUE);
class TestStructure extends Structure {
// field overwritten, wrong value before write
// NL bug, wrong value written
{ setAlignType(ALIGN_NONE); }
public NativeLong nl = INITIAL;
public NativeLong uninitialized;
}
TestStructure ts = new TestStructure();
assertEquals("Wrong value in field", VALUE, ts.nl.longValue());
assertSame("Initial value overwritten", INITIAL, ts.nl);
assertEquals("Wrong field value before write", VALUE, ts.nl.longValue());
assertNotNull("Uninitialized field should be initialized", ts.uninitialized);
assertEquals("Wrong initialized value", 0, ts.uninitialized.longValue());
ts.write();
assertEquals("Wrong field value written", VALUE, ts.getPointer().getNativeLong(0).longValue());
assertEquals("Wrong field value written (2)", 0, ts.getPointer().getNativeLong(NativeLong.SIZE).longValue());
ts.read();
assertEquals("Wrong field value", VALUE, ts.nl.longValue());
assertEquals("Wrong field value (2)", 0, ts.uninitialized.longValue());
}
public void testInheritedStructureFieldOrder() {
class TestStructure extends Structure {
public int first = 1;
}
class TestStructureSub extends TestStructure {
public int second = 2;
}
TestStructureSub s = new TestStructureSub();
assertEquals("Wrong size", 8, s.size());
s.write();
assertEquals("Wrong first field: " + s,
s.first, s.getPointer().getInt(0));
assertEquals("Wrong second field: " + s,
s.second, s.getPointer().getInt(4));
}
public void testExplicitStructureFieldOrder() {
final String[] ORDER = new String[] { "one", "two", "three" };
final String[] ORDER2 = new String[] { "one", "two", "three", "four" };
class TestStructure extends Structure {
public int one = 1;
public int three = 3;
public int two = 2;
{
setFieldOrder(ORDER);
}
public List getFieldOrder() {
return super.getFieldOrder();
}
}
class DerivedTestStructure extends TestStructure {
public int four = 4;
{
setFieldOrder(new String[] { "four" });
}
}
TestStructure s = new TestStructure();
assertEquals("Wrong field order",
Arrays.asList(ORDER), s.getFieldOrder());
s.write();
assertEquals("Wrong first field: " + s,
s.one, s.getPointer().getInt(0));
assertEquals("Wrong second field: " + s,
s.two, s.getPointer().getInt(4));
assertEquals("Wrong third field: " + s,
s.three, s.getPointer().getInt(8));
DerivedTestStructure s2 = new DerivedTestStructure();
assertEquals("Wrong field order",
Arrays.asList(ORDER2), s2.getFieldOrder());
s2.write();
assertEquals("Wrong first field: " + s2,
s2.one, s2.getPointer().getInt(0));
assertEquals("Wrong second field: " + s2,
s2.two, s2.getPointer().getInt(4));
assertEquals("Wrong third field: " + s2,
s2.three, s2.getPointer().getInt(8));
assertEquals("Wrong derived field: " + s2,
s2.four, s2.getPointer().getInt(12));
}
public void testCustomTypeMapper() {
class TestField { }
class TestStructure extends Structure {
public TestField field;
public TestStructure() {
DefaultTypeMapper m = new DefaultTypeMapper();
m.addTypeConverter(TestField.class, new TypeConverter() {
public Object fromNative(Object value, FromNativeContext context) {