1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.apf
18 
19 import android.net.apf.BaseApfGenerator.MemorySlot
20 import android.net.apf.BaseApfGenerator.Register.R0
21 import androidx.test.filters.SmallTest
22 import androidx.test.runner.AndroidJUnit4
23 import com.android.testutils.assertThrows
24 import java.util.NoSuchElementException
25 import java.util.concurrent.atomic.AtomicReference
26 import kotlin.test.assertEquals
27 import org.junit.Before
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.mockito.Mock
31 import org.mockito.Mockito.inOrder
32 import org.mockito.MockitoAnnotations
33 
34 @RunWith(AndroidJUnit4::class)
35 @SmallTest
36 class JumpTableTest {
37 
38     @Mock
39     lateinit var gen: ApfV4Generator
40 
41     @Before
setUpnull42     fun setUp() {
43         MockitoAnnotations.initMocks(this)
44     }
45 
46     @Test(expected = NullPointerException::class)
testNullStartLabelnull47     fun testNullStartLabel() {
48         // Can't use "null" because the method is @NonNull.
49         JumpTable(AtomicReference<String>(null).get(), MemorySlot.SLOT_0)
50     }
51 
52     @Test(expected = IllegalArgumentException::class)
testSlotTooLargenull53     fun testSlotTooLarge() {
54         JumpTable("my_jump_table", MemorySlot.IPV4_HEADER_SIZE)
55     }
56 
57     @Test
testValidSlotNumbersnull58     fun testValidSlotNumbers() {
59         JumpTable("my_jump_table", MemorySlot.SLOT_1)
60         JumpTable("my_jump_table", MemorySlot.SLOT_4)
61         JumpTable("my_jump_table", MemorySlot.SLOT_6)
62     }
63 
64     @Test
testGetStartLabelnull65     fun testGetStartLabel() {
66         assertEquals("xyz", JumpTable("xyz", MemorySlot.SLOT_3).startLabel)
67         assertEquals("abc", JumpTable("abc", MemorySlot.SLOT_5).startLabel)
68     }
69 
70     @Test
testCodeGenerationnull71     fun testCodeGeneration() {
72         val name = "my_jump_table"
73         val slot = MemorySlot.SLOT_7
74 
75         val j = JumpTable(name, slot)
76         j.addLabel("foo")
77         j.addLabel("bar")
78         j.addLabel("bar")
79         j.addLabel("baz")
80 
81         assertEquals(0, j.getIndex("foo"))
82         assertEquals(1, j.getIndex("bar"))
83         assertEquals(2, j.getIndex("baz"))
84 
85         assertThrows(NoSuchElementException::class.java) {
86             j.getIndex("nonexistent")
87         }
88 
89         val inOrder = inOrder(gen)
90 
91         j.generate(gen)
92 
93         inOrder.verify(gen).defineLabel(name)
94         inOrder.verify(gen).addLoadFromMemory(R0, slot)
95         inOrder.verify(gen).addJumpIfR0Equals(0, "foo")
96         inOrder.verify(gen).addJumpIfR0Equals(1, "bar")
97         inOrder.verify(gen).addJumpIfR0Equals(2, "baz")
98         inOrder.verify(gen).addJump(ApfV4Generator.PASS_LABEL)
99         inOrder.verifyNoMoreInteractions()
100     }
101 }
102