How to use Bytes method of got Package

Best Got code snippet using got.Bytes

chainio_test.go

Source:chainio_test.go Github

copy

Full Screen

...39		{40			name: "Spends last output of coinbase",41			stxo: SpentTxOut{42				Amount:     5000000000,43				PkScript:   hexToBytes("410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac"),44				IsCoinBase: true,45				Height:     9,46			},47			serialized: hexToBytes("1300320511db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5c"),48		},49		// Adapted from block 100025 in main blockchain.50		{51			name: "Spends last output of non coinbase",52			stxo: SpentTxOut{53				Amount:     13761000000,54				PkScript:   hexToBytes("76a914b2fb57eadf61e106a100a7445a8c3f67898841ec88ac"),55				IsCoinBase: false,56				Height:     100024,57			},58			serialized: hexToBytes("8b99700086c64700b2fb57eadf61e106a100a7445a8c3f67898841ec"),59		},60		// Adapted from block 100025 in main blockchain.61		{62			name: "Does not spend last output, legacy format",63			stxo: SpentTxOut{64				Amount:   34405000000,65				PkScript: hexToBytes("76a9146edbc6c4d31bae9f1ccc38538a114bf42de65e8688ac"),66			},67			serialized: hexToBytes("0091f20f006edbc6c4d31bae9f1ccc38538a114bf42de65e86"),68		},69	}70	for _, test := range tests {71		// Ensure the function to calculate the serialized size without actually serializing it is calculated properly.72		gotSize := spentTxOutSerializeSize(&test.stxo)73		if gotSize != len(test.serialized) {74			t.Errorf("SpentTxOutSerializeSize (%s): did not get "+75				"expected size - got %d, want %d", test.name,76				gotSize, len(test.serialized),77			)78			continue79		}80		// Ensure the stxo serializes to the expected value.81		gotSerialized := make([]byte, gotSize)82		gotBytesWritten := putSpentTxOut(gotSerialized, &test.stxo)83		if !bytes.Equal(gotSerialized, test.serialized) {84			t.Errorf("putSpentTxOut (%s): did not get expected "+85				"bytes - got %x, want %x", test.name,86				gotSerialized, test.serialized,87			)88			continue89		}90		if gotBytesWritten != len(test.serialized) {91			t.Errorf("putSpentTxOut (%s): did not get expected "+92				"number of bytes written - got %d, want %d",93				test.name, gotBytesWritten,94				len(test.serialized),95			)96			continue97		}98		// Ensure the serialized bytes are decoded back to the expected stxo.99		var gotStxo SpentTxOut100		gotBytesRead, e := decodeSpentTxOut(test.serialized, &gotStxo)101		if e != nil {102			t.Errorf("decodeSpentTxOut (%s): unexpected error: %v",103				test.name, e,104			)105			continue106		}107		if !reflect.DeepEqual(gotStxo, test.stxo) {108			t.Errorf("decodeSpentTxOut (%s) mismatched entries - "+109				"got %v, want %v", test.name, gotStxo, test.stxo,110			)111			continue112		}113		if gotBytesRead != len(test.serialized) {114			t.Errorf("decodeSpentTxOut (%s): did not get expected "+115				"number of bytes read - got %d, want %d",116				test.name, gotBytesRead, len(test.serialized),117			)118			continue119		}120	}121}122// TestStxoDecodeErrors performs negative tests against decoding spent transaction outputs to ensure error paths work as123// expected.124func TestStxoDecodeErrors(t *testing.T) {125	t.Parallel()126	tests := []struct {127		name       string128		stxo       SpentTxOut129		serialized []byte130		bytesRead  int131		errType    error132	}{133		{134			name:       "nothing serialized",135			stxo:       SpentTxOut{},136			serialized: hexToBytes(""),137			errType:    errDeserialize(""),138			bytesRead:  0,139		},140		{141			name:       "no data after header code w/o reserved",142			stxo:       SpentTxOut{},143			serialized: hexToBytes("00"),144			errType:    errDeserialize(""),145			bytesRead:  1,146		},147		{148			name:       "no data after header code with reserved",149			stxo:       SpentTxOut{},150			serialized: hexToBytes("13"),151			errType:    errDeserialize(""),152			bytesRead:  1,153		},154		{155			name:       "no data after reserved",156			stxo:       SpentTxOut{},157			serialized: hexToBytes("1300"),158			errType:    errDeserialize(""),159			bytesRead:  2,160		},161		{162			name:       "incomplete compressed txout",163			stxo:       SpentTxOut{},164			serialized: hexToBytes("1332"),165			errType:    errDeserialize(""),166			bytesRead:  2,167		},168	}169	for _, test := range tests {170		// Ensure the expected error type is returned.171		gotBytesRead, e := decodeSpentTxOut(test.serialized,172			&test.stxo,173		)174		if reflect.TypeOf(e) != reflect.TypeOf(test.errType) {175			t.Errorf("decodeSpentTxOut (%s): expected error type "+176				"does not match - got %T, want %T", test.name,177				e, test.errType,178			)179			continue180		}181		// Ensure the expected number of bytes read is returned.182		if gotBytesRead != test.bytesRead {183			t.Errorf("decodeSpentTxOut (%s): unexpected number of "+184				"bytes read - got %d, want %d", test.name,185				gotBytesRead, test.bytesRead,186			)187			continue188		}189	}190}191// TestSpendJournalSerialization ensures serializing and deserializing spend journal entries works as expected.192func TestSpendJournalSerialization(t *testing.T) {193	t.Parallel()194	tests := []struct {195		name       string196		entry      []SpentTxOut197		blockTxns  []*wire.MsgTx198		serialized []byte199	}{200		// From block 2 in main blockchain.201		{202			name:       "No spends",203			entry:      nil,204			blockTxns:  nil,205			serialized: nil,206		},207		// From block 170 in main blockchain.208		{209			name: "One tx with one input spends last output of coinbase",210			entry: []SpentTxOut{{211				Amount:     5000000000,212				PkScript:   hexToBytes("410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac"),213				IsCoinBase: true,214				Height:     9,215			},216			},217			blockTxns: []*wire.MsgTx{{ // Coinbase omitted.218				Version: 1,219				TxIn: []*wire.TxIn{{220					PreviousOutPoint: wire.OutPoint{221						Hash:  *newHashFromStr("0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9"),222						Index: 0,223					},224					SignatureScript: hexToBytes("47304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901"),225					Sequence:        0xffffffff,226				},227				},228				TxOut: []*wire.TxOut{{229					Value:    1000000000,230					PkScript: hexToBytes("4104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac"),231				},232					{233						Value:    4000000000,234						PkScript: hexToBytes("410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac"),235					},236				},237				LockTime: 0,238			},239			},240			serialized: hexToBytes("1300320511db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5c"),241		},242		// Adapted from block 100025 in main blockchain.243		{244			name: "Two txns when one spends last output, one doesn't",245			entry: []SpentTxOut{{246				Amount:     34405000000,247				PkScript:   hexToBytes("76a9146edbc6c4d31bae9f1ccc38538a114bf42de65e8688ac"),248				IsCoinBase: false,249				Height:     100024,250			},251				{252					Amount:     13761000000,253					PkScript:   hexToBytes("76a914b2fb57eadf61e106a100a7445a8c3f67898841ec88ac"),254					IsCoinBase: false,255					Height:     100024,256				},257			},258			blockTxns: []*wire.MsgTx{{ // Coinbase omitted.259				Version: 1,260				TxIn: []*wire.TxIn{{261					PreviousOutPoint: wire.OutPoint{262						Hash:  *newHashFromStr("c0ed017828e59ad5ed3cf70ee7c6fb0f426433047462477dc7a5d470f987a537"),263						Index: 1,264					},265					SignatureScript: hexToBytes("493046022100c167eead9840da4a033c9a56470d7794a9bb1605b377ebe5688499b39f94be59022100fb6345cab4324f9ea0b9ee9169337534834638d818129778370f7d378ee4a325014104d962cac5390f12ddb7539507065d0def320d68c040f2e73337c3a1aaaab7195cb5c4d02e0959624d534f3c10c3cf3d73ca5065ebd62ae986b04c6d090d32627c"),266					Sequence:        0xffffffff,267				},268				},269				TxOut: []*wire.TxOut{{270					Value:    5000000,271					PkScript: hexToBytes("76a914f419b8db4ba65f3b6fcc233acb762ca6f51c23d488ac"),272				},273					{274						Value:    34400000000,275						PkScript: hexToBytes("76a914cadf4fc336ab3c6a4610b75f31ba0676b7f663d288ac"),276					},277				},278				LockTime: 0,279			},280				{281					Version: 1,282					TxIn: []*wire.TxIn{{283						PreviousOutPoint: wire.OutPoint{284							Hash:  *newHashFromStr("92fbe1d4be82f765dfabc9559d4620864b05cc897c4db0e29adac92d294e52b7"),285							Index: 0,286						},287						SignatureScript: hexToBytes("483045022100e256743154c097465cf13e89955e1c9ff2e55c46051b627751dee0144183157e02201d8d4f02cde8496aae66768f94d35ce54465bd4ae8836004992d3216a93a13f00141049d23ce8686fe9b802a7a938e8952174d35dd2c2089d4112001ed8089023ab4f93a3c9fcd5bfeaa9727858bf640dc1b1c05ec3b434bb59837f8640e8810e87742"),288						Sequence:        0xffffffff,289					},290					},291					TxOut: []*wire.TxOut{{292						Value:    5000000,293						PkScript: hexToBytes("76a914a983ad7c92c38fc0e2025212e9f972204c6e687088ac"),294					},295						{296							Value:    13756000000,297							PkScript: hexToBytes("76a914a6ebd69952ab486a7a300bfffdcb395dc7d47c2388ac"),298						},299					},300					LockTime: 0,301				},302			},303			serialized: hexToBytes("8b99700086c64700b2fb57eadf61e106a100a7445a8c3f67898841ec8b99700091f20f006edbc6c4d31bae9f1ccc38538a114bf42de65e86"),304		},305	}306	for i, test := range tests {307		// Ensure the journal entry serializes to the expected value.308		gotBytes := serializeSpendJournalEntry(test.entry)309		if !bytes.Equal(gotBytes, test.serialized) {310			t.Errorf("serializeSpendJournalEntry #%d (%s): "+311				"mismatched bytes - got %x, want %x", i,312				test.name, gotBytes, test.serialized,313			)314			continue315		}316		// Deserialize to a spend journal entry.317		gotEntry, e := deserializeSpendJournalEntry(test.serialized,318			test.blockTxns,319		)320		if e != nil {321			t.Errorf("deserializeSpendJournalEntry #%d (%s) "+322				"unexpected error: %v", i, test.name, e,323			)324			continue325		}326		// Ensure that the deserialized spend journal entry has the327		// correct properties.328		if !reflect.DeepEqual(gotEntry, test.entry) {329			t.Errorf("deserializeSpendJournalEntry #%d (%s) "+330				"mismatched entries - got %v, want %v",331				i, test.name, gotEntry, test.entry,332			)333			continue334		}335	}336}337// TestSpendJournalErrors performs negative tests against deserializing spend journal entries to ensure error paths work338// as expected.339func TestSpendJournalErrors(t *testing.T) {340	t.Parallel()341	tests := []struct {342		name       string343		blockTxns  []*wire.MsgTx344		serialized []byte345		errType    error346	}{347		// Adapted from block 170 in main blockchain.348		{349			name: "Force assertion due to missing stxos",350			blockTxns: []*wire.MsgTx{{ // Coinbase omitted.351				Version: 1,352				TxIn: []*wire.TxIn{{353					PreviousOutPoint: wire.OutPoint{354						Hash:  *newHashFromStr("0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9"),355						Index: 0,356					},357					SignatureScript: hexToBytes("47304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901"),358					Sequence:        0xffffffff,359				},360				},361				LockTime: 0,362			},363			},364			serialized: hexToBytes(""),365			errType:    AssertError(""),366		},367		{368			name: "Force deserialization error in stxos",369			blockTxns: []*wire.MsgTx{{ // Coinbase omitted.370				Version: 1,371				TxIn: []*wire.TxIn{{372					PreviousOutPoint: wire.OutPoint{373						Hash:  *newHashFromStr("0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9"),374						Index: 0,375					},376					SignatureScript: hexToBytes("47304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901"),377					Sequence:        0xffffffff,378				},379				},380				LockTime: 0,381			},382			},383			serialized: hexToBytes("1301320511db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a"),384			errType:    errDeserialize(""),385		},386	}387	for _, test := range tests {388		// Ensure the expected error type is returned and the returned slice is nil.389		stxos, e := deserializeSpendJournalEntry(test.serialized,390			test.blockTxns,391		)392		if reflect.TypeOf(e) != reflect.TypeOf(test.errType) {393			t.Errorf("deserializeSpendJournalEntry (%s): expected "+394				"error type does not match - got %T, want %T",395				test.name, e, test.errType,396			)397			continue398		}399		if stxos != nil {400			t.Errorf("deserializeSpendJournalEntry (%s): returned "+401				"slice of spent transaction outputs is not nil",402				test.name,403			)404			continue405		}406	}407}408// TestUtxoSerialization ensures serializing and deserializing unspent trasaction output entries works as expected.409func TestUtxoSerialization(t *testing.T) {410	t.Parallel()411	tests := []struct {412		name       string413		entry      *UtxoEntry414		serialized []byte415	}{416		// From tx in main blockchain: 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098:0417		{418			name: "height 1, coinbase",419			entry: &UtxoEntry{420				amount:      5000000000,421				pkScript:    hexToBytes("410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac"),422				blockHeight: 1,423				packedFlags: tfCoinBase,424			},425			serialized: hexToBytes("03320496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52"),426		},427		// From tx in main blockchain: 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098:0428		{429			name: "height 1, coinbase, spent",430			entry: &UtxoEntry{431				amount:      5000000000,432				pkScript:    hexToBytes("410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac"),433				blockHeight: 1,434				packedFlags: tfCoinBase | tfSpent,435			},436			serialized: nil,437		},438		// From tx in main blockchain: 8131ffb0a2c945ecaf9b9063e59558784f9c3a74741ce6ae2a18d0571dac15bb:1439		{440			name: "height 100001, not coinbase",441			entry: &UtxoEntry{442				amount:      1000000,443				pkScript:    hexToBytes("76a914ee8bd501094a7d5ca318da2506de35e1cb025ddc88ac"),444				blockHeight: 100001,445				packedFlags: 0,446			},447			serialized: hexToBytes("8b99420700ee8bd501094a7d5ca318da2506de35e1cb025ddc"),448		},449		// From tx in main blockchain: 8131ffb0a2c945ecaf9b9063e59558784f9c3a74741ce6ae2a18d0571dac15bb:1450		{451			name: "height 100001, not coinbase, spent",452			entry: &UtxoEntry{453				amount:      1000000,454				pkScript:    hexToBytes("76a914ee8bd501094a7d5ca318da2506de35e1cb025ddc88ac"),455				blockHeight: 100001,456				packedFlags: tfSpent,457			},458			serialized: nil,459		},460	}461	for i, test := range tests {462		// Ensure the utxo entry serializes to the expected value.463		gotBytes, e := serializeUtxoEntry(test.entry)464		if e != nil {465			t.Errorf("serializeUtxoEntry #%d (%s) unexpected "+466				"error: %v", i, test.name, e,467			)468			continue469		}470		if !bytes.Equal(gotBytes, test.serialized) {471			t.Errorf("serializeUtxoEntry #%d (%s): mismatched "+472				"bytes - got %x, want %x", i, test.name,473				gotBytes, test.serialized,474			)475			continue476		}477		// Don't try to deserialize if the test entry was spent since it will have a nil serialization.478		if test.entry.IsSpent() {479			continue480		}481		// Deserialize to a utxo entry.482		utxoEntry, e := deserializeUtxoEntry(test.serialized)483		if e != nil {484			t.Errorf("deserializeUtxoEntry #%d (%s) unexpected "+485				"error: %v", i, test.name, e,486			)487			continue488		}489		// The deserialized entry must not be marked spent since unspent entries are not serialized.490		if utxoEntry.IsSpent() {491			t.Errorf("deserializeUtxoEntry #%d (%s) output should "+492				"not be marked spent", i, test.name,493			)494			continue495		}496		// Ensure the deserialized entry has the same properties as the ones in the test entry.497		if utxoEntry.Amount() != test.entry.Amount() {498			t.Errorf("deserializeUtxoEntry #%d (%s) mismatched "+499				"amounts: got %d, want %d", i, test.name,500				utxoEntry.Amount(), test.entry.Amount(),501			)502			continue503		}504		if !bytes.Equal(utxoEntry.PkScript(), test.entry.PkScript()) {505			t.Errorf("deserializeUtxoEntry #%d (%s) mismatched "+506				"scripts: got %x, want %x", i, test.name,507				utxoEntry.PkScript(), test.entry.PkScript(),508			)509			continue510		}511		if utxoEntry.BlockHeight() != test.entry.BlockHeight() {512			t.Errorf("deserializeUtxoEntry #%d (%s) mismatched "+513				"block height: got %d, want %d", i, test.name,514				utxoEntry.BlockHeight(), test.entry.BlockHeight(),515			)516			continue517		}518		if utxoEntry.IsCoinBase() != test.entry.IsCoinBase() {519			t.Errorf("deserializeUtxoEntry #%d (%s) mismatched "+520				"coinbase flag: got %v, want %v", i, test.name,521				utxoEntry.IsCoinBase(), test.entry.IsCoinBase(),522			)523			continue524		}525	}526}527// TestUtxoEntryHeaderCodeErrors performs negative tests against unspent transaction output header codes to ensure error528// paths work as expected.529func TestUtxoEntryHeaderCodeErrors(t *testing.T) {530	t.Parallel()531	tests := []struct {532		name  string533		entry *UtxoEntry534		// code    uint64535		errType error536	}{537		{538			name:    "Force assertion due to spent output",539			entry:   &UtxoEntry{packedFlags: tfSpent},540			errType: AssertError(""),541		},542	}543	for _, test := range tests {544		// Ensure the expected error type is returned and the code is 0.545		code, e := utxoEntryHeaderCode(test.entry)546		if reflect.TypeOf(e) != reflect.TypeOf(test.errType) {547			t.Errorf("utxoEntryHeaderCode (%s): expected error "+548				"type does not match - got %T, want %T",549				test.name, e, test.errType,550			)551			continue552		}553		if code != 0 {554			t.Errorf("utxoEntryHeaderCode (%s): unexpected code "+555				"on error - got %d, want 0", test.name, code,556			)557			continue558		}559	}560}561// TestUtxoEntryDeserializeErrors performs negative tests against deserializing unspent transaction outputs to ensure562// error paths work as expected.563func TestUtxoEntryDeserializeErrors(t *testing.T) {564	t.Parallel()565	tests := []struct {566		name       string567		serialized []byte568		errType    error569	}{570		{571			name:       "no data after header code",572			serialized: hexToBytes("02"),573			errType:    errDeserialize(""),574		},575		{576			name:       "incomplete compressed txout",577			serialized: hexToBytes("0232"),578			errType:    errDeserialize(""),579		},580	}581	for _, test := range tests {582		// Ensure the expected error type is returned and the returned entry is nil.583		entry, e := deserializeUtxoEntry(test.serialized)584		if reflect.TypeOf(e) != reflect.TypeOf(test.errType) {585			t.Errorf("deserializeUtxoEntry (%s): expected error "+586				"type does not match - got %T, want %T",587				test.name, e, test.errType,588			)589			continue590		}591		if entry != nil {592			t.Errorf("deserializeUtxoEntry (%s): returned entry "+593				"is not nil", test.name,594			)595			continue596		}597	}598}599// // TestBestChainStateSerialization ensures serializing and deserializing the best chain state works as expected.600// func TestBestChainStateSerialization(// 	t *testing.T) {601// 	t.Parallel()602// 	workSum := new(big.Int)603// 	tests := []struct {604// 		name       string605// 		state      bestChainState606// 		serialized []byte607// 	}{608// 		{609// 			name: "genesis",610// 			state: bestChainState{611// 				hash:      *newHashFromStr("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"),612// 				height:    0,613// 				totalTxns: 1,614// 				workSum: func() *big.Int {615// 					workSum.Add(workSum, CalcWork(486604799, 0, 2))616// 					return new(big.Int).Set(workSum)617// 				}(),618// 				// 0x0100010001619// 			},620// 			serialized: hexToBytes("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000100000000000000050000000100010001"),621// 		},622// 		{623// 			name: "block 1",624// 			state: bestChainState{625// 				hash:      *newHashFromStr("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"),626// 				height:    1,627// 				totalTxns: 2,628// 				workSum: func() *big.Int {629// 					workSum.Add(workSum, CalcWork(486604799, 1, 2))630// 					return new(big.Int).Set(workSum)631// 				}(),632// 				// 0x0200020002633// 			},634// 			serialized: hexToBytes("4860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300000000010000000200000000000000050000000200020002"),635// 		},636// 	}637// 	for i, test := range tests {638// 		// Ensure the state serializes to the expected value.639// 		gotBytes := serializeBestChainState(test.state)640// 		if !bytes.Equal(gotBytes, test.serialized) {641// 			t.Errorf("serializeBestChainState #%d (%s): mismatched "+642// 				"bytes - got %x, want %x", i, test.name,643// 				gotBytes, test.serialized)644// 			continue645// 		}646// 		// Ensure the serialized bytes are decoded back to the expected state.647// 		state, e := deserializeBestChainState(test.serialized)648// 		if e != nil  {649// 			t.Errorf("deserializeBestChainState #%d (%s) "+650// 				"unexpected error: %v", i, test.name, e)651// 			continue652// 		}653// 		if !reflect.DeepEqual(state, test.state) {654// 			t.Errorf("deserializeBestChainState #%d (%s) "+655// 				"mismatched state - got %v, want %v", i,656// 				test.name, state, test.state)657// 			continue658// 		}659// 	}660// }661// TestBestChainStateDeserializeErrors performs negative tests against deserializing the chain state to ensure error662// paths work as expected.663func TestBestChainStateDeserializeErrors(t *testing.T) {664	t.Parallel()665	tests := []struct {666		name       string667		serialized []byte668		errType    error669	}{670		{671			name:       "nothing serialized",672			serialized: hexToBytes(""),673			errType:    database.DBError{ErrorCode: database.ErrCorruption},674		},675		{676			name:       "short data in hash",677			serialized: hexToBytes("0000"),678			errType:    database.DBError{ErrorCode: database.ErrCorruption},679		},680		{681			name:       "short data in work sum",682			serialized: hexToBytes("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000001000000000000000500000001000100"),683			errType:    database.DBError{ErrorCode: database.ErrCorruption},684		},685	}686	for _, test := range tests {687		// Ensure the expected error type and code is returned.688		_, e := deserializeBestChainState(test.serialized)689		if reflect.TypeOf(e) != reflect.TypeOf(test.errType) {690			t.Errorf("deserializeBestChainState (%s): expected "+691				"error type does not match - got %T, want %T",692				test.name, e, test.errType,693			)694			continue695		}696		if derr, ok := e.(database.DBError); ok {...

