utils_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package psbt
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. "github.com/pkt-cash/pktd/chaincfg/chainhash"
  7. "github.com/pkt-cash/pktd/wire"
  8. )
  9. func TestSumUtxoInputValues(t *testing.T) {
  10. // Expect sum to fail for packet with non-matching txIn and PInputs.
  11. tx := wire.NewMsgTx(2)
  12. badPacket, err := NewFromUnsignedTx(tx)
  13. if err != nil {
  14. t.Fatalf("could not create packet from TX: %v", err)
  15. }
  16. badPacket.Inputs = append(badPacket.Inputs, PInput{})
  17. _, err = SumUtxoInputValues(badPacket)
  18. if err == nil {
  19. t.Fatalf("expected sum of bad packet to fail")
  20. }
  21. // Expect sum to fail if any inputs don't have UTXO information added.
  22. op := []*wire.OutPoint{{}, {}}
  23. noUtxoInfoPacket, err := New(op, nil, 2, 0, []uint32{0, 0})
  24. if err != nil {
  25. t.Fatalf("could not create new packet: %v", err)
  26. }
  27. _, err = SumUtxoInputValues(noUtxoInfoPacket)
  28. if err == nil {
  29. t.Fatalf("expected sum of missing UTXO info to fail")
  30. }
  31. // Create a packet that is OK and contains both witness and non-witness
  32. // UTXO information.
  33. okPacket, err := New(op, nil, 2, 0, []uint32{0, 0})
  34. if err != nil {
  35. t.Fatalf("could not create new packet: %v", err)
  36. }
  37. okPacket.Inputs[0].WitnessUtxo = &wire.TxOut{Value: 1234}
  38. okPacket.Inputs[1].NonWitnessUtxo = &wire.MsgTx{
  39. TxOut: []*wire.TxOut{{Value: 6543}},
  40. }
  41. sum, err := SumUtxoInputValues(okPacket)
  42. if err != nil {
  43. t.Fatalf("could not sum input: %v", err)
  44. }
  45. if sum != (1234 + 6543) {
  46. t.Fatalf("unexpected sum, got %d wanted %d", sum, 1234+6543)
  47. }
  48. }
  49. func TestTxOutsEqual(t *testing.T) {
  50. testCases := []struct {
  51. name string
  52. out1 *wire.TxOut
  53. out2 *wire.TxOut
  54. expectEqual bool
  55. }{{
  56. name: "both nil",
  57. out1: nil,
  58. out2: nil,
  59. expectEqual: true,
  60. }, {
  61. name: "one nil",
  62. out1: nil,
  63. out2: &wire.TxOut{},
  64. expectEqual: false,
  65. }, {
  66. name: "both empty",
  67. out1: &wire.TxOut{},
  68. out2: &wire.TxOut{},
  69. expectEqual: true,
  70. }, {
  71. name: "one pk script set",
  72. out1: &wire.TxOut{},
  73. out2: &wire.TxOut{
  74. PkScript: []byte("foo"),
  75. },
  76. expectEqual: false,
  77. }, {
  78. name: "both fully set",
  79. out1: &wire.TxOut{
  80. Value: 1234,
  81. PkScript: []byte("bar"),
  82. },
  83. out2: &wire.TxOut{
  84. Value: 1234,
  85. PkScript: []byte("bar"),
  86. },
  87. expectEqual: true,
  88. }}
  89. for _, tc := range testCases {
  90. tc := tc
  91. t.Run(tc.name, func(t *testing.T) {
  92. result := TxOutsEqual(tc.out1, tc.out2)
  93. if result != tc.expectEqual {
  94. t.Fatalf("unexpected result, got %v wanted %v",
  95. result, tc.expectEqual)
  96. }
  97. })
  98. }
  99. }
  100. func TestVerifyOutputsEqual(t *testing.T) {
  101. testCases := []struct {
  102. name string
  103. outs1 []*wire.TxOut
  104. outs2 []*wire.TxOut
  105. expectErr bool
  106. }{{
  107. name: "both nil",
  108. outs1: nil,
  109. outs2: nil,
  110. expectErr: false,
  111. }, {
  112. name: "one nil",
  113. outs1: nil,
  114. outs2: []*wire.TxOut{{}},
  115. expectErr: true,
  116. }, {
  117. name: "both empty",
  118. outs1: []*wire.TxOut{{}},
  119. outs2: []*wire.TxOut{{}},
  120. expectErr: false,
  121. }, {
  122. name: "one pk script set",
  123. outs1: []*wire.TxOut{{}},
  124. outs2: []*wire.TxOut{{
  125. PkScript: []byte("foo"),
  126. }},
  127. expectErr: true,
  128. }, {
  129. name: "both fully set",
  130. outs1: []*wire.TxOut{{
  131. Value: 1234,
  132. PkScript: []byte("bar"),
  133. }, {}},
  134. outs2: []*wire.TxOut{{
  135. Value: 1234,
  136. PkScript: []byte("bar"),
  137. }, {}},
  138. expectErr: false,
  139. }}
  140. for _, tc := range testCases {
  141. tc := tc
  142. t.Run(tc.name, func(t *testing.T) {
  143. err := VerifyOutputsEqual(tc.outs1, tc.outs2)
  144. if (tc.expectErr && err == nil) ||
  145. (!tc.expectErr && err != nil) {
  146. t.Fatalf("got error '%v' but wanted it to be "+
  147. "nil: %v", err, tc.expectErr)
  148. }
  149. })
  150. }
  151. }
  152. func TestVerifyInputPrevOutpointsEqual(t *testing.T) {
  153. testCases := []struct {
  154. name string
  155. ins1 []*wire.TxIn
  156. ins2 []*wire.TxIn
  157. expectErr bool
  158. }{{
  159. name: "both nil",
  160. ins1: nil,
  161. ins2: nil,
  162. expectErr: false,
  163. }, {
  164. name: "one nil",
  165. ins1: nil,
  166. ins2: []*wire.TxIn{{}},
  167. expectErr: true,
  168. }, {
  169. name: "both empty",
  170. ins1: []*wire.TxIn{{}},
  171. ins2: []*wire.TxIn{{}},
  172. expectErr: false,
  173. }, {
  174. name: "one previous output set",
  175. ins1: []*wire.TxIn{{}},
  176. ins2: []*wire.TxIn{{
  177. PreviousOutPoint: wire.OutPoint{
  178. Hash: chainhash.Hash{11, 22, 33},
  179. Index: 7,
  180. },
  181. }},
  182. expectErr: true,
  183. }, {
  184. name: "both fully set",
  185. ins1: []*wire.TxIn{{
  186. PreviousOutPoint: wire.OutPoint{
  187. Hash: chainhash.Hash{11, 22, 33},
  188. Index: 7,
  189. },
  190. }, {}},
  191. ins2: []*wire.TxIn{{
  192. PreviousOutPoint: wire.OutPoint{
  193. Hash: chainhash.Hash{11, 22, 33},
  194. Index: 7,
  195. },
  196. }, {}},
  197. expectErr: false,
  198. }}
  199. for _, tc := range testCases {
  200. tc := tc
  201. t.Run(tc.name, func(t *testing.T) {
  202. err := VerifyInputPrevOutpointsEqual(tc.ins1, tc.ins2)
  203. if (tc.expectErr && err == nil) ||
  204. (!tc.expectErr && err != nil) {
  205. t.Fatalf("got error '%v' but wanted it to be "+
  206. "nil: %v", err, tc.expectErr)
  207. }
  208. })
  209. }
  210. }
  211. func TestVerifyInputOutputLen(t *testing.T) {
  212. testCases := []struct {
  213. name string
  214. packet *Packet
  215. needInputs bool
  216. needOutputs bool
  217. expectErr bool
  218. }{{
  219. name: "packet nil",
  220. packet: nil,
  221. expectErr: true,
  222. }, {
  223. name: "wire tx nil",
  224. packet: &Packet{},
  225. expectErr: true,
  226. }, {
  227. name: "both empty don't need outputs",
  228. packet: &Packet{
  229. UnsignedTx: &wire.MsgTx{},
  230. },
  231. expectErr: false,
  232. }, {
  233. name: "both empty but need outputs",
  234. packet: &Packet{
  235. UnsignedTx: &wire.MsgTx{},
  236. },
  237. needOutputs: true,
  238. expectErr: true,
  239. }, {
  240. name: "both empty but need inputs",
  241. packet: &Packet{
  242. UnsignedTx: &wire.MsgTx{},
  243. },
  244. needInputs: true,
  245. expectErr: true,
  246. }, {
  247. name: "input len mismatch",
  248. packet: &Packet{
  249. UnsignedTx: &wire.MsgTx{
  250. TxIn: []*wire.TxIn{{}},
  251. },
  252. },
  253. needInputs: true,
  254. expectErr: true,
  255. }, {
  256. name: "output len mismatch",
  257. packet: &Packet{
  258. UnsignedTx: &wire.MsgTx{
  259. TxOut: []*wire.TxOut{{}},
  260. },
  261. },
  262. needOutputs: true,
  263. expectErr: true,
  264. }, {
  265. name: "all fully set",
  266. packet: &Packet{
  267. UnsignedTx: &wire.MsgTx{
  268. TxIn: []*wire.TxIn{{}},
  269. TxOut: []*wire.TxOut{{}},
  270. },
  271. Inputs: []PInput{{}},
  272. Outputs: []POutput{{}},
  273. },
  274. needInputs: true,
  275. needOutputs: true,
  276. expectErr: false,
  277. }}
  278. for _, tc := range testCases {
  279. tc := tc
  280. t.Run(tc.name, func(t *testing.T) {
  281. err := VerifyInputOutputLen(
  282. tc.packet, tc.needInputs, tc.needOutputs,
  283. )
  284. if (tc.expectErr && err == nil) ||
  285. (!tc.expectErr && err != nil) {
  286. t.Fatalf("got error '%v' but wanted it to be "+
  287. "nil: %v", err, tc.expectErr)
  288. }
  289. })
  290. }
  291. }
  292. func TestNewFromSignedTx(t *testing.T) {
  293. orig := &wire.MsgTx{
  294. TxIn: []*wire.TxIn{{
  295. PreviousOutPoint: wire.OutPoint{},
  296. SignatureScript: []byte("script"),
  297. Witness: [][]byte{[]byte("witness")},
  298. Sequence: 1234,
  299. }},
  300. TxOut: []*wire.TxOut{{
  301. PkScript: []byte{77, 88},
  302. Value: 99,
  303. }},
  304. }
  305. packet, scripts, witnesses, err := NewFromSignedTx(orig)
  306. if err != nil {
  307. t.Fatalf("could not create packet from signed TX: %v", err)
  308. }
  309. tx := packet.UnsignedTx
  310. expectedTxIn := []*wire.TxIn{{
  311. PreviousOutPoint: wire.OutPoint{},
  312. Sequence: 1234,
  313. }}
  314. if !reflect.DeepEqual(tx.TxIn, expectedTxIn) {
  315. t.Fatalf("unexpected txin, got %#v wanted %#v",
  316. tx.TxIn, expectedTxIn)
  317. }
  318. if !reflect.DeepEqual(tx.TxOut, orig.TxOut) {
  319. t.Fatalf("unexpected txout, got %#v wanted %#v",
  320. tx.TxOut, orig.TxOut)
  321. }
  322. if len(scripts) != 1 || !bytes.Equal(scripts[0], []byte("script")) {
  323. t.Fatalf("script not extracted correctly")
  324. }
  325. if len(witnesses) != 1 ||
  326. !bytes.Equal(witnesses[0][0], []byte("witness")) {
  327. t.Fatalf("witness not extracted correctly")
  328. }
  329. }