simple.spec.ts 23 KB

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