func TestParsePktLen(t *testing.T) { tests := []struct { in string wantLen int wantErr error }{ // Valid pkt-len. {"00a5", 165, nil}, {"01a5", 421, nil}, {"0032", 50, nil}, {"000b", 11, nil}, {"000B", 11, nil}, // Valud flush-pkt. {"0000", 0, nil}, {"0001", 0, errors.New("invalid pkt-len: 1")}, {"0003", 0, errors.New("invalid pkt-len: 3")}, {"abyz", 0, hex.InvalidByteError('y')}, {"-<%^", 0, hex.InvalidByteError('-')}, // Maximum length. {"fff4", 65524, nil}, {"fff5", 0, errors.New("invalid pkt-len: 65525")}, {"ffff", 0, errors.New("invalid pkt-len: 65535")}, } for _, tt := range tests { gotLen, gotErr := parsePktLen([]byte(tt.in)) if gotLen != tt.wantLen || !reflect.DeepEqual(gotErr, tt.wantErr) { t.Errorf("test %q:\n got: %#v, %#v\nwant: %#v, %#v\n", tt.in, gotLen, gotErr, tt.wantLen, tt.wantErr) } } }
// ParseHex creates a UUID object from given hex string // representation. Function accepts UUID string in following // formats: // // uuid.ParseHex("6ba7b814-9dad-11d1-80b4-00c04fd430c8") // uuid.ParseHex("{6ba7b814-9dad-11d1-80b4-00c04fd430c8}") // uuid.ParseHex("urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8") // func ParseHex(s string) (u *UUID, err error) { if strings.HasPrefix(s, urnUuidPrefix) { s = s[len(urnUuidPrefix):] } if strings.HasPrefix(s, "{") { if !strings.HasSuffix(s, "}") { err = errors.New("Invalid UUID string has an opening '{' bracket but no closing '}' bracket") return } else if len(s) != 38 { err = errors.New("Invalid UUID string") return } s = s[1:37] } else if strings.HasSuffix(s, "}") { err = errors.New("Invalid UUID string has no opening '{' bracket but does have a closing '}' bracket") return } else if len(s) != 36 { err = errors.New("Invalid UUID string") return } var a, b byte var half bool u = new(UUID) v := u[0:0] for i, c := range []byte(s) { if i == 8 || i == 13 || i == 18 || i == 23 { if c != dash { return nil, errors.New("Invalid UUID string had an improper character where '-' expected") } continue } switch { case '0' <= c && c <= '9': b = c - '0' case 'a' <= c && c <= 'f': b = c - 'a' + 10 default: return nil, hex.InvalidByteError(i) } if half { v = append(v, (a<<4)|b) } else { a = b } half = !half } return }
// TestNewShaHashFromStr executes tests against the NewShaHashFromStr function. func TestNewShaHashFromStr(t *testing.T) { tests := []struct { in string want btcwire.ShaHash err error }{ // Genesis hash. { "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", btcwire.GenesisHash, nil, }, // Genesis hash with stripped leading zeros. { "19d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", btcwire.GenesisHash, nil, }, // Single digit hash. { "1", btcwire.ShaHash{ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, nil, }, // Block 203707 with stripped leading zeros. { "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc", btcwire.ShaHash{ 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7, 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b, 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b, 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, nil, }, // Hash string that is too long. { "01234567890123456789012345678901234567890123456789012345678912345", btcwire.ShaHash{}, btcwire.ErrHashStrSize, }, // Hash string that is contains non-hex chars. { "abcdefg", btcwire.ShaHash{}, hex.InvalidByteError('g'), }, } unexpectedErrStr := "NewShaHashFromStr #%d failed to detect expected error - got: %v want: %v" unexpectedResultStr := "NewShaHashFromStr #%d got: %v want: %v" t.Logf("Running %d tests", len(tests)) for i, test := range tests { result, err := btcwire.NewShaHashFromStr(test.in) if err != test.err { t.Errorf(unexpectedErrStr, i, err, test.err) continue } else if err != nil { // Got expected error. Move on to the next test. continue } if !test.want.IsEqual(result) { t.Errorf(unexpectedResultStr, i, result, &test.want) continue } } }