• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.android.modules.utils;
18 
19 import androidx.test.filters.SmallTest;
20 
21 import libcore.util.HexEncoding;
22 
23 import junit.framework.TestCase;
24 
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 @SmallTest
31 public class BytesMatcherTest extends TestCase {
32     @Test
testEmpty()33     public void testEmpty() throws Exception {
34         BytesMatcher matcher = BytesMatcher.decode("");
35         assertFalse(matcher.test(HexEncoding.decode("cafe")));
36         assertFalse(matcher.test(HexEncoding.decode("")));
37     }
38 
39     @Test
testExact()40     public void testExact() throws Exception {
41         BytesMatcher matcher = BytesMatcher.decode("+cafe");
42         assertTrue(matcher.test(HexEncoding.decode("cafe")));
43         assertFalse(matcher.test(HexEncoding.decode("beef")));
44         assertFalse(matcher.test(HexEncoding.decode("ca")));
45         assertFalse(matcher.test(HexEncoding.decode("cafe00")));
46     }
47 
48     @Test
testMask()49     public void testMask() throws Exception {
50         BytesMatcher matcher = BytesMatcher.decode("+cafe/ff00");
51         assertTrue(matcher.test(HexEncoding.decode("cafe")));
52         assertTrue(matcher.test(HexEncoding.decode("ca88")));
53         assertFalse(matcher.test(HexEncoding.decode("beef")));
54         assertFalse(matcher.test(HexEncoding.decode("ca")));
55         assertFalse(matcher.test(HexEncoding.decode("cafe00")));
56     }
57 
58     @Test
testPrefix()59     public void testPrefix() throws Exception {
60         BytesMatcher matcher = BytesMatcher.decode("⊆cafe,⊆beef/ff00");
61         assertTrue(matcher.test(HexEncoding.decode("cafe")));
62         assertFalse(matcher.test(HexEncoding.decode("caff")));
63         assertTrue(matcher.test(HexEncoding.decode("cafecafe")));
64         assertFalse(matcher.test(HexEncoding.decode("ca")));
65         assertTrue(matcher.test(HexEncoding.decode("beef")));
66         assertTrue(matcher.test(HexEncoding.decode("beff")));
67         assertTrue(matcher.test(HexEncoding.decode("beffbeff")));
68         assertFalse(matcher.test(HexEncoding.decode("be")));
69     }
70 
71     @Test
testSerialize_Empty()72     public void testSerialize_Empty() throws Exception {
73         BytesMatcher matcher = new BytesMatcher();
74         matcher = BytesMatcher.decode(BytesMatcher.encode(matcher));
75 
76         // Also very empty and null values
77         BytesMatcher.decode("");
78         BytesMatcher.decode(null);
79     }
80 
81     @Test
testSerialize_Exact()82     public void testSerialize_Exact() throws Exception {
83         BytesMatcher matcher = new BytesMatcher();
84         matcher.addExactRejectRule(HexEncoding.decode("cafe00112233"),
85                 HexEncoding.decode("ffffff000000"));
86         matcher.addExactRejectRule(HexEncoding.decode("beef00112233"),
87                 null);
88         matcher.addExactAcceptRule(HexEncoding.decode("000000000000"),
89                 HexEncoding.decode("000000000000"));
90 
91         assertFalse(matcher.test(HexEncoding.decode("cafe00ffffff")));
92         assertFalse(matcher.test(HexEncoding.decode("beef00112233")));
93         assertTrue(matcher.test(HexEncoding.decode("beef00ffffff")));
94 
95         // Bounce through serialization pass and confirm it still works
96         matcher = BytesMatcher.decode(BytesMatcher.encode(matcher));
97 
98         assertFalse(matcher.test(HexEncoding.decode("cafe00ffffff")));
99         assertFalse(matcher.test(HexEncoding.decode("beef00112233")));
100         assertTrue(matcher.test(HexEncoding.decode("beef00ffffff")));
101     }
102 
103     @Test
testSerialize_Prefix()104     public void testSerialize_Prefix() throws Exception {
105         BytesMatcher matcher = new BytesMatcher();
106         matcher.addExactRejectRule(HexEncoding.decode("aa"), null);
107         matcher.addExactAcceptRule(HexEncoding.decode("bb"), null);
108         matcher.addPrefixAcceptRule(HexEncoding.decode("aa"), null);
109         matcher.addPrefixRejectRule(HexEncoding.decode("bb"), null);
110 
111         assertFalse(matcher.test(HexEncoding.decode("aa")));
112         assertTrue(matcher.test(HexEncoding.decode("bb")));
113         assertTrue(matcher.test(HexEncoding.decode("aaaa")));
114         assertFalse(matcher.test(HexEncoding.decode("bbbb")));
115 
116         // Bounce through serialization pass and confirm it still works
117         matcher = BytesMatcher.decode(BytesMatcher.encode(matcher));
118 
119         assertFalse(matcher.test(HexEncoding.decode("aa")));
120         assertTrue(matcher.test(HexEncoding.decode("bb")));
121         assertTrue(matcher.test(HexEncoding.decode("aaaa")));
122         assertFalse(matcher.test(HexEncoding.decode("bbbb")));
123     }
124 
125     @Test
testOrdering_RejectFirst()126     public void testOrdering_RejectFirst() throws Exception {
127         BytesMatcher matcher = BytesMatcher.decode("-ff/0f,+ff/f0");
128         assertFalse(matcher.test(HexEncoding.decode("ff")));
129         assertTrue(matcher.test(HexEncoding.decode("f0")));
130         assertFalse(matcher.test(HexEncoding.decode("0f")));
131     }
132 
133     @Test
testOrdering_AcceptFirst()134     public void testOrdering_AcceptFirst() throws Exception {
135         BytesMatcher matcher = BytesMatcher.decode("+ff/f0,-ff/0f");
136         assertTrue(matcher.test(HexEncoding.decode("ff")));
137         assertTrue(matcher.test(HexEncoding.decode("f0")));
138         assertFalse(matcher.test(HexEncoding.decode("0f")));
139     }
140 }
141