Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
test with sha1 verification on download
Browse files Browse the repository at this point in the history
  • Loading branch information
kmpm committed Dec 12, 2019
1 parent 8edf2b4 commit 0924555
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
14 changes: 11 additions & 3 deletions nodemcu_uploader/luacode.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@
local on,w=uart.on,uart.write
local fd
local function send_block(d) l = string.len(d) w(0, '\001' .. string.char(l) .. d .. string.rep('\0', 128 - l)) return l end
local function send_file(f) fd=file.open(f) s=fd:seek('end', 0) p=0 on('data', 1, function(data)
if data == '\006' and p<s then fd:seek('set',p) p=p+send_block(fd:read(128)) else
send_block('') fd:close() on('data') print('interrupted') end end, 0) w(0, f .. '\000')
local function send_file(f)
local s, p
fd=file.open(f) s=fd:seek('end', 0) p=0
on('data', 1, function(data)
if data == '\006' and p<s then
fd:seek('set',p) p=p+send_block(fd:read(128))
else
send_block('') fd:close() on('data') print('interrupted')
end
end, 0)
w(0, f .. '\000')
end
uart.on('data') if data == 'C' then send_file(f) else print('transfer interrupted') end end, 0)
end
Expand Down
14 changes: 8 additions & 6 deletions nodemcu_uploader/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,23 +321,25 @@ def write_file(self, path, destination='', verify='none'):
if verify != 'none':
self.verify_file(path, destination, verify)

def verify_file(self, path, destination, verify='none'):
"""Tries to verify if path has same checksum as destination.
def verify_file(self, local, remote, verify='none'):
"""Tries to verify if local has same checksum as remote.
Valid options for verify is 'raw', 'sha1' or 'none'
"""
content = from_file(path)
# get the local file contents
self.__writeln(';')
self.__expect('> ')
content = from_file(local)
log.info('Verifying using %s...' % verify)
if verify == 'raw':

data = self.download_file(destination)
data = self.download_file(remote)
if content != data:
log.error('Raw verification failed.')
raise VerificationError('Verification failed.')
else:
log.info('Verification successful. Contents are identical.')
elif verify == 'sha1':
# Calculate SHA1 on remote file. Extract just hash from result
data = self.__exchange('shafile("'+destination+'")').splitlines()[1]
data = self.__exchange('shafile("'+remote+'")').splitlines()[1]
log.info('Remote SHA1: %s', data)

# Calculate hash of local data
Expand Down
6 changes: 3 additions & 3 deletions tests/torture.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def tearDown(self):
def task_upload_verify_compile(self):
print('upload-verify-compile')
self.assertTrue(self.uploader.prepare())
pattern = os.path.join('tests', 'fixtures', '*.lua')
self.assertEqual(pattern, "tests\\fixtures\\*.lua")
dests = operation_upload(self.uploader, "tests/fixtures/*.lua", 'sha1', True, False, False)
return len(dests)

Expand All @@ -61,7 +59,9 @@ def task_download_all_files(self, files):
dest = os.path.join('.', 'tmp')
operation_download(self.uploader, files, dest=dest)
for f in files:
self.assertTrue(os.path.isfile(os.path.join(dest, f)))
local = os.path.join(dest, f)
self.assertTrue(os.path.isfile(local))
self.uploader.verify_file(local, f, 'sha1')

def task_remove_tmp(self):
dest = os.path.join('.', 'tmp')
Expand Down

0 comments on commit 0924555

Please sign in to comment.