mirror of
https://github.com/token2/snapd.git
synced 2026-03-13 11:15:47 -07:00
Exported object is running in a separate goroutine, accesses to fields need to
by synchronized.
```
==================
WARNING: DATA RACE
Read at 0x00c000536430 by goroutine 36:
github.com/snapcore/snapd/timeutil_test.timedate1Api.Get()
/home/maciek/work/canonical/snapd/timeutil/synchronized_test.go:87 +0x126
runtime.call64()
/usr/lib/go/src/runtime/asm_amd64.s:772 +0x42
reflect.Value.Call()
/usr/lib/go/src/reflect/value.go:380 +0xb5
github.com/godbus/dbus.exportedMethod.Call()
/home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/default_handler.go:128 +0x219
github.com/godbus/dbus.(*exportedMethod).Call()
<autogenerated>:1 +0x84
github.com/godbus/dbus.(*Conn).handleCall()
/home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/export.go:153 +0x591
github.com/godbus/dbus.(*Conn).inWorker.gowrap1()
/home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/conn.go:334 +0x44
Previous write at 0x00c000536430 by goroutine 30:
github.com/snapcore/snapd/timeutil_test.(*syncedSuite).TestIsNTPSynchronized()
/home/maciek/work/canonical/snapd/timeutil/synchronized_test.go:118 +0x298
runtime.call16()
/usr/lib/go/src/runtime/asm_amd64.s:770 +0x42
reflect.Value.Call()
/usr/lib/go/src/reflect/value.go:380 +0xb5
gopkg.in/check%2ev1.(*suiteRunner).forkTest.func1()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:775 +0x9c5
gopkg.in/check%2ev1.(*suiteRunner).forkCall.func1()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:669 +0xe9
Goroutine 36 (running) created at:
github.com/godbus/dbus.(*Conn).inWorker()
/home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/conn.go:334 +0x346
github.com/godbus/dbus.(*Conn).Auth.gowrap1()
/home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/auth.go:118 +0x33
Goroutine 30 (running) created at:
gopkg.in/check%2ev1.(*suiteRunner).forkCall()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:666 +0x5ba
gopkg.in/check%2ev1.(*suiteRunner).forkTest()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:757 +0x155
gopkg.in/check%2ev1.(*suiteRunner).runTest()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:812 +0x419
gopkg.in/check%2ev1.(*suiteRunner).run()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:618 +0x3c6
gopkg.in/check%2ev1.Run()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/run.go:92 +0x44
gopkg.in/check%2ev1.RunAll()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/run.go:84 +0x124
gopkg.in/check%2ev1.TestingT()
/home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/run.go:72 +0x5d3
github.com/snapcore/snapd/timeutil_test.Test()
/home/maciek/work/canonical/snapd/timeutil/schedule_test.go:33 +0x26
testing.tRunner()
/usr/lib/go/src/testing/testing.go:1689 +0x21e
testing.(*T).Run.gowrap1()
/usr/lib/go/src/testing/testing.go:1742 +0x44
==================
```
Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
165 lines
4.0 KiB
Go
165 lines
4.0 KiB
Go
// -*- Mode: Go; indent-tabs-mode: t -*-
|
|
|
|
/*
|
|
* Copyright (C) 2021 Canonical Ltd
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 3 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
package timeutil_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/godbus/dbus"
|
|
. "gopkg.in/check.v1"
|
|
|
|
"github.com/snapcore/snapd/dbusutil"
|
|
"github.com/snapcore/snapd/testutil"
|
|
"github.com/snapcore/snapd/timeutil"
|
|
)
|
|
|
|
const (
|
|
timedate1BusName = "org.freedesktop.timedate1"
|
|
timedate1ObjectPath = "/org/freedesktop/timedate1"
|
|
)
|
|
|
|
type mockTimedate1 struct {
|
|
conn *dbus.Conn
|
|
|
|
NTPSynchronized bool
|
|
|
|
m sync.Mutex
|
|
getPropertyCalled []string
|
|
}
|
|
|
|
func newMockTimedate1() (*mockTimedate1, error) {
|
|
conn, err := dbusutil.SessionBusPrivate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
server := &mockTimedate1{
|
|
conn: conn,
|
|
}
|
|
|
|
reply, err := conn.RequestName(timedate1BusName, dbus.NameFlagDoNotQueue)
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
|
|
if reply != dbus.RequestNameReplyPrimaryOwner {
|
|
conn.Close()
|
|
return nil, fmt.Errorf("cannot obtain bus name %q", timedate1BusName)
|
|
}
|
|
|
|
return server, nil
|
|
}
|
|
|
|
func (server *mockTimedate1) Export() {
|
|
server.conn.Export(&timedate1Api{server}, timedate1ObjectPath, "org.freedesktop.DBus.Properties")
|
|
}
|
|
|
|
func (server *mockTimedate1) Stop() error {
|
|
if _, err := server.conn.ReleaseName(timedate1BusName); err != nil {
|
|
return err
|
|
}
|
|
return server.conn.Close()
|
|
}
|
|
|
|
func (server *mockTimedate1) reset(ntpSynchronized bool) {
|
|
server.m.Lock()
|
|
defer server.m.Unlock()
|
|
|
|
server.NTPSynchronized = ntpSynchronized
|
|
server.getPropertyCalled = nil
|
|
}
|
|
|
|
type timedate1Api struct {
|
|
server *mockTimedate1
|
|
}
|
|
|
|
func (a *timedate1Api) Get(iff, prop string) (dbus.Variant, *dbus.Error) {
|
|
a.server.m.Lock()
|
|
defer a.server.m.Unlock()
|
|
|
|
a.server.getPropertyCalled = append(a.server.getPropertyCalled, fmt.Sprintf("if=%s;prop=%s", iff, prop))
|
|
return dbus.MakeVariant(a.server.NTPSynchronized), nil
|
|
}
|
|
|
|
type syncedSuite struct {
|
|
testutil.BaseTest
|
|
testutil.DBusTest
|
|
}
|
|
|
|
var _ = Suite(&syncedSuite{})
|
|
|
|
func (s *syncedSuite) SetUpTest(c *C) {
|
|
s.BaseTest.SetUpTest(c)
|
|
s.DBusTest.SetUpTest(c)
|
|
|
|
restore := dbusutil.MockOnlySystemBusAvailable(s.SessionBus)
|
|
s.AddCleanup(restore)
|
|
}
|
|
|
|
func (s *syncedSuite) TearDownTest(c *C) {
|
|
s.DBusTest.TearDownTest(c)
|
|
s.BaseTest.TearDownTest(c)
|
|
}
|
|
|
|
func (s *syncedSuite) TestIsNTPSynchronized(c *C) {
|
|
backend, err := newMockTimedate1()
|
|
c.Assert(err, IsNil)
|
|
defer backend.Stop()
|
|
backend.Export()
|
|
|
|
for _, v := range []bool{true, false} {
|
|
backend.reset(v)
|
|
|
|
synced, err := timeutil.IsNTPSynchronized()
|
|
c.Assert(err, IsNil)
|
|
c.Check(synced, Equals, v)
|
|
|
|
func() {
|
|
backend.m.Lock()
|
|
defer backend.m.Unlock()
|
|
c.Check(backend.getPropertyCalled, DeepEquals, []string{
|
|
"if=org.freedesktop.timedate1;prop=NTPSynchronized",
|
|
})
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (s *syncedSuite) TestIsNTPSynchronizedStrangeEr(c *C) {
|
|
backend, err := newMockTimedate1()
|
|
c.Assert(err, IsNil)
|
|
defer backend.Stop()
|
|
// Note that we did not export anything here - this error is a bit
|
|
// artificial
|
|
|
|
_, err = timeutil.IsNTPSynchronized()
|
|
c.Check(err, ErrorMatches, `cannot check for ntp sync: Object does not implement the interface`)
|
|
}
|
|
|
|
func (s *syncedSuite) TestIsNTPSynchronizedNoTimedatectlNoErr(c *C) {
|
|
// note that there is no mock timedate1 created so we are on an empty bus
|
|
synced, err := timeutil.IsNTPSynchronized()
|
|
c.Check(err, ErrorMatches, `cannot find org.freedesktop.timedate1 dbus service: .*`)
|
|
c.Check(errors.As(err, &timeutil.NoTimedate1Error{}), Equals, true)
|
|
c.Check(synced, Equals, false)
|
|
}
|