simple.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { loadContractAndCallConstructor } from './setup';
  4. import crypto from 'crypto';
  5. import { BN } from '@coral-xyz/anchor';
  6. describe('Simple solang tests', function () {
  7. this.timeout(500000);
  8. it('flipper', async function () {
  9. let { program, storage } = await loadContractAndCallConstructor('flipper', [true]);
  10. // make sure we can't run the constructor twice
  11. await expect(program.methods.new(false)
  12. .accounts({ dataAccount: storage.publicKey })
  13. .rpc()).rejects.toThrow();
  14. let res = await program.methods.get().accounts({ dataAccount: storage.publicKey }).view();
  15. expect(res).toStrictEqual(true);
  16. await program.methods.flip().accounts({ dataAccount: storage.publicKey }).rpc();
  17. res = await program.methods.get().accounts({ dataAccount: storage.publicKey }).view();
  18. expect(res).toStrictEqual(false);
  19. });
  20. it('primitives', async function () {
  21. let { program, payer, storage } = await loadContractAndCallConstructor('primitives', []);
  22. // TEST Basic enums
  23. // in ethereum, an enum is described as an uint8 so can't use the enum
  24. // names programmatically. 0 = add, 1 = sub, 2 = mul, 3 = div, 4 = mod, 5 = pow, 6 = shl, 7 = shr
  25. let res = await program.methods.isMul({ mul: {} }).view();
  26. expect(res).toBe(true);
  27. res = await program.methods.returnDiv().view();
  28. expect(res.div).toBeDefined();
  29. // TEST uint and int types, and arithmetic/bitwise ops
  30. res = await program.methods.opI64({ add: {} }, new BN(1000), new BN(4100)).view();
  31. expect(Number(res)).toBe(5100);
  32. res = await program.methods.opI64({ sub: {} }, new BN(1000), new BN(4100)).view();
  33. expect(Number(res)).toStrictEqual(-3100);
  34. res = await program.methods.opI64({ mul: {} }, new BN(1000), new BN(4100)).view();
  35. expect(Number(res)).toBe(4100000);
  36. res = await program.methods.opI64({ div: {} }, new BN(1000), new BN(10)).view();
  37. expect(Number(res)).toBe(100);
  38. res = await program.methods.opI64({ mod: {} }, new BN(1000), new BN(99)).view();
  39. expect(Number(res)).toBe(10);
  40. res = await program.methods.opI64({ shl: {} }, new BN(-1000), new BN(8)).view();
  41. expect(Number(res)).toBe(-256000);
  42. res = await program.methods.opI64({ shr: {} }, new BN(-1000), new BN(8)).view();
  43. expect(Number(res)).toBe(-4);
  44. res = await program.methods.opU64({ add: {} }, new BN(1000), new BN(4100)).view();
  45. expect(Number(res)).toBe(5100);
  46. res = await program.methods.opU64({ sub: {} }, new BN(1000), new BN(4100)).view();
  47. expect(Number(res)).toBe(18446744073709548516); // (2^64)-18446744073709548516 = 3100
  48. res = await program.methods.opU64({ mul: {} }, new BN(123456789), new BN(123456789)).view();
  49. expect(Number(res)).toBe(15241578750190521);
  50. res = await program.methods.opU64({ div: {} }, new BN(123456789), new BN(100)).view();
  51. expect(Number(res)).toBe(1234567);
  52. res = await program.methods.opU64({ mod: {} }, new BN(123456789), new BN(100)).view();
  53. expect(Number(res)).toBe(89);
  54. res = await program.methods.opU64({ pow: {} }, new BN(3), new BN(7)).view();
  55. expect(Number(res)).toBe(2187);
  56. res = await program.methods.opI64({ shl: {} }, new BN(1000), new BN(8)).view();
  57. expect(Number(res)).toBe(256000);
  58. res = await program.methods.opI64({ shr: {} }, new BN(1000), new BN(8)).view();
  59. expect(Number(res)).toBe(3);
  60. // now for 256 bit operations
  61. res = await program.methods.opI256({ add: {} }, new BN(1000), new BN(4100)).view();
  62. expect(Number(res)).toBe(5100);
  63. res = await program.methods.opI256({ sub: {} }, new BN(1000), new BN(4100)).view();
  64. expect(res).toStrictEqual(new BN(-3100));
  65. res = await program.methods.opI256({ mul: {} }, new BN(1000), new BN(4100)).view();
  66. expect(Number(res)).toBe(4100000);
  67. res = await program.methods.opI256({ div: {} }, new BN(1000), new BN(10)).view();
  68. expect(Number(res)).toBe(100);
  69. res = await program.methods.opI256({ mod: {} }, new BN(1000), new BN(99)).view();
  70. expect(Number(res)).toBe(10);
  71. res = await program.methods.opI256({ shl: {} }, new BN(-10000000000000), new BN(8)).view();
  72. expect(Number(res)).toBe(-2560000000000000);
  73. res = await program.methods.opI256({ shr: {} }, new BN(-10000000000000), new BN(8)).view();
  74. expect(Number(res)).toBe(-39062500000);
  75. res = await program.methods.opU256({ add: {} }, new BN(1000), new BN(4100)).view();
  76. expect(Number(res)).toBe(5100);
  77. res = await program.methods.opU256({ sub: {} }, new BN(1000), new BN(4100)).view();
  78. expect(Number(res)).toBe(115792089237316195423570985008687907853269984665640564039457584007913129636836); // (2^64)-18446744073709548516 = 3100
  79. res = await program.methods.opU256({ mul: {} }, new BN(123456789), new BN(123456789)).view();
  80. expect(Number(res)).toBe(15241578750190521);
  81. res = await program.methods.opU256({ div: {} }, new BN(123456789), new BN(100)).view();
  82. expect(Number(res)).toBe(1234567);
  83. res = await program.methods.opU256({ mod: {} }, new BN(123456789), new BN(100)).view();
  84. expect(Number(res)).toBe(89);
  85. res = await program.methods.opU256({ pow: {} }, new BN(123456789), new BN(9)).view();
  86. expect(Number(res)).toBe(6662462759719942007440037531362779472290810125440036903063319585255179509);
  87. res = await program.methods.opU256({ shl: {} }, new BN(10000000000000), new BN(8)).view();
  88. expect(Number(res)).toBe(2560000000000000);
  89. res = await program.methods.opU256({ shr: {} }, new BN(10000000000000), new BN(8)).view();
  90. expect(Number(res)).toBe(39062500000);
  91. // TEST bytesN
  92. res = await program.methods.returnU86().view();
  93. expect(res).toStrictEqual([0x41, 0x42, 0x43, 0x44, 0x45, 0x46]);
  94. // TEST bytes5
  95. res = await program.methods.opU85Shift({ shl: {} },
  96. Buffer.from("deadcafe59", "hex"), new BN(8)).view();
  97. expect(res).toStrictEqual([0xad, 0xca, 0xfe, 0x59, 0x00]);
  98. res = await program.methods.opU85Shift({ shr: {} }, Buffer.from("deadcafe59", "hex"), new BN(8)).view();
  99. expect(res).toStrictEqual([0x00, 0xde, 0xad, 0xca, 0xfe]);
  100. res = await program.methods.opU85({ or: {} },
  101. Buffer.from("deadcafe59", "hex"),
  102. Buffer.from("0000000006", "hex")).view();
  103. expect(res).toStrictEqual([0xde, 0xad, 0xca, 0xfe, 0x5f]);
  104. res = await program.methods.opU85({ and: {} },
  105. Buffer.from("deadcafe59", "hex"),
  106. Buffer.from("00000000ff", "hex")).view();
  107. expect(res).toStrictEqual([0x00, 0x00, 0x00, 0x00, 0x59]);
  108. res = await program.methods.opU85({ xor: {} },
  109. Buffer.from("deadcafe59", "hex"),
  110. Buffer.from("00000000ff", "hex")).view();
  111. expect(res).toStrictEqual([0xde, 0xad, 0xca, 0xfe, 0xa6]);
  112. // TEST bytes14
  113. res = await program.methods.opU814Shift({ shl: {} },
  114. Buffer.from("deadcafe123456789abcdefbeef7", "hex"), new BN(9))
  115. .view();
  116. expect(res).toStrictEqual([0x5b, 0x95, 0xfc, 0x24, 0x68, 0xac, 0xf1, 0x35, 0x79, 0xbd, 0xf7, 0xdd, 0xee, 0x00]);
  117. res = await program.methods.opU814Shift({ shr: {} },
  118. Buffer.from("deadcafe123456789abcdefbeef7", "hex"), new BN(9)).view();
  119. expect(res).toStrictEqual([0x00, 0x6f, 0x56, 0xe5, 0x7f, 0x09, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, 0x6f, 0x7d, 0xf7]);
  120. res = await program.methods.opU814({ or: {} },
  121. Buffer.from("deadcafe123456789abcdefbeef7", "hex"),
  122. Buffer.from("0000060000000000000000000000", "hex")).view();
  123. expect(res).toStrictEqual([0xde, 0xad, 0xce, 0xfe, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xfb, 0xee, 0xf7]);
  124. res = await program.methods.opU814({ and: {} },
  125. Buffer.from("deadcafe123456789abcdefbeef7", "hex"),
  126. Buffer.from("000000000000000000ff00000000", "hex")).view();
  127. expect(res).toStrictEqual(
  128. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbc, 0x00, 0x00, 0x00, 0x00]);
  129. res = await program.methods.opU814({ xor: {} },
  130. Buffer.from("deadcafe123456789abcdefbeef7", "hex"),
  131. Buffer.from("ff00000000000000000000000000", "hex")).view();
  132. expect(res).toStrictEqual(
  133. [0x21, 0xad, 0xca, 0xfe, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xfb, 0xee, 0xf7]);
  134. res = await program.methods.addressPassthrough(payer.publicKey).view();
  135. expect(res).toStrictEqual(payer.publicKey);
  136. });
  137. it('store', async function () {
  138. const { storage, program } = await loadContractAndCallConstructor('store', []);
  139. let res = await program.methods.getValues1().accounts({ dataAccount: storage.publicKey }).view();
  140. expect(Number(res.return0)).toEqual(0);
  141. expect(Number(res.return1)).toEqual(0);
  142. expect(Number(res.return2)).toEqual(0);
  143. expect(Number(res.return3)).toEqual(0);
  144. res = await program.methods.getValues2().accounts({ dataAccount: storage.publicKey }).view();
  145. expect(Number(res.return0)).toEqual(0);
  146. expect(res.return1).toEqual("");
  147. expect(res.return2).toEqual(Buffer.from("b00b1e", "hex"));
  148. expect(res.return3).toEqual([0, 0, 0, 0]);
  149. expect(Number(res.return4.bar1)).toBeDefined();
  150. await program.methods.setValues().accounts({ dataAccount: storage.publicKey }).rpc();
  151. res = await program.methods.getValues1().accounts({ dataAccount: storage.publicKey }).view();
  152. expect(BigInt(res.return0)).toEqual(18446744073709551615n);
  153. expect(Number(res.return1)).toEqual(0xdad0feef);
  154. expect(Number(res.return2)).toEqual(0x7ffe);
  155. expect(BigInt(res.return3)).toEqual(57896044618658097711785492504343953926634992332820282019728792003956564819967n);
  156. res = await program.methods.getValues2().accounts({ dataAccount: storage.publicKey }).view();
  157. expect(Number(res.return0)).toEqual(102);
  158. expect(res.return1).toEqual("the course of true love never did run smooth");
  159. expect(res.return2).toEqual(Buffer.from("b00b1e", "hex"));
  160. expect(res.return3).toEqual([0x41, 0x42, 0x43, 0x44]);
  161. expect(Number(res.return4.bar2)).toBeDefined();
  162. await program.methods.doOps().accounts({ dataAccount: storage.publicKey }).rpc();
  163. res = await program.methods.getValues1().accounts({ dataAccount: storage.publicKey }).view();
  164. expect(BigInt(res.return0)).toEqual(1n);
  165. expect(Number(res.return1)).toEqual(65263);
  166. expect(Number(res.return2)).toEqual(32767);
  167. expect(BigInt(res.return3)).toEqual(57896044618658097711785492504343953926634992332820282019728792003956564819966n);
  168. res = await program.methods.getValues2().accounts({ dataAccount: storage.publicKey }).view();
  169. expect(Number(res.return0)).toEqual(61200);
  170. expect(res.return1).toEqual("");
  171. expect(res.return2).toEqual(Buffer.from("b0ff1e", "hex"));
  172. expect(res.return3).toEqual([0x61, 0x62, 0x63, 0x64]);
  173. expect(Number(res.return4.bar3)).toBeDefined();
  174. await program.methods.pushZero().accounts({ dataAccount: storage.publicKey }).rpc();
  175. let bs = "0xb0ff1e00";
  176. for (let i = 0; i < 20; i++) {
  177. res = await program.methods.getBs().accounts({ dataAccount: storage.publicKey }).view();
  178. expect(res).toStrictEqual(Buffer.from(bs.substring(2), "hex"));
  179. if (bs.length <= 4 || Math.random() >= 0.5) {
  180. let val = ((Math.random() * 256) | 0);
  181. await program.methods.push([val]).accounts({ dataAccount: storage.publicKey }).rpc();
  182. let valStr = val.toString(16);
  183. valStr = valStr.length == 1 ? "0" + valStr : valStr;
  184. bs += valStr;
  185. } else {
  186. await program.methods.pop().accounts({ dataAccount: storage.publicKey }).rpc();
  187. let last = bs.slice(-2);
  188. // TODO: rpc calls cannot return anything; this should be fixed in Anchor, so that
  189. // this line can be uncommented.
  190. //expect(res).toStrictEqual(Buffer.from(last, "hex"));
  191. bs = bs.slice(0, -2);
  192. }
  193. }
  194. });
  195. it('structs', async function () {
  196. const { program, storage } = await loadContractAndCallConstructor('store', []);
  197. await program.methods.setFoo1().accounts({ dataAccount: storage.publicKey }).rpc();
  198. // get foo1
  199. let res = await program.methods.getBothFoos().accounts({ dataAccount: storage.publicKey }).view();
  200. expect(res.return0.f1.bar2).toBeDefined();
  201. expect(res.return0.f2).toEqual(Buffer.from("Don't count your chickens before they hatch", "utf-8"));
  202. expect(res.return0.f3).toEqual(new BN(-102));
  203. expect(res.return0.f4).toEqual([0xed, 0xae, 0xda]);
  204. expect(res.return0.f5).toEqual("You can't have your cake and eat it too");
  205. expect(res.return0.f6.in1).toEqual(true);
  206. expect(res.return0.f6.in2).toEqual("There are other fish in the sea");
  207. expect(res.return1.f1.bar1).toBeDefined();
  208. expect(res.return1.f2).toEqual(Buffer.from([]));
  209. expect(res.return1.f3).toEqual(new BN(0));
  210. expect(res.return1.f4).toEqual([0, 0, 0]);
  211. expect(res.return1.f5).toEqual("");
  212. expect(res.return1.f6.in1).toEqual(false);
  213. expect(res.return1.f6.in2).toEqual("");
  214. await program.methods.setFoo2(
  215. {
  216. f1: { bar2: {} },
  217. f2: Buffer.from("b52b073595ccb35eaebb87178227b779", "hex"),
  218. f3: new BN(-123112321),
  219. f4: [0x12, 0x34, 0x56],
  220. f5: "Barking up the wrong tree",
  221. f6: { in1: true, in2: "Drive someone up the wall" }
  222. },
  223. "nah"
  224. ).accounts({ dataAccount: storage.publicKey }).rpc();
  225. res = await program.methods.getBothFoos().accounts({ dataAccount: storage.publicKey }).view();
  226. expect(res.return0.f1.bar2).toBeDefined();
  227. expect(res.return0.f2).toEqual(Buffer.from("Don't count your chickens before they hatch", "utf-8"));
  228. expect(res.return0.f3).toEqual(new BN(-102));
  229. expect(res.return0.f4).toEqual([0xed, 0xae, 0xda]);
  230. expect(res.return0.f5).toEqual("You can't have your cake and eat it too");
  231. expect(res.return0.f6.in1).toEqual(true);
  232. expect(res.return0.f6.in2).toEqual("There are other fish in the sea");
  233. expect(res.return1.f1.bar2).toBeDefined();
  234. expect(res.return1.f2).toEqual(Buffer.from("b52b073595ccb35eaebb87178227b779", "hex"));
  235. expect(res.return1.f3).toEqual(new BN(-123112321));
  236. expect(res.return1.f4).toEqual([0x12, 0x34, 0x56]);
  237. expect(res.return1.f5).toEqual("Barking up the wrong tree");
  238. expect(res.return1.f6.in1).toEqual(true);
  239. expect(res.return1.f6.in2).toEqual("nah");
  240. await program.methods.deleteFoo(true).accounts({ dataAccount: storage.publicKey }).rpc();
  241. res = await program.methods.getFoo(false).accounts({ dataAccount: storage.publicKey }).view();
  242. expect(res.f1.bar2).toBeDefined();
  243. expect(res.f2).toEqual(Buffer.from("b52b073595ccb35eaebb87178227b779", "hex"));
  244. expect(res.f3).toEqual(new BN(-123112321));
  245. expect(res.f4).toEqual([0x12, 0x34, 0x56]);
  246. expect(res.f5).toEqual("Barking up the wrong tree");
  247. expect(res.f6.in1).toEqual(true);
  248. expect(res.f6.in2).toEqual("nah");
  249. res = await program.methods.getFoo(true).accounts({ dataAccount: storage.publicKey }).view();
  250. expect(res.f1.bar1).toBeDefined();
  251. expect(res.f2).toEqual(Buffer.from([]));
  252. expect(res.f3).toEqual(new BN(0));
  253. expect(res.f4).toEqual([0, 0, 0]);
  254. expect(res.f5).toEqual("");
  255. expect(res.f6.in1).toEqual(false);
  256. expect(res.f6.in2).toEqual("");
  257. await program.methods.deleteFoo(false).accounts({ dataAccount: storage.publicKey }).rpc();
  258. res = await program.methods.getBothFoos().accounts({ dataAccount: storage.publicKey }).view();
  259. expect(res.return0.f1.bar1).toBeDefined();
  260. expect(res.return0.f2).toEqual(Buffer.from([]));
  261. expect(res.return0.f3).toEqual(new BN(0));
  262. expect(res.return0.f4).toEqual([0, 0, 0]);
  263. expect(res.return0.f5).toEqual("");
  264. expect(res.return0.f6.in1).toEqual(false);
  265. expect(res.return0.f6.in2).toEqual("");
  266. expect(res.return1.f1.bar1).toBeDefined();
  267. expect(res.return1.f2).toEqual(Buffer.from([]));
  268. expect(res.return1.f3).toEqual(new BN(0));
  269. expect(res.return1.f4).toEqual([0, 0, 0]);
  270. expect(res.return1.f5).toEqual("");
  271. expect(res.return1.f6.in1).toEqual(false);
  272. expect(res.return1.f6.in2).toEqual("");
  273. await program.methods.structLiteral().accounts({ dataAccount: storage.publicKey }).rpc();
  274. res = await program.methods.getFoo(true).accounts({ dataAccount: storage.publicKey }).view();
  275. expect(res.f1.bar4).toBeDefined();
  276. expect(res.f2).toEqual(Buffer.from("537570657263616c6966726167696c697374696365787069616c69646f63696f7573", "hex"));
  277. expect(res.f3).toEqual(new BN(64927));
  278. expect(res.f4).toEqual([0xe2, 0x82, 0xac]);
  279. expect(res.f5).toEqual("Antidisestablishmentarianism");
  280. expect(res.f6.in1).toEqual(true);
  281. expect(res.f6.in2).toEqual("Pseudopseudohypoparathyroidism");
  282. });
  283. it('account storage too small constructor', async function () {
  284. await expect(loadContractAndCallConstructor('store', [], 100))
  285. .rejects
  286. .toThrowError('Transaction simulation failed: Error processing Instruction 0: account data too small for instruction');
  287. });
  288. it('account storage too small dynamic alloc', async function () {
  289. const { program, storage } = await loadContractAndCallConstructor('store', [], 233);
  290. // storage.sol needs 168 bytes on constructor, more for string data
  291. // set a load of string which will overflow
  292. await expect(program.methods.setFoo1().accounts({ dataAccount: storage.publicKey }).rpc())
  293. .rejects
  294. .toThrowError('Transaction simulation failed: Error processing Instruction 0: account data too small for instruction');
  295. });
  296. it('account storage too small dynamic realloc', async function () {
  297. const { program, storage } = await loadContractAndCallConstructor('store', [], 233);
  298. async function push_until_bang() {
  299. for (let i = 0; i < 100; i++) {
  300. await program.methods.push(new Uint8Array([1])).accounts({ dataAccount: storage.publicKey }).rpc();
  301. }
  302. }
  303. // do realloc until failure
  304. await expect(push_until_bang())
  305. .rejects
  306. .toThrowError('Transaction simulation failed: Error processing Instruction 0: account data too small for instruction');
  307. });
  308. it('arrays in account storage', async function () {
  309. const { program, storage } = await loadContractAndCallConstructor('arrays', []);
  310. let users = [];
  311. for (let i = 0; i < 3; i++) {
  312. let addr = [...crypto.randomBytes(32)];
  313. let name = `name${i}`;
  314. let id = new BN(crypto.randomBytes(4).readUInt32BE(0));
  315. let perms = [];
  316. for (let j = 0; j < Math.random() * 3; j++) {
  317. let p = Math.floor(Math.random() * 8);
  318. let perm = { [`perm${p + 1}`]: {} };
  319. perms.push(perm);
  320. }
  321. await program.methods.addUser(id, addr, name, perms)
  322. .accounts({ dataAccount: storage.publicKey })
  323. .rpc();
  324. users.push([
  325. name, addr, id, perms
  326. ]);
  327. }
  328. let user = users[Math.floor(Math.random() * users.length)];
  329. let res = await program.methods.getUserById(user[2]).accounts({ dataAccount: storage.publicKey }).view();
  330. expect(res.name).toEqual(user[0]);
  331. expect(res.addr).toEqual(user[1]);
  332. expect(res.id.cmp(user[2])).toBe(0);
  333. expect(res.perms).toEqual(user[3]);
  334. // @ts-ignore
  335. const perms: string[] = user[3];
  336. if (perms.length > 0) {
  337. let p = perms[Math.floor(Math.random() * perms.length)];
  338. res = await program.methods.hasPermission(user[2], p).accounts({ dataAccount: storage.publicKey }).view();
  339. expect(res).toStrictEqual(true);
  340. }
  341. user = users[Math.floor(Math.random() * users.length)];
  342. res = await program.methods.getUserByAddress(user[1]).accounts({ dataAccount: storage.publicKey }).view();
  343. expect(res.name).toEqual(user[0]);
  344. expect(res.addr).toEqual(user[1]);
  345. expect(res.id.cmp(user[2])).toBe(0);
  346. expect(res.perms).toEqual(user[3]);
  347. await program.methods.removeUser(user[2]).accounts({ dataAccount: storage.publicKey }).rpc();
  348. res = await program.methods.userExists(user[2]).accounts({ dataAccount: storage.publicKey }).view();
  349. expect(res).toStrictEqual(false);
  350. });
  351. });