github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/mobile/android_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:40</date> 10 //</624450100860555264> 11 12 13 package geth 14 15 import ( 16 "io/ioutil" 17 "os" 18 "os/exec" 19 "path/filepath" 20 "runtime" 21 "testing" 22 "time" 23 24 "github.com/ethereum/go-ethereum/internal/build" 25 ) 26 27 //ANDROIDTestCclass是一个Java类,用于对Android进行一些轻量级测试 28 //绑定。目标不是测试每个单独的功能,而是 29 //捕获中断的API和/或实现更改。 30 const androidTestClass = ` 31 package go; 32 33 import android.test.InstrumentationTestCase; 34 import android.test.MoreAsserts; 35 36 import java.math.BigInteger; 37 import java.util.Arrays; 38 39 import org.ethereum.geth.*; 40 41 public class AndroidTest extends InstrumentationTestCase { 42 public AndroidTest() {} 43 44 public void testAccountManagement() { 45 //使用轻量级加密参数创建加密密钥库。 46 KeyStore ks = new KeyStore(getInstrumentation().getContext().getFilesDir() + "/keystore", Geth.LightScryptN, Geth.LightScryptP); 47 48 try { 49 //使用指定的加密密码创建新帐户。 50 Account newAcc = ks.newAccount("Creation password"); 51 52 //使用其他密码短语导出新创建的帐户。归还的人 53 //此方法调用中的数据是一个JSON编码的加密密钥文件。 54 byte[] jsonAcc = ks.exportKey(newAcc, "Creation password", "Export password"); 55 56 //在本地密钥库中更新上面创建的帐户的密码。 57 ks.updateAccount(newAcc, "Creation password", "Update password"); 58 59 //从本地密钥库中删除上面更新的帐户。 60 ks.deleteAccount(newAcc, "Update password"); 61 62 //导入回我们在上面导出(然后删除)的帐户 63 //又是一个新的密码。 64 Account impAcc = ks.importKey(jsonAcc, "Export password", "Import password"); 65 66 //创建用于签署交易记录的新帐户 67 Account signer = ks.newAccount("Signer password"); 68 69 Transaction tx = new Transaction( 70 1, new Address("0x0000000000000000000000000000000000000000"), 71 new BigInt(0), 0, new BigInt(1), null); //随机空事务 72 BigInt chain = new BigInt(1); //主网链标识符 73 74 //用单一授权签署交易 75 Transaction signed = ks.signTxPassphrase(signer, "Signer password", tx, chain); 76 77 //使用多个手动取消的授权签署交易 78 ks.unlock(signer, "Signer password"); 79 signed = ks.signTx(signer, tx, chain); 80 ks.lock(signer.getAddress()); 81 82 //使用多个自动取消的授权签署交易 83 ks.timedUnlock(signer, "Signer password", 1000000000); 84 signed = ks.signTx(signer, tx, chain); 85 } catch (Exception e) { 86 fail(e.toString()); 87 } 88 } 89 90 public void testInprocNode() { 91 Context ctx = new Context(); 92 93 try { 94 //启动新的进程内节点 95 Node node = new Node(getInstrumentation().getContext().getFilesDir() + "/.ethereum", new NodeConfig()); 96 node.start(); 97 98 //通过函数调用检索一些数据(我们并不真正关心结果) 99 NodeInfo info = node.getNodeInfo(); 100 info.getName(); 101 info.getListenerAddress(); 102 info.getProtocols(); 103 104 //通过API检索一些数据(我们并不真正关心结果) 105 EthereumClient ec = node.getEthereumClient(); 106 ec.getBlockByNumber(ctx, -1).getNumber(); 107 108 NewHeadHandler handler = new NewHeadHandler() { 109 @Override public void onError(String error) {} 110 @Override public void onNewHead(final Header header) {} 111 }; 112 ec.subscribeNewHead(ctx, handler, 16); 113 } catch (Exception e) { 114 fail(e.toString()); 115 } 116 } 117 118 //恢复事务签名者对homestead和eip155都有效的测试 119 //签名也是。Go Ethereum问题的回归测试14599。 120 public void testIssue14599() { 121 try { 122 byte[] preEIP155RLP = new BigInteger("f901fc8032830138808080b901ae60056013565b6101918061001d6000396000f35b3360008190555056006001600060e060020a6000350480630a874df61461003a57806341c0e1b514610058578063a02b161e14610066578063dbbdf0831461007757005b610045600435610149565b80600160a060020a031660005260206000f35b610060610161565b60006000f35b6100716004356100d4565b60006000f35b61008560043560243561008b565b60006000f35b600054600160a060020a031632600160a060020a031614156100ac576100b1565b6100d0565b8060018360005260205260406000208190555081600060005260206000a15b5050565b600054600160a060020a031633600160a060020a031614158015610118575033600160a060020a0316600182600052602052604060002054600160a060020a031614155b61012157610126565b610146565b600060018260005260205260406000208190555080600060005260206000a15b50565b60006001826000526020526040600020549050919050565b600054600160a060020a031633600160a060020a0316146101815761018f565b600054600160a060020a0316ff5b561ca0c5689ed1ad124753d54576dfb4b571465a41900a1dff4058d8adf16f752013d0a01221cbd70ec28c94a3b55ec771bcbc70778d6ee0b51ca7ea9514594c861b1884", 16).toByteArray(); 123 preEIP155RLP = Arrays.copyOfRange(preEIP155RLP, 1, preEIP155RLP.length); 124 125 byte[] postEIP155RLP = new BigInteger("f86b80847735940082520894ef5bbb9bba2e1ca69ef81b23a8727d889f3ef0a1880de0b6b3a7640000802ba06fef16c44726a102e6d55a651740636ef8aec6df3ebf009e7b0c1f29e4ac114aa057e7fbc69760b522a78bb568cfc37a58bfdcf6ea86cb8f9b550263f58074b9cc", 16).toByteArray(); 126 postEIP155RLP = Arrays.copyOfRange(postEIP155RLP, 1, postEIP155RLP.length); 127 128 Transaction preEIP155 = new Transaction(preEIP155RLP); 129 Transaction postEIP155 = new Transaction(postEIP155RLP); 130 131 preEIP155.getFrom(null); //宅基地应接受宅基地 132 preEIP155.getFrom(new BigInt(4)); //EIP155应接受宅基地(缺少链ID) 133 postEIP155.getFrom(new BigInt(4)); //EIP155应接受EIP 155 134 135 try { 136 postEIP155.getFrom(null); 137 fail("EIP155 transaction accepted by Homestead"); 138 } catch (Exception e) {} 139 } catch (Exception e) { 140 fail(e.toString()); 141 } 142 } 143 } 144 ` 145 146 //TESTALIDLE运行上面指定的Android Java测试类。 147 // 148 //这需要path中的gradle命令和路径可用的android sdk 149 //通过android_home环境变量。为了成功地运行测试,Android 150 //设备还必须在启用调试的情况下可用。 151 // 152 //此方法改编自golang.org/x/mobile/bind/java/seq_test.go/runtest。 153 func TestAndroid(t *testing.T) { 154 //完全跳过Windows上的测试 155 if runtime.GOOS == "windows" { 156 t.Skip("cannot test Android bindings on Windows, skipping") 157 } 158 //确保已安装所有Android工具 159 if _, err := exec.Command("which", "gradle").CombinedOutput(); err != nil { 160 t.Skip("command gradle not found, skipping") 161 } 162 if sdk := os.Getenv("ANDROID_HOME"); sdk == "" { 163 //Android SDK没有明确给出,请尝试自动解决 164 autopath := filepath.Join(os.Getenv("HOME"), "Android", "Sdk") 165 if _, err := os.Stat(autopath); err != nil { 166 t.Skip("ANDROID_HOME environment var not set, skipping") 167 } 168 os.Setenv("ANDROID_HOME", autopath) 169 } 170 if _, err := exec.Command("which", "gomobile").CombinedOutput(); err != nil { 171 t.Log("gomobile missing, installing it...") 172 if out, err := exec.Command("go", "get", "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil { 173 t.Fatalf("install failed: %v\n%s", err, string(out)) 174 } 175 t.Log("initializing gomobile...") 176 start := time.Now() 177 if _, err := exec.Command("gomobile", "init").CombinedOutput(); err != nil { 178 t.Fatalf("initialization failed: %v", err) 179 } 180 t.Logf("initialization took %v", time.Since(start)) 181 } 182 //创建并切换到临时工作区 183 workspace, err := ioutil.TempDir("", "geth-android-") 184 if err != nil { 185 t.Fatalf("failed to create temporary workspace: %v", err) 186 } 187 defer os.RemoveAll(workspace) 188 189 pwd, err := os.Getwd() 190 if err != nil { 191 t.Fatalf("failed to get current working directory: %v", err) 192 } 193 if err := os.Chdir(workspace); err != nil { 194 t.Fatalf("failed to switch to temporary workspace: %v", err) 195 } 196 defer os.Chdir(pwd) 197 198 //创建Android项目的框架 199 for _, dir := range []string{"src/main", "src/androidTest/java/org/ethereum/gethtest", "libs"} { 200 err = os.MkdirAll(dir, os.ModePerm) 201 if err != nil { 202 t.Fatal(err) 203 } 204 } 205 //为geth生成移动绑定并添加Tester类 206 gobind := exec.Command("gomobile", "bind", "-javapkg", "org.ethereum", "github.com/ethereum/go-ethereum/mobile") 207 if output, err := gobind.CombinedOutput(); err != nil { 208 t.Logf("%s", output) 209 t.Fatalf("failed to run gomobile bind: %v", err) 210 } 211 build.CopyFile(filepath.Join("libs", "geth.aar"), "geth.aar", os.ModePerm) 212 213 if err = ioutil.WriteFile(filepath.Join("src", "androidTest", "java", "org", "ethereum", "gethtest", "AndroidTest.java"), []byte(androidTestClass), os.ModePerm); err != nil { 214 t.Fatalf("failed to write Android test class: %v", err) 215 } 216 //完成项目创建并通过Gradle运行测试 217 if err = ioutil.WriteFile(filepath.Join("src", "main", "AndroidManifest.xml"), []byte(androidManifest), os.ModePerm); err != nil { 218 t.Fatalf("failed to write Android manifest: %v", err) 219 } 220 if err = ioutil.WriteFile("build.gradle", []byte(gradleConfig), os.ModePerm); err != nil { 221 t.Fatalf("failed to write gradle build file: %v", err) 222 } 223 if output, err := exec.Command("gradle", "connectedAndroidTest").CombinedOutput(); err != nil { 224 t.Logf("%s", output) 225 t.Errorf("failed to run gradle test: %v", err) 226 } 227 } 228 229 const androidManifest = `<?xml version="1.0" encoding="utf-8"?> 230 <manifest xmlns:android="http://schemas.android.com/apk/res/android“ 231 package="org.ethereum.gethtest" 232 android:versionCode="1" 233 android:versionName="1.0"> 234 235 <uses-permission android:name="android.permission.INTERNET" /> 236 </manifest>` 237 238 const gradleConfig = `buildscript { 239 repositories { 240 jcenter() 241 } 242 dependencies { 243 classpath 'com.android.tools.build:gradle:2.2.3' 244 } 245 } 246 allprojects { 247 repositories { jcenter() } 248 } 249 apply plugin: 'com.android.library' 250 android { 251 compileSdkVersion 'android-19' 252 buildToolsVersion '21.1.2' 253 defaultConfig { minSdkVersion 15 } 254 } 255 repositories { 256 flatDir { dirs 'libs' } 257 } 258 dependencies { 259 compile 'com.android.support:appcompat-v7:19.0.0' 260 compile(name: "geth", ext: "aar") 261 } 262 ` 263