Skip to content

Commit 9e0031e

Browse files
feat: add transaction code resolve helpers for bindercli
Add resolveCodeToMethod (reverse lookup), resolveMethodToCode (forward lookup), and getActiveTable (extract VersionTable from a Conn) to support upcoming CLI commands that translate between method names and transaction codes.
1 parent 607713d commit 9e0031e

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

cmd/bindercli/resolve.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:build linux
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/AndroidGoLab/binder/binder"
9+
"github.com/AndroidGoLab/binder/binder/versionaware"
10+
)
11+
12+
// resolveCodeToMethod performs reverse lookup: given a descriptor and
13+
// transaction code, returns the method name. Returns ("", false) if not found.
14+
func resolveCodeToMethod(
15+
table versionaware.VersionTable,
16+
descriptor string,
17+
code binder.TransactionCode,
18+
) (string, bool) {
19+
methods, ok := table[descriptor]
20+
if !ok {
21+
return "", false
22+
}
23+
for name, c := range methods {
24+
if c == code {
25+
return name, true
26+
}
27+
}
28+
return "", false
29+
}
30+
31+
// resolveMethodToCode performs forward lookup: given a descriptor and
32+
// method name, returns the transaction code. Returns (0, false) if not found.
33+
func resolveMethodToCode(
34+
table versionaware.VersionTable,
35+
descriptor string,
36+
method string,
37+
) (binder.TransactionCode, bool) {
38+
code := table.Resolve(descriptor, method)
39+
if code == 0 {
40+
return 0, false
41+
}
42+
return code, true
43+
}
44+
45+
// getActiveTable extracts the active VersionTable from a Conn's transport.
46+
// Returns an error if the transport is not version-aware.
47+
func getActiveTable(c *Conn) (versionaware.VersionTable, error) {
48+
if c == nil {
49+
return nil, fmt.Errorf("connection is nil")
50+
}
51+
vat, ok := c.Transport.(*versionaware.Transport)
52+
if !ok {
53+
return nil, fmt.Errorf("transport is not version-aware")
54+
}
55+
return vat.ActiveTable(), nil
56+
}

cmd/bindercli/resolve_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//go:build linux
2+
3+
package main
4+
5+
import (
6+
"testing"
7+
8+
"github.com/AndroidGoLab/binder/binder"
9+
"github.com/AndroidGoLab/binder/binder/versionaware"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestResolveCodeToMethod(t *testing.T) {
14+
table := versionaware.VersionTable{
15+
"android.app.IFoo": {
16+
"doThing": binder.FirstCallTransaction + 0,
17+
"doOther": binder.FirstCallTransaction + 1,
18+
},
19+
}
20+
21+
name, ok := resolveCodeToMethod(table, "android.app.IFoo", binder.FirstCallTransaction+1)
22+
assert.True(t, ok)
23+
assert.Equal(t, "doOther", name)
24+
}
25+
26+
func TestResolveCodeToMethod_NotFound(t *testing.T) {
27+
table := versionaware.VersionTable{
28+
"android.app.IFoo": {
29+
"doThing": binder.FirstCallTransaction + 0,
30+
},
31+
}
32+
33+
_, ok := resolveCodeToMethod(table, "android.app.IFoo", binder.FirstCallTransaction+99)
34+
assert.False(t, ok)
35+
}
36+
37+
func TestResolveCodeToMethod_UnknownDescriptor(t *testing.T) {
38+
table := versionaware.VersionTable{}
39+
40+
_, ok := resolveCodeToMethod(table, "android.app.IFoo", binder.FirstCallTransaction)
41+
assert.False(t, ok)
42+
}
43+
44+
func TestResolveMethodToCode(t *testing.T) {
45+
table := versionaware.VersionTable{
46+
"android.app.IFoo": {
47+
"doThing": binder.FirstCallTransaction + 0,
48+
"doOther": binder.FirstCallTransaction + 1,
49+
},
50+
}
51+
52+
code, ok := resolveMethodToCode(table, "android.app.IFoo", "doOther")
53+
assert.True(t, ok)
54+
assert.Equal(t, binder.FirstCallTransaction+1, code)
55+
}
56+
57+
func TestResolveMethodToCode_NotFound(t *testing.T) {
58+
table := versionaware.VersionTable{
59+
"android.app.IFoo": {
60+
"doThing": binder.FirstCallTransaction + 0,
61+
},
62+
}
63+
64+
_, ok := resolveMethodToCode(table, "android.app.IFoo", "noSuchMethod")
65+
assert.False(t, ok)
66+
}
67+
68+
func TestGetActiveTable_NilConn(t *testing.T) {
69+
_, err := getActiveTable(nil)
70+
assert.Error(t, err)
71+
}

0 commit comments

Comments
 (0)