def _loadCertPath(self, path): if path is None: return None byts = s_common.getbytes(path) if byts is None: return None return crypto.load_certificate(crypto.FILETYPE_PEM, byts)
def _loadP12Path(self, path): if path is None: return None byts = s_common.getbytes(path) if byts is None: return None return crypto.load_pkcs12(byts)
def _loadKeyPath(self, path): if path is None: return None byts = s_common.getbytes(path) if byts is None: return None return crypto.load_privatekey(crypto.FILETYPE_PEM, byts)
def _loadCsrPath(self, path): byts = s_common.getbytes(path) if byts is None: return None return crypto.load_certificate_request(crypto.FILETYPE_PEM, byts)
def _getPathBytes(self, path): if path is None: return None return s_common.getbytes(path)
def test_common_file_helpers(self): # genfile with self.getTestDir() as testdir: fd = s_common.genfile(testdir, 'woot', 'foo.bin') fd.write(b'genfile_test') fd.close() with open(os.path.join(testdir, 'woot', 'foo.bin'), 'rb') as fd: buf = fd.read() self.eq(buf, b'genfile_test') # reqpath with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'') self.eq(os.path.join(testdir, 'test.txt'), s_common.reqpath(testdir, 'test.txt')) self.raises(s_exc.NoSuchFile, s_common.reqpath, testdir, 'newp') # reqfile with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'reqfile_test') fd = s_common.reqfile(testdir, 'test.txt') buf = fd.read() self.eq(buf, b'reqfile_test') fd.close() self.raises(s_exc.NoSuchFile, s_common.reqfile, testdir, 'newp') # getfile with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'getfile_test') fd = s_common.getfile(testdir, 'test.txt') buf = fd.read() self.eq(buf, b'getfile_test') fd.close() self.none(s_common.getfile(testdir, 'newp')) # getbytes with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'getbytes_test') buf = s_common.getbytes(testdir, 'test.txt') self.eq(buf, b'getbytes_test') self.none(s_common.getbytes(testdir, 'newp')) # reqbytes with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'reqbytes_test') buf = s_common.reqbytes(testdir, 'test.txt') self.eq(buf, b'reqbytes_test') self.raises(s_exc.NoSuchFile, s_common.reqbytes, testdir, 'newp') # listdir with self.getTestDir() as dirn: path = os.path.join(dirn, 'woot.txt') with open(path, 'wb') as fd: fd.write(b'woot') os.makedirs(os.path.join(dirn, 'nest')) with open(os.path.join(dirn, 'nest', 'nope.txt'), 'wb') as fd: fd.write(b'nope') retn = tuple(s_common.listdir(dirn)) self.len(2, retn) retn = tuple(s_common.listdir(dirn, glob='*.txt')) self.eq(retn, ((path, )))
def test_common_file_helpers(self): # genfile with self.getTestDir() as testdir: fd = s_common.genfile(testdir, 'woot', 'foo.bin') fd.write(b'genfile_test') fd.close() with open(os.path.join(testdir, 'woot', 'foo.bin'), 'rb') as fd: buf = fd.read() self.eq(buf, b'genfile_test') # reqpath with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'') self.eq(os.path.join(testdir, 'test.txt'), s_common.reqpath(testdir, 'test.txt')) self.raises(s_exc.NoSuchFile, s_common.reqpath, testdir, 'newp') # reqfile with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'reqfile_test') fd = s_common.reqfile(testdir, 'test.txt') buf = fd.read() self.eq(buf, b'reqfile_test') fd.close() self.raises(s_exc.NoSuchFile, s_common.reqfile, testdir, 'newp') # getfile with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'getfile_test') fd = s_common.getfile(testdir, 'test.txt') buf = fd.read() self.eq(buf, b'getfile_test') fd.close() self.none(s_common.getfile(testdir, 'newp')) # getbytes with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'getbytes_test') buf = s_common.getbytes(testdir, 'test.txt') self.eq(buf, b'getbytes_test') self.none(s_common.getbytes(testdir, 'newp')) # reqbytes with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'reqbytes_test') buf = s_common.reqbytes(testdir, 'test.txt') self.eq(buf, b'reqbytes_test') self.raises(s_exc.NoSuchFile, s_common.reqbytes, testdir, 'newp') # listdir with self.getTestDir() as dirn: path = os.path.join(dirn, 'woot.txt') with open(path, 'wb') as fd: fd.write(b'woot') os.makedirs(os.path.join(dirn, 'nest')) with open(os.path.join(dirn, 'nest', 'nope.txt'), 'wb') as fd: fd.write(b'nope') retn = tuple(s_common.listdir(dirn)) self.len(2, retn) retn = tuple(s_common.listdir(dirn, glob='*.txt')) self.eq(retn, ((path, ))) # getDirSize: check against du real, appr = s_common.getDirSize(dirn) durealstr = subprocess.check_output(['du', '-B', '1', '-s', dirn]) dureal = int(durealstr.split()[0]) duapprstr = subprocess.check_output(['du', '-bs', dirn]) duappr = int(duapprstr.split()[0]) self.eq(dureal, real) self.eq(duappr, appr)
def test_common_file_helpers(self): # genfile with self.getTestDir() as testdir: fd = s_common.genfile(testdir, 'woot', 'foo.bin') fd.write(b'genfile_test') fd.close() with open(os.path.join(testdir, 'woot', 'foo.bin'), 'rb') as fd: buf = fd.read() self.eq(buf, b'genfile_test') # reqpath with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'') self.eq(os.path.join(testdir, 'test.txt'), s_common.reqpath(testdir, 'test.txt')) self.raises(s_exc.NoSuchFile, s_common.reqpath, testdir, 'newp') # reqfile with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'reqfile_test') fd = s_common.reqfile(testdir, 'test.txt') buf = fd.read() self.eq(buf, b'reqfile_test') fd.close() self.raises(s_exc.NoSuchFile, s_common.reqfile, testdir, 'newp') # getfile with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'getfile_test') fd = s_common.getfile(testdir, 'test.txt') buf = fd.read() self.eq(buf, b'getfile_test') fd.close() self.none(s_common.getfile(testdir, 'newp')) # getbytes with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'getbytes_test') buf = s_common.getbytes(testdir, 'test.txt') self.eq(buf, b'getbytes_test') self.none(s_common.getbytes(testdir, 'newp')) # reqbytes with self.getTestDir() as testdir: with s_common.genfile(testdir, 'test.txt') as fd: fd.write(b'reqbytes_test') buf = s_common.reqbytes(testdir, 'test.txt') self.eq(buf, b'reqbytes_test') self.raises(s_exc.NoSuchFile, s_common.reqbytes, testdir, 'newp') # listdir with self.getTestDir() as dirn: path = os.path.join(dirn, 'woot.txt') with open(path, 'wb') as fd: fd.write(b'woot') os.makedirs(os.path.join(dirn, 'nest')) with open(os.path.join(dirn, 'nest', 'nope.txt'), 'wb') as fd: fd.write(b'nope') retn = tuple(s_common.listdir(dirn)) self.len(2, retn) retn = tuple(s_common.listdir(dirn, glob='*.txt')) self.eq(retn, ((path,)))