Skip to content
test_interfaces_client.py 3.56 KiB
Newer Older
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""
Test cases for wpa_supplicant.client.interfaces.InterfaceClient
"""

import unittest
from unittest.mock import call

import anyio
from tests.unit import anyio as anyio_mock
from wpa_supplicant import config
from wpa_supplicant.client import interfaces


class MethodsTests(unittest.TestCase):
	"""
	Tests for InterfaceClient methods
	"""

	def setUp(self):
		async def _make_client():
			return interfaces.InterfaceClient()
		self.client = client = anyio.run(_make_client)
		client.sock = anyio_mock.AsyncMock()
		client.sock.send.side_effect = len

	@anyio_mock.with_anyio()
	async def test_scan(self):
		"""
		Check that a scan command waits for a notification then terminates correctly
		"""
		async with self.client as client:
			client.sock.recv.side_effect = [
				b"OK",  # Response to "ATTACH"
				b"OK",  # Response to "SCAN"
				b"<3>CTRL-EVENT-SCAN-RESULTS",
				b"good=value\nid=1\n",  # Response to "BSS 1"
				b"good=value\nid=2\n",  # Response to "BSS 2"
				b"",  # Response to "BSS 3"
				b"OK",  # Response to "DETACH"
			]

			async for bss in client.scan():
				self.assertIsInstance(bss, dict)
Dom Sekotill's avatar
Dom Sekotill committed
				self.assertIn("good", bss)

	@anyio_mock.with_anyio()
	async def test_set_network(self):
		"""
		Check that set_network sends values to the daemon and raises TypeError for bad types
		"""
		async with self.client as client:
			client.sock.recv.return_value = b"OK"
			send = client.sock.send

			with self.subTest("good string"):
				await client.set_network("0", "ssid", "example")
				send.assert_called_once_with(b'SET_NETWORK 0 ssid "example"')
				send.reset_mock()

			with self.subTest("good int"):
				await client.set_network("0", "priority", 1)
				send.assert_called_once_with(b"SET_NETWORK 0 priority 1")
				send.reset_mock()

			with self.subTest("good enum"):
				await client.set_network("0", "key_mgmt", config.KeyManagementValue.WPA_PSK)
				send.assert_called_once_with(b"SET_NETWORK 0 key_mgmt WPA-PSK")
				send.reset_mock()

			with self.subTest("int not a string"), self.assertRaises(TypeError):
				await client.set_network("0", "ssid", 1)
				send.reset_mock()

			with self.subTest("string not an int"), self.assertRaises(TypeError):
				await client.set_network("0", "priority", "example")
				send.reset_mock()

			with self.subTest("string not an enum"), self.assertRaises(TypeError):
				await client.set_network("0", "key_mgmt", "example")
				send.reset_mock()

			with self.subTest("int not an enum"), self.assertRaises(TypeError):
				await client.set_network("0", "key_mgmt", 1)
				send.reset_mock()

	@anyio_mock.with_anyio()
	async def test_add_network(self):
		"""
		Check that add_network adds a new network and configures it
		"""
		async with self.client as client:
			send = client.sock.send
			client.sock.recv.side_effect = [
				b"0",
				b"OK",
				b"OK",
			]

			await client.add_network({"ssid": "example"})

			send.assert_has_calls(
				[
					call(b"ADD_NETWORK"),
					call(b'SET_NETWORK 0 ssid "example"'),
					call(b"ENABLE_NETWORK 0"),