Esempio n. 1
0
 def _rotateLogs(self):
     """Don't have rename function in my micropython build"""
     self.fh.close()
     if self.exists(self.last_log):
         uos.unlink(self.last_log)
     uos.rename(self.current_log, self.last_log)
     self.fh = open(self.current_log, mode="w")
Esempio n. 2
0
def delete_file(fn):
    # "unlink" gets renamed to "remove" in micropython,
    # so support both
    if hasattr(os, 'unlink'):
        os.unlink(fn)
    else:
        os.remove(fn)
Esempio n. 3
0
def cleanup():
    try:
        uos.unlink(__filename())
        pycom.nvs_set('queue_size', 0)
    except OSError as e:
        print('error while removing data file: {}'.format(e.errno))
        pass
Esempio n. 4
0
    def testSendFileNotFound(self):
        """Verify 404 error for non existing files"""
        self.srv.add_route('/', self.send_file_handler)
        rdr = mockReader(['GET / HTTP/1.0\r\n',
                          HDRE])
        wrt = mockWriter()

        # Intentionally delete file before request
        os.unlink(self.tempfn)
        run_coro(self.srv._handler(rdr, wrt))

        exp = ['HTTP/1.0 404 MSG\r\n\r\n']
        self.assertEqual(wrt.history, exp)
        self.assertTrue(wrt.closed)
Esempio n. 5
0
async def test_7z():
    # test full 7z round-trip
    # Altho cleartext mode is not for real, if the code is written, I must test it.
    from backups import write_complete_backup, restore_complete_doit
    from sffile import SFFile
    import ngu, version, uos
    from glob import numpad
    from pincodes import pa
    from nvstore import settings

    if version.has_fatram:
        import hsm
        had_policy = hsm.hsm_policy_available()
    else:
        had_policy = False

    today = ngu.random.uniform(1000000)

    import machine
    machine.reset = lambda: None

    for chain in ['BTC', 'XTN']:
        for words in ([], ['abc', 'def']):
            settings.set('check', today)
            settings.set('chain', chain)

            if version.has_608:
                ls = b'%416d' % today
                pa.ls_change(ls)

            ll, sha = await write_complete_backup(words, None, True)

            result = SFFile(0, ll).read()

            if words:
                #open('debug.7z', 'wb').write(result)
                assert ll > 800
                assert len(sha) == 32
                assert result[0:6] == b"7z\xbc\xaf'\x1c"
                assert ngu.hash.sha256s(result) == sha
                assert len(set(result)) >= 240  # encrypted
            else:
                sr = str(result, 'ascii')
                print("Backup contents:\n" + sr)
                assert sr[0] == '#', result
                assert 'Coldcard' in sr
                assert len(set(sr)) < 100  # cleartext, english
                assert ('chain = "%s"' % chain) in result

            # test restore
            # - cant wipe flash, since the backup file is there
            # - cant wipe all settings becuase PIN and stuff is simulated there
            del settings.current['check']

            if had_policy:
                from hsm import POLICY_FNAME
                uos.unlink(POLICY_FNAME)
                assert not hsm.hsm_policy_available()

            with SFFile(0, ll) as fd:
                numpad.inject('y')  # for 'success' message
                await restore_complete_doit(fd, words)

                assert settings.get('check') == today, \
                            (settings.get('check'), '!=',  today)
                assert settings.get('chain') == chain, \
                            (settings.get('chain'), '!=',  chain)

                if version.has_608:
                    assert pa.ls_fetch() == ls

            if had_policy:
                assert had_policy == hsm.hsm_policy_available()

            today += 3

            import ux
            ux.restore_menu()
Esempio n. 6
0
def cleanup():
    for fname in cleanup_files:
        try:
            os.unlink(fname)
        except OSError:
            print("Warning: Cannot delete " + fname)
Esempio n. 7
0
def cleanup():
    for fname in cleanup_files:
        try:
            os.unlink(fname)
        except OSError:
            print("Warning: Cannot delete " + fname)
Esempio n. 8
0
 def tearDown(self):
     try:
         os.unlink(self.tempfn)
     except OSError:
         pass
Esempio n. 9
0
try:
    import uos as os
except ImportError:
    import os

if not hasattr(os, "unlink"):
    print("SKIP")
    raise SystemExit

# cleanup in case testfile exists
try:
    os.unlink("testfile")
except OSError:
    pass

try:
    f = open("testfile", "r+b")
    print("Unexpectedly opened non-existing file")
except OSError:
    print("Expected OSError")
    pass

f = open("testfile", "w+b")
f.write(b"1234567890")
f.seek(0)
print(f.read())
f.close()

# Open with truncation
f = open("testfile", "w+b")
f.write(b"abcdefg")