Merge test folder of test branch
[iotivity.git] / test / src / common / commonutil / java / java-android / iotivityhelpers / ri / RIHelperCommon.java
1 /******************************************************************
2  * Copyright 2018 Samsung Electronics All Rights Reserved.
3  *
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  ******************************************************************/
18
19 package org.iotivity.test.ri.common;
20
21 import android.content.Context;
22
23 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.InputStream;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.OutputStream;
32
33 import java.util.concurrent.locks.Lock;
34 import java.util.concurrent.locks.ReentrantLock;
35 import java.util.Arrays;
36 import java.util.ArrayList;
37 import java.util.EnumSet;
38 import java.util.List;
39 import java.util.LinkedList;
40 import java.util.Map;
41
42 import org.iotivity.base.ModeType;
43 import org.iotivity.base.OcPlatform;
44 import org.iotivity.base.PlatformConfig;
45 import org.iotivity.base.QualityOfService;
46 import org.iotivity.base.ServiceType;
47
48 import org.iotivity.testcase.IoTivityLog;
49 import org.iotivity.testcase.IoTivityTc;
50 import org.iotivity.test.braodcast.TestBroadCast;
51
52 public class RIHelperCommon {
53     protected static Context    s_helperContext;
54     public static final String  CONFIG_FILE_PATH                 = "/data/local/tmp/";
55     private static final String DATABASES                        = "databases";
56     private static final String FILES                            = "files";
57     private static final String TAG                              = "RIHelperCommon";
58     private static final int    BUFFER_SIZE                      = 1024;
59     public static String        s_filePath                       = "";
60     public static String        s_sqLPath                        = "";
61     public static final String  DIR_NAME_FILE                    = "files";
62     public static final String  DIR_NAME_DATABASES               = "databases";
63
64     private static final String JUSTWORKS_SERVER_UNOWNED_CBOR_01 = "oic_svr_db_server_justworks.dat";
65     private static final String JUSTWORKS_SERVER_UNOWNED_CBOR_02 = "oic_svr_db_server.dat";
66     private static final String PRECONFIG_SERVER_UNOWNED_CBOR_01 = "oic_svr_db_server_preconfpin.dat";
67     private static final String JUSTWORKS_SERVER_01              = "oic_svr_db_server.dat";
68     private static final String JUSTWORKS_SERVER_02              = "oic_svr_db_server.dat";
69     private static final String UNOWNED_DIR                      = "unowned";
70     private static final String KILL_ALL_SERVER                  = "kill all server";
71     public static final String  START_JUSTWORKS_SERVER_01        = "./iotivity_pm_server "
72             + JUSTWORKS_SERVER_UNOWNED_CBOR_01 + " 1";
73     public static final String  START_JUSTWORKS_SERVER_02        = "./iotivity_pm_server "
74             + JUSTWORKS_SERVER_UNOWNED_CBOR_02 + " 1";
75     public static final String  START_PRE_CONFIG_SERVER_01       = "./iotivity_pm_server "
76             + PRECONFIG_SERVER_UNOWNED_CBOR_01 + " 3";
77     public static final String  START_RE_SERVER                  = "./iotivity_re_server";
78     public static final String  PROVISION_DB_FILE                = "./Pdm.db";
79     public static final String  DEVICE_PROP_CBOR_FILE            = "./device_properties.dat";
80
81     private TestBroadCast       mTestBroadCast;
82
83     protected RIHelperCommon(IoTivityTc iotivityTcObj) {
84         s_helperContext = iotivityTcObj.getInstrumentation().getTargetContext();
85         s_filePath = s_helperContext.getFilesDir().getPath();
86
87         s_sqLPath = s_helperContext.getFilesDir().getAbsolutePath()
88                 .replace(FILES, DATABASES) + File.separator;
89         mTestBroadCast = new TestBroadCast(s_helperContext);
90     }
91
92     public boolean configClientServerPlatform() {
93         PlatformConfig cfg = new PlatformConfig(s_helperContext,
94                 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0", 0,
95                 QualityOfService.HIGH);
96         OcPlatform.Configure(cfg);
97         IoTivityLog.i(TAG, "Platform Configuration with Context Succesful");
98         return true;
99     }
100
101     public void configClientServerPlatform(String fileName) {
102         PlatformConfig cfg = new PlatformConfig(s_helperContext,
103                 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0", // bind
104                                                                         // to
105                                                                         // all
106                                                                         // available
107                 // interfaces
108                 0, QualityOfService.LOW, s_filePath + fileName);
109         OcPlatform.Configure(cfg);
110         IoTivityLog.i(TAG, "Platform Configuration with Context Succesful");
111     }
112
113     public static String read(String filename) throws IOException {
114
115         StringBuffer buffer = new StringBuffer();
116
117         String Read = "";
118         FileInputStream fis = s_helperContext.openFileInput(filename);
119         BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
120         if (fis != null) {
121             while ((Read = reader.readLine()) != null) {
122                 buffer.append(Read + "\n");
123             }
124         }
125         fis.close();
126         return buffer.toString();
127     }
128
129     public boolean copyCborFromAsset(String clientCborDbFile) {
130         InputStream inputStream = null;
131         OutputStream outputStream = null;
132         int length;
133         byte[] buffer = new byte[BUFFER_SIZE];
134         s_filePath = s_helperContext.getFilesDir().getPath() + "/"; // data/data/<package>/files/
135
136         try {
137             inputStream = s_helperContext.getAssets().open(clientCborDbFile);
138             File file = new File(s_filePath);
139             // check files directory exists
140             if (!(file.exists() && file.isDirectory())) {
141                 file.mkdirs();
142             }
143             outputStream = new FileOutputStream(s_filePath + clientCborDbFile);
144             while ((length = inputStream.read(buffer)) != -1) {
145                 outputStream.write(buffer, 0, length);
146             }
147         } catch (NullPointerException e) {
148             IoTivityLog.e(TAG, "Null pointer exception " + e.getMessage());
149         } catch (FileNotFoundException e) {
150             IoTivityLog.e(TAG, "Cbor svr db file not found " + e.getMessage());
151         } catch (IOException e) {
152             IoTivityLog.e(TAG, clientCborDbFile + " file copy failed");
153         } finally {
154             if (inputStream != null) {
155                 try {
156                     inputStream.close();
157                 } catch (IOException e) {
158                     IoTivityLog.e(TAG, e.getMessage());
159                 }
160             }
161             if (outputStream != null) {
162                 try {
163                     outputStream.close();
164                 } catch (IOException e) {
165                     IoTivityLog.e(TAG, e.getMessage());
166                 }
167             }
168         }
169
170         return true;
171     }
172
173     public void enableMocking() {
174         System.setProperty("dexmaker.dexcache",
175                 s_helperContext.getCacheDir().getPath());
176     }
177
178     public void startREServer(String command) {
179         mTestBroadCast.sendBroadCastToTcpClient(command, true);
180     }
181
182     public void startSecuredServer(String command) {
183         mTestBroadCast.sendBroadCastToTcpClient(command, true);
184     }
185
186     public void stopServers() {
187         mTestBroadCast.sendBroadCastToTcpClient(KILL_ALL_SERVER, true);
188     }
189
190     public void clearAll() {
191
192         File folder = new File(s_filePath);
193
194         File[] listOfFiles = folder.listFiles();
195
196         for (int i = 0; i < listOfFiles.length; i++) {
197             if (listOfFiles[i].isFile()) {
198                 IoTivityLog.i(TAG, "File " + listOfFiles[i].getName());
199                 listOfFiles[i].delete();
200             } else if (listOfFiles[i].isDirectory()) {
201                 IoTivityLog.i(TAG, "Directory " + listOfFiles[i].getName());
202             }
203         }
204
205         String filePath = s_helperContext.getFilesDir().getAbsolutePath()
206                 .replace(DIR_NAME_FILE, DIR_NAME_DATABASES) + File.separator;
207         IoTivityLog.i(TAG, "Clearing File from : " + filePath);
208
209         folder = new File(filePath);
210
211         if (!folder.exists()) {
212             return;
213         }
214
215         listOfFiles = folder.listFiles();
216
217         for (int i = 0; i < listOfFiles.length; i++) {
218             if (listOfFiles[i].isFile()) {
219                 IoTivityLog.i(TAG, "File " + listOfFiles[i].getName());
220                 listOfFiles[i].delete();
221             } else if (listOfFiles[i].isDirectory()) {
222                 IoTivityLog.i(TAG, "Directory " + listOfFiles[i].getName());
223             }
224         }
225     }
226
227 }