Weiwei | e5640c6 | 2015-07-31 01:43:01 -0700 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2014 Regents of the University of California. |
| 4 | # Author: Weiwei Liu <summerwing10@gmail.com> |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU Lesser General Public License as published by |
| 8 | # the Free Software Foundation, either version 3 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | # A copy of the GNU General Public License is in the file COPYING. |
| 19 | |
| 20 | import unittest as ut |
| 21 | import os.path |
| 22 | from pyndn import Name |
Weiwei | f3132ed | 2015-08-05 11:20:49 -0700 | [diff] [blame^] | 23 | from device_storage import DeviceStorage |
Weiwei | e5640c6 | 2015-07-31 01:43:01 -0700 | [diff] [blame] | 24 | from device_profile import DeviceProfile |
| 25 | from hmac_key import HMACKey |
| 26 | |
Weiwei | f3132ed | 2015-08-05 11:20:49 -0700 | [diff] [blame^] | 27 | class TestDeviceStorageMethods(ut.TestCase): |
Weiwei | e5640c6 | 2015-07-31 01:43:01 -0700 | [diff] [blame] | 28 | def setUp(self): |
| 29 | if not "HOME" in os.environ: |
| 30 | home = '.' |
| 31 | else: |
| 32 | home = os.environ["HOME"] |
| 33 | |
| 34 | dbDirectory = os.path.join(home, '.ndn') |
| 35 | self.databaseFilePath = os.path.join(dbDirectory, 'ndnhome-controller.db') |
| 36 | |
| 37 | if os.path.isfile(self.databaseFilePath): |
| 38 | os.remove(self.databaseFilePath) |
| 39 | |
Weiwei | f3132ed | 2015-08-05 11:20:49 -0700 | [diff] [blame^] | 40 | self.storage = DeviceStorage() |
Weiwei | e5640c6 | 2015-07-31 01:43:01 -0700 | [diff] [blame] | 41 | |
| 42 | def tearDown(self): |
| 43 | pass |
| 44 | |
| 45 | def test_01_storage_constructor(self): |
| 46 | |
| 47 | self.assertTrue(os.path.isfile(self.databaseFilePath), 'fail to create database file') |
| 48 | self.assertTrue(self.storage.doesTableExist("Device"), "Device table doesn't exist") |
| 49 | self.assertTrue(self.storage.doesTableExist("Command"), "Command table doesn't exist") |
| 50 | self.assertTrue(self.storage.doesTableExist("ServiceProfile"), "ServiceProfile table doesn't exist") |
| 51 | #test constructor with file path HOME/.ndn/homedb |
| 52 | home = os.environ["HOME"] |
| 53 | dir = os.path.join(home, '.ndn') |
| 54 | dbdir = os.path.join(dir, 'homedb') |
| 55 | |
| 56 | if not os.path.exists(dbdir): |
| 57 | os.makedirs(dbdir) |
| 58 | |
| 59 | filePath = os.path.join(dbdir, 'ndnhome-controller.db') |
Weiwei | f3132ed | 2015-08-05 11:20:49 -0700 | [diff] [blame^] | 60 | storage2 = DeviceStorage(filePath) |
Weiwei | e5640c6 | 2015-07-31 01:43:01 -0700 | [diff] [blame] | 61 | self.assertTrue(os.path.isfile(filePath), 'fail to create database file' ) |
| 62 | os.remove(filePath) |
| 63 | |
| 64 | def test_02_entry_existence_methods(self): |
| 65 | # test the existence of non-existed device (prefix = /home/sensor/LED/1) |
| 66 | prefixStr = '/home/sensor/LED/1' |
| 67 | name = Name(prefixStr) |
| 68 | profile = DeviceProfile(prefix = name) |
| 69 | keyContent = 'this is key content' |
| 70 | seed = HMACKey( 0, 0 ,keyContent, 'led1') |
| 71 | configurationToken = HMACKey(0, 0, keyContent) |
| 72 | result = self.storage.doesDeviceExist(name) |
| 73 | self.assertFalse(result, "device with prefix '" + prefixStr + "' shouldn't exist") |
| 74 | |
| 75 | #test existence of an existed device |
| 76 | self.storage.addDevice(profile, seed, configurationToken) |
| 77 | result = self.storage.doesDeviceExist(name) |
| 78 | self.assertTrue(result, "device with prefix '" + prefixStr +"' should exist") |
| 79 | |
| 80 | #test existence of a non-existed command |
| 81 | deviceId = 1 |
| 82 | commandName = 'turn_on' |
| 83 | result = self.storage.doesCommandExist(deviceId, commandName) |
| 84 | self.assertFalse(result, "Command : 'device_id =" + str(deviceId) + ", name = " + commandName + "' shouldn't exist" ) |
| 85 | |
| 86 | #test existence of a existed command |
| 87 | deviceId = self.storage.getDeviceId(name) |
| 88 | commandName = 'turn_on' |
| 89 | commandToken = self.create_a_default_key('turn_on') |
| 90 | self.storage.addCommand(deviceId, commandName, commandToken) |
| 91 | result = self.storage.doesCommandExist(deviceId, commandName) |
| 92 | self.assertTrue(result, "Command : 'device_id =" + str(deviceId) + ", name = " + commandName + "' should exist" ) |
| 93 | |
| 94 | #test existence of a non-existed service profile |
| 95 | deviceId = 1 |
| 96 | serviceProfileName = '/standard/sensor/simple-LED-control/v0' |
| 97 | |
| 98 | result = self.storage.doesServiceProfileExist(deviceId, serviceProfileName) |
| 99 | self.assertFalse(result, "service profile : 'device_id =" + str(deviceId) + ", name = " + serviceProfileName + "' shouldn't exist" ) |
| 100 | |
| 101 | #test existence of a existed service profile |
| 102 | deviceId = self.storage.getDeviceId(name) |
| 103 | serviceProfileName = '/standard/sensor/simple-LED-control/v0' |
| 104 | |
| 105 | self.storage.addServiceProfile(deviceId, serviceProfileName) |
| 106 | result = self.storage.doesServiceProfileExist(deviceId, serviceProfileName) |
| 107 | self.assertTrue(result, "service profile : 'device_id =" + str(deviceId) + ", name = " + serviceProfileName + "' should exist" ) |
| 108 | |
| 109 | |
| 110 | def test_03_add_device(self): |
| 111 | prefixStr = '/home/sensor/LED/1' |
| 112 | name = Name(prefixStr) |
| 113 | profile = DeviceProfile(prefix = name) |
| 114 | keyContent = 'this is key content' |
| 115 | seed = HMACKey( 0, 0 ,keyContent, 'led1') |
| 116 | configurationToken = HMACKey(0, 0, keyContent) |
| 117 | |
| 118 | result = self.storage.addDevice(profile, seed, configurationToken) |
| 119 | |
| 120 | self.assertTrue(result > 0, 'fail to add device entry') |
| 121 | self.assertTrue(self.storage.doesDeviceExist(name), "device doesn't exist in table") |
| 122 | row = self.storage.getDeviceEntry(name) |
| 123 | self.assertTrue(row[1] == prefixStr, "column prefix has incorrect value") |
| 124 | #print row[2:7] |
| 125 | self.assertTrue(row[2:8] == (None, None, None, None, None, None), "column 2-7 have incorrect values ") |
| 126 | self.assertTrue(row[8] == 'led1', 'colum seed_name has incorrect values') |
| 127 | self.assertTrue(row[9:12] == (0,0,keyContent), 'colum seed_sequence or seed_counter has incorrect value') |
| 128 | self.assertTrue(row[12:] == (keyContent,0,0), 'one or more configuration token columns have incoorect value') |
| 129 | #try to insert the same device |
| 130 | result = self.storage.addDevice(profile, seed, configurationToken) |
| 131 | self.assertTrue(result == 0, 'unexpected result when trying to insert an already existed device') |
| 132 | |
| 133 | def test_04_delete_device(self): |
| 134 | name = Name('/home/sensor/LED/1') |
| 135 | profile = DeviceProfile(prefix = name) |
| 136 | seed = HMACKey( 0, 0 ,'this is key content', 'led1') |
| 137 | configurationToken = HMACKey(0, 0, 'this is key content') |
| 138 | self.storage.addDevice(profile, seed, configurationToken) |
| 139 | self.assertTrue(self.storage.doesDeviceExist(name), "device doesn't exist in table") |
| 140 | result = self.storage.deleteDevice(name) |
| 141 | #print 'result : %d' %(result) |
| 142 | self.assertTrue(result == 1, 'fail to delete device') |
| 143 | |
| 144 | def test_05_get_deviceid(self): |
| 145 | name = Name('/home/sensor/LED/1') |
| 146 | profile = DeviceProfile(prefix = name) |
| 147 | seed = HMACKey( 0, 0 ,'this is key content', 'led1') |
| 148 | configurationToken = HMACKey(0, 0, 'this is key content') |
| 149 | self.storage.addDevice(profile, seed, configurationToken) |
| 150 | deviceId = self.storage.getDeviceId(name) |
| 151 | self.assertTrue(deviceId==1, "get a wrong device id") |
| 152 | |
| 153 | name2 = Name('/home/sensor/LED/2') |
| 154 | profile = DeviceProfile(prefix = name2) |
| 155 | seed = HMACKey( 0, 0 ,'this is key content', 'led1') |
| 156 | configurationToken = HMACKey(0, 0, 'this is key content') |
| 157 | self.storage.addDevice(profile, seed, configurationToken) |
| 158 | deviceId2 = self.storage.getDeviceId(name2) |
| 159 | self.assertTrue(deviceId2 == 2, "get a wrong device id") |
| 160 | |
| 161 | def test_06_update_device(self): |
| 162 | prefixStr = '/home/sensor/LED/1' |
| 163 | name = Name(prefixStr) |
| 164 | self.add_a_default_device(prefixStr) |
| 165 | |
| 166 | #update column prefix of device |
| 167 | newPrefixStr = '/home/sensor/LED/2' |
| 168 | newSeedName = 'led2' |
| 169 | self.storage.updateOneColumnOfDevice(name, 'seed_name', newSeedName) |
| 170 | row = self.storage.getDeviceEntry(name) |
| 171 | self.assertTrue(row[8] == newSeedName, "fail to update column: seed_name") |
| 172 | |
| 173 | self.storage.updateOneColumnOfDevice(name, 'prefix', newPrefixStr) |
| 174 | row = self.storage.getDeviceEntry(Name(newPrefixStr)) |
| 175 | |
| 176 | self.assertTrue(row[1] == newPrefixStr, "fail to update coumn: prefix") |
| 177 | |
| 178 | def test_07_add_command(self): |
| 179 | prefixStr = '/home/sensor/LED/2' |
| 180 | name = Name(prefixStr) |
| 181 | self.add_a_default_device(prefixStr) |
| 182 | deviceId = self.storage.getDeviceId(name) |
| 183 | commandName = 'turn_on' |
| 184 | commandToken = self.create_a_default_key('turn_on') |
| 185 | self.storage.addCommand(deviceId, commandName, commandToken) |
| 186 | result = self.storage.doesCommandExist(deviceId, commandName) |
| 187 | self.assertTrue(result == True, "fail to add command") |
| 188 | |
| 189 | def test_08_delete_command(self): |
| 190 | prefixStr1 = 'home/sensor/LED/1' |
| 191 | name1 = Name(prefixStr1) |
| 192 | self.add_a_default_device(prefixStr1) |
| 193 | deviceId = self.storage.getDeviceId(name1) |
| 194 | commandName = 'turn_on' |
| 195 | commandToken = self.create_a_default_key(commandName) |
| 196 | self.storage.addCommand(deviceId, commandName, commandToken) |
| 197 | self.assertTrue(self.storage.doesCommandExist(deviceId, commandName), 'before delete, the command should exist') |
| 198 | self.storage.deleteCommand(deviceId, commandName) |
| 199 | self.assertFalse(self.storage.doesCommandExist(deviceId, commandName), "after delete, the command shouldn't exist") |
| 200 | |
| 201 | def test_09_get_commands_of_device(self): |
| 202 | #test with an empty table |
| 203 | deviceId = 2 |
| 204 | result = self.storage.getCommandsOfDevice(deviceId) |
| 205 | print result |
| 206 | self.assertTrue(not result, "there should be no command found") |
| 207 | |
| 208 | #test with a device has two commands |
| 209 | prefixStr = 'home/sensor/LED/1' |
| 210 | name = Name(prefixStr) |
| 211 | self.add_a_default_device(prefixStr) |
| 212 | deviceId = self.storage.getDeviceId(name) |
| 213 | commandName = 'turn_on' |
| 214 | commandName2 = 'turn_off' |
| 215 | commandToken = self.create_a_default_key(commandName) |
| 216 | commandToken2 = self.create_a_default_key(commandName2) |
| 217 | self.storage.addCommand(deviceId, commandName, commandToken) |
| 218 | self.storage.addCommand(deviceId, commandName2, commandToken) |
| 219 | result = self.storage.getCommandsOfDevice(deviceId) |
| 220 | print 'result %s' %(result) |
| 221 | |
| 222 | def test_10_add_service_profile(self): |
| 223 | prefixStr = '/home/sensor/LED/2' |
| 224 | name = Name(prefixStr) |
| 225 | self.add_a_default_device(prefixStr) |
| 226 | deviceId = self.storage.getDeviceId(name) |
| 227 | |
| 228 | serviceProfileName = '/standard/sensor/simple-LED-control/v0' |
| 229 | self.storage.addServiceProfile(deviceId, serviceProfileName) |
| 230 | result = self.storage.doesServiceProfileExist(deviceId, serviceProfileName) |
| 231 | self.assertTrue(result == True, "fail to add service profile") |
| 232 | |
| 233 | def test_11_delete_service_profile(self): |
| 234 | prefixStr1 = 'home/sensor/LED/1' |
| 235 | name1 = Name(prefixStr1) |
| 236 | self.add_a_default_device(prefixStr1) |
| 237 | deviceId = self.storage.getDeviceId(name1) |
| 238 | serviceProfileName = '/standard/sensor/simple-LED-control/v0' |
| 239 | |
| 240 | self.storage.addServiceProfile(deviceId, serviceProfileName) |
| 241 | self.assertTrue(self.storage.doesServiceProfileExist(deviceId, serviceProfileName), 'before delete, the service profile should exist') |
| 242 | self.storage.deleteServiceProfile(deviceId, serviceProfileName) |
| 243 | self.assertFalse(self.storage.doesServiceProfileExist(deviceId, serviceProfileName), "after delete, the service profile shouldn't exist") |
| 244 | |
| 245 | def test_12_get_service_profiles_of_device(self): |
| 246 | #test with an empty table |
| 247 | deviceId = 2 |
| 248 | result = self.storage.getServiceProfilesOfDevice(deviceId) |
| 249 | print result |
| 250 | self.assertTrue(not result, "there should be no command found") |
| 251 | |
| 252 | #test with a device has two commands |
| 253 | prefixStr = 'home/sensor/LED/1' |
| 254 | name = Name(prefixStr) |
| 255 | self.add_a_default_device(prefixStr) |
| 256 | deviceId = self.storage.getDeviceId(name) |
| 257 | serviceProfileName = '/standard/sensor/simple-LED-control/v0' |
| 258 | serviceProfileName2 = '/standard/sensor/simple-LED-control/v2' |
| 259 | self.storage.addServiceProfile(deviceId, serviceProfileName) |
| 260 | self.storage.addServiceProfile(deviceId, serviceProfileName2) |
| 261 | result = self.storage.getServiceProfilesOfDevice(deviceId) |
| 262 | print 'result %s' %(result) |
| 263 | |
| 264 | def test_13_get_device_profile_from_device(self): |
| 265 | #test with a non exisited device prefix |
| 266 | prefixStr = '/home/sensor/LED/2' |
| 267 | name = Name(prefixStr) |
| 268 | deviceProfile = self.storage.getDeviceProfileFromDevice(name) |
| 269 | self.assertTrue(deviceProfile == None, "no device profile should be return with an non-existed prefix ") |
| 270 | |
| 271 | #test with a existed device prefix |
| 272 | profile = DeviceProfile(prefix = name, location = 'bedroom', category = 'sensors') |
| 273 | seed = self.create_a_default_key('led1') |
| 274 | configurationToken = self.create_a_default_key() |
| 275 | self.storage.addDevice(profile, seed, configurationToken) |
| 276 | |
| 277 | deviceProfile = self.storage.getDeviceProfileFromDevice(name) |
| 278 | |
| 279 | self.assertTrue(deviceProfile.getLocation() == 'bedroom', 'wrong field: location') |
| 280 | self.assertTrue(deviceProfile.getCategory() == 'sensors', 'wrong field: category') |
| 281 | |
| 282 | def test_14_get_seed_and_get_configuration_token(self): |
| 283 | #test with a non existed device prefix |
| 284 | prefixStr = '/home/sensor/LED/2' |
| 285 | name = Name(prefixStr) |
| 286 | seed = self.storage.getSeed(name) |
| 287 | configurationToken = self.storage.getConfigurationToken(name) |
| 288 | self.assertTrue(seed == None, "no seed should be returned with an non-existed prefix ") |
| 289 | self.assertTrue(configurationToken == None, "no configuration token should be returned with an non-existed prefix ") |
| 290 | |
| 291 | #test with an existed device prefix |
| 292 | profile= DeviceProfile(prefix = name) |
| 293 | seed = self.create_a_default_key('led2') |
| 294 | configurationToken = self.create_a_default_key() |
| 295 | self.storage.addDevice(profile, seed, configurationToken) |
| 296 | seed = self.storage.getSeed(name) |
| 297 | configurationToken = self.storage.getConfigurationToken(name) |
| 298 | keyContent = 'this is key content' |
| 299 | self.assertTrue(seed.getKey() == keyContent, 'key content of seed is incorrect') |
| 300 | self.assertTrue(configurationToken.getKey() == keyContent, 'key content of configuration token is incorrect') |
| 301 | self.assertTrue(seed.getName() == 'led2', 'name of seed is incorrect') |
| 302 | self.assertTrue(configurationToken.getName() == None, 'name of configuration token is incorrect') |
| 303 | |
| 304 | def test_15_get_Command_Token(self): |
| 305 | #test with a non existed command |
| 306 | deviceId = 5 |
| 307 | commandName = "turn_on" |
| 308 | commandToken = self.storage.getCommandToken(deviceId, commandName) |
| 309 | self.assertTrue(commandToken == None, 'no commandToken should be returned with an non-existed command') |
| 310 | |
| 311 | #test with an existed command |
| 312 | prefixStr = 'home/sensor/LED/1' |
| 313 | name = Name(prefixStr) |
| 314 | self.add_a_default_device(prefixStr) |
| 315 | deviceId = self.storage.getDeviceId(name) |
| 316 | commandToken = self.create_a_default_key(commandName) |
| 317 | self.storage.addCommand(deviceId, commandName, commandToken) |
| 318 | |
| 319 | commandTokenReturned = self.storage.getCommandToken(deviceId, commandName) |
| 320 | #print commandTokenReturned.getName() |
| 321 | self.assertTrue(commandTokenReturned.getName() == commandName, 'wrong command token') |
| 322 | |
| 323 | def add_a_default_device(self, prefixStr): |
| 324 | name = Name(prefixStr) |
| 325 | profile = DeviceProfile(prefix = name) |
| 326 | seed = self.create_a_default_key('led1') |
| 327 | configurationToken = self.create_a_default_key() |
| 328 | self.storage.addDevice(profile, seed, configurationToken) |
| 329 | |
| 330 | def create_a_default_key(self, keyName = None): |
| 331 | keyContent = 'this is key content' |
| 332 | seed = HMACKey(0,0, keyContent, keyName) |
| 333 | return seed |
| 334 | |
| 335 | if __name__ == '__main__': |
| 336 | ut.main(verbosity=2) |
| 337 | |
| 338 | |