Full Screen

Full Screen

compress_test.go

Source:compress_test.go Github

copy

Full Screen

...3	"bytes"4	"encoding/hex"5	"testing"6)7// hexToBytes converts the passed hex string into bytes and will panic if there is an error. This is only provided for8// the hard-coded constants so errors in the source code can be detected. It will only (and must only) be called with9// hard-coded values.10func hexToBytes(s string) []byte {11	b, e := hex.DecodeString(s)12	if e != nil {13		panic("invalid hex in source file: " + s)14	}15	return b16}17// TestVLQ ensures the variable length quantity serialization, deserialization, and size calculation works as expected.18func TestVLQ(t *testing.T) {19	t.Parallel()20	tests := []struct {21		val        uint6422		serialized []byte23	}{24		{0, hexToBytes("00")},25		{1, hexToBytes("01")},26		{127, hexToBytes("7f")},27		{128, hexToBytes("8000")},28		{129, hexToBytes("8001")},29		{255, hexToBytes("807f")},30		{256, hexToBytes("8100")},31		{16383, hexToBytes("fe7f")},32		{16384, hexToBytes("ff00")},33		{16511, hexToBytes("ff7f")}, // Max 2-byte value34		{16512, hexToBytes("808000")},35		{16513, hexToBytes("808001")},36		{16639, hexToBytes("80807f")},37		{32895, hexToBytes("80ff7f")},38		{2113663, hexToBytes("ffff7f")}, // Max 3-byte value39		{2113664, hexToBytes("80808000")},40		{270549119, hexToBytes("ffffff7f")}, // Max 4-byte value41		{270549120, hexToBytes("8080808000")},42		{2147483647, hexToBytes("86fefefe7f")},43		{2147483648, hexToBytes("86fefeff00")},44		{4294967295, hexToBytes("8efefefe7f")}, // Max uint32, 5 bytes45		// Max uint64, 10 bytes46		{18446744073709551615, hexToBytes("80fefefefefefefefe7f")},47	}48	for _, test := range tests {49		// Ensure the function to calculate the serialized size without actually serializing the value is calculated50		// properly.51		gotSize := serializeSizeVLQ(test.val)52		if gotSize != len(test.serialized) {53			t.Errorf("serializeSizeVLQ: did not get expected size "+54				"for %d - got %d, want %d", test.val, gotSize,55				len(test.serialized),56			)57			continue58		}59		// Ensure the value serializes to the expected bytes.60		gotBytes := make([]byte, gotSize)61		gotBytesWritten := putVLQ(gotBytes, test.val)62		if !bytes.Equal(gotBytes, test.serialized) {63			t.Errorf("putVLQUnchecked: did not get expected bytes "+64				"for %d - got %x, want %x", test.val, gotBytes,65				test.serialized,66			)67			continue68		}69		if gotBytesWritten != len(test.serialized) {70			t.Errorf("putVLQUnchecked: did not get expected number "+71				"of bytes written for %d - got %d, want %d",72				test.val, gotBytesWritten, len(test.serialized),73			)74			continue75		}76		// Ensure the serialized bytes deserialize to the expected value.77		gotVal, gotBytesRead := deserializeVLQ(test.serialized)78		if gotVal != test.val {79			t.Errorf("deserializeVLQ: did not get expected value "+80				"for %x - got %d, want %d", test.serialized,81				gotVal, test.val,82			)83			continue84		}85		if gotBytesRead != len(test.serialized) {86			t.Errorf("deserializeVLQ: did not get expected number "+87				"of bytes read for %d - got %d, want %d",88				test.serialized, gotBytesRead,89				len(test.serialized),90			)91			continue92		}93	}94}95// TestScriptCompression ensures the domain-specific script compression and decompression works as expected.96func TestScriptCompression(t *testing.T) {97	t.Parallel()98	tests := []struct {99		name         string100		uncompressed []byte101		compressed   []byte102	}{103		{104			name:         "nil",105			uncompressed: nil,106			compressed:   hexToBytes("06"),107		},108		{109			name:         "pay-to-pubkey-hash 1",110			uncompressed: hexToBytes("76a9141018853670f9f3b0582c5b9ee8ce93764ac32b9388ac"),111			compressed:   hexToBytes("001018853670f9f3b0582c5b9ee8ce93764ac32b93"),112		},113		{114			name:         "pay-to-pubkey-hash 2",115			uncompressed: hexToBytes("76a914e34cce70c86373273efcc54ce7d2a491bb4a0e8488ac"),116			compressed:   hexToBytes("00e34cce70c86373273efcc54ce7d2a491bb4a0e84"),117		},118		{119			name:         "pay-to-script-hash 1",120			uncompressed: hexToBytes("a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87"),121			compressed:   hexToBytes("01da1745e9b549bd0bfa1a569971c77eba30cd5a4b"),122		},123		{124			name:         "pay-to-script-hash 2",125			uncompressed: hexToBytes("a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087"),126			compressed:   hexToBytes("01f815b036d9bbbce5e9f2a00abd1bf3dc91e95510"),127		},128		{129			name:         "pay-to-pubkey compressed 0x02",130			uncompressed: hexToBytes("2102192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4ac"),131			compressed:   hexToBytes("02192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4"),132		},133		{134			name:         "pay-to-pubkey compressed 0x03",135			uncompressed: hexToBytes("2103b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65ac"),136			compressed:   hexToBytes("03b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65"),137		},138		{139			name:         "pay-to-pubkey uncompressed 0x04 even",140			uncompressed: hexToBytes("4104192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b40d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453eac"),141			compressed:   hexToBytes("04192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4"),142		},143		{144			name:         "pay-to-pubkey uncompressed 0x04 odd",145			uncompressed: hexToBytes("410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac"),146			compressed:   hexToBytes("0511db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5c"),147		},148		{149			name:         "pay-to-pubkey invalid pubkey",150			uncompressed: hexToBytes("3302aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac"),151			compressed:   hexToBytes("293302aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac"),152		},153		{154			name:         "null data",155			uncompressed: hexToBytes("6a200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"),156			compressed:   hexToBytes("286a200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"),157		},158		{159			name:         "requires 2 size bytes - data push 200 bytes",160			uncompressed: append(hexToBytes("4cc8"), bytes.Repeat([]byte{0x00}, 200)...),161			// [0x80, 0x50] = 208 as a variable length quantity162			// [0x4c, 0xc8] = OP_PUSHDATA1 200163			compressed: append(hexToBytes("80504cc8"), bytes.Repeat([]byte{0x00}, 200)...),164		},165	}166	for _, test := range tests {167		// Ensure the function to calculate the serialized size without actually serializing the value is calculated168		// properly.169		gotSize := compressedScriptSize(test.uncompressed)170		if gotSize != len(test.compressed) {171			t.Errorf("compressedScriptSize (%s): did not get "+172				"expected size - got %d, want %d", test.name,173				gotSize, len(test.compressed),174			)175			continue176		}177		// Ensure the script compresses to the expected bytes.178		gotCompressed := make([]byte, gotSize)179		gotBytesWritten := putCompressedScript(gotCompressed,180			test.uncompressed,181		)182		if !bytes.Equal(gotCompressed, test.compressed) {183			t.Errorf("putCompressedScript (%s): did not get "+184				"expected bytes - got %x, want %x", test.name,185				gotCompressed, test.compressed,186			)187			continue188		}189		if gotBytesWritten != len(test.compressed) {190			t.Errorf("putCompressedScript (%s): did not get "+191				"expected number of bytes written - got %d, "+192				"want %d", test.name, gotBytesWritten,193				len(test.compressed),194			)195			continue196		}197		// Ensure the compressed script size is properly decoded from the compressed script.198		gotDecodedSize := decodeCompressedScriptSize(test.compressed)199		if gotDecodedSize != len(test.compressed) {200			t.Errorf("decodeCompressedScriptSize (%s): did not get "+201				"expected size - got %d, want %d", test.name,202				gotDecodedSize, len(test.compressed),203			)204			continue205		}206		// Ensure the script decompresses to the expected bytes.207		gotDecompressed := decompressScript(test.compressed)208		if !bytes.Equal(gotDecompressed, test.uncompressed) {209			t.Errorf("decompressScript (%s): did not get expected "+210				"bytes - got %x, want %x", test.name,211				gotDecompressed, test.uncompressed,212			)213			continue214		}215	}216}217// TestScriptCompressionErrors ensures calling various functions related to script compression with incorrect data218// returns the expected results.219func TestScriptCompressionErrors(t *testing.T) {220	t.Parallel()221	// A nil script must result in a decoded size of 0.222	if gotSize := decodeCompressedScriptSize(nil); gotSize != 0 {223		t.Fatalf("decodeCompressedScriptSize with nil script did not "+224			"return 0 - got %d", gotSize,225		)226	}227	// A nil script must result in a nil decompressed script.228	if gotScript := decompressScript(nil); gotScript != nil {229		t.Fatalf("decompressScript with nil script did not return nil "+230			"decompressed script - got %x", gotScript,231		)232	}233	// A compressed script for a pay-to-pubkey (uncompressed) that results in an invalid pubkey must result in a nil234	// decompressed script.235	compressedScript := hexToBytes("04012d74d0cb94344c9569c2e77901573d8d" +236		"7903c3ebec3a957724895dca52c6b4",237	)238	if gotScript := decompressScript(compressedScript); gotScript != nil {239		t.Fatalf("decompressScript with compressed pay-to-"+240			"uncompressed-pubkey that is invalid did not return "+241			"nil decompressed script - got %x", gotScript,242		)243	}244}245// TestAmountCompression ensures the domain-specific transaction output amount compression and decompression works as246// expected.247func TestAmountCompression(t *testing.T) {248	t.Parallel()249	tests := []struct {250		name         string251		uncompressed uint64252		compressed   uint64253	}{254		{255			name:         "0 DUO (sometimes used in nulldata)",256			uncompressed: 0,257			compressed:   0,258		},259		{260			name:         "546 Satoshi (current network dust value)",261			uncompressed: 546,262			compressed:   4911,263		},264		{265			name:         "0.00001 DUO (typical transaction fee)",266			uncompressed: 1000,267			compressed:   4,268		},269		{270			name:         "0.0001 DUO (typical transaction fee)",271			uncompressed: 10000,272			compressed:   5,273		},274		{275			name:         "0.12345678 DUO",276			uncompressed: 12345678,277			compressed:   111111101,278		},279		{280			name:         "0.5 DUO",281			uncompressed: 50000000,282			compressed:   48,283		},284		{285			name:         "1 DUO",286			uncompressed: 100000000,287			compressed:   9,288		},289		{290			name:         "5 DUO",291			uncompressed: 500000000,292			compressed:   49,293		},294		{295			name:         "21000000 DUO (max minted coins)",296			uncompressed: 2100000000000000,297			compressed:   21000000,298		},299	}300	for _, test := range tests {301		// Ensure the amount compresses to the expected value.302		gotCompressed := compressTxOutAmount(test.uncompressed)303		if gotCompressed != test.compressed {304			t.Errorf("compressTxOutAmount (%s): did not get "+305				"expected value - got %d, want %d", test.name,306				gotCompressed, test.compressed,307			)308			continue309		}310		// Ensure the value decompresses to the expected value.311		gotDecompressed := decompressTxOutAmount(test.compressed)312		if gotDecompressed != test.uncompressed {313			t.Errorf("decompressTxOutAmount (%s): did not get "+314				"expected value - got %d, want %d", test.name,315				gotDecompressed, test.uncompressed,316			)317			continue318		}319	}320}321// TestCompressedTxOut ensures the transaction output serialization and deserialization works as expected.322func TestCompressedTxOut(t *testing.T) {323	t.Parallel()324	tests := []struct {325		name       string326		amount     uint64327		pkScript   []byte328		compressed []byte329	}{330		{331			name:       "nulldata with 0 DUO",332			amount:     0,333			pkScript:   hexToBytes("6a200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"),334			compressed: hexToBytes("00286a200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"),335		},336		{337			name:       "pay-to-pubkey-hash dust",338			amount:     546,339			pkScript:   hexToBytes("76a9141018853670f9f3b0582c5b9ee8ce93764ac32b9388ac"),340			compressed: hexToBytes("a52f001018853670f9f3b0582c5b9ee8ce93764ac32b93"),341		},342		{343			name:       "pay-to-pubkey uncompressed 1 DUO",344			amount:     100000000,345			pkScript:   hexToBytes("4104192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b40d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453eac"),346			compressed: hexToBytes("0904192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4"),347		},348	}349	for _, test := range tests {350		// Ensure the function to calculate the serialized size without actually serializing the txout is calculated351		// properly.352		gotSize := compressedTxOutSize(test.amount, test.pkScript)353		if gotSize != len(test.compressed) {354			t.Errorf("compressedTxOutSize (%s): did not get "+355				"expected size - got %d, want %d", test.name,356				gotSize, len(test.compressed),357			)358			continue359		}360		// Ensure the txout compresses to the expected value.361		gotCompressed := make([]byte, gotSize)362		gotBytesWritten := putCompressedTxOut(gotCompressed,363			test.amount, test.pkScript,364		)365		if !bytes.Equal(gotCompressed, test.compressed) {366			t.Errorf("compressTxOut (%s): did not get expected "+367				"bytes - got %x, want %x", test.name,368				gotCompressed, test.compressed,369			)370			continue371		}372		if gotBytesWritten != len(test.compressed) {373			t.Errorf("compressTxOut (%s): did not get expected "+374				"number of bytes written - got %d, want %d",375				test.name, gotBytesWritten,376				len(test.compressed),377			)378			continue379		}380		// Ensure the serialized bytes are decoded back to the expected uncompressed values.381		gotAmount, gotScript, gotBytesRead, e := decodeCompressedTxOut(382			test.compressed,383		)384		if e != nil {385			t.Errorf("decodeCompressedTxOut (%s): unexpected "+386				"error: %v", test.name, e,387			)388			continue389		}390		if gotAmount != test.amount {391			t.Errorf("decodeCompressedTxOut (%s): did not get "+392				"expected amount - got %d, want %d",393				test.name, gotAmount, test.amount,394			)395			continue396		}397		if !bytes.Equal(gotScript, test.pkScript) {398			t.Errorf("decodeCompressedTxOut (%s): did not get "+399				"expected script - got %x, want %x",400				test.name, gotScript, test.pkScript,401			)402			continue403		}404		if gotBytesRead != len(test.compressed) {405			t.Errorf("decodeCompressedTxOut (%s): did not get "+406				"expected number of bytes read - got %d, want %d",407				test.name, gotBytesRead, len(test.compressed),408			)409			continue410		}411	}412}413// TestTxOutCompressionErrors ensures calling various functions related to txout compression with incorrect data returns414// the expected results.415func TestTxOutCompressionErrors(t *testing.T) {416	t.Parallel()417	// A compressed txout with missing compressed script must error.418	compressedTxOut := hexToBytes("00")419	_, _, _, e := decodeCompressedTxOut(compressedTxOut)420	if !isDeserializeErr(e) {421		t.Fatalf("decodeCompressedTxOut with missing compressed script "+422			"did not return expected error type - got %T, want "+423			"errDeserialize", e,424		)425	}426	// A compressed txout with short compressed script must error.427	compressedTxOut = hexToBytes("0010")428	_, _, _, e = decodeCompressedTxOut(compressedTxOut)429	if !isDeserializeErr(e) {430		t.Fatalf("decodeCompressedTxOut with short compressed script "+431			"did not return expected error type - got %T, want "+432			"errDeserialize", e,433		)434	}435}...

