blob: 1bb1ed12638d62e044a6da1dfb268acd7289af0f [file] [log] [blame]
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package devicepolicy
import (
"os"
"testing"
"time"
"github.com/godbus/dbus"
)
func dialDbus(address string) (conn *dbus.Conn, err error) {
conn, err = dbus.Dial(address)
if err != nil {
return
}
if err = conn.Auth(nil); err != nil {
conn.Close()
return
}
if err = conn.Hello(); err != nil {
conn.Close()
return
}
return
}
func TestSignalPropertyChange(t *testing.T) {
// Replace system bus with the session bus for this test.
oldSystemBus := os.Getenv("DBUS_SYSTEM_BUS_ADDRESS")
address := os.Getenv("DBUS_SESSION_BUS_ADDRESS")
if address == "" {
t.Skip("no session dbus")
return
}
os.Setenv("DBUS_SYSTEM_BUS_ADDRESS", address)
defer os.Setenv("DBUS_SYSTEM_BUS_ADDRESS", oldSystemBus)
conn, err := dialDbus(address)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
// Subscribe to signals from SessionManager.
conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0,
"type='signal',path='/org/chromium/SessionManager',"+
"interface='org.chromium.SessionManagerInterface',"+
"sender='org.chromium.SessionManager'")
c := make(chan *dbus.Signal, 1)
conn.Signal(c)
err = signalPropertyChange()
if err != nil {
t.Fatal(err)
}
select {
case signal := <-c:
if signal.Name != "org.chromium.SessionManagerInterface.PropertyChangeComplete" {
t.Errorf("Wrong signal: %s", signal.Name)
}
case <-time.After(time.Second * 1):
t.Error("Timeout waiting for signal")
}
}