1 /* 2 * Copyright (C) 2011 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.nfc.snep; 18 19 import java.io.IOException; 20 21 import com.android.nfc.snep.SnepClient; 22 import com.android.nfc.snep.SnepMessage; 23 import com.android.nfc.snep.SnepServer; 24 25 import android.nfc.NdefMessage; 26 import android.nfc.NdefRecord; 27 import android.test.AndroidTestCase; 28 import android.util.Log; 29 30 import java.lang.StringBuffer; 31 import java.util.Arrays; 32 import java.util.HashMap; 33 import java.util.Map; 34 35 /** 36 * Uses the Android testing instrumentation framework to run a listening SNEP 37 * server. 38 */ 39 public class SnepValidationServerTests extends AndroidTestCase implements SnepServer.Callback { 40 static final int SERVICE_SAP = 0x11; 41 static final String SERVICE_NAME = "urn:nfc:xsn:nfc-forum.org:snep-validation"; 42 private static final String TAG = "nfcTest"; 43 44 private Map<ByteArrayWrapper, NdefMessage> mStoredNdef = 45 new HashMap<ByteArrayWrapper, NdefMessage>(); 46 private static final ByteArrayWrapper DEFAULT_NDEF = new ByteArrayWrapper(new byte[] {}); 47 48 public void setUp() { 49 Log.d(TAG, "Waiting for service to restart..."); 50 try { 51 Thread.sleep(6000); 52 } catch (InterruptedException e) { 53 } 54 Log.d(TAG, "Running test."); 55 } 56 57 public void testStartSnepServer() throws IOException { 58 SnepServer server = new SnepServer(SERVICE_NAME, SERVICE_SAP, this); 59 server.start(); 60 61 try { 62 Thread.sleep(24 * 60 * 60 * 1000); 63 } catch (InterruptedException e) { 64 65 } 66 } 67 68 private NdefMessage getSmallNdef() { 69 NdefRecord rec = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI, 70 new byte[0], "http://android.com".getBytes()); 71 return new NdefMessage(new NdefRecord[] { rec }); 72 } 73 74 private NdefMessage getLargeNdef() { 75 int size = 3000; 76 StringBuffer string = new StringBuffer(size); 77 for (int i = 0; i < size; i++) { 78 string.append('A' + (i % 26)); 79 } 80 NdefRecord rec = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(), 81 new byte[0], string.toString().getBytes()); 82 return new NdefMessage(new NdefRecord[] { rec }); 83 } 84 85 static class ByteArrayWrapper { 86 private final byte[] data; 87 88 public ByteArrayWrapper(byte[] data) { 89 if (data == null) { 90 throw new NullPointerException(); 91 } 92 this.data = data; 93 } 94 95 @Override 96 public boolean equals(Object other) { 97 if (!(other instanceof ByteArrayWrapper)) { 98 return false; 99 } 100 return Arrays.equals(data, ((ByteArrayWrapper) other).data); 101 } 102 103 @Override 104 public int hashCode() { 105 return Arrays.hashCode(data); 106 } 107 } 108 109 @Override 110 public SnepMessage doPut(NdefMessage msg) { 111 Log.d(TAG, "doPut()"); 112 NdefRecord record = msg.getRecords()[0]; 113 ByteArrayWrapper id = (record.getId().length > 0) ? 114 new ByteArrayWrapper(record.getId()) : DEFAULT_NDEF; 115 mStoredNdef.put(id, msg); 116 return SnepMessage.getMessage(SnepMessage.RESPONSE_SUCCESS); 117 } 118 119 @Override 120 public SnepMessage doGet(int acceptableLength, NdefMessage msg) { 121 Log.d(TAG, "doGet()"); 122 NdefRecord record = msg.getRecords()[0]; 123 ByteArrayWrapper id = (record.getId().length > 0) ? 124 new ByteArrayWrapper(record.getId()) : DEFAULT_NDEF; 125 NdefMessage result = mStoredNdef.get(id); 126 127 if (result == null) { 128 return SnepMessage.getMessage(SnepMessage.RESPONSE_NOT_FOUND); 129 } 130 if (acceptableLength < result.toByteArray().length) { 131 return SnepMessage.getMessage(SnepMessage.RESPONSE_EXCESS_DATA); 132 } 133 return SnepMessage.getSuccessResponse(result); 134 } 135 } 136