Full Screen

Full Screen

encode_test.go

Source:encode_test.go Github

copy

Full Screen

...306	if got != want {307		t.Fatalf("Marshal: got %s want %s", got, want)308	}309}310func TestStringBytes(t *testing.T) {311	// Test that encodeState.stringBytes and encodeState.string use the same encoding.312	es := &encodeState{}313	var r []rune314	for i := '\u0000'; i <= unicode.MaxRune; i++ {315		r = append(r, i)316	}317	s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too318	_, err := es.string(s)319	if err != nil {320		t.Fatal(err)321	}322	esBytes := &encodeState{}323	_, err = esBytes.stringBytes([]byte(s))324	if err != nil {325		t.Fatal(err)326	}327	enc := es.Buffer.String()328	encBytes := esBytes.Buffer.String()329	if enc != encBytes {330		i := 0331		for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {332			i++333		}334		enc = enc[i:]335		encBytes = encBytes[i:]336		i = 0337		for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {338			i++339		}340		enc = enc[:len(enc)-i]341		encBytes = encBytes[:len(encBytes)-i]342		if len(enc) > 20 {343			enc = enc[:20] + "..."344		}345		if len(encBytes) > 20 {346			encBytes = encBytes[:20] + "..."347		}348		t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)349	}350}351func TestIssue6458(t *testing.T) {352	type Foo struct {353		M RawMessage354	}355	x := Foo{RawMessage(`"foo"`)}356	b, err := Marshal(&x)357	if err != nil {358		t.Fatal(err)359	}360	if want := `{"M":"foo"}`; string(b) != want {361		t.Errorf("Marshal(&x) = %#q; want %#q", b, want)362	}...

Full Screen

Full Screen

Bytes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	r := gin.Default()4	r.POST("/post", func(c *gin.Context) {5		value, _ := ioutil.ReadAll(body)6		fmt.Println(string(value))7		c.String(200, "success")8	})9	r.Run(":8080")10}11import (12type Post struct {13}14func main() {15	r := gin.Default()16	r.POST("/post", func(c *gin.Context) {17		if err := c.ShouldBindBodyWith(&post, binding.JSON); err == nil {18			fmt.Println(post.Id)19			fmt.Println(post.Name)20			c.String(200, "success")21		} else {22			c.String(500, "error")23		}24	})25	r.Run(":8080")26}27import (28type Post struct {29}30func main() {31	r := gin.Default()32	r.POST("/post", func(c *gin.Context) {33		if err := c.ShouldBindJSON(&post); err == nil {34			fmt.Println(post.Id)35			fmt.Println(post.Name)36			c.String(200, "success")37		} else {38			c.String(500, "error")39		}40	})41	r.Run(":8080")42}43import (44type Post struct {45}46func main() {47	r := gin.Default()48	r.POST("/post", func(c *gin.Context) {49		if err := c.ShouldBindXML(&post); err == nil {50			fmt.Println(post.Id)51			fmt.Println(post.Name)52			c.String(200, "success")53		} else {

Full Screen

Full Screen

Bytes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    i, err := strconv.Atoi(s)4    if err != nil {5        fmt.Println(err)6    }7    fmt.Println(i)8}9import (10func main() {11    i, err := strconv.Atoi(s)12    if err != nil {13        fmt.Println(err)14    }15    fmt.Println(i)16}17import (18func main() {19    i, err := strconv.Atoi(s)20    if err != nil {21        fmt.Println(err)22    }23    fmt.Println(i)24}25import (26func main() {27    i, err := strconv.Atoi(s)28    if err != nil {29        fmt.Println(err)30    }31    fmt.Println(i)32}33import (34func main() {35    i, err := strconv.Atoi(s)36    if err != nil {37        fmt.Println(err)38    }39    fmt.Println(i)40}41import (42func main() {43    i, err := strconv.Atoi(s)44    if err != nil {45        fmt.Println(err)46    }47    fmt.Println(i)48}49import (50func main() {51    i, err := strconv.Atoi(s)52    if err != nil {53        fmt.Println(err)54    }55    fmt.Println(i)56}57import (

Full Screen

Full Screen

Bytes

Using AI Code Generation

copy

Full Screen

1func main() {2    got = Got{[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}3    fmt.Printf("%v4", got.Bytes())5}6func main() {7    got = Got{[9]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}8    fmt.Printf("%v9", got.Bytes())10}

Full Screen

Full Screen

Bytes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3parser := argparse.NewParser("1", "bytes")4num := parser.Int("n", "num", &argparse.Options{Required: true, Help: "num of bytes"})5err := parser.Parse(os.Args)6if err != nil {7fmt.Print(parser.Usage(err))8}9fmt.Println(num)10}11import (12func main() {13parser := argparse.NewParser("2", "bytes")14num := parser.Int("n", "num", &argparse.Options{Required: true, Help: "num of bytes"})15err := parser.Parse(os.Args)16if err != nil {17fmt.Print(parser.Usage(err))18}19fmt.Println(num)20}21import (22func main() {23parser := argparse.NewParser("3", "bytes")24num := parser.Int("n", "num", &argparse.Options{Required: true, Help: "num of bytes"})25err := parser.Parse(os.Args)26if err != nil {27fmt.Print(parser.Usage(err))28}29fmt.Println(num)30}31import (32func main() {33parser := argparse.NewParser("4", "bytes")34num := parser.Int("n", "num", &argparse.Options{Required: true, Help: "num of bytes"})35err := parser.Parse(os.Args)36if err != nil {37fmt.Print(parser.Usage(err))38}39fmt.Println(num)40}41import (42func main() {43parser := argparse.NewParser("5", "bytes")44num := parser.Int("n", "num", &argparse.Options{Required: true, Help: "num of bytes"})45err := parser.Parse(os.Args)46if err != nil {47fmt.Print(parser.Usage(err))48}49fmt.Println(num)50}

Full Screen

Full Screen

Bytes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println(got.Bytes("Hello"))4}5import (6func main() {7	fmt.Println(got.string([]byte("Hello")))8}9import (10func main() {11	fmt.Println(got.string(100))12}13import (14func main() {15	fmt.Println(got.Int("100"))16}17import (18func main() {19	fmt.Println(got.Float("100"))20}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful