85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package whatsmeow_service
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/evolution-foundation/evolution-go/pkg/config"
|
|
"go.mau.fi/whatsmeow/proto/waAdv"
|
|
"go.mau.fi/whatsmeow/store/sqlstore"
|
|
"go.mau.fi/whatsmeow/types"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func TestSharedAuthContainerRetryAndConcurrentReuse(t *testing.T) {
|
|
root := t.TempDir()
|
|
w := whatsmeowService{config: &config.Config{}, exPath: root, authStore: &sharedSQLStore{}}
|
|
t.Cleanup(func() {
|
|
if w.authStore.sqliteDB != nil {
|
|
if err := w.authStore.sqliteDB.Close(); err != nil {
|
|
t.Errorf("close auth store: %v", err)
|
|
}
|
|
}
|
|
})
|
|
if _, err := w.getSharedSQLStoreContainer(); err == nil {
|
|
t.Fatal("expected failure before dbdata exists")
|
|
}
|
|
if err := os.Mkdir(filepath.Join(root, "dbdata"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const callers = 16
|
|
containers := make([]*sqlstore.Container, callers)
|
|
errs := make([]error, callers)
|
|
var wg sync.WaitGroup
|
|
for i := range callers {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
containers[i], errs[i] = w.getSharedSQLStoreContainer()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
for i := range callers {
|
|
if errs[i] != nil || containers[i] == nil {
|
|
t.Fatalf("caller %d could not retry: %v", i, errs[i])
|
|
}
|
|
if containers[i] != containers[0] {
|
|
t.Fatalf("caller %d opened a different container", i)
|
|
}
|
|
}
|
|
if got := w.authStore.sqliteDB.Stats().MaxOpenConnections; got != 1 {
|
|
t.Fatalf("SQLite connection limit = %d, want 1", got)
|
|
}
|
|
// Persist a paired-device record and reopen the existing schema as on restart.
|
|
device := containers[0].NewDevice()
|
|
jid := types.NewJID("5511999999999", types.DefaultUserServer)
|
|
device.ID = &jid
|
|
device.Account = &waAdv.ADVSignedDeviceIdentity{
|
|
Details: []byte("test device"),
|
|
AccountSignature: make([]byte, 64),
|
|
AccountSignatureKey: make([]byte, 32),
|
|
DeviceSignature: make([]byte, 64),
|
|
}
|
|
if err := device.Save(context.Background()); err != nil {
|
|
t.Fatalf("save session: %v", err)
|
|
}
|
|
if err := w.authStore.sqliteDB.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.authStore = &sharedSQLStore{}
|
|
reopened, err := w.getSharedSQLStoreContainer()
|
|
if err != nil {
|
|
t.Fatalf("reopen existing auth database: %v", err)
|
|
}
|
|
restored, err := reopened.GetDevice(context.Background(), jid)
|
|
if err != nil || restored == nil {
|
|
t.Fatalf("restore session: device=%v err=%v", restored, err)
|
|
}
|
|
if restored.RegistrationID != device.RegistrationID || *restored.NoiseKey.Pub != *device.NoiseKey.Pub {
|
|
t.Fatal("session keys changed after reopening")
|
|
}
|
|
}
|