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 default SNEP server, using a physical NFC device.
34  */
35 public class SnepDefaultClientTests extends AndroidTestCase {
36     private static final String TAG = "nfcTest";
37 
setUp()38     public void setUp() {
39         Log.d(TAG, "Waiting for service to restart...");
40         try {
41             Thread.sleep(6000);
42         } catch (InterruptedException e) {}
43         Log.d(TAG, "Running test.");
44     }
45 
testPutSmallToDefaultServer()46     public void testPutSmallToDefaultServer() throws IOException {
47         SnepClient client = new SnepClient();
48         client.connect();
49         client.put(getSmallNdef());
50         client.close();
51     }
52 
testPutLargeToDefaultServer()53     public void testPutLargeToDefaultServer() throws IOException {
54         SnepClient client = new SnepClient();
55         client.connect();
56         client.put(getLargeNdef());
57         client.close();
58     }
59 
testPutTwiceToDefaultServer()60     public void testPutTwiceToDefaultServer() throws IOException {
61         SnepClient client = new SnepClient();
62         client.connect();
63         client.put(getSmallNdef());
64         client.put(getSmallNdef());
65         client.close();
66     }
67 
testGetFail()68     public void testGetFail() throws IOException {
69         SnepClient client = new SnepClient();
70         client.connect();
71         SnepMessage response = client.get(getSmallNdef());
72         assertEquals(SnepMessage.RESPONSE_NOT_IMPLEMENTED, response.getField());
73     }
74 
getSmallNdef()75     private NdefMessage getSmallNdef() {
76         NdefRecord rec = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI,
77                 new byte[0], "http://android.com".getBytes());
78         return new NdefMessage(new NdefRecord[] { rec });
79     }
80 
getLargeNdef()81     private NdefMessage getLargeNdef() {
82         int size = 3000;
83         StringBuffer string = new StringBuffer(size);
84         for (int i = 0; i < size; i++) {
85             string.append('A' + (i % 26));
86         }
87         NdefRecord rec = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(),
88                 new byte[0], string.toString().getBytes());
89         return new NdefMessage(new NdefRecord[] { rec });
90     }
91 }
92