def verify_retrieval_above_max_limit_not_allowed(): handle = posix.open("Alphabets", 2) chars_to_be_retrieved = 2**31 try: posix.read(handle, chars_to_be_retrieved) except: logger.error("Overflow error")
def verify_retrieval_with_negative_numbers(): handle = posix.open("Alphabets", 2) chars_to_be_retrieved = -1 try: posix.read(handle, chars_to_be_retrieved) except Exception as e: print("Characters to be retrieved cannot be negative")
def cannot_read(): try: posix.read(fd_read, 1) except OSError: pass else: raise AssertionError("os.read(fd_read, 1) succeeded?")
def execute_command(): pipe_fd = posix.pipe() pid = posix.fork() if pid == 0: cmdline = ["lsblk"] if check_fs_val.get() or check_UUID_val.get(): cmdline.append("-o") col = "+" if check_fs_val.get(): col += "fstype" if check_fs_val.get() and check_UUID_val.get(): col += "," if check_UUID_val.get(): col += "UUID" cmdline.append(col) posix.dup2(pipe_fd[1], 1) posix.close(pipe_fd[0]) posix.close(pipe_fd[1]) posix.execv("/bin/lsblk", cmdline) quit() else: posix.close(pipe_fd[1]) ret = bytearray() readbytes = posix.read(pipe_fd[0], 1000) while readbytes != b"": ret += readbytes readbytes = posix.read(pipe_fd[0], 1000) posix.close(pipe_fd[0]) posix.wait() return str(ret, sys.stdout.encoding)
def cannot_read(): try: posix.read(fd_read, 1) except OSError: pass else: raise AssertionError, "os.read(fd_read, 1) succeeded?"
def verify_if_offset_increase_post_read(): handle = posix.open("Alphabets", 2) chars_to_be_retrieved = 2 new_chars_to_be_retrieved = chars_to_be_retrieved + 1 check = posix.read(handle, chars_to_be_retrieved) != posix.read( handle, new_chars_to_be_retrieved) if check: print("File offset increase validated") else: logger.error("File offset increase failed")
def verify_response_time(): start_time = time.time() handle = posix.open("Large_Text", 2) chars_to_be_retrieved = 100000 posix.read(handle, chars_to_be_retrieved) end_time = time.time() print("Time to read file large file " + str(end_time - start_time)) if end_time - start_time < 0.1: print("Response time validated") else: logger.error("Response time validation failed")
def test_rename_at_close_correct_read(self): firstFileFd = posix.open("mount/file1", posix.O_RDONLY) secondFileFd = posix.open("mount/file2", posix.O_RDONLY) for i in range(1000): position = random.randint(0, 10000) size = random.randint(1, 10000) posix.lseek(firstFileFd,position, 0) posix.lseek(secondFileFd, position, 0) posix.read(firstFileFd, size) posix.read(secondFileFd, size) posix.close(firstFileFd) posix.close(secondFileFd) posix.rename("mount/file2", "mount/file3") posix.rename("mount/file1","mount/file2") self.assertTrue(open("mount/file2").read()== open("src/file2").read())
def __import__(filename, module_name): import sys, _imp, posix module = sys.modules[module_name] if filename.startswith("%s"): full_filename = filename % __graalpython__.core_home filename = filename[len("%s"):] elif filename.startswith(__graalpython__.stdlib_home): full_filename = filename filename = filename[len(__graalpython__.stdlib_home):] else: raise RuntimeError( "There was an import during bootstrap outside the core or stdlib home." ) # If we can, avoid opening the file and use our cached code if not __graalpython__.has_cached_code(filename): fd = posix.open(full_filename, posix.O_RDONLY) content = posix.read(fd, sys.maxsize) posix.close(fd) code = compile(content, filename, "exec") else: # n.b.: for these builtin modules, there's never a full path and none of # them can be packages code = __graalpython__.get_cached_code(filename) exec(code, module.__dict__) return module
def test_randomAccessToFile(self): position = 0 size = 0 srcFileFd = posix.open("src/file1", posix.O_RDONLY) mountFileFd = posix.open("mount/file1", posix.O_RDONLY) for i in range(10000): position = random.randint(0, 10000) size = random.randint(1, 10000) posix.lseek(srcFileFd,position, 0) posix.lseek(mountFileFd, position, 0) if (posix.read(srcFileFd,size) != posix.read(mountFileFd, size)): posix.close(srcFileFd) posix.close(mountFileFd) self.assertTrue(False) posix.close(srcFileFd) posix.close(mountFileFd)
def read_sensor(self): fd = posix.open('/dev/i2c-%d' % self._i2cbus, posix.O_RDWR) ioctl(fd, I2C_SLAVE, self.I2C_ADDR) try: posix.write(fd, b'\0x00') # wake up AM2320 except: pass time.sleep(0.01) posix.write( fd, b'\x03\x00\x04' ) # x03(function code) x00(starting address) x04(register length) time.sleep(0.002) data = bytearray(posix.read(fd, 8)) posix.close(fd) if data[0] != 0x03 or data[ 1] != 0x04: # Modbus function code (0x03), number of registers to read (0x04) raise Exception('I2C read failure') if self._calc_crc16(data[0:6]) != self._combine_bytes( data[7], data[6]): raise Exception('CRC failure') humi = self._combine_bytes(data[2], data[3]) / 10.0 temp = self._combine_bytes(data[4], data[5]) if temp & 0x8000: temp = -(temp & 0x7FFF) temp /= 10.0 return (temp, humi)
def test_set_wakeup_fd(self): import signal, posix, fcntl def myhandler(signum, frame): pass signal.signal(signal.SIGUSR1, myhandler) # def cannot_read(): try: posix.read(fd_read, 1) except OSError: pass else: raise AssertionError, "os.read(fd_read, 1) succeeded?" # fd_read, fd_write = posix.pipe() flags = fcntl.fcntl(fd_write, fcntl.F_GETFL, 0) flags = flags | posix.O_NONBLOCK fcntl.fcntl(fd_write, fcntl.F_SETFL, flags) flags = fcntl.fcntl(fd_read, fcntl.F_GETFL, 0) flags = flags | posix.O_NONBLOCK fcntl.fcntl(fd_read, fcntl.F_SETFL, flags) # old_wakeup = signal.set_wakeup_fd(fd_write) try: cannot_read() posix.kill(posix.getpid(), signal.SIGUSR1) res = posix.read(fd_read, 1) assert res == '\x00' cannot_read() finally: old_wakeup = signal.set_wakeup_fd(old_wakeup) # signal.signal(signal.SIGUSR1, signal.SIG_DFL)
def ReadAllPacketsOn(self, netid, include_multicast=False): packets = [] retries = 0 max_retries = 1 while True: try: packet = posix.read(self.tuns[netid].fileno(), 4096) if not packet: break ether = scapy.Ether(packet) # Multicast frames are frames where the first byte of the destination # MAC address has 1 in the least-significant bit. if include_multicast or not int(ether.dst.split(":")[0], 16) & 0x1: packets.append(ether.payload) except OSError, e: # EAGAIN means there are no more packets waiting. if re.match(e.message, os.strerror(errno.EAGAIN)): # If we didn't see any packets, try again for good luck. if not packets and retries < max_retries: time.sleep(0.01) retries += 1 continue else: break # Anything else is unexpected. else: raise e
def check_module_inspection(): this_module = __nemesys__.get_module(__name__) phase = __nemesys__.module_phase(__name__) compiled_size = __nemesys__.module_compiled_size(__name__) global_count = __nemesys__.module_global_count(__name__) source = __nemesys__.module_source(__name__) # these functions should also work if passed the module object assert phase == __nemesys__.module_phase(this_module) assert compiled_size == __nemesys__.module_compiled_size(this_module) assert global_count == __nemesys__.module_global_count(this_module) assert source == __nemesys__.module_source(this_module) assert phase == "Analyzed" # doesn't become Imported until the root scope returns assert compiled_size > 0 assert global_count == 8 # note: the globals are __doc__, __name__, __nemesys__, posix, and the four functions assert b'this string appears verbatim in the module source' in source assert b'this string does not appear verbatim because it has an escaped\x20character' not in source # read the contents of this file and make sure it matches module_source fd = posix.open(__file__, posix.O_RDONLY) data = posix.read(fd, 2 * len(source)) posix.close(fd) assert data == source, '%s != %s' % (repr(data), repr(source))
def readSensor(self): fd = posix.open("/dev/i2c-%d" % self._i2cbus, posix.O_RDWR) ioctl(fd, self.I2C_SLAVE, self.I2C_ADDR) # wake AM2320 up, goes to sleep to not warm up and affect the humidity sensor # This write will fail as AM2320 won't ACK this write try: posix.write(fd, b'\0x00') except: pass time.sleep(0.001) #Wait at least 0.8ms, at most 3ms # write at addr 0x03, start reg = 0x00, num regs = 0x04 */ try: posix.write(fd, b'\x03\x00\x04') except: posix.close(fd) return (0, 0, 1, "Device did not acknowledge request") time.sleep(0.0016) #Wait at least 1.5ms for result # Read out 8 bytes of result data # Byte 0: Should be Modbus function code 0x03 # Byte 1: Should be number of registers to read (0x04) # Byte 2: Humidity msb # Byte 3: Humidity lsb # Byte 4: Temperature msb # Byte 5: Temperature lsb # Byte 6: CRC lsb byte # Byte 7: CRC msb byte data = bytearray(posix.read(fd, 8)) posix.close(fd) # Check data[0] and data[1] if data[0] != 0x03 or data[1] != 0x04: return (0, 0, 4, "First two read bytes are a mismatch") # raise Exception("First two read bytes are a mismatch") # CRC check if self._calc_crc16(data[0:6]) != self._combine_bytes( data[7], data[6]): return (0, 0, 4, "CRC failed") # raise Exception("CRC failed") # Temperature resolution is 16Bit, # temperature highest bit (Bit15) is equal to 1 indicates a # negative temperature, the temperature highest bit (Bit15) # is equal to 0 indicates a positive temperature; # temperature in addition to the most significant bit (Bit14 ~ Bit0) # indicates the temperature sensor string value. # Temperature sensor value is a string of 10 times the # actual temperature value. temp = self._combine_bytes(data[4], data[5]) if temp & 0x8000: temp = -(temp & 0x7FFF) temp /= 10.0 humi = self._combine_bytes(data[2], data[3]) / 10.0 return (temp, humi, 0, '')
def readSensor(self): fd = posix.open("/dev/i2c-%d" % self._i2cbus, posix.O_RDWR) ioctl(fd, self.I2C_SLAVE, self.I2C_ADDR) try: posix.write(fd, b'\0x00') except: pass time.sleep(0.001) posix.write(fd, b'\x03\x00\x04') time.sleep(0.0016) data = bytearray(posix.read(fd, 8)) if data[0] != 0x03 or data[1] != 0x04: raise Exception("First two read bytes are a mismatch") if self._calc_crc16(data[0:6]) != self._combine_bytes( data[7], data[6]): raise Exception("CRC failed") temp = self._combine_bytes(data[4], data[5]) if temp & 0x8000: temp = -(temp & 0x7FFF) temp /= 10.0 humi = self._combine_bytes(data[2], data[3]) / 10.0 return (temp, humi)
def testIPv6NoCrash(self): # Python 2.x does not provide either read() or recvmsg. s = net_test.IPv6PingSocket() written = s.sendto(net_test.IPV6_PING, ("::1", 55)) self.assertEquals(len(net_test.IPV6_PING), written) fd = s.fileno() reply = posix.read(fd, 4096) self.assertEquals(written, len(reply))
def __getitem__(self, key): addr = struct.pack('B', key) if posix.write(self.fd, addr) != 1: raise Exception() ret_val = struct.unpack('B', posix.read(self.fd, 1))[0] return ret_val
def verify_if_file_content_retrieved(): handle = posix.open("Alphabets", 2) chars_to_be_retrieved = 1 if len(posix.read(handle, chars_to_be_retrieved)) > 0: print("File content retrieval validated") else: logger.error("File content retrieval failed")
def verify_upper_bound_edge_case(): handle = posix.open("Alphabets", 2) chars_to_be_retrieved = 2**31 - 1 if len(posix.read(handle, chars_to_be_retrieved)) > 0: print("Read validated till 2 ^ 31 - 1") else: logger.error("Read not working for large numbers ")
def Read(argv, splitter, mem): arg, i = READ_SPEC.Parse(argv) names = argv[i:] if arg.n is not None: # read a certain number of bytes try: name = names[0] except IndexError: name = 'REPLY' # default variable name s = posix.read(sys.stdin.fileno(), arg.n) #log('read -n: %s = %s', name, s) state.SetLocalString(mem, name, s) # NOTE: Even if we don't get n bytes back, there is no error? return 0 if not names: names.append('REPLY') # leftover words assigned to the last name max_results = len(names) # We have to read more than one line if there is a line continuation (and # it's not -r). parts = [] join_next = False while True: line = ReadLineFromStdin() #log('LINE %r', line) if not line: # EOF status = 1 break if line.endswith('\n'): # strip trailing newline line = line[:-1] status = 0 else: # odd bash behavior: fail even if we can set variables. status = 1 spans = splitter.SplitForRead(line, not arg.r) done, join_next = _AppendParts(line, spans, max_results, join_next, parts) #log('PARTS %s continued %s', parts, continued) if done: break for i in xrange(max_results): try: s = parts[i] except IndexError: s = '' # if there are too many variables #log('read: %s = %s', names[i], s) state.SetLocalString(mem, names[i], s) return status
def __import__(filename, module_name): import sys, posix module = sys.modules[module_name] if filename.startswith("%s"): filename = filename % sys.graal_python_core_home fd = posix.open(filename, posix.O_RDONLY) content = posix.read(fd, sys.maxsize) posix.close(fd) code = compile(content, filename, "exec") eval(code, module.__dict__) return module
def read(self, addr, _len): if isinstance(addr, EnumMeta) and hasattr(addr, "ADDR"): addr = addr.ADDR.value elif isinstance(addr, Enum): addr = addr.value addr = struct.pack('B', addr) if posix.write(self.fd, addr) != 1: raise Exception() return posix.read(self.fd, _len)
def ReadLineFromStdin(): chars = [] while True: c = posix.read(0, 1) if not c: break chars.append(c) if c == '\n': break return ''.join(chars)
def test_rename_folder_correct_read(self): os.mkdir("src/folder1") os.mkdir("src/folder2") os.system("cp src/file1 src/folder1/file") os.system("cp src/file2 src/folder2/file") firstFileFd = posix.open("mount/folder1/file", posix.O_RDONLY) secondFileFd = posix.open("mount/folder2/file", posix.O_RDONLY) for i in range(1000): position = random.randint(0, 10000) size = random.randint(1, 10000) posix.lseek(firstFileFd,position, 0) posix.lseek(secondFileFd, position, 0) posix.read(firstFileFd, size) posix.read(secondFileFd, size) posix.rename("mount/folder2", "mount/folder3") posix.rename("mount/folder1","mount/folder2") posix.close(firstFileFd) posix.close(secondFileFd) self.assertTrue(open("mount/folder2/file").read()== open("src/folder2/file").read())
def run(self): self._started = True while not self._stopped: try: packet = posix.read(self._tun.fileno(), 4096) except OSError, e: if e.errno == errno.EAGAIN: continue else: break except ValueError, e: if not self._stopped: raise e
def readSensor(self): fd = posix.open("/dev/i2c-%d" % self._i2cbus, posix.O_RDWR) ioctl(fd, self.I2C_SLAVE, self.I2C_ADDR) # wake up AM2320 sensor, it sleeps when not being # called to keep from heating up and effecting temp, humidity try: posix.write(fd, b'\0x00') except: pass time.sleep(0.001) # sleep at least 0.8ms # write at addr 0x03, start register 0x00, n_registers = 0x04 posix.write(fd, b'\x03\x00\x04') time.sleep(0.0016) # wait for sensor result min 1.5ms # 8 bytes of data # 0: modbus function code 0x03 # 1: n registers to read 0x04 # 2: humidity msb ( most significate byte ) # 3: humidity lsb ( least significate byte ) # 4: temp msb # 5: temp lsb # 6: CRC lsb # 7: CRC msb data = bytearray(posix.read(fd, 8)) # check data[0], data[1] if data[0] != 0x03 or data[1] != 0x04: print("First two bytes mismatch") #CRC check if self._calc_crc16(data[0:6]) != self._combine_bytes(data[7], data[6]): pass # temp resolution 16bits # temp bit 15 == 1 means negative temperature, 0 positive # temp in addition to most significant bit Bit14 ~ Bit0 # indicates string value # sensor value is string of 10x actual temp temp = self._combine_bytes(data[4], data[5]) if temp & 0x8000: temp = -(temp & 0x7FFF) # wrap back to positive, and with 32767 temp /= 10.0 humidity = self._combine_bytes(data[2], data[3]) / 10.0 return (temp, humidity)
def test_pass_fds_make_inheritable(self): import subprocess, posix fd1, fd2 = posix.pipe() assert posix.get_inheritable(fd1) is False assert posix.get_inheritable(fd2) is False subprocess.check_call(['/usr/bin/env', 'python', '-c', 'import os;os.write(%d,b"K")' % fd2], close_fds=True, pass_fds=[fd2]) res = posix.read(fd1, 1) assert res == b"K" posix.close(fd1) posix.close(fd2)
def one_loop(self): ret = True for (fd, ev) in self.poller.poll(1000): if ev & (select.POLLERR | select.POLLHUP): self.flush_outputs() self.poller.unregister(fd) ret = False if ev & select.POLLIN: data = posix.read(fd, 4096) if not data: posix.close(self.fd_map[fd]) ret = False self.queue_write(self.fd_map[fd], data) if ev & select.POLLOUT: self.do_write(fd) return ret
def run(self): while not self._stopped: try: packet = posix.read(self._tun.fileno(), 4096) except OSError, e: if e.errno == errno.EAGAIN: continue else: break ether = scapy.Ether(packet) if ether.type == net_test.ETH_P_IPV6: self.IPv6Packet(ether.payload) elif ether.type == net_test.ETH_P_IP: self.IPv4Packet(ether.payload)
def ReadAllPacketsOn(self, netid, include_multicast=False): packets = [] while True: try: packet = posix.read(self.tuns[netid].fileno(), 4096) if not packet: break ether = scapy.Ether(packet) # Multicast frames are frames where the first byte of the destination # MAC address has 1 in the least-significant bit. if include_multicast or not int(ether.dst.split(":")[0], 16) & 0x1: packets.append(ether.payload) except OSError, e: # EAGAIN means there are no more packets waiting. if re.match(e.message, os.strerror(errno.EAGAIN)): break # Anything else is unexpected. else: raise e
def leerTemHume(self): self._despertar(0.008) # write command 0x03, start reg = 0x00, num regs = 0x04 posix.write(self._fd, b'\x03\x00\x04') #Wait at least 1.5ms for result time.sleep(0.0016) # Read out 8 bytes of result data # Byte 0: Should be Modbus function code 0x03 # Byte 1: Should be number of registers to read (0x04) # Byte 2: Humidity msb # Byte 3: Humidity lsb # Byte 4: Temperature msb # Byte 5: Temperature lsb # Byte 6: CRC lsb byte # Byte 7: CRC msb byte data = bytearray(posix.read(self._fd, 8)) # Check data[0] and data[1] if not correct throw Exception if data[0] != 0x03 or data[1] != 0x04: raise Exception("First two bytes not as expected") # CRC check if self._calc_crc16(data[0:6]) != self._combine_bytes(data[7], data[6]): raise Exception("CRC failed") # Temperature resolution is 16Bit, # temperature highest bit (Bit15) is equal to 1 indicates a # negative temperature, the temperature highest bit (Bit15) # is equal to 0 indicates a positive temperature; # temperature in addition to the most significant bit (Bit14 ~ Bit0) # indicates the temperature sensor string value. # Temperature sensor value is a string of 10 times the # actual temperature value. temp = self._combine_bytes(data[4], data[5]) if temp & 0x8000: temp = -(temp & 0x7FFF) temp /= 10.0 humi = self._combine_bytes(data[2], data[3]) / 10.0 return (temp, humi)
#!/usr/bin/python import os import posix import sys tempdir = sys.argv[1] stdin = sys.stdin pktnum = 0 while True: pktnum += 1 packet = posix.read(0, 10240) # packet = stdin.read() if len(packet) == 0: break print >>sys.stderr, "chunking packet", pktnum, "hash", hash(packet), "and writing to file" out = open(tempdir + "/" + str(pktnum), "w") out.write(packet) out.flush() out.close()
import fcntl
def read(self, len): return posix.read(self.fd, len)