GODT-2041: Crash after factory reset

I forgot to remove the user from the users map during factory reset.
This meant the (deleted) would attempt to be closed during teardown.
This commit is contained in:
James Houlahan
2022-11-28 19:58:10 +01:00
parent b902f1490f
commit ae7ae2886f
3 changed files with 23 additions and 4 deletions

View File

@ -421,6 +421,27 @@ func TestBridge_AddressWithoutKeys(t *testing.T) {
})
}
func TestBridge_FactoryReset(t *testing.T) {
withEnv(t, func(ctx context.Context, s *server.Server, netCtl *proton.NetCtl, locator bridge.Locator, vaultKey []byte) {
withBridge(ctx, t, s.GetHostURL(), netCtl, locator, vaultKey, func(bridge *bridge.Bridge, mocks *bridge.Mocks) {
// Login the user.
userID, err := bridge.LoginFull(ctx, username, password, nil, nil)
require.NoError(t, err)
// The user is now connected.
require.Equal(t, []string{userID}, bridge.GetUserIDs())
require.Equal(t, []string{userID}, getConnectedUserIDs(t, bridge))
// Perform a factory reset.
bridge.FactoryReset(ctx)
// The user is gone.
require.Equal(t, []string{}, bridge.GetUserIDs())
require.Equal(t, []string{}, getConnectedUserIDs(t, bridge))
})
})
}
// withEnv creates the full test environment and runs the tests.
func withEnv(t *testing.T, tests func(context.Context, *server.Server, *proton.NetCtl, bridge.Locator, []byte), opts ...server.Option) {
server := server.New(opts...)

View File

@ -223,8 +223,6 @@ func (bridge *Bridge) LogoutUser(ctx context.Context, userID string) error {
return ErrNoSuchUser
}
defer delete(bridge.users, user.ID())
bridge.logoutUser(ctx, user, true, false)
bridge.publish(events.UserLoggedOut{
@ -245,7 +243,6 @@ func (bridge *Bridge) DeleteUser(ctx context.Context, userID string) error {
}
if user, ok := bridge.users[userID]; ok {
defer delete(bridge.users, user.ID())
bridge.logoutUser(ctx, user, true, true)
}
@ -528,6 +525,8 @@ func (bridge *Bridge) newVaultUser(
// logout logs out the given user, optionally logging them out from the API too.
func (bridge *Bridge) logoutUser(ctx context.Context, user *user.User, withAPI, withData bool) {
defer delete(bridge.users, user.ID())
logrus.WithFields(logrus.Fields{
"userID": user.ID(),
"withAPI": withAPI,

View File

@ -119,7 +119,6 @@ func (bridge *Bridge) handleUserRefreshed(ctx context.Context, user *user.User)
func (bridge *Bridge) handleUserDeauth(ctx context.Context, user *user.User) {
safe.Lock(func() {
defer delete(bridge.users, user.ID())
bridge.logoutUser(ctx, user, false, false)
}, bridge.usersLock)
}