1 29 package com.eaio.nativecall; 30 import java.io.File ; 31 32 import junit.framework.TestCase; 33 34 40 public class IntCallTest1Win32 extends TestCase { 41 42 static { 43 try { 44 NativeCall.init(); 45 } 46 catch (Throwable thrw) { 47 thrw.printStackTrace(); 48 fail(thrw.getLocalizedMessage()); 49 } 50 } 51 52 56 public IntCallTest1Win32(String arg0) { 57 super(arg0); 58 } 59 60 public static void main(String [] args) { 61 junit.awtui.TestRunner.run(IntCallTest1Win32.class); 62 } 63 64 67 public void testExecuteCall() { 68 IntCall cT = new IntCall("GetCurrentThread"); 69 assertEquals(0, cT.getLastErrorCode()); 70 int threadHandle = cT.executeCall(); 71 assertEquals(0, cT.getLastErrorCode()); 72 assertTrue(threadHandle != 0); 73 cT.destroy(); 74 assertEquals(0, cT.getLastErrorCode()); 75 } 76 77 80 public void testExecuteCallObject() { 81 82 IntCall cT = new IntCall("GetCurrentThread"); 83 84 int threadHandle = cT.executeCall(); 85 86 IntCall tP = new IntCall("GetThreadPriority"); 87 88 int threadPriority = tP.executeCall(new Integer (threadHandle)); 89 90 Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 91 92 Object o = new Object (); 93 synchronized (o) { 94 try { 95 o.wait(1000); 96 } 97 catch (InterruptedException ex) {} 98 } 99 100 tP.destroy(); 101 cT.destroy(); 102 103 synchronized (o) { 104 try { 105 o.wait(1000); 106 } 107 catch (InterruptedException ex) {} 108 } 109 110 } 111 112 115 public void testExecuteCallObjectArray() { 116 117 119 IntCall cF = new IntCall("kernel32", "CopyFileA"); 120 121 assertEquals("{ IntCall: module = kernel32, function = CopyFileA }", cF.toString()); 122 123 File copy = new File ("documentation_copy.txt"); 124 if (copy.exists()) 125 copy.delete(); 126 127 boolean copied = 128 cF.executeBooleanCall( 129 new Object [] { 130 "documentation/copying.txt", 131 "documentation_copy.txt", 132 Boolean.FALSE }); 133 134 cF.destroy(); 135 136 assertTrue(copied); 137 assertTrue(copy.exists()); 138 assertTrue( 139 copy.length() == new File ("documentation/copying.txt").length()); 140 141 copy.delete(); 142 143 145 File copying = new File ("documentation/copying.txt"); 146 assertTrue(copying.exists()); 147 148 IntCall getFileAttributesA = new IntCall("GetFileAttributesA"); 149 150 int attributes = 151 getFileAttributesA.executeCall(copying.getAbsolutePath()); 152 153 assertEquals(0, attributes & 0xdf); 155 IntCall setFileAttributesA = new IntCall("SetFileAttributesA"); 156 157 assertTrue( 158 setFileAttributesA.executeBooleanCall( 159 new Object [] { 160 copying.getAbsolutePath(), 161 new Integer (attributes | 2)})); 162 163 assertEquals( 164 2, 165 getFileAttributesA.executeCall(copying.getAbsolutePath()) & 0xdf); 166 167 assertTrue( 168 setFileAttributesA.executeBooleanCall( 169 new Object [] { 170 copying.getAbsolutePath(), 171 new Integer (attributes)})); 172 173 assertEquals( 174 0, 175 getFileAttributesA.executeCall(copying.getAbsolutePath()) & 0xdf); 176 177 getFileAttributesA.destroy(); 178 179 setFileAttributesA.destroy(); 180 181 183 IntCall getFileAttributesW = new IntCall("GetFileAttributesW"); 184 185 attributes = 186 getFileAttributesW.executeCall( 187 "\\\\?\\" + copying.getAbsolutePath()); 188 189 assertEquals(0, attributes & 0xdf); 191 IntCall setFileAttributesW = new IntCall("SetFileAttributesW"); 192 193 assertTrue( 194 setFileAttributesW.executeBooleanCall( 195 new Object [] { 196 "\\\\?\\" + copying.getAbsolutePath(), 197 new Integer (attributes | 2)})); 198 199 assertEquals( 200 2, 201 getFileAttributesW.executeCall( 202 "\\\\?\\" + copying.getAbsolutePath()) 203 & 0xdf); 204 205 assertTrue( 206 setFileAttributesW.executeBooleanCall( 207 new Object [] { 208 copying.getAbsolutePath(), 209 new Integer (attributes)})); 210 211 assertEquals( 212 0, 213 getFileAttributesW.executeCall( 214 "\\\\?\\" + copying.getAbsolutePath()) 215 & 0xdf); 216 217 getFileAttributesW.destroy(); 218 219 setFileAttributesW.destroy(); 220 221 IntCall messageBoxA = new IntCall("user32", "MessageBoxA"); 222 223 messageBoxA.executeCall( 224 new Object [] { 225 new Integer (0), 226 "No native code required!", 227 "NativeCall", 228 new Integer (0)}); 229 230 messageBoxA.destroy(); 231 232 234 IntCall enumProcesses = new IntCall("psapi", "EnumProcesses"); 235 236 byte[] buffer = new byte[1024]; 237 Holder length = new Holder(new Integer (0)); 238 239 boolean success = 240 enumProcesses.executeBooleanCall( 241 new Object [] { buffer, new Integer (buffer.length), length }); 242 243 assertTrue(success); 244 assertTrue(((Integer ) length.get()).intValue() > 0); 245 246 enumProcesses.destroy(); 247 248 } 249 250 } 251 | Popular Tags |