Skip to content

Commit

Permalink
reformat go/mysql
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Jul 6, 2024
1 parent c98c0a4 commit 513e457
Show file tree
Hide file tree
Showing 120 changed files with 4,806 additions and 3,348 deletions.
1 change: 0 additions & 1 deletion go/mysql/auth_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ func (n *mysqlCachingSha2AuthMethod) HandleAuthPluginData(c *Conn, user string,

salt := serverAuthPluginData[:len(serverAuthPluginData)-1]
result, cacheState, err := n.cache.UserEntryWithCacheHash(c, salt, user, clientAuthPluginData, remoteAddr)

if err != nil {
return nil, err
}
Expand Down
14 changes: 4 additions & 10 deletions go/mysql/auth_server_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func TestJsonConfigParser(t *testing.T) {
}`
err = ParseConfig([]byte(jsonConfig), &config)
require.Error(t, err, "Invalid config should have errored, but didn't")

}

func TestValidateHashGetter(t *testing.T) {
Expand Down Expand Up @@ -101,7 +100,6 @@ func TestHostMatcher(t *testing.T) {
socket := &net.UnixAddr{Name: "unixSocket", Net: "1"}
match = MatchSourceHost(net.Addr(socket), "localhost")
require.True(t, match, "Should match socket when target is localhost")

}

func TestStaticConfigHUP(t *testing.T) {
Expand All @@ -112,7 +110,7 @@ func TestStaticConfigHUP(t *testing.T) {

oldStr := "str5"
jsonConfig := fmt.Sprintf("{\"%s\":[{\"Password\":\"%s\"}]}", oldStr, oldStr)
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0o600); err != nil {
t.Fatalf("couldn't write temp file: %v", err)
}

Expand All @@ -137,7 +135,7 @@ func TestStaticConfigHUPWithRotation(t *testing.T) {

oldStr := "str1"
jsonConfig := fmt.Sprintf("{\"%s\":[{\"Password\":\"%s\"}]}", oldStr, oldStr)
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0o600); err != nil {
t.Fatalf("couldn't write temp file: %v", err)
}

Expand All @@ -151,7 +149,7 @@ func TestStaticConfigHUPWithRotation(t *testing.T) {

func hupTest(t *testing.T, aStatic *AuthServerStatic, tmpFile *os.File, oldStr, newStr string) {
jsonConfig := fmt.Sprintf("{\"%s\":[{\"Password\":\"%s\"}]}", newStr, newStr)
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0o600); err != nil {
t.Fatalf("couldn't overwrite temp file: %v", err)
}
require.Equal(t, oldStr, aStatic.getEntries()[oldStr][0].Password, "%s's Password should still be '%s'", oldStr, oldStr)
Expand All @@ -161,20 +159,18 @@ func hupTest(t *testing.T, aStatic *AuthServerStatic, tmpFile *os.File, oldStr,
// wait for signal handler
require.Nil(t, aStatic.getEntries()[oldStr], "Should not have old %s after config reload", oldStr)
require.Equal(t, newStr, aStatic.getEntries()[newStr][0].Password, "%s's Password should be '%s'", newStr, newStr)

}

func hupTestWithRotation(t *testing.T, aStatic *AuthServerStatic, tmpFile *os.File, oldStr, newStr string) {
jsonConfig := fmt.Sprintf("{\"%s\":[{\"Password\":\"%s\"}]}", newStr, newStr)
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0o600); err != nil {
t.Fatalf("couldn't overwrite temp file: %v", err)
}

time.Sleep(20 * time.Millisecond)
// wait for signal handler
require.Nil(t, aStatic.getEntries()[oldStr], "Should not have old %s after config reload", oldStr)
require.Equal(t, newStr, aStatic.getEntries()[newStr][0].Password, "%s's Password should be '%s'", newStr, newStr)

}

func TestStaticPasswords(t *testing.T) {
Expand Down Expand Up @@ -233,10 +229,8 @@ func TestStaticPasswords(t *testing.T) {

if c.success {
require.NoError(t, err, "authentication should have succeeded: %v", err)

} else {
require.Error(t, err, "authentication should have failed")

}
})
}
Expand Down
12 changes: 8 additions & 4 deletions go/mysql/auth_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func TestVerifyHashedMysqlNativePassword(t *testing.T) {
password := "secret"

// Double SHA1 of "secret"
passwordHash := []byte{0x38, 0x81, 0x21, 0x9d, 0x08, 0x7d, 0xd9, 0xc6, 0x34, 0x37, 0x3f, 0xd3,
passwordHash := []byte{
0x38, 0x81, 0x21, 0x9d, 0x08, 0x7d, 0xd9, 0xc6, 0x34, 0x37, 0x3f, 0xd3,
0x3d, 0xfa, 0x33, 0xa2, 0xcb, 0x6b, 0xfc, 0x6c, 0x52, 0x0b, 0x64, 0xb8,
0xbb, 0x60, 0xef, 0x2c, 0xeb, 0x53, 0x4a, 0xe7}
0xbb, 0x60, 0xef, 0x2c, 0xeb, 0x53, 0x4a, 0xe7,
}

reply := ScrambleCachingSha2Password(salt, []byte(password))

Expand All @@ -44,8 +46,10 @@ func TestVerifyHashedCachingSha2Password(t *testing.T) {
password := "secret"

// Double SHA1 of "secret"
passwordHash := []byte{0x14, 0xe6, 0x55, 0x67, 0xab, 0xdb, 0x51, 0x35, 0xd0, 0xcf, 0xd9, 0xa7,
0x0b, 0x30, 0x32, 0xc1, 0x79, 0xa4, 0x9e, 0xe7}
passwordHash := []byte{
0x14, 0xe6, 0x55, 0x67, 0xab, 0xdb, 0x51, 0x35, 0xd0, 0xcf, 0xd9, 0xa7,
0x0b, 0x30, 0x32, 0xc1, 0x79, 0xa4, 0x9e, 0xe7,
}

reply := ScrambleMysqlNativePassword(salt, []byte(password))
assert.True(t, VerifyHashedMysqlNativePassword(reply, salt, passwordHash), "password hash mismatch")
Expand Down
15 changes: 7 additions & 8 deletions go/mysql/binlog/binlog_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ const (
jsonInt32 = 7
jsonUint32 = 8
jsonInt64 = 9
jsonUint64 = 10 //0x0a
jsonDouble = 11 //0x0b
jsonString = 12 //0x0c a utf8mb4 string
jsonOpaque = 15 //0x0f "custom" data
jsonUint64 = 10 // 0x0a
jsonDouble = 11 // 0x0b
jsonString = 12 // 0x0c a utf8mb4 string
jsonOpaque = 15 // 0x0f "custom" data
)

// literals in the binary json format can be one of three types: null, true, false
Expand Down Expand Up @@ -200,7 +200,7 @@ func binparserElement(data []byte, pos int, large bool) (*json.Value, int, error
return nil, 0, fmt.Errorf("unable to decode element: %+v", data)
}
newData := data[offset:]
//newPos ignored because this is an offset into the "extra" section of the buffer
// newPos ignored because this is an offset into the "extra" section of the buffer
elem, err = binparserNode(typ, newData, 1)
if err != nil {
return nil, 0, err
Expand All @@ -209,8 +209,7 @@ func binparserElement(data []byte, pos int, large bool) (*json.Value, int, error
return elem, pos, nil
}

//endregion

// endregion
var binaryIntSizes = map[jsonDataType]int{
jsonUint64: 8,
jsonInt64: 8,
Expand Down Expand Up @@ -359,7 +358,7 @@ func binparserArray(typ jsonDataType, data []byte, pos int) (node *json.Value, e
// | type_identifier(0/1) | elem count | obj size | list of offsets+lengths of keys | list of offsets+lengths of values | actual keys | actual values |
func binparserObject(typ jsonDataType, data []byte, pos int) (node *json.Value, err error) {
// "large" decides number of bytes used to specify element count and total object size: 4 bytes for large, 2 for small
var large = typ == jsonLargeObject
large := typ == jsonLargeObject
var elementCount int // total number of elements (== keys) in this object map. (element can be another object: recursively handled)

elementCount, pos = readInt(data, pos, large)
Expand Down
12 changes: 8 additions & 4 deletions go/mysql/binlog/binlog_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func TestBinaryJSON(t *testing.T) {
},
{
name: `scalar "scalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar string"`,
data: []byte{12, 130, 1,
data: []byte{
12, 130, 1,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
Expand All @@ -113,7 +114,8 @@ func TestBinaryJSON(t *testing.T) {
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103},
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
},
expected: json.NewString("scalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar string"),
},
{
Expand Down Expand Up @@ -314,7 +316,9 @@ func TestMarshalJSONToSQL(t *testing.T) {
},
{
name: `scalar "scalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar string"`,
data: []byte{12, 130, 1,
data: []byte{
12, 130, 1,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
Expand All @@ -324,7 +328,7 @@ func TestMarshalJSONToSQL(t *testing.T) {
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103,
115, 99, 97, 108, 97, 114, 32, 115, 116, 114, 105, 110, 103},
},
expected: `CAST(JSON_QUOTE(_utf8mb4'scalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar stringscalar string') as JSON)`,
},
{
Expand Down
12 changes: 8 additions & 4 deletions go/mysql/binlog/rbr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,19 @@ func TestCellLengthAndData(t *testing.T) {
}, {
typ: TypeJSON,
metadata: 2,
data: []byte{0x0f, 0x00,
0, 1, 0, 14, 0, 11, 0, 1, 0, 12, 12, 0, 97, 1, 98},
data: []byte{
0x0f, 0x00,
0, 1, 0, 14, 0, 11, 0, 1, 0, 12, 12, 0, 97, 1, 98,
},
out: sqltypes.MakeTrusted(sqltypes.Expression,
[]byte(`{"a": "b"}`)),
}, {
typ: TypeJSON,
metadata: 4,
data: []byte{0x0f, 0x00, 0x00, 0x00,
0, 1, 0, 14, 0, 11, 0, 1, 0, 12, 12, 0, 97, 1, 98},
data: []byte{
0x0f, 0x00, 0x00, 0x00,
0, 1, 0, 14, 0, 11, 0, 1, 0, 12, 12, 0, 97, 1, 98,
},
out: sqltypes.MakeTrusted(sqltypes.Expression,
[]byte(`{"a": "b"}`)),
}, {
Expand Down
2 changes: 0 additions & 2 deletions go/mysql/binlog_event_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func TestBinlogEventFormat(t *testing.T) {
assert.NoError(t, err, "unexpected error: %v", err)
assert.True(t, reflect.DeepEqual(got, want), "%#v.Format() = %v, want %v", input, got, want)
assert.False(t, input.IsHeartbeat())

}

func TestBinlogEventFormatWrongVersion(t *testing.T) {
Expand Down Expand Up @@ -267,7 +266,6 @@ primary key(eid, id)
return
}
assert.True(t, reflect.DeepEqual(got, want), "%#v.Query() = %v, want %v", input, got, want)

}

func TestBinlogEventQueryBadLength(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions go/mysql/binlog_event_make.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func NewMySQL56BinlogFormat() BinlogFormat {
56, 13, 0, 8, 0, 18, 0, 4, 4, 4,
4, 18, 0, 0, 92, 0, 4, 26, 8, 0,
0, 0, 8, 8, 8, 2, 0, 0, 0, 10,
10, 10, 25, 25, 0},
10, 10, 25, 25, 0,
},
}
}

Expand Down Expand Up @@ -70,7 +71,8 @@ func NewMariaDBBinlogFormat() BinlogFormat {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 19, 4},
4, 19, 4,
},
}
}

Expand Down
8 changes: 1 addition & 7 deletions go/mysql/binlog_event_make_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestFormatDescriptionEvent(t *testing.T) {
gotF, err = event.Format()
require.NoError(t, err, "Format failed: %v", err)
require.True(t, reflect.DeepEqual(gotF, f), "Parsed BinlogFormat doesn't match, got:\n%v\nexpected:\n%v", gotF, f)

}

func TestQueryEvent(t *testing.T) {
Expand All @@ -82,7 +81,6 @@ func TestQueryEvent(t *testing.T) {
gotQ, err := event.Query(f)
require.NoError(t, err, "event.Query() failed: %v", err)
require.True(t, reflect.DeepEqual(gotQ, q), "event.Query() returned %v was expecting %v", gotQ, q)

}

func TestXIDEvent(t *testing.T) {
Expand All @@ -92,7 +90,6 @@ func TestXIDEvent(t *testing.T) {
event := NewXIDEvent(f, s)
require.True(t, event.IsValid(), "NewXIDEvent().IsValid() is false")
require.True(t, event.IsXID(), "NewXIDEvent().IsXID() is false")

}

func TestIntVarEvent(t *testing.T) {
Expand All @@ -114,7 +111,6 @@ func TestIntVarEvent(t *testing.T) {

name, value, err = event.IntVar(f)
require.Error(t, err, "IntVar(invalid) returned %v/%v/%v", name, value, err)

}

func TestInvalidEvents(t *testing.T) {
Expand Down Expand Up @@ -243,7 +239,6 @@ func TestTableMapEvent(t *testing.T) {
gotTm, err := event.TableMap(f)
require.NoError(t, err, "NewTableMapEvent().TableMapEvent() returned error: %v", err)
require.True(t, reflect.DeepEqual(gotTm, tm), "NewTableMapEvent().TableMapEvent() got TableMap:\n%v\nexpected:\n%v", gotTm, tm)

}

func TestLargeTableMapEvent(t *testing.T) {
Expand Down Expand Up @@ -286,7 +281,6 @@ func TestLargeTableMapEvent(t *testing.T) {
gotTm, err := event.TableMap(f)
require.NoError(t, err, "NewTableMapEvent().TableMapEvent() returned error: %v", err)
require.True(t, reflect.DeepEqual(gotTm, tm), "NewTableMapEvent().TableMapEvent() got TableMap:\n%v\nexpected:\n%v", gotTm, tm)

}

func TestRowsEvent(t *testing.T) {
Expand Down Expand Up @@ -411,6 +405,7 @@ func TestFakeRotateEvent(t *testing.T) {
assert.Equal(t, 4, int(pos))
assert.Equal(t, "mysql-bin.000123", nextFile)
}

func TestLargeRowsEvent(t *testing.T) {
f := NewMySQL56BinlogFormat()
s := NewFakeBinlogStream()
Expand Down Expand Up @@ -500,5 +495,4 @@ func TestLargeRowsEvent(t *testing.T) {
gotRows, err := event.Rows(f, tm)
require.NoError(t, err, "NewRowsEvent().Rows() returned error: %v", err)
require.True(t, reflect.DeepEqual(gotRows, rows), "NewRowsEvent().Rows() got Rows:\n%v\nexpected:\n%v", gotRows, rows)

}
4 changes: 0 additions & 4 deletions go/mysql/binlog_event_mariadb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func TestMariadbStandaloneBinlogEventGTID(t *testing.T) {
assert.NoError(t, err, "unexpected error: %v", err)
assert.False(t, hasBegin, "unexpected hasBegin")
assert.True(t, reflect.DeepEqual(got, want), "%#v.GTID() = %#v, want %#v", input, got, want)

}

func TestMariadbBinlogEventGTID(t *testing.T) {
Expand All @@ -122,7 +121,6 @@ func TestMariadbBinlogEventGTID(t *testing.T) {
assert.NoError(t, err, "unexpected error: %v", err)
assert.True(t, hasBegin, "unexpected !hasBegin")
assert.True(t, reflect.DeepEqual(got, want), "%#v.GTID() = %#v, want %#v", input, got, want)

}

func TestMariadbBinlogEventFormat(t *testing.T) {
Expand All @@ -137,7 +135,6 @@ func TestMariadbBinlogEventFormat(t *testing.T) {
got, err := input.Format()
assert.NoError(t, err, "unexpected error: %v", err)
assert.True(t, reflect.DeepEqual(got, want), "%#v.Format() = %v, want %v", input, got, want)

}

func TestMariadbBinlogEventChecksumFormat(t *testing.T) {
Expand All @@ -152,7 +149,6 @@ func TestMariadbBinlogEventChecksumFormat(t *testing.T) {
got, err := input.Format()
assert.NoError(t, err, "unexpected error: %v", err)
assert.True(t, reflect.DeepEqual(got, want), "%#v.Format() = %v, want %v", input, got, want)

}

func TestMariadbBinlogEventStripChecksum(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion go/mysql/binlog_event_mysql56_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func TestMysql56ParsePosition(t *testing.T) {
got, err := replication.ParsePosition(replication.Mysql56FlavorID, input)
assert.NoError(t, err, "unexpected error: %v", err)
assert.True(t, got.Equal(want), "(&mysql56{}).ParsePosition(%#v) = %#v, want %#v", input, got, want)

}

func TestMysql56SemiSyncAck(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion go/mysql/binlog_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestQueryString(t *testing.T) {
want := `{Database: "test_database", Charset:`
got := input.String()
assert.True(t, strings.HasPrefix(got, want), "%#v.String() = %#v, want %#v", input, got, want)

}

func TestQueryStringNilCharset(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions go/mysql/capabilities/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
"vitess.io/vitess/go/vt/vterrors"
)

var (
ErrUnspecifiedServerVersion = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "server version unspecified")
)
var ErrUnspecifiedServerVersion = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "server version unspecified")

type FlavorCapability int

Expand Down
Loading

0 comments on commit 513e457

Please sign in to comment.