From 80d0c4461660c0bb27894c4e52ea1cda3fce425e Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 11 Feb 2020 00:23:10 +0100 Subject: [PATCH] Move test code to testutils.spec --- packages/sdk/src/leb128.spec.ts | 39 ---------------- packages/sdk/src/restclient.spec.ts | 41 ++-------------- packages/sdk/src/testutils.spec.ts | 72 +++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 75 deletions(-) delete mode 100644 packages/sdk/src/leb128.spec.ts create mode 100644 packages/sdk/src/testutils.spec.ts diff --git a/packages/sdk/src/leb128.spec.ts b/packages/sdk/src/leb128.spec.ts deleted file mode 100644 index c95f8e38..00000000 --- a/packages/sdk/src/leb128.spec.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Encoding } from "@iov/encoding"; - -const { fromHex } = Encoding; - -export function leb128Encode(uint: number): Uint8Array { - if (uint < 0) throw new Error("Only non-negative values supported"); - if (uint > 0x7fffffff) throw new Error("Only values in signed int32 range allowed"); - const out = new Array(); - let value = uint; - do { - // tslint:disable: no-bitwise - let byte = value & 0b01111111; - value >>= 7; - - // more bytes to come: set high order bit of byte - if (value !== 0) byte ^= 0b10000000; - - out.push(byte); - // tslint:enable: no-bitwise - } while (value !== 0); - return new Uint8Array(out); -} - -describe("leb128", () => { - describe("leb128Encode", () => { - it("works for single byte values", () => { - // Values in 7 bit range are encoded as one byte - expect(leb128Encode(0)).toEqual(fromHex("00")); - expect(leb128Encode(20)).toEqual(fromHex("14")); - expect(leb128Encode(127)).toEqual(fromHex("7f")); - }); - - it("works for multi byte values", () => { - // from external souce (wasm-objdump) - expect(leb128Encode(145)).toEqual(fromHex("9101")); - expect(leb128Encode(1539)).toEqual(fromHex("830c")); - }); - }); -}); diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 3cb01de3..c0930d07 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -1,16 +1,15 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { Random, Sha256 } from "@iov/crypto"; -import { Bech32, Encoding } from "@iov/encoding"; +import { Sha256 } from "@iov/crypto"; +import { Encoding } from "@iov/encoding"; import { assert } from "@iov/utils"; import { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; -import { leb128Encode } from "./leb128.spec"; import { findAttribute, parseLogs } from "./logs"; import { Pen, Secp256k1Pen } from "./pen"; import { encodeBech32Pubkey } from "./pubkey"; import { PostTxsResponse, RestClient } from "./restclient"; -import contract from "./testdata/contract.json"; import cosmoshub from "./testdata/cosmoshub.json"; +import { getRandomizedHackatom, makeRandomAddress } from "./testutils.spec"; import { Coin, Msg, @@ -60,36 +59,6 @@ function makeSignedTx(firstMsg: Msg, fee: StdFee, memo: string, firstSignature: }; } -function getRandomizedContract(): Uint8Array { - const data = fromBase64(contract.data); - // The return value of the export function cosmwasm_api_0_6 is unused and - // can be randomized for testing. - // - // Find position of mutable bytes as follows: - // $ wasm-objdump -d contract.wasm | grep -F "cosmwasm_api_0_6" -A 1 - // 00e67c func[149] : - // 00e67d: 41 83 0c | i32.const 1539 - // - // In the last line, the addresses 00e67d-00e67f hold a one byte instruction - // (https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#constants-described-here) - // and a two byte value (leb128 encoded 1539) - - // Any unsigned integer from 128 to 16383 is encoded to two leb128 bytes - const min = 128; - const max = 16383; - const random = Math.floor(Math.random() * (max - min)) + min; - const bytes = leb128Encode(random); - - data[0x00e67d + 1] = bytes[0]; - data[0x00e67d + 2] = bytes[1]; - - return data; -} - -function makeRandomAddress(): string { - return Bech32.encode("cosmos", Random.getBytes(20)); -} - async function uploadCustomContract( client: RestClient, pen: Pen, @@ -123,7 +92,7 @@ async function uploadCustomContract( } async function uploadContract(client: RestClient, pen: Pen): Promise { - return uploadCustomContract(client, pen, getRandomizedContract()); + return uploadCustomContract(client, pen, getRandomizedHackatom()); } async function instantiateContract( @@ -376,7 +345,7 @@ describe("RestClient", () => { const numExisting = existingInfos.length; // upload data - const wasmCode = getRandomizedContract(); + const wasmCode = getRandomizedHackatom(); const result = await uploadCustomContract(client, pen, wasmCode); expect(result.code).toBeFalsy(); const logs = parseLogs(result.logs); diff --git a/packages/sdk/src/testutils.spec.ts b/packages/sdk/src/testutils.spec.ts new file mode 100644 index 00000000..5e099de6 --- /dev/null +++ b/packages/sdk/src/testutils.spec.ts @@ -0,0 +1,72 @@ +import { Random } from "@iov/crypto"; +import { Bech32, Encoding } from "@iov/encoding"; + +import hackatom from "./testdata/contract.json"; + +const { fromHex } = Encoding; + +export function leb128Encode(uint: number): Uint8Array { + if (uint < 0) throw new Error("Only non-negative values supported"); + if (uint > 0x7fffffff) throw new Error("Only values in signed int32 range allowed"); + const out = new Array(); + let value = uint; + do { + // tslint:disable: no-bitwise + let byte = value & 0b01111111; + value >>= 7; + + // more bytes to come: set high order bit of byte + if (value !== 0) byte ^= 0b10000000; + + out.push(byte); + // tslint:enable: no-bitwise + } while (value !== 0); + return new Uint8Array(out); +} + +export function getRandomizedHackatom(): Uint8Array { + const data = Encoding.fromBase64(hackatom.data); + // The return value of the export function cosmwasm_api_0_6 is unused and + // can be randomized for testing. + // + // Find position of mutable bytes as follows: + // $ wasm-objdump -d contract.wasm | grep -F "cosmwasm_api_0_6" -A 1 + // 00e67c func[149] : + // 00e67d: 41 83 0c | i32.const 1539 + // + // In the last line, the addresses 00e67d-00e67f hold a one byte instruction + // (https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#constants-described-here) + // and a two byte value (leb128 encoded 1539) + + // Any unsigned integer from 128 to 16383 is encoded to two leb128 bytes + const min = 128; + const max = 16383; + const random = Math.floor(Math.random() * (max - min)) + min; + const bytes = leb128Encode(random); + + data[0x00e67d + 1] = bytes[0]; + data[0x00e67d + 2] = bytes[1]; + + return data; +} + +export function makeRandomAddress(): string { + return Bech32.encode("cosmos", Random.getBytes(20)); +} + +describe("leb128", () => { + describe("leb128Encode", () => { + it("works for single byte values", () => { + // Values in 7 bit range are encoded as one byte + expect(leb128Encode(0)).toEqual(fromHex("00")); + expect(leb128Encode(20)).toEqual(fromHex("14")); + expect(leb128Encode(127)).toEqual(fromHex("7f")); + }); + + it("works for multi byte values", () => { + // from external souce (wasm-objdump) + expect(leb128Encode(145)).toEqual(fromHex("9101")); + expect(leb128Encode(1539)).toEqual(fromHex("830c")); + }); + }); +});