• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 /**
33  * Tests connectivity to a custom SNEP server, using a physical NFC device.
34  */
35 public class SnepValidationClientTests extends AndroidTestCase {
36     private static final String TAG = "nfcTest";
37 
38     private static final int FRAGMENT_LENGTH = 50;
39 
40     public static final String SERVICE_NAME = SnepValidationServerTests.SERVICE_NAME;
41 
setUp()42     public void setUp() {
43         Log.d(TAG, "Waiting for service to restart...");
44         try {
45             Thread.sleep(8000);
46         } catch (InterruptedException e) {
47         }
48         Log.d(TAG, "Running test.");
49     }
50 
testNonFragmented()51     public void testNonFragmented() throws IOException {
52         try {
53             SnepClient client = getSnepClient();
54             NdefMessage msg = getSmallNdef();
55             Log.d(TAG, "Connecting to service " + SERVICE_NAME + "...");
56             client.connect();
57             Log.d(TAG, "Putting ndef message...");
58             client.put(msg);
59 
60             Log.d(TAG, "Getting ndef message...");
61             byte[] responseBytes = client.get(msg).getNdefMessage().toByteArray();
62             int i = 0;
63             byte[] msgBytes = msg.toByteArray();
64             Log.d(TAG, "Done. Checking " + msgBytes.length + " bytes.");
65             for (byte b : msgBytes) {
66                 assertEquals(b, responseBytes[i++]);
67             }
68             Log.d(TAG, "Closing client.");
69             client.close();
70         } catch (IOException e) {
71             Log.d(TAG, "Test failed.", e);
72             throw e;
73         }
74     }
75 
testFragmented()76     public void testFragmented() throws IOException {
77         try {
78             SnepClient client = getSnepClient();
79             NdefMessage msg = getLargeNdef();
80             Log.d(TAG, "Connecting to service " + SERVICE_NAME + "...");
81             client.connect();
82             Log.d(TAG, "Putting ndef message of size " + msg.toByteArray().length + "...");
83             client.put(msg);
84 
85             Log.d(TAG, "Getting ndef message...");
86             byte[] responseBytes = client.get(msg).getNdefMessage().toByteArray();
87             int i = 0;
88             byte[] msgBytes = msg.toByteArray();
89             Log.d(TAG, "Done. Checking " + msgBytes.length + " bytes.");
90             for (byte b : msgBytes) {
91                 assertEquals(b, responseBytes[i++]);
92             }
93             client.close();
94         } catch (IOException e) {
95             Log.d(TAG, "Error running fragmented", e);
96             throw e;
97         }
98     }
99 
testMultipleNdef()100     public void testMultipleNdef() throws IOException {
101         try {
102             SnepClient client = getSnepClient();
103             Log.d(TAG, "Connecting to service " + SERVICE_NAME + "...");
104             client.connect();
105 
106             NdefMessage msgA = getSmallNdef();
107             NdefMessage msgB = getLargeNdef();
108 
109             Log.d(TAG, "Putting ndef message A...");
110             client.put(msgA);
111             Log.d(TAG, "Putting ndef message B...");
112             client.put(msgB);
113 
114             byte[] responseBytes;
115             byte[] msgBytes;
116             int i;
117 
118             Log.d(TAG, "Getting ndef message A...");
119             responseBytes = client.get(msgA).getNdefMessage().toByteArray();
120             i = 0;
121             msgBytes = msgA.toByteArray();
122             Log.d(TAG, "Done. Checking " + msgBytes.length + " bytes.");
123             for (byte b : msgBytes) {
124                 assertEquals(b, responseBytes[i++]);
125             }
126 
127             Log.d(TAG, "Getting ndef message B...");
128             responseBytes = client.get(msgB).getNdefMessage().toByteArray();
129             i = 0;
130             msgBytes = msgB.toByteArray();
131             Log.d(TAG, "Done. Checking " + msgBytes.length + " bytes.");
132             for (byte b : msgBytes) {
133                 assertEquals(b, responseBytes[i++]);
134             }
135 
136             Log.d(TAG, "Closing client.");
137             client.close();
138         } catch (IOException e) {
139             Log.d(TAG, "Test failed.", e);
140             throw e;
141         }
142     }
143 
testUnavailable()144     public void testUnavailable() throws IOException {
145         try {
146             SnepClient client = getSnepClient();
147             NdefMessage msg = getSmallNdef();
148             Log.d(TAG, "Connecting to service " + SERVICE_NAME + "...");
149             client.connect();
150 
151             Log.d(TAG, "Getting ndef message...");
152             SnepMessage response = client.get(msg);
153             assertEquals(SnepMessage.RESPONSE_NOT_FOUND, response.getField());
154             Log.d(TAG, "Closing client.");
155             client.close();
156         } catch (IOException e) {
157             Log.d(TAG, "Test failed.", e);
158             throw e;
159         }
160     }
161 
testUndeliverable()162     public void testUndeliverable() throws IOException {
163         try {
164             SnepClient client = new SnepClient(SERVICE_NAME, 100, FRAGMENT_LENGTH);
165             NdefMessage msg = getLargeNdef();
166             Log.d(TAG, "Connecting to service " + SERVICE_NAME + "...");
167             client.connect();
168             Log.d(TAG, "Putting ndef message of size " + msg.toByteArray().length + "...");
169             client.put(msg);
170 
171             Log.d(TAG, "Getting ndef message...");
172             SnepMessage response = client.get(msg);
173             assertEquals(SnepMessage.RESPONSE_EXCESS_DATA, response.getField());
174             client.close();
175         } catch (IOException e) {
176             Log.d(TAG, "Error running fragmented", e);
177             throw e;
178         }
179     }
180 
getSmallNdef()181     private NdefMessage getSmallNdef() {
182         NdefRecord rec = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI,
183                 new byte[] { 'A' }, "http://android.com".getBytes());
184         return new NdefMessage(new NdefRecord[] { rec });
185     }
186 
getLargeNdef()187     private NdefMessage getLargeNdef() {
188         int size = 500;
189         StringBuffer string = new StringBuffer(size);
190         for (int i = 0; i < size; i++) {
191             string.append('A' + (i % 26));
192         }
193         NdefRecord rec = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(),
194                 new byte[] { 'B' }, string.toString().getBytes());
195         return new NdefMessage(new NdefRecord[] { rec });
196     }
197 
getSnepClient()198     private SnepClient getSnepClient() {
199         return new SnepClient(SERVICE_NAME, FRAGMENT_LENGTH);
200     }
201 }
202