-
Notifications
You must be signed in to change notification settings - Fork 2
/
spl_token_instruction_test.c
1028 lines (886 loc) · 32.9 KB
/
spl_token_instruction_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common_byte_strings.h"
#include "instruction.h"
#include "spl_token_instruction.c"
#include <stdio.h>
#include <assert.h>
#include "memzero.h"
void print_pubkey(const Pubkey* pubkey) {
char buf[45];
encode_base58(pubkey, 32, buf, sizeof(buf));
printf("%s\n", buf);
}
#define BLOCKHASH BYTES32_BS58_1
#define MINT_ACCOUNT BYTES32_BS58_2
#define TOKEN_ACCOUNT BYTES32_BS58_3
#define OWNER_ACCOUNT BYTES32_BS58_4
#define MULTISIG_ACCOUNT OWNER_ACCOUNT
#define SIGNER1 BYTES32_BS58_5
#define SIGNER2 BYTES32_BS58_6
#define SIGNER3 BYTES32_BS58_7
#define DEST_ACCOUNT BYTES32_BS58_8
#define DELEGATE DEST_ACCOUNT
#define NEW_OWNER DEST_ACCOUNT
void test_parse_spl_token_create_token() {
uint8_t message[] = {2,
0,
3,
5,
OWNER_ACCOUNT,
MINT_ACCOUNT,
SYSVAR_RENT,
PROGRAM_ID_SYSTEM,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
2,
3,
2,
0,
1,
52,
0,
0,
0,
0,
245,
1,
0,
0,
0,
0,
0,
0,
88,
0,
0,
0,
0,
0,
0,
0,
PROGRAM_ID_SPL_TOKEN,
4,
2,
1,
2,
35,
0,
9,
OWNER_ACCOUNT,
0};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SystemCreateAccount (ignored)
assert(instruction_validate(&instruction, &header) == 0);
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenInitializeMint
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(InitializeMint));
const SplTokenInitializeMintInfo* init_mint = &info.initialize_mint;
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(init_mint->mint_account, &mint_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(init_mint->mint_authority, &owner);
assert(init_mint->decimals == 9);
assert(init_mint->freeze_authority == NULL);
}
void test_parse_spl_token_create_account() {
uint8_t message[] = {
0x02,
0x00,
0x03,
0x06,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
MINT_ACCOUNT,
SYSVAR_RENT,
PROGRAM_ID_SYSTEM,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
0x02,
// SystemCreateAccount
0x04,
0x02,
0x00,
0x01,
0x34,
0x00,
0x00,
0x00,
0x00,
0x80,
0x56,
0x1a,
0x00,
0x00,
0x00,
0x00,
0x00,
0x78,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
PROGRAM_ID_SPL_TOKEN,
// SplTokenInitializeAccount
0x05,
0x04,
0x01,
0x02,
0x00,
0x03,
0x01,
0x01,
};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SystemCreateAccount (ignored)
assert(instruction_validate(&instruction, &header) == 0);
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenInitializeAccount
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(InitializeAccount));
const SplTokenInitializeAccountInfo* init_acc = &info.initialize_account;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(init_acc->token_account, &token_account);
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(init_acc->mint_account, &mint_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(init_acc->owner, &owner);
}
void test_parse_spl_token_create_account2() {
uint8_t message[] = {0x02,
0x00,
0x04,
0x06,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
MINT_ACCOUNT,
SYSVAR_RENT,
PROGRAM_ID_SYSTEM,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
0x02,
// SystemCreateAccount
0x04,
0x02,
0x00,
0x01,
0x34,
0x00,
0x00,
0x00,
0x00,
0x80,
0x56,
0x1a,
0x00,
0x00,
0x00,
0x00,
0x00,
0x78,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
PROGRAM_ID_SPL_TOKEN,
// SplTokenInitializeAccount2
0x05,
0x03,
0x01,
0x02,
0x03,
0x21,
0x10,
OWNER_ACCOUNT};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SystemCreateAccount (ignored)
assert(instruction_validate(&instruction, &header) == 0);
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenInitializeAccount2
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(InitializeAccount2));
const SplTokenInitializeAccountInfo* init_acc = &info.initialize_account;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(init_acc->token_account, &token_account);
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(init_acc->mint_account, &mint_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(init_acc->owner, &owner);
}
void test_parse_spl_token_create_multisig() {
uint8_t message[] = {2,
0,
5,
8,
OWNER_ACCOUNT,
MULTISIG_ACCOUNT,
SYSVAR_RENT,
SIGNER1,
SIGNER2,
SIGNER3,
PROGRAM_ID_SYSTEM,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
2,
// SystemCreateAccount
6,
2,
0,
1,
52,
0,
0,
0,
0,
245,
1,
0,
0,
0,
0,
0,
0,
40,
0,
0,
0,
0,
0,
0,
0,
PROGRAM_ID_SPL_TOKEN,
// SplTokenInitializeMultisig
7,
5,
1,
2,
3,
4,
5,
2,
2,
2};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SystemCreateAccount (ignored)
assert(instruction_validate(&instruction, &header) == 0);
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenInitializeMultisig
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(InitializeMultisig));
const SplTokenInitializeMultisigInfo* init_ms = &info.initialize_multisig;
assert(init_ms->body.m == 2);
const Pubkey multisig_account = {{MULTISIG_ACCOUNT}};
assert_pubkey_equal(init_ms->multisig_account, &multisig_account);
assert(init_ms->signers.count == 3);
const Pubkey* signer = init_ms->signers.first;
const Pubkey signer1 = {{SIGNER1}};
assert_pubkey_equal(signer++, &signer1);
const Pubkey signer2 = {{SIGNER2}};
assert_pubkey_equal(signer++, &signer2);
const Pubkey signer3 = {{SIGNER3}};
assert_pubkey_equal(signer++, &signer3);
}
void test_parse_spl_token_transfer() {
uint8_t message[] = {1,
0,
2,
5,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
DEST_ACCOUNT,
MINT_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
4,
4,
1,
3,
2,
0,
10,
12,
42,
0,
0,
0,
0,
0,
0,
0,
9};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenTransfer2
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(TransferChecked));
const SplTokenTransferInfo* tr_info = &info.transfer;
assert(tr_info->body.amount == 42);
assert(tr_info->body.decimals == 9);
const Pubkey src_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(tr_info->src_account, &src_account);
const Pubkey dest_account = {{DEST_ACCOUNT}};
assert_pubkey_equal(tr_info->dest_account, &dest_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(tr_info->sign.single.signer, &owner);
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(tr_info->mint_account, &mint_account);
}
void test_parse_spl_token_approve() {
uint8_t message[] = {1,
0,
2,
4,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
DEST_ACCOUNT,
BLOCKHASH,
1,
2,
4,
1,
2,
3,
0,
10,
13,
42,
0,
0,
0,
0,
0,
0,
0,
9};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenApprove2
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(ApproveChecked));
const SplTokenApproveInfo* ap_info = &info.approve;
assert(ap_info->body.amount == 42);
assert(ap_info->body.decimals == 9);
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(ap_info->token_account, &token_account);
const Pubkey delegate = {{DELEGATE}};
assert_pubkey_equal(ap_info->delegate, &delegate);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(ap_info->sign.single.signer, &owner);
const Pubkey mint_account = {{PROGRAM_ID_SPL_TOKEN}};
assert_pubkey_equal(ap_info->mint_account, &mint_account);
}
void test_parse_spl_token_revoke() {
uint8_t message[] = {1,
0,
2,
3,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
2,
2,
1,
0,
1,
5};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenRevoke
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(Revoke));
const SplTokenRevokeInfo* re_info = &info.revoke;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(re_info->token_account, &token_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(re_info->sign.single.signer, &owner);
}
void test_parse_spl_token_set_authority() {
uint8_t message[] = {1,
0,
1,
3,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
2,
2,
1,
0,
35,
6,
2,
1,
NEW_OWNER};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenSetAuthority
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(SetAuthority));
const SplTokenSetAuthorityInfo* so_info = &info.set_owner;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(so_info->account, &token_account);
assert(so_info->authority_type == Token_AuthorityType_AccountOwner);
const Pubkey new_owner = {{NEW_OWNER}};
assert_pubkey_equal(so_info->new_authority, &new_owner);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(so_info->sign.single.signer, &owner);
}
void test_parse_spl_token_mint_to() {
uint8_t message[] = {1,
0,
0,
3,
OWNER_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
TOKEN_ACCOUNT,
BLOCKHASH,
1,
1,
3,
1,
2,
0,
10,
14,
42,
0,
0,
0,
0,
0,
0,
0,
9};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenMintTo2
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(MintToChecked));
const SplTokenMintToInfo* mt_info = &info.mint_to;
assert(mt_info->body.amount == 42);
assert(mt_info->body.decimals == 9);
const Pubkey mint_account = {{PROGRAM_ID_SPL_TOKEN}};
assert_pubkey_equal(mt_info->mint_account, &mint_account);
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(mt_info->token_account, &token_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(mt_info->sign.single.signer, &owner);
}
void test_parse_spl_token_burn() {
uint8_t message[] = {1,
0,
0,
4,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
MINT_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
3,
3,
1,
2,
0,
10,
15,
42,
0,
0,
0,
0,
0,
0,
0,
9};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenBurn
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(BurnChecked));
const SplTokenBurnInfo* bn_info = &info.burn;
assert(bn_info->body.amount == 42);
assert(bn_info->body.decimals == 9);
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(bn_info->token_account, &token_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(bn_info->sign.single.signer, &owner);
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(bn_info->mint_account, &mint_account);
}
void test_parse_spl_token_close_account() {
uint8_t message[] = {
0x01,
0x00,
0x01,
0x03,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
0x01,
// SplTokenCloseAccount
0x02,
0x03,
0x01,
0x00,
0x00,
0x01,
0x09,
};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenCloseAccount
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(CloseAccount));
const SplTokenCloseAccountInfo* close_acc = &info.close_account;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(close_acc->token_account, &token_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(close_acc->dest_account, &owner);
assert_pubkey_equal(close_acc->sign.single.signer, &owner);
}
void test_parse_spl_token_freeze_account() {
uint8_t message[] = {1,
0,
2,
4,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
MINT_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
3,
3,
1,
2,
0,
1,
10};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenFreezeAccount
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(FreezeAccount));
const SplTokenFreezeAccountInfo* freeze_account = &info.freeze_account;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(freeze_account->token_account, &token_account);
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(freeze_account->mint_account, &mint_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(freeze_account->sign.single.signer, &owner);
}
void test_parse_spl_token_thaw_account() {
uint8_t message[] = {1,
0,
2,
4,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
MINT_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
3,
3,
1,
2,
0,
1,
11};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenThawAccount
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(ThawAccount));
const SplTokenThawAccountInfo* thaw_account = &info.thaw_account;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(thaw_account->token_account, &token_account);
const Pubkey mint_account = {{MINT_ACCOUNT}};
assert_pubkey_equal(thaw_account->mint_account, &mint_account);
const Pubkey owner = {{OWNER_ACCOUNT}};
assert_pubkey_equal(thaw_account->sign.single.signer, &owner);
}
void test_parse_spl_token_sync_native() {
uint8_t message[] = {1,
0,
1,
4,
OWNER_ACCOUNT,
TOKEN_ACCOUNT,
PROGRAM_ID_SPL_TOKEN,
BLOCKHASH,
1,
2,
1,
1,
1,
17};
Parser parser = {message, sizeof(message)};
MessageHeader header;
assert(parse_message_header(&parser, &header) == 0);
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0); // SplTokenSyncNative
assert(instruction_validate(&instruction, &header) == 0);
SplTokenInfo info;
assert(parse_spl_token_instructions(&instruction, &header, &info) == 0);
assert(parser.buffer_length == 0);
assert(info.kind == SplTokenKind(SyncNative));
const SplTokenSyncNativeInfo* sync_native = &info.sync_native;
const Pubkey token_account = {{TOKEN_ACCOUNT}};
assert_pubkey_equal(sync_native->token_account, &token_account);
}
void test_parse_spl_token_instruction_kind() {
SplTokenInstructionKind kind;
uint8_t buf[] = {0};
Parser parser = {buf, ARRAY_LEN(buf)};
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(InitializeMint));
buf[0] = 1;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(InitializeAccount));
buf[0] = 2;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(InitializeMultisig));
buf[0] = 5;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(Revoke));
buf[0] = 6;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(SetAuthority));
buf[0] = 9;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(CloseAccount));
buf[0] = 10;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(FreezeAccount));
buf[0] = 11;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(ThawAccount));
buf[0] = 12;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(TransferChecked));
buf[0] = 13;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(ApproveChecked));
buf[0] = 14;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(MintToChecked));
buf[0] = 15;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(BurnChecked));
buf[0] = 16;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(InitializeAccount2));
buf[0] = 17;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 0);
assert(kind == SplTokenKind(SyncNative));
// First unused enum value fails
buf[0] = 18;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 1);
// Largest buffer value fails
buf[0] = 255;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 1);
// Deprecated instructions fail
buf[0] = 3;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 1);
buf[0] = 4;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 1);
buf[0] = 7;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 1);
buf[0] = 8;
parser.buffer = buf;
parser.buffer_length = ARRAY_LEN(buf);
assert(parse_spl_token_instruction_kind(&parser, &kind) == 1);
}
void test_parse_spl_token_sign() {
const size_t max_signers = Token_MAX_SIGNERS;
const size_t max_accounts = 1 + max_signers; // multisig_account + signers
const size_t accounts_len = max_accounts + 1; // one too many
const Pubkey pubkeys[1];
const MessageHeader header = {
.pubkeys_header = {0, 0, 0, 1},
.pubkeys = pubkeys,
};
uint8_t accounts[accounts_len];
memzero(accounts, accounts_len);
Instruction ix = {
.program_id_index = 0,
.accounts = accounts,
.accounts_length = 0,
};
InstructionAccountsIterator it;
SplTokenSign sign;
// too few remaining keys fails
instruction_accounts_iterator_init(&it, &header, &ix);
assert(parse_spl_token_sign(&it, &sign) == 1);
// too many remaining keys fails
ix.accounts_length = accounts_len;
instruction_accounts_iterator_init(&it, &header, &ix);
assert(parse_spl_token_sign(&it, &sign) == 1);
// one key resolves to single signer
ix.accounts_length = 1;
instruction_accounts_iterator_init(&it, &header, &ix);
assert(parse_spl_token_sign(&it, &sign) == 0);
assert(sign.kind == SplTokenSignKindSingle);
// two keys resolves to multi signer
ix.accounts_length = 2;
instruction_accounts_iterator_init(&it, &header, &ix);
assert(parse_spl_token_sign(&it, &sign) == 0);
assert(sign.kind == SplTokenSignKindMulti);
assert(sign.multi.signers.count == 1);
// max keys resolves to multi signer
ix.accounts_length = max_accounts;
instruction_accounts_iterator_init(&it, &header, &ix);
assert(parse_spl_token_sign(&it, &sign) == 0);
assert(sign.kind == SplTokenSignKindMulti);
assert(sign.multi.signers.count == max_signers);
}
void test_print_m_of_n_string() {
char s[M_OF_N_MAX_LEN];
// n too big fails
assert(print_m_of_n_string(0, Token_MAX_SIGNERS + 1, s, sizeof(s)) == 1);
// m > n fails
assert(print_m_of_n_string(1, 0, s, sizeof(s)) == 1);
// Buffer too small fails
assert(print_m_of_n_string(0, 0, s, sizeof(s) - 1) == 1);
assert(print_m_of_n_string(3, 5, s, sizeof(s)) == 0);