github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/mobile/android_test.go (about) 1 package geth 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "runtime" 9 "testing" 10 "time" 11 12 "github.com/quickchainproject/quickchain/internal/build" 13 ) 14 15 // androidTestClass is a Java class to do some lightweight tests against the Android 16 // bindings. The goal is not to test each individual functionality, rather just to 17 // catch breaking API and/or implementation changes. 18 const androidTestClass = ` 19 package go; 20 21 import android.test.InstrumentationTestCase; 22 import android.test.MoreAsserts; 23 24 import java.math.BigInteger; 25 import java.util.Arrays; 26 27 import org.quickchain.geth.*; 28 29 public class AndroidTest extends InstrumentationTestCase { 30 public AndroidTest() {} 31 32 public void testAccountManagement() { 33 // Create an encrypted keystore with light crypto parameters. 34 KeyStore ks = new KeyStore(getInstrumentation().getContext().getFilesDir() + "/keystore", Geth.LightScryptN, Geth.LightScryptP); 35 36 try { 37 // Create a new account with the specified encryption passphrase. 38 Account newAcc = ks.newAccount("Creation password"); 39 40 // Export the newly created account with a different passphrase. The returned 41 // data from this method invocation is a JSON encoded, encrypted key-file. 42 byte[] jsonAcc = ks.exportKey(newAcc, "Creation password", "Export password"); 43 44 // Update the passphrase on the account created above inside the local keystore. 45 ks.updateAccount(newAcc, "Creation password", "Update password"); 46 47 // Delete the account updated above from the local keystore. 48 ks.deleteAccount(newAcc, "Update password"); 49 50 // Import back the account we've exported (and then deleted) above with yet 51 // again a fresh passphrase. 52 Account impAcc = ks.importKey(jsonAcc, "Export password", "Import password"); 53 54 // Create a new account to sign transactions with 55 Account signer = ks.newAccount("Signer password"); 56 57 Transaction tx = new Transaction( 58 1, new Address("0x0000000000000000000000000000000000000000"), 59 new BigInt(0), 0, new BigInt(1), null); // Random empty transaction 60 BigInt chain = new BigInt(1); // Chain identifier of the main net 61 62 // Sign a transaction with a single authorization 63 Transaction signed = ks.signTxPassphrase(signer, "Signer password", tx, chain); 64 65 // Sign a transaction with multiple manually cancelled authorizations 66 ks.unlock(signer, "Signer password"); 67 signed = ks.signTx(signer, tx, chain); 68 ks.lock(signer.getAddress()); 69 70 // Sign a transaction with multiple automatically cancelled authorizations 71 ks.timedUnlock(signer, "Signer password", 1000000000); 72 signed = ks.signTx(signer, tx, chain); 73 } catch (Exception e) { 74 fail(e.toString()); 75 } 76 } 77 78 public void testInprocNode() { 79 Context ctx = new Context(); 80 81 try { 82 // Start up a new inprocess node 83 Node node = new Node(getInstrumentation().getContext().getFilesDir() + "/.quickchain", new NodeConfig()); 84 node.start(); 85 86 // Retrieve some data via function calls (we don't really care about the results) 87 NodeInfo info = node.getNodeInfo(); 88 info.getName(); 89 info.getListenerAddress(); 90 info.getProtocols(); 91 92 // Retrieve some data via the APIs (we don't really care about the results) 93 EthereumClient ec = node.getEthereumClient(); 94 ec.getBlockByNumber(ctx, -1).getNumber(); 95 96 NewHeadHandler handler = new NewHeadHandler() { 97 @Override public void onError(String error) {} 98 @Override public void onNewHead(final Header header) {} 99 }; 100 ec.subscribeNewHead(ctx, handler, 16); 101 } catch (Exception e) { 102 fail(e.toString()); 103 } 104 } 105 106 // Tests that recovering transaction signers works for both Homestead and EIP155 107 // signatures too. Regression test for quickchain issue #14599. 108 public void testIssue14599() { 109 try { 110 byte[] preEIP155RLP = new BigInteger("f901fc8032830138808080b901ae60056013565b6101918061001d6000396000f35b3360008190555056006001600060e060020a6000350480630a874df61461003a57806341c0e1b514610058578063a02b161e14610066578063dbbdf0831461007757005b610045600435610149565b80600160a060020a031660005260206000f35b610060610161565b60006000f35b6100716004356100d4565b60006000f35b61008560043560243561008b565b60006000f35b600054600160a060020a031632600160a060020a031614156100ac576100b1565b6100d0565b8060018360005260205260406000208190555081600060005260206000a15b5050565b600054600160a060020a031633600160a060020a031614158015610118575033600160a060020a0316600182600052602052604060002054600160a060020a031614155b61012157610126565b610146565b600060018260005260205260406000208190555080600060005260206000a15b50565b60006001826000526020526040600020549050919050565b600054600160a060020a031633600160a060020a0316146101815761018f565b600054600160a060020a0316ff5b561ca0c5689ed1ad124753d54576dfb4b571465a41900a1dff4058d8adf16f752013d0a01221cbd70ec28c94a3b55ec771bcbc70778d6ee0b51ca7ea9514594c861b1884", 16).toByteArray(); 111 preEIP155RLP = Arrays.copyOfRange(preEIP155RLP, 1, preEIP155RLP.length); 112 113 byte[] postEIP155RLP = new BigInteger("f86b80847735940082520894ef5bbb9bba2e1ca69ef81b23a8727d889f3ef0a1880de0b6b3a7640000802ba06fef16c44726a102e6d55a651740636ef8aec6df3ebf009e7b0c1f29e4ac114aa057e7fbc69760b522a78bb568cfc37a58bfdcf6ea86cb8f9b550263f58074b9cc", 16).toByteArray(); 114 postEIP155RLP = Arrays.copyOfRange(postEIP155RLP, 1, postEIP155RLP.length); 115 116 Transaction preEIP155 = new Transaction(preEIP155RLP); 117 Transaction postEIP155 = new Transaction(postEIP155RLP); 118 119 preEIP155.getFrom(null); // Homestead should accept homestead 120 preEIP155.getFrom(new BigInt(4)); // EIP155 should accept homestead (missing chain ID) 121 postEIP155.getFrom(new BigInt(4)); // EIP155 should accept EIP 155 122 123 try { 124 postEIP155.getFrom(null); 125 fail("EIP155 transaction accepted by Homestead"); 126 } catch (Exception e) {} 127 } catch (Exception e) { 128 fail(e.toString()); 129 } 130 } 131 } 132 ` 133 134 // TestAndroid runs the Android java test class specified above. 135 // 136 // This requires the gradle command in PATH and the Android SDK whose path is available 137 // through ANDROID_HOME environment variable. To successfully run the tests, an Android 138 // device must also be available with debugging enabled. 139 // 140 // This method has been adapted from golang.org/x/mobile/bind/java/seq_test.go/runTest 141 func TestAndroid(t *testing.T) { 142 // Skip tests on Windows altogether 143 if runtime.GOOS == "windows" { 144 t.Skip("cannot test Android bindings on Windows, skipping") 145 } 146 // Make sure all the Android tools are installed 147 if _, err := exec.Command("which", "gradle").CombinedOutput(); err != nil { 148 t.Skip("command gradle not found, skipping") 149 } 150 if sdk := os.Getenv("ANDROID_HOME"); sdk == "" { 151 // Android SDK not explicitly given, try to auto-resolve 152 autopath := filepath.Join(os.Getenv("HOME"), "Android", "Sdk") 153 if _, err := os.Stat(autopath); err != nil { 154 t.Skip("ANDROID_HOME environment var not set, skipping") 155 } 156 os.Setenv("ANDROID_HOME", autopath) 157 } 158 if _, err := exec.Command("which", "gomobile").CombinedOutput(); err != nil { 159 t.Log("gomobile missing, installing it...") 160 if out, err := exec.Command("go", "get", "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil { 161 t.Fatalf("install failed: %v\n%s", err, string(out)) 162 } 163 t.Log("initializing gomobile...") 164 start := time.Now() 165 if _, err := exec.Command("gomobile", "init").CombinedOutput(); err != nil { 166 t.Fatalf("initialization failed: %v", err) 167 } 168 t.Logf("initialization took %v", time.Since(start)) 169 } 170 // Create and switch to a temporary workspace 171 workspace, err := ioutil.TempDir("", "geth-android-") 172 if err != nil { 173 t.Fatalf("failed to create temporary workspace: %v", err) 174 } 175 defer os.RemoveAll(workspace) 176 177 pwd, err := os.Getwd() 178 if err != nil { 179 t.Fatalf("failed to get current working directory: %v", err) 180 } 181 if err := os.Chdir(workspace); err != nil { 182 t.Fatalf("failed to switch to temporary workspace: %v", err) 183 } 184 defer os.Chdir(pwd) 185 186 // Create the skeleton of the Android project 187 for _, dir := range []string{"src/main", "src/androidTest/java/org/quickchain/gethtest", "libs"} { 188 err = os.MkdirAll(dir, os.ModePerm) 189 if err != nil { 190 t.Fatal(err) 191 } 192 } 193 // Generate the mobile bindings for Geth and add the tester class 194 gobind := exec.Command("gomobile", "bind", "-javapkg", "org.quickchain", "github.com/quickchainproject/quickchain/mobile") 195 if output, err := gobind.CombinedOutput(); err != nil { 196 t.Logf("%s", output) 197 t.Fatalf("failed to run gomobile bind: %v", err) 198 } 199 build.CopyFile(filepath.Join("libs", "geth.aar"), "geth.aar", os.ModePerm) 200 201 if err = ioutil.WriteFile(filepath.Join("src", "androidTest", "java", "org", "quickchain", "gethtest", "AndroidTest.java"), []byte(androidTestClass), os.ModePerm); err != nil { 202 t.Fatalf("failed to write Android test class: %v", err) 203 } 204 // Finish creating the project and run the tests via gradle 205 if err = ioutil.WriteFile(filepath.Join("src", "main", "AndroidManifest.xml"), []byte(androidManifest), os.ModePerm); err != nil { 206 t.Fatalf("failed to write Android manifest: %v", err) 207 } 208 if err = ioutil.WriteFile("build.gradle", []byte(gradleConfig), os.ModePerm); err != nil { 209 t.Fatalf("failed to write gradle build file: %v", err) 210 } 211 if output, err := exec.Command("gradle", "connectedAndroidTest").CombinedOutput(); err != nil { 212 t.Logf("%s", output) 213 t.Errorf("failed to run gradle test: %v", err) 214 } 215 } 216 217 const androidManifest = `<?xml version="1.0" encoding="utf-8"?> 218 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 219 package="org.quickchain.gethtest" 220 android:versionCode="1" 221 android:versionName="1.0"> 222 223 <uses-permission android:name="android.permission.INTERNET" /> 224 </manifest>` 225 226 const gradleConfig = `buildscript { 227 repositories { 228 jcenter() 229 } 230 dependencies { 231 classpath 'com.android.tools.build:gradle:2.2.3' 232 } 233 } 234 allprojects { 235 repositories { jcenter() } 236 } 237 apply plugin: 'com.android.library' 238 android { 239 compileSdkVersion 'android-19' 240 buildToolsVersion '21.1.2' 241 defaultConfig { minSdkVersion 15 } 242 } 243 repositories { 244 flatDir { dirs 'libs' } 245 } 246 dependencies { 247 compile 'com.android.support:appcompat-v7:19.0.0' 248 compile(name: "geth", ext: "aar") 249 } 250 `