1 /* 2 * Copyright (C) 2025 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 package com.android.nfc; 17 18 import static org.junit.Assert.assertArrayEquals; 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertThrows; 22 import static org.junit.Assert.assertTrue; 23 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.util.HexFormat; 30 31 @RunWith(AndroidJUnit4.class) 32 public class ExitFrameTest { 33 34 @Test testBasicFilter()35 public void testBasicFilter() { 36 ExitFrame frame = new ExitFrame("aa11bb22"); 37 38 assertArrayEquals(HexFormat.of().parseHex("aa11bb22"), frame.getData()); 39 assertArrayEquals(HexFormat.of().parseHex("FFFFFFFF"), frame.getDataMask()); 40 assertEquals(0b00, frame.getNfcTech()); 41 assertEquals(0b00111011, frame.getPowerState()); 42 assertFalse(frame.isPrefixMatchingAllowed()); 43 } 44 45 @Test testPrefixMatching()46 public void testPrefixMatching() { 47 ExitFrame frame = new ExitFrame("aa11.*"); 48 49 assertArrayEquals(HexFormat.of().parseHex("aa11"), frame.getData()); 50 assertArrayEquals(HexFormat.of().parseHex("FFFF"), frame.getDataMask()); 51 assertTrue(frame.isPrefixMatchingAllowed()); 52 } 53 54 @Test testPrefixMatchingMaxLength()55 public void testPrefixMatchingMaxLength() { 56 ExitFrame frame = new ExitFrame("00112233445566778899AABBCCDD.*"); 57 58 assertArrayEquals(HexFormat.of().parseHex("00112233445566778899AABBCCDD"), 59 frame.getData()); 60 assertTrue(frame.isPrefixMatchingAllowed()); 61 } 62 63 @Test testPrefixMatchingMidByte()64 public void testPrefixMatchingMidByte() { 65 ExitFrame frame = new ExitFrame("123.*"); 66 67 assertArrayEquals(HexFormat.of().parseHex("1230"), frame.getData()); 68 assertArrayEquals(HexFormat.of().parseHex("FFF0"), frame.getDataMask()); 69 assertTrue(frame.isPrefixMatchingAllowed()); 70 } 71 72 @Test testMatchEverything()73 public void testMatchEverything() { 74 ExitFrame frame = new ExitFrame("*"); 75 assertEquals(0, frame.getData().length); 76 assertTrue(frame.isPrefixMatchingAllowed()); 77 } 78 79 @Test testDataMask()80 public void testDataMask() { 81 ExitFrame frame = new ExitFrame("123.56.."); 82 83 assertArrayEquals(HexFormat.of().parseHex("12305600"), frame.getData()); 84 assertArrayEquals(HexFormat.of().parseHex("FFF0FF00"), frame.getDataMask()); 85 assertFalse(frame.isPrefixMatchingAllowed()); 86 } 87 88 @Test testPrefixAndMaskCombined()89 public void testPrefixAndMaskCombined() { 90 ExitFrame frame = new ExitFrame("123.56.*"); 91 92 assertArrayEquals(HexFormat.of().parseHex("123056"), frame.getData()); 93 assertArrayEquals(HexFormat.of().parseHex("FFF0FF"), frame.getDataMask()); 94 assertTrue(frame.isPrefixMatchingAllowed()); 95 } 96 97 @Test testStarInMiddleOfFilter()98 public void testStarInMiddleOfFilter() { 99 assertThrows(IllegalArgumentException.class, () -> new ExitFrame("123*56")); 100 } 101 102 @Test testStarWithoutDot()103 public void testStarWithoutDot() { 104 assertThrows(IllegalArgumentException.class, () -> new ExitFrame("1234*")); 105 } 106 107 @Test testTooLong()108 public void testTooLong() { 109 assertThrows(IllegalArgumentException.class, 110 () -> new ExitFrame("00112233445566778899AABBCCDDEEFF00")); 111 } 112 113 @Test testQuestionMarkInFilter()114 public void testQuestionMarkInFilter() { 115 assertThrows(IllegalArgumentException.class, () -> new ExitFrame("123?")); 116 } 117 118 @Test testOddNumberOfCharactersNoStar()119 public void testOddNumberOfCharactersNoStar() { 120 assertThrows(IllegalArgumentException.class, () -> new ExitFrame("123")); 121 } 122 } 123