def test_wrong_number_of_parameters(self): for num in range(1, PARAMS): tmp_parameters = tuple(VALID_ARGS()[0][0:num]) with player_context(tmp_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as program: self.assertExitFailure(program) for num in range(PARAMS + 1, PARAMS + 3): tmp_parameters = tuple(VALID_ARGS()[0][0:PARAMS]) + tuple(["0"] * (num - PARAMS)) with player_context(tmp_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as program: self.assertExitFailure(program)
def test_play_pause_command(self): valid_parameters = VALID_ARGS()[2] with player_context(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as program: # check if file is filling up size = os.path.getsize(valid_parameters[3]) time.sleep(QUANTUM_SECONDS) self.assertNotEqual(size, os.path.getsize(valid_parameters[3])) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(b'PAUSE', ('127.0.0.1', int(valid_parameters[4]))) time.sleep(QUANTUM_SECONDS) # wait for player to parse command # check if file has stopped filling up size = os.path.getsize(valid_parameters[3]) time.sleep(QUANTUM_SECONDS) self.assertEqual(size, os.path.getsize(valid_parameters[3])) sock.sendto(b'PLAY', ('127.0.0.1', int(valid_parameters[4]))) time.sleep(QUANTUM_SECONDS) # wait for player to parse command # check if file is filling up again size = os.path.getsize(valid_parameters[3]) time.sleep(QUANTUM_SECONDS) self.assertNotEqual(size, os.path.getsize(valid_parameters[3])) sock.close() program.kill() program.wait() os.remove(valid_parameters[3])
def test_server_close_connection_when_header(self): valid_parameters = VALID_ARGS()[5] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): sock.shutdown(socket.SHUT_WR) self.assertExitFailure(program)
def test_timeout_when_header(self): valid_parameters = VALID_ARGS()[5] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): time.sleep(WAIT_TIMEOUT) self.assertExitFailure(program)
def test_no_metadata_requested(self): valid_parameters = VALID_ARGS()[4] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): data = sock.recv(1000) self.assertIn(b"Icy-MetaData:0", data)
def test_title_command_with_custom_server(self): valid_parameters = VALID_ARGS()[5] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as (sock, program): sock.send(b'ICY 200 OK\r\n') sock.send(b'icy-metaint:16\r\n') sock.send(b'\r\n') for _ in range(0, 10): sock.send(b'Z' * 16) sock.send(b'\x00') sock.send(b'Z' * 16) sock.send(b'\x02') sock.send(b"StreamTitle='title of the song';") for _ in range(0, 10): sock.send(b'Z' * 16) sock.send(b'\x00') time.sleep(LONG_PAUSE) command_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) command_sock.sendto(b'TITLE', ('127.0.0.1', int(valid_parameters[4]))) time.sleep(LONG_PAUSE) response = command_sock.recvfrom(100) command_sock.close() self.assertIn(response[0], [b"title of the song", b"'title of the song'"])
def test_invalid_response_streamer(self): valid_parameters = VALID_ARGS()[4] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): sock.send(b'ICY 404 OK\r\n') sock.send(b'\r\n') self.assertExitFailure(program)
def test_requests_crlf(self): """HTTP header should have lines ending with CRLF.""" valid_parameters = VALID_ARGS()[5] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): data = sock.recv(1000) self.assertIn(b"\r\n", data)
def test_quit(self): with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") client.send(b"START %s %s\n" % (PLAYER_HOSTNAME, args)) player_id = self.assertOK(client.readline()) client.send(b"QUIT %s\n" % player_id) time.sleep(QUANTUM_SECONDS) self.assertEqual(subprocess.check_output(["pidof", "player"]), b"")
def test_telnet_control_sequences(self): sequences = (b"\xff\xfe\x06", b"\xff\xf5", b"\xff\xf7") with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") for seq in sequences: index = randint(0, len(args)) args = args[0:index] + seq + args[index:] client.send(b"START %s %s\n" % (PLAYER_HOSTNAME, args)) player_id = self.assertOK(client.readline())
def test_client_crash(self): with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") client.send(b"START %s %s\n" % (PLAYER_HOSTNAME, args)) player_id = self.assertOK(client.readline()) time.sleep(QUANTUM_SECONDS) self.assertEqual(subprocess.call(["killall", "player"]), 0) line = client.readline() self.assertTrue(line.startswith(b"ERROR %s" % player_id))
def test_quit_command(self): valid_parameters = VALID_ARGS()[1] with player_context(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as program: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(WAIT_TIMEOUT) sock.sendto(b'QUIT', ('localhost', int(valid_parameters[4]))) sock.close() self.assertEqual(program.wait(timeout=QUANTUM_SECONDS), 0)
def test_wrong_parameters(self): valid_parameters = list(VALID_ARGS()[0]) for num in range(0, PARAMS): for wrong_param in INVALID_ARG_VALUES[num]: tmp_parameters = valid_parameters.copy() tmp_parameters[num] = wrong_param with player_context(tuple(tmp_parameters), stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as program: self.assertExitFailure(program)
def test_timeout_when_sending_data(self): valid_parameters = VALID_ARGS()[5] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): sock.send(b'ICY 200 OK\r\n') sock.send(b'icy-metaint:16\r\n') sock.send(b'\r\n') sock.send(b'Z' * 12) time.sleep(WAIT_TIMEOUT) self.assertExitFailure(program)
def test_server_close_connection_when_sending_data(self): valid_parameters = VALID_ARGS()[5] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as (sock, program): sock.send(b'ICY 200 OK\r\n') sock.send(b'icy-metaint:16\r\n') sock.send(b'\r\n') sock.send(b'Z' * 12) sock.shutdown(socket.SHUT_WR) self.assertEqual(program.wait(timeout=QUANTUM_SECONDS), 0)
def test_no_meta_data(self): valid_parameters = VALID_ARGS()[1] with player_context(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as program: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(b'TITLE', ('127.0.0.1', int(valid_parameters[4]))) response = sock.recvfrom(1000) sock.close() self.assertEqual(response[0], b'') self.assertEqual(response[1], ('127.0.0.1', int(valid_parameters[4])))
def test_invalid_command(self): valid_parameters = VALID_ARGS()[0] with player_context(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as program: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for _ in range(0, 10000): command = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(4, 6))) sock.sendto(bytes(command, 'utf-8'), ('127.0.0.1', int(valid_parameters[4]))) sock.close() with self.assertRaises(subprocess.TimeoutExpired): self.assertEqual(program.wait(timeout=QUANTUM_SECONDS), 0)
def test_saving_data_without_meta(self): valid_parameters = VALID_ARGS()[6] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as (sock, program): sock.send(b'ICY 200 OK\r\n') sock.send(b'\r\n') for _ in range(0, 1000): sock.send(b'Z' * 16) time.sleep(LONG_PAUSE) # flush can take a while :C self.assertEqual(os.path.getsize(valid_parameters[3]), 16000) self.assertAllZ(valid_parameters[3]) os.remove(valid_parameters[3])
def test_spam_command(self): valid_parameters = VALID_ARGS()[0] with player_context(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as program: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for _ in range(0, 10000): sock.sendto(b'PAUSE', ('127.0.0.1', int(valid_parameters[4]))) sock.sendto(b'PLAY', ('127.0.0.1', int(valid_parameters[4]))) sock.sendto(b'TITLE', ('127.0.0.1', int(valid_parameters[4]))) sock.close() with self.assertRaises(subprocess.TimeoutExpired): self.assertEqual(program.wait(timeout=QUANTUM_SECONDS), 0)
def test_saving_data_with_meta(self): valid_parameters = VALID_ARGS()[7] with streamer_server(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as (sock, program): sock.send(b'ICY 200 OK\r\n') sock.send(b'icy-metaint:16\r\n') sock.send(b'\r\n') for _ in range(0, 1000): sock.send(((b'Z' * 16) + (b'\x02') + b"StreamTitle='title of the song';")) time.sleep(LONG_PAUSE) self.assertEqual(os.path.getsize(valid_parameters[3]), 16 * 1000) self.assertAllZ(valid_parameters[3]) os.remove(valid_parameters[3])
def test_title_command(self): valid_parameters = VALID_ARGS()[0] with player_context(valid_parameters, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as program: time.sleep(LONG_PAUSE) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(WAIT_TIMEOUT) sock.sendto(b'TITLE', ('127.0.0.1', int(valid_parameters[4]))) time.sleep(QUANTUM_SECONDS) response = sock.recvfrom(100) sock.close() self.assertTrue(response[0]) self.assertEqual(response[1], ('127.0.0.1', int(valid_parameters[4])))
def test_at_negative_duration(self): with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") client.send(b"AT 21.12 -1 %s %s\n" % (PLAYER_HOSTNAME, args)) self.assertTrue(client.readline().startswith(b"ERROR"))
def test_start_with_large_spaces(self): with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") client.send(b"START %s %s\n" % (PLAYER_HOSTNAME, args)) self.assertOK(client.readline())
def test_valid(self): with player_context(VALID_ARGS()[3], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as program: with self.assertRaises(subprocess.TimeoutExpired): self.assertExitFailure(program)
def test_at_invalid_time5(self): with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") client.send(b"AT 1.a22 10 %s %s\n" % (PLAYER_HOSTNAME, args)) self.assertTrue(client.readline().startswith(b"ERROR"))
def test_at(self): with master_and_mock_client() as client: arg_list = VALID_ARGS()[8] output_path = os.path.expanduser(os.path.join("~", arg_list[3])) if os.path.exists(output_path): os.remove(output_path) # Launch client now = datetime.datetime.now() starttime = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute) + \ datetime.timedelta(minutes=1) time_str = b"%d.%d" % (starttime.hour, starttime.minute) args = bytes(" ".join(arg_list), "utf-8") client.send(b"AT %s 10 %s %s\n" % (time_str, PLAYER_HOSTNAME, args)) player_id = self.assertOK(client.readline()) to_wait = starttime - now to_wait_s = int(to_wait.seconds + (to_wait.microseconds / 1e6) + 2) if to_wait_s > 5: client.send(b"PAUSE %s\n" % player_id) response = client.readline() self.assertTrue(response.startswith(b"ERROR %s" % player_id)) else: print( "\nIgnoring some tests, because there's not enough wait time" ) print("") while to_wait_s > 0: print("Waiting %d \r" % to_wait_s, end="") time.sleep(1) to_wait_s -= 1 print("Running ") self.assertTrue(os.path.exists(output_path)) # Check it's filling up initial_size = os.path.getsize(output_path) time.sleep(LONG_PAUSE) size = os.path.getsize(output_path) self.assertTrue(size > initial_size) # Check PAUSE works client.send(b"PAUSE %s\n" % player_id) time.sleep(QUANTUM_SECONDS) self.assertEqual(self.assertOK(client.readline()), player_id) initial_size = os.path.getsize(output_path) time.sleep(LONG_PAUSE) size = os.path.getsize(output_path) self.assertEqual(size, initial_size) # Check PLAY works initial_size = os.path.getsize(output_path) client.send(b"PLAY %s\n" % player_id) time.sleep(LONG_PAUSE) self.assertEqual(self.assertOK(client.readline()), player_id) size = os.path.getsize(output_path) self.assertTrue(size > initial_size) # Check QUIT works initial_size = os.path.getsize(output_path) client.send(b"QUIT %s\n" % player_id) self.assertEqual(self.assertOK(client.readline()), player_id) time.sleep(LONG_PAUSE) self.assertEqual(subprocess.check_output(["pidof", "player"]), b"")
def test_at_command(self): with master_and_mock_client() as client: args = bytes(" ".join(VALID_ARGS()[8]), "utf-8") client.send(b"AT 21.12 100 %s %s\n" % (PLAYER_HOSTNAME, args)) self.assertOK(client.readline())