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 |
| 23 | from device_user_access_manager import DeviceUserAccessManager |
| 24 | from device_profile import DeviceProfile |
| 25 | from hmac_key import HMACKey |
| 26 | |
| 27 | class TestManagerMethods(ut.TestCase): |
| 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 | self.manager = DeviceUserAccessManager() |
| 40 | |
| 41 | def tearDown(self): |
| 42 | pass |
| 43 | |
| 44 | def test_01_manager_constructor(self): |
| 45 | print("test") |
| 46 | if not "HOME" in os.environ: |
| 47 | home = '.' |
| 48 | else: |
| 49 | home = os.environ["HOME"] |
| 50 | |
| 51 | dbDirectory = os.path.join(home, '.ndn') |
| 52 | self.databaseFilePath = os.path.join(dbDirectory, 'ndnhome-controller.db') |
| 53 | self.manager = DeviceUserAccessManager() |
| 54 | self.assertTrue(os.path.isfile(self.databaseFilePath), 'fail to create database file') |
| 55 | |
| 56 | def test_02_methods(self): |
| 57 | #create device |
| 58 | prefixStr = '/home/sensor/LED/1' |
| 59 | name = Name(prefixStr) |
| 60 | profile = DeviceProfile(prefix = name) |
| 61 | location = 'living_room' |
| 62 | profile.setLocation(location) |
| 63 | serviceProfileList = ['/standard/sensor/simple-camera-control/v0', '/standard/sensor/simple-motionsensor-control/v0'] |
| 64 | profile.setServiceProfile(serviceProfileList) |
| 65 | keyContent = 'this is key content' |
| 66 | seedName = 'led1' |
| 67 | seed = HMACKey( 0, 0 ,keyContent, seedName) |
| 68 | configurationToken = HMACKey(0, 0, keyContent) |
| 69 | commandTokenName1 = 'commandToken1' |
| 70 | commandTokenName2 = 'commandToken2' |
| 71 | commandToken = HMACKey(0,0, keyContent, commandTokenName1) |
| 72 | commandToken2 = HMACKey(0,0,keyContent, commandTokenName2) |
| 73 | commandName1 = 'turn_on' |
| 74 | commandName2 = 'turn_off' |
| 75 | commandTuple = (commandName1, commandToken) |
| 76 | commandTuple2 = (commandName2, commandToken2) |
| 77 | commandList = [commandTuple, commandTuple2] |
| 78 | result = self.manager.createDevice(profile, seed, configurationToken, commandList) |
| 79 | self.assertTrue(result, 'fail to create device') |
| 80 | |
| 81 | #getDeviceProfile() |
| 82 | deviceProfile = self.manager.getDeviceProfile(name) |
| 83 | self.assertTrue(deviceProfile.getLocation() == location, 'wrong location in device profile ') |
| 84 | self.assertTrue(deviceProfile.getServiceProfileList() == serviceProfileList, 'wrong service profile list in device profile' ) |
| 85 | |
| 86 | #getSeed() |
| 87 | seed = self.manager.getSeed(name) |
| 88 | self.assertTrue(seed.getName() == seedName, 'wrong seed name') |
| 89 | |
| 90 | #getConfigurationToken() |
| 91 | configurationToken = self.manager.getConfigurationToken(name) |
| 92 | self.assertTrue(configurationToken.getKey()== keyContent, 'wrong configration token') |
| 93 | |
| 94 | #getCommandToken() |
| 95 | commandToken1 = self.manager.getCommandToken(name, commandName1 ) |
| 96 | commandToken2 = self.manager.getCommandToken(name, commandName2) |
| 97 | self.assertTrue(commandToken1.getName() == commandTokenName1, 'wrong commandToken') |
| 98 | self.assertTrue(commandToken2.getName() == commandTokenName2, 'wrong commandToken') |
| 99 | |
| 100 | #getCommandsOfDevice() |
| 101 | commandNameList = self.manager.getCommandsOfDevice(name) |
| 102 | self.assertTrue(commandName1 in commandNameList, 'command:' + commandName1 + ' not found') |
| 103 | self.assertTrue(commandName2 in commandNameList, 'command:' + commandName2 + ' not found') |
| 104 | |
| 105 | #getServiceProfilesOfDevice() |
| 106 | serviceProfileListReturned = self.manager.getServiceProfilesOfDevice(name) |
| 107 | self.assertTrue(serviceProfileList[0] in serviceProfileListReturned, 'service profile:' + serviceProfileList[0] + ' not found') |
| 108 | self.assertTrue(serviceProfileList[1] in serviceProfileListReturned, 'service profile:' + serviceProfileList[1] + ' not found') |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | ut.main(verbosity=2) |
| 113 | |