|
| 1 | +package versionaware |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/AndroidGoLab/binder/binder" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCompiledTable_Resolve(t *testing.T) { |
| 13 | + sorted := CompiledTable{ |
| 14 | + {Descriptor: "android.app.IBar", Methods: []MethodEntry{ |
| 15 | + {Method: "barMethod", Code: binder.FirstCallTransaction + 0}, |
| 16 | + }}, |
| 17 | + {Descriptor: "android.app.IFoo", Methods: []MethodEntry{ |
| 18 | + {Method: "doOther", Code: binder.FirstCallTransaction + 1}, |
| 19 | + {Method: "doThing", Code: binder.FirstCallTransaction + 0}, |
| 20 | + }}, |
| 21 | + } |
| 22 | + require.True(t, sorted.IsSorted()) |
| 23 | + |
| 24 | + assert.Equal(t, binder.FirstCallTransaction+0, sorted.Resolve("android.app.IFoo", "doThing")) |
| 25 | + assert.Equal(t, binder.FirstCallTransaction+1, sorted.Resolve("android.app.IFoo", "doOther")) |
| 26 | + assert.Equal(t, binder.FirstCallTransaction+0, sorted.Resolve("android.app.IBar", "barMethod")) |
| 27 | + assert.Equal(t, binder.TransactionCode(0), sorted.Resolve("android.app.IFoo", "nonExistent")) |
| 28 | + assert.Equal(t, binder.TransactionCode(0), sorted.Resolve("nonexistent.IFoo", "bar")) |
| 29 | +} |
| 30 | + |
| 31 | +func TestCompiledTable_ReverseResolve(t *testing.T) { |
| 32 | + table := CompiledTable{ |
| 33 | + {Descriptor: "android.app.IFoo", Methods: []MethodEntry{ |
| 34 | + {Method: "doOther", Code: binder.FirstCallTransaction + 1}, |
| 35 | + {Method: "doThing", Code: binder.FirstCallTransaction + 0}, |
| 36 | + }}, |
| 37 | + } |
| 38 | + |
| 39 | + name, ok := table.ReverseResolve("android.app.IFoo", binder.FirstCallTransaction+1) |
| 40 | + assert.True(t, ok) |
| 41 | + assert.Equal(t, "doOther", name) |
| 42 | + |
| 43 | + _, ok = table.ReverseResolve("android.app.IFoo", binder.FirstCallTransaction+99) |
| 44 | + assert.False(t, ok) |
| 45 | + |
| 46 | + _, ok = table.ReverseResolve("nonexistent", binder.FirstCallTransaction+0) |
| 47 | + assert.False(t, ok) |
| 48 | +} |
| 49 | + |
| 50 | +func TestCompiledTable_IsSorted(t *testing.T) { |
| 51 | + sorted := CompiledTable{ |
| 52 | + {Descriptor: "aaa", Methods: []MethodEntry{ |
| 53 | + {Method: "aMethod", Code: 1}, |
| 54 | + {Method: "bMethod", Code: 2}, |
| 55 | + }}, |
| 56 | + {Descriptor: "bbb", Methods: []MethodEntry{ |
| 57 | + {Method: "xMethod", Code: 1}, |
| 58 | + }}, |
| 59 | + } |
| 60 | + assert.True(t, sorted.IsSorted()) |
| 61 | + |
| 62 | + unsortedDesc := CompiledTable{ |
| 63 | + {Descriptor: "bbb", Methods: nil}, |
| 64 | + {Descriptor: "aaa", Methods: nil}, |
| 65 | + } |
| 66 | + assert.False(t, unsortedDesc.IsSorted()) |
| 67 | + |
| 68 | + unsortedMethods := CompiledTable{ |
| 69 | + {Descriptor: "aaa", Methods: []MethodEntry{ |
| 70 | + {Method: "bMethod", Code: 2}, |
| 71 | + {Method: "aMethod", Code: 1}, |
| 72 | + }}, |
| 73 | + } |
| 74 | + assert.False(t, unsortedMethods.IsSorted()) |
| 75 | + |
| 76 | + dupDesc := CompiledTable{ |
| 77 | + {Descriptor: "aaa", Methods: nil}, |
| 78 | + {Descriptor: "aaa", Methods: nil}, |
| 79 | + } |
| 80 | + assert.False(t, dupDesc.IsSorted()) |
| 81 | +} |
| 82 | + |
| 83 | +func TestCompiledTable_ToVersionTable(t *testing.T) { |
| 84 | + ct := CompiledTable{ |
| 85 | + {Descriptor: "android.app.IFoo", Methods: []MethodEntry{ |
| 86 | + {Method: "doOther", Code: binder.FirstCallTransaction + 1}, |
| 87 | + {Method: "doThing", Code: binder.FirstCallTransaction + 0}, |
| 88 | + }}, |
| 89 | + } |
| 90 | + vt := ct.ToVersionTable() |
| 91 | + assert.Equal(t, binder.FirstCallTransaction+0, vt["android.app.IFoo"]["doThing"]) |
| 92 | + assert.Equal(t, binder.FirstCallTransaction+1, vt["android.app.IFoo"]["doOther"]) |
| 93 | +} |
| 94 | + |
| 95 | +func TestCompiledTable_HasDescriptor(t *testing.T) { |
| 96 | + ct := CompiledTable{ |
| 97 | + {Descriptor: "android.app.IFoo", Methods: nil}, |
| 98 | + } |
| 99 | + assert.True(t, ct.HasDescriptor("android.app.IFoo")) |
| 100 | + assert.False(t, ct.HasDescriptor("nonexistent")) |
| 101 | +} |
| 102 | + |
| 103 | +func TestCompiledTable_MethodsForDescriptor(t *testing.T) { |
| 104 | + methods := []MethodEntry{ |
| 105 | + {Method: "alpha", Code: 1}, |
| 106 | + {Method: "beta", Code: 2}, |
| 107 | + } |
| 108 | + ct := CompiledTable{ |
| 109 | + {Descriptor: "android.app.IFoo", Methods: methods}, |
| 110 | + } |
| 111 | + assert.Equal(t, methods, ct.MethodsForDescriptor("android.app.IFoo")) |
| 112 | + assert.Nil(t, ct.MethodsForDescriptor("nonexistent")) |
| 113 | +} |
| 114 | + |
| 115 | +func TestTablesAreSorted(t *testing.T) { |
| 116 | + if len(Tables) == 0 { |
| 117 | + t.Skip("Tables not yet populated with CompiledTable data") |
| 118 | + } |
| 119 | + for rev, table := range Tables { |
| 120 | + if !table.IsSorted() { |
| 121 | + t.Errorf("Tables[%q] is not sorted", rev) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments