test(idle): copy fake portal member name and cover failed inhibit rollback
- commit
- 731bbf717f9d0994ab13cc0a0b0de5613d5d2b8a
- author
- brice · · 2026-10-14 09:40 UTC
- parent
- 39fad31
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +26 -1
M internal/infrastructure/idle/portal_inhibitor_test.go +26 -1
| @@ -30,6 +30,8 @@ type fakePortal struct { | |||
| 30 | 30 | // endRequest makes the portal answer Close with an error, like a | |
| 31 | 31 | // portal that already ended the request with a Response signal. | |
| 32 | 32 | endRequest bool | |
| 33 | + | // failInhibit makes the portal reject Inhibit calls. | |
| 34 | + | failInhibit bool | |
| 33 | 35 | } | |
| 34 | 36 | ||
| 35 | 37 | func (f *fakePortal) serve(t *testing.T, conn *zerobus.Conn) { | |
| @@ -53,13 +55,17 @@ func (f *fakePortal) serve(t *testing.T, conn *zerobus.Conn) { | |||
| 53 | 55 | } | |
| 54 | 56 | ||
| 55 | 57 | func (f *fakePortal) answer(conn *zerobus.Conn, m *zerobus.Message) { | |
| 56 | - | call := portalCall{member: m.Member, path: strings.Clone(m.Path)} | |
| 58 | + | call := portalCall{member: strings.Clone(m.Member), path: strings.Clone(m.Path)} | |
| 57 | 59 | switch m.Member { | |
| 58 | 60 | case "Get": | |
| 59 | 61 | e := conn.NewReply(m, "v") | |
| 60 | 62 | e.Variant("u") | |
| 61 | 63 | e.Uint32(3) | |
| 62 | 64 | case "Inhibit": | |
| 65 | + | if f.failInhibit { | |
| 66 | + | conn.NewError(m, "org.freedesktop.DBus.Error.AccessDenied", "s").Str("denied") | |
| 67 | + | break | |
| 68 | + | } | |
| 63 | 69 | body := m.Body() | |
| 64 | 70 | body.Str() // window | |
| 65 | 71 | call.flags = body.Uint32() | |
| @@ -183,6 +189,25 @@ func TestPortalInhibitorToleratesEndedRequest(t *testing.T) { | |||
| 183 | 189 | } | |
| 184 | 190 | } | |
| 185 | 191 | ||
| 192 | + | func TestPortalInhibitorRollsBackFailedInhibit(t *testing.T) { | |
| 193 | + | portal := &fakePortal{failInhibit: true} | |
| 194 | + | p := newTestInhibitor(t, portal) | |
| 195 | + | ||
| 196 | + | if err := p.Inhibit(context.Background(), "video playing"); err == nil { | |
| 197 | + | t.Fatal("Inhibit succeeded although the portal rejected it") | |
| 198 | + | } | |
| 199 | + | portal.next(t) | |
| 200 | + | if p.IsInhibited() { | |
| 201 | + | t.Fatal("refcount kept after a failed Inhibit") | |
| 202 | + | } | |
| 203 | + | ||
| 204 | + | // Nothing to release: Uninhibit must not call Close. | |
| 205 | + | if err := p.Uninhibit(context.Background()); err != nil { | |
| 206 | + | t.Fatal(err) | |
| 207 | + | } | |
| 208 | + | portal.none(t) | |
| 209 | + | } | |
| 210 | + | ||
| 186 | 211 | func TestPortalInhibitorCloseReleasesActiveRequest(t *testing.T) { | |
| 187 | 212 | portal := &fakePortal{} | |
| 188 | 213 | p := newTestInhibitor(t, portal) | |
| @@ -30,6 +30,8 @@ type fakePortal struct { | |||||
| 30 | // endRequest makes the portal answer Close with an error, like a | 30 | // endRequest makes the portal answer Close with an error, like a | ||
| 31 | // portal that already ended the request with a Response signal. | 31 | // portal that already ended the request with a Response signal. | ||
| 32 | endRequest bool | 32 | endRequest bool | ||
| 33 | + | // failInhibit makes the portal reject Inhibit calls. | |||
| 34 | + | failInhibit bool | |||
| 33 | } | 35 | } | ||
| 34 | 36 | ||||
| 35 | func (f *fakePortal) serve(t *testing.T, conn *zerobus.Conn) { | 37 | func (f *fakePortal) serve(t *testing.T, conn *zerobus.Conn) { | ||
| @@ -53,13 +55,17 @@ func (f *fakePortal) serve(t *testing.T, conn *zerobus.Conn) { | |||||
| 53 | } | 55 | } | ||
| 54 | 56 | ||||
| 55 | func (f *fakePortal) answer(conn *zerobus.Conn, m *zerobus.Message) { | 57 | func (f *fakePortal) answer(conn *zerobus.Conn, m *zerobus.Message) { | ||
| 56 | - | call := portalCall{member: m.Member, path: strings.Clone(m.Path)} | 58 | + | call := portalCall{member: strings.Clone(m.Member), path: strings.Clone(m.Path)} |
| 57 | switch m.Member { | 59 | switch m.Member { | ||
| 58 | case "Get": | 60 | case "Get": | ||
| 59 | e := conn.NewReply(m, "v") | 61 | e := conn.NewReply(m, "v") | ||
| 60 | e.Variant("u") | 62 | e.Variant("u") | ||
| 61 | e.Uint32(3) | 63 | e.Uint32(3) | ||
| 62 | case "Inhibit": | 64 | case "Inhibit": | ||
| 65 | + | if f.failInhibit { | |||
| 66 | + | conn.NewError(m, "org.freedesktop.DBus.Error.AccessDenied", "s").Str("denied") | |||
| 67 | + | break | |||
| 68 | + | } | |||
| 63 | body := m.Body() | 69 | body := m.Body() | ||
| 64 | body.Str() // window | 70 | body.Str() // window | ||
| 65 | call.flags = body.Uint32() | 71 | call.flags = body.Uint32() | ||
| @@ -183,6 +189,25 @@ func TestPortalInhibitorToleratesEndedRequest(t *testing.T) { | |||||
| 183 | } | 189 | } | ||
| 184 | } | 190 | } | ||
| 185 | 191 | ||||
| 192 | + | func TestPortalInhibitorRollsBackFailedInhibit(t *testing.T) { | |||
| 193 | + | portal := &fakePortal{failInhibit: true} | |||
| 194 | + | p := newTestInhibitor(t, portal) | |||
| 195 | + | ||||
| 196 | + | if err := p.Inhibit(context.Background(), "video playing"); err == nil { | |||
| 197 | + | t.Fatal("Inhibit succeeded although the portal rejected it") | |||
| 198 | + | } | |||
| 199 | + | portal.next(t) | |||
| 200 | + | if p.IsInhibited() { | |||
| 201 | + | t.Fatal("refcount kept after a failed Inhibit") | |||
| 202 | + | } | |||
| 203 | + | ||||
| 204 | + | // Nothing to release: Uninhibit must not call Close. | |||
| 205 | + | if err := p.Uninhibit(context.Background()); err != nil { | |||
| 206 | + | t.Fatal(err) | |||
| 207 | + | } | |||
| 208 | + | portal.none(t) | |||
| 209 | + | } | |||
| 210 | + | ||||
| 186 | func TestPortalInhibitorCloseReleasesActiveRequest(t *testing.T) { | 211 | func TestPortalInhibitorCloseReleasesActiveRequest(t *testing.T) { | ||
| 187 | portal := &fakePortal{} | 212 | portal := &fakePortal{} | ||
| 188 | p := newTestInhibitor(t, portal) | 213 | p := newTestInhibitor(t, portal) | ||