~hggdh2/+junk/pasaffe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Copyright (C) 2015 C de-Avillez <hggdh2@ubuntu.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MErcHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import sys
import os.path
import unittest
import subprocess

myPath = os.path.dirname(__file__)
sys.path.insert(0, os.path.realpath(os.path.join(myPath, "..", "../bin", "../lib")))

db_name = "/tmp/test_pasaffe-cli.psafe3"


# from pasaffe_lib.readdb import PassSafeFile


def cliCmd():
    return "%s/../bin/pasaffe-cli" % myPath


def subproc(command, test):
    rc = 0
    result = None
    try:
        result = subprocess.check_output(command).splitlines()  # pylint: disable=E1103
    except subprocess.CalledProcessError as err:
        print("%s failed with rc=%s" % (test, err.returncode))
        print("%s" % err.output)
        rc = err.returncode
        result = None
    return result, rc


def gen_pswd(size=16):
    command = ("%s" % cliCmd(), "--genpswd", "--pswdlen=%s" % size)
    db_pswd, rc = subproc(command, "gen_pswd")
    if rc == 0:
        db_pswd = db_pswd[0].decode('utf-8').strip()
    return db_pswd, rc


def createDB(filename, password):
    command = (cliCmd(), "--createdb", "--masterpassword=%s" % password,
               "--file=%s" % filename)
    result, rc = subproc(command, "createDB")
    return result, rc


def creatEntry(suffix):
    entry = "Entry%s" % suffix
    group = "Group%s" % suffix
    userId = "userId%s" % suffix
    passwd, rc = gen_pswd()
    password = passwd
    url = "https://url%s.com" % suffix
    notes = "Note%s line 1\nNote%s line 2" % (suffix, suffix)
    return [entry, group, userId, password, url, notes]


def addEntry(db, pswd, entry=None, group=None, userid=None, password=None, url=None, notes=None):
    command = []
    command.append(cliCmd())
    #command.append("--debug")
    command.append("--add")
    command.append("--file=%s" % db)
    command.append("--masterpassword=%s" % pswd)
    command.append("--entry=%s" % entry)
    if group is not None:
        command.append("--group=%s" % group)
    if userid is not None:
        command.append("--user=%s" % userid)
    if password is not None:
        command.append("--pswd=%s" % password)
    if url is not None:
        command.append("--url=%s" % url)
    if notes is not None:
        command.append("--notes=%s" % notes)
    print("command=%s" % command)
    result, rc = subproc(command, "addEntry")
    return result, rc


def replEntry(db, pswd, entry=None, newentry=None, group=None, userid=None, password=None, url=None, notes=None):
    command = []
    command.append(cliCmd())
    #command.append("--debug")
    command.append("--repl")
    command.append("--file=%s" % db)
    command.append("--masterpassword=%s" % pswd)
    command.append("--entry=%s" % entry)
    if newentry is not None:
        command.append("--newentry=%s" % newentry)
    if group is not None:
        command.append("--group=%s" % group)
    if userid is not None:
        command.append("--user=%s" % userid)
    if password is not None:
        command.append("--pswd=%s" % password)
    if url is not None:
        command.append("--url=%s" % url)
    if notes is not None:
        command.append("--notes=%s" % notes)
    print("command=%s" % command)
    result, rc = subproc(command, "replEntry")
    return result, rc


def listEntry(db, pswd, entry="Dummy", fuzzy=False, lstUserId=False, lstPswd=False, lstGroup=False, lstURL=False,
              lstNotes=False, lstAll=False):
    command = []
    command.append(cliCmd())
    #command.append("--debug")
    command.append("--file=%s" % db)
    command.append("--masterpassword=%s" % pswd)
    command.append("--entry=%s" % entry)
    if lstAll:
        command.append("--listall")
    else:
        command.append("--list")
        if fuzzy:
           command.append("--fuzzy")
        if lstUserId:
           command.append("--listuser")
        if lstGroup:
           command.append("--listgroup")
        if lstPswd:
           command.append("--listpswd")
        if lstURL:
           command.append("--listurl")
        if lstNotes:
           command.append("--listnotes")
    print("command=%s" % command)
    result, rc = subproc(command, "listEntry")
    return result, rc



class TestPasaffeCLI(unittest.TestCase):
    def csetUp(self):
        # remove any left-over pasaffe DB
        if os.path.isfile(db_name):
            os.remove(db_name)

    def tearDown(self):
        if os.path.isfile(db_name):
            os.remove(db_name)


    def test_t01GenPassword(self):
        Len = 8
        while Len < 32:
            password, rc = gen_pswd(size=Len)
            self.assertEqual(rc, 0, msg="call to genpswd failed")
            self.assertNotEqual(len(password), 0, msg="Failed to generate a password")
            self.assertEqual(len(password), Len, msg="Length of generated password does not match requested length")
            Len += 1

    def test_t02createDB(self):
        db_pswd, rc = gen_pswd()
        self.assertEqual(rc, 0, msg="call to genpswd failed")
        result, rc = createDB(db_name, db_pswd)
        self.assertEqual(rc, 0, msg="DB creation failed")
        self.assertTrue(os.path.isfile(db_name))

    def test_t03addEntry(self):
        db_pswd, rc = gen_pswd()
        self.assertEqual(rc, 0, msg="call to genpswd failed")
        result, rc = createDB(db_name, db_pswd)
        self.assertEqual(rc, 0, msg="DB creation failed")
        values = creatEntry(1)
        result, rc = addEntry(db_name, db_pswd, entry=values[0])
        self.assertNotEqual(rc, 0)
        values = creatEntry(2)
        result, rc = addEntry(db_name, db_pswd, entry=values[0],
                              group=values[1])
        self.assertNotEqual(rc, 0)
        values = creatEntry(3)
        result, rc = addEntry(db_name, db_pswd, entry=values[0],
                              group=values[1],
                              userid=values[2])
        self.assertEqual(rc, 0)
        values = creatEntry(4)
        result, rc = addEntry(db_name, db_pswd, entry=values[0],
                              group=values[1],
                              userid=values[2],
                              password=values[3])
        self.assertEqual(rc, 0)
        values = creatEntry(5)
        result, rc = addEntry(db_name, db_pswd, entry=values[0],
                              group=values[1],
                              userid=values[2],
                              password=values[3],
                              url=values[4])
        self.assertEqual(rc, 0)
        values = creatEntry(6)
        result, rc = addEntry(db_name, db_pswd, entry=values[0],
                              group=values[1],
                              userid=values[2],
                              password=values[3],
                              url=values[4],
                              notes=values[5])
        self.assertEqual(rc, 0)

    def test_t04replaceEntry(self):
        db_pswd, rc = gen_pswd()
        self.assertEqual(rc, 0, msg="call to genpswd failed")
        result, rc = createDB(db_name, db_pswd)
        self.assertEqual(rc, 0, msg="DB creation failed")
        values = creatEntry(1)
        result, rc = addEntry(db_name, db_pswd, entry=values[0],
                              group=values[1],
                              userid=values[2])
        self.assertEqual(rc, 0, msg="adding entry 1 failed")
        result, rc = replEntry(db_name, db_pswd, entry=values[0],
                               group=values[1],
                               newentry="NewEntry1")
        self.assertEqual(rc, 0, msg="replacing entry string failed")
        result, rc = replEntry(db_name, db_pswd, entry="NewEntry1",
                               group="NewGroup1")
        self.assertEqual(rc, 0, msg="replacing group string failed")
        result, rc = replEntry(db_name, db_pswd, entry="NewEntry1",
                               group="Group1",
                               userid="NewUser1")
        self.assertEqual(rc, 0, msg="replacing user string failed")
        result, rc = replEntry(db_name, db_pswd, entry="NewEntry1",
                               group="Group1",
                               userid="User1",
                               password="Password1")
        self.assertEqual(rc, 0, msg="replacing password string failed")
        result, rc = replEntry(db_name, db_pswd, entry="NewEntry1",
                               group="Group1",
                               userid="User1",
                               password="Password1",
                               url="http://127.0.0.1")
        self.assertEqual(rc, 0, msg="replacing URL string failed")
        result, rc = replEntry(db_name, db_pswd, entry="NewEntry1",
                               group="Group1",
                               userid="User1",
                               password="Password1",
                               url="http://127.0.0.1",
                               notes="This is a note for newEntry1")
        self.assertEqual(rc, 0, msg="replacing notes string failed")


    def test_t05listEntry(self):
        db_pswd, rc = gen_pswd()
        self.assertEqual(rc, 0, msg="call to genpswd failed")
        result, rc = createDB(db_name, db_pswd)
        self.assertEqual(rc, 0, msg="DB creation failed")
        result, rc = addEntry(db_name, db_pswd, entry="Entry1", group="Group1", userid="User1",
                          password="Password1", url="http://127.0.0.1",
                          notes="This is a note for Entry1")
        self.assertEqual(rc, 0, msg="adding entry 1 failed")
        result, rc = addEntry(db_name, db_pswd, entry="Entry2", group="Group2", userid="User2",
                          password="Password2", url="http://127.0.0.2",
                          notes="This is a note for Entry2")
        self.assertEqual(rc, 0, msg="adding entry 2 failed")
        result, rc = addEntry(db_name, db_pswd, entry="Entry3", group="Group3", userid="User3",
                          password="Password3", url="http://127.0.0.3",
                          notes="This is a note for Entry3")
        self.assertEqual(rc, 0, msg="adding entry 3 failed")
        result, rc = listEntry(db_name, db_pswd, entry="Entry1")
        self.assertEqual(rc, 0, "list Entry1 1/n failed")
        print("Result:%s" % result)
        result, rc = listEntry(db_name, db_pswd, entry="Entry2", )
        self.assertEqual(rc, 0, "list Entry1 2/n failed")
        result, rc = listEntry(db_name, db_pswd, entry="Entry3", )
        self.assertEqual(rc, 0, "list Entry1 2/n failed")


def test_t06removeEntry(self):
    pass


if __name__ == '__main__':
    unittest.main()