blob: e060b1f7c231b51518f8453f595bfe1643b57d97 [file] [log] [blame]
Weiwei074778b2015-08-07 21:30:44 -07001# -*- 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
20import unittest as ut
21import os.path
22from pyndn import Name
23from user_access_storage import UserAccessStorage
24from device_profile import DeviceProfile
25from hmac_key import HMACKey
26
27class TestUserAccessStorageMethods(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
40 self.storage = UserAccessStorage()
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("User"), "Device table doesn't exist")
49 self.assertTrue(self.storage.doesTableExist("Access"), "Command table doesn't exist")
50
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')
60 storage2 = UserAccessStorage(filePath)
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 user (prefix = /home/weiwei)
66 username = 'weiwei'
67 prefixBase = '/home'
68 prefixStr =prefixBase +'/' + username
69 prefixName = Name(prefixStr)
70 result1 = self.storage.doesUserExistByPrefix(prefixName)
71 result2 = self.storage.doesUserExistByUsername(username)
72 self.assertFalse(result1, "user with prefix '" + prefixStr + "' shouldn't exist")
73 self.assertFalse(result2, "user with username '" + username + "' shouldn't exist")
74
75 #test existence of an existed user
76 self.add_a_default_user(username)
77 result1 = self.storage.doesUserExistByPrefix(prefixName)
78 result2 = self.storage.doesUserExistByUsername(username)
79
80 self.assertTrue(result1, "user with prefix '" + prefixStr + "' should exist")
81 self.assertTrue(result2, "user with username '" + username + "' should exist")
82
83 #test the existence of a non-existed access
84 commandId = 1
85 userId = 1
86 result = self.storage.doesAccessExist(commandId, userId)
87 self.assertFalse(result, "access shouldn't exist with a empty Access table")
88
89 #test the exitence of an existed access
90 accessToken = self.create_a_default_access_token('user1_command1')
91 self.storage.addAccess(1,1, 'laptop', accessToken)
92 result = self.storage.doesAccessExist(commandId, userId)
93 self.assertTrue(result, "access should exist")
94
95 def test_03_add_user(self):
96 username = 'weiwei'
97 prefixBase = '/home'
98 prefixStr =prefixBase +'/' + username
99 prefixName = Name(prefixStr)
100 hash_ = 'EEADFADSFAGASLGALS'
101 salt = 'adfafdwekldsfljcdc'
102 type_ = 'guest'
103
104 result = self.storage.addUser(prefixName, username, hash_, salt, type_)
105
106 self.assertTrue(result > 0, 'fail to add user')
107 self.assertTrue(self.storage.doesUserExistByPrefix(prefixName), "user doesn't exist in table")
108 row = self.storage.getUserEntry(prefixName)
109
110 self.assertTrue(row[1] == prefixStr, "column prefix has incorrect value")
111 self.assertTrue(row[2] == username, "column username has incorrect value ")
112 self.assertTrue(row[3] == hash_, 'colum hash has incorrect value')
113 self.assertTrue(row[4] == salt, 'colum salt has incorrect value')
114 self.assertTrue(row[5] == type_, 'column type_ has incoorect value')
115
116 #try to insert the same device
117 result = self.storage.addUser(prefixName, username, hash_, salt, type_)
118 self.assertTrue(result == 0, 'unexpected result when trying to insert an already existed user')
119
120 def test_04_delete_user(self):
121 username = 'user1'
122 self.add_a_default_user(username)
123 prefixBase = '/home'
124 prefixStr =prefixBase +'/' + username
125 prefixName = Name(prefixStr)
126
127 self.assertTrue(self.storage.doesUserExistByPrefix(prefixName), "user doesn't exist in table")
128 result = self.storage.deleteUser(prefixName)
129 self.assertTrue(result == 1, 'fail to delete device')
130
131 def test_05_get_userid(self):
132 username = 'user1'
133 self.add_a_default_user(username)
134 prefixBase = '/home'
135 prefixStr =prefixBase +'/' + username
136 prefixName = Name(prefixStr)
137
138 userId = self.storage.getUserId(prefixName)
139 self.assertTrue(userId==1, "get a wrong user id")
140
141 username = 'user2'
142 prefixStr =prefixBase +'/' + username
143 prefixName = Name(prefixStr)
144 self.add_a_default_user(username)
145
146 userId = self.storage.getUserId(prefixName)
147 self.assertTrue(userId==2, "get a wrong user id")
148
149 def test_06_update_user(self):
150 username = 'user1'
151 self.add_a_default_user(username)
152 prefixBase = '/home'
153 prefixStr =prefixBase +'/' + username
154 prefixName = Name(prefixStr)
155
156 #update hash_ salt, type_ value of user
157 newHash = 'NNNMMHGFGKUOIPRT'
158 newSalt = 'kjkjfioewprwerke'
159 newType = 'user'
160 self.storage.updateUser(prefixName, newHash, newSalt, newType)
161 row = self.storage.getUserEntry(prefixName)
162 self.assertTrue(row[3] == newHash, "fail to update column: hash")
163 self.assertTrue(row[4] == newSalt, "fail to update column: salt")
164 self.assertTrue(row[5] == newType, "fail to update column: type")
165
166 def test_07_add_access(self):
167 username = 'user1'
168 self.add_a_default_user(username)
169
170 accessToken = self.create_a_default_access_token('user1_command1')
171 self.storage.addAccess(1,1, 'laptop', accessToken)
172 result = self.storage.doesAccessExist(1, 1)
173 self.assertTrue(result == True, "fail to add command")
174
175 def test_08_delete_access(self):
176 username = 'user1'
177 self.add_a_default_user(username)
178
179 accessToken = self.create_a_default_access_token('user1_command1')
180 self.storage.addAccess(1,1, 'laptop', accessToken)
181
182 self.assertTrue(self.storage.doesAccessExist(1, 1), 'before delete, the command should exist')
183 self.storage.deleteAccess(1, 1)
184 self.assertFalse(self.storage.doesAccessExist(1, 1), "after delete, the command shouldn't exist")
185
186 def test_09_get_commands_of_user(self):
187 accessToken = self.create_a_default_access_token('token1')
188 self.storage.addAccess(1,1, 'laptop', accessToken)
189 self.storage.addAccess(1,1, 'desktop', accessToken)
190 self.storage.addAccess(2,1,'laptop', accessToken)
191 self.storage.addAccess(2,1, 'desktop', accessToken)
192 result = self.storage.getCommandsOfUser(1)
193 self.assertTrue(result == [1,2], 'wrong command list returned')
194
195 def test_10_get_access_token(self):
196 accessTokenName1 = 'accessToken1'
197 accessTokenName2 = 'accessToken2'
198 accessToken1 = self.create_a_default_access_token(accessTokenName1)
199 accessToken2 = self.create_a_default_access_token(accessTokenName2)
200 userDevice1 = 'laptop'
201 userDevice2 = 'desktop'
202 accessId1 = self.storage.addAccess(1,1, userDevice1, accessToken1)
203 accessId2 = self.storage.addAccess(1,1, userDevice2, accessToken2)
204
205 if accessId1 <= 0 or accessId2 <= 0:
206 raise RuntimeError('fail to add access')
207 result1 = self.storage.getAccessToken(1,1, userDevice1)
208 result2 = self.storage.getAccessToken(1,1, userDevice2)
209 if result1 == None or result2 == None:
210 self.assertTrue(False, 'no access returned')
211 self.assertTrue(result1.getName() == accessTokenName1, 'wrong access token returned')
212 self.assertTrue(result2.getName() == accessTokenName2, 'wrong access token returned')
213
214 def add_a_default_user(self, username):
215 prefixBase = '/home'
216 prefixStr =prefixBase +'/' + username
217 prefixName = Name(prefixStr)
218 hash_ = 'EEADFADSFAGASLGALS'
219 salt = 'adfafdwekldsfljcdc'
220 type_ = 'guest'
221
222 self.storage.addUser(prefixName, username, hash_, salt, type_)
223
224 def create_a_default_access_token(self, keyName = None):
225 keyContent = 'this is key content'
226 accessToken = HMACKey(sequence = 0, key = keyContent, name = keyName)
227 return accessToken
228
229if __name__ == '__main__':
230 ut.main(verbosity=2)
231