Exemplo n.º 1
0
async def test_baas():
	async def handler(client, request):
		if request.path == "/1.0.0/application/token":
			assert request.encode().decode() == ACCESS_REQUEST
			response = http.HTTPResponse(200)
			response.json = {
				"accessToken": "access.token"
			}
			return response
		else:
			assert request.encode().decode() == LOGIN_REQUEST
			response = http.HTTPResponse(200)
			response.json = {
				"idToken": "id token"
			}
			return response
			
	
	async with http.serve(handler, "127.0.0.1", 12345):
		client = baas.BAASClient()
		client.set_url("127.0.0.1:12345")
		client.set_system_version(1200)
		client.set_context(None)
		response = await client.authenticate("device.token")
		token = response["accessToken"]
		response = await client.login(
			0x1234567890abcdef, "a" * 40, token, "app.token"
		)
		assert response["idToken"] == "id token"
Exemplo n.º 2
0
async def test_dauth_1200():
    async def handler(client, request):
        if request.path == "/v6/challenge":
            assert request.encode().decode() == CHALLENGE_REQUEST_1200
            response = http.HTTPResponse(200)
            response.json = {
                "challenge": "vaNgVZZH7gUse0y3t8Cksuln-TAVtvBmcD-ow59qp0E=",
                "data": "dlL7ZBNSLmYo1hUlKYZiUA=="
            }
            return response
        else:
            assert request.encode().decode() == TOKEN_REQUEST_1200
            response = http.HTTPResponse(200)
            response.json = {"device_auth_token": "device token"}
            return response

    async with http.serve(handler, "127.0.0.1", 12345):
        keys = switch.KeySet()
        keys["aes_kek_generation_source"] = bytes.fromhex(
            "485d45ad27c07c7e538c0183f90ee845")
        keys["master_key_0a"] = bytes.fromhex(
            "37eed242e0f2ce6f8371e783c1a6a0ae")
        client = dauth.DAuthClient(keys)
        client.set_url("127.0.0.1:12345")
        client.set_system_version(1200)
        client.set_context(None)
        response = await client.device_token(client.BAAS)
        token = response["device_auth_token"]
        assert token == "device token"
Exemplo n.º 3
0
async def test_dauth_1300():
    async def handler(client, request):
        if request.path == "/v7/challenge":
            assert request.encode().decode() == CHALLENGE_REQUEST_1300
            response = http.HTTPResponse(200)
            response.json = {
                "challenge": "TzJ0EB3EvsWvQI5aPj15uaNVH9paGdsWB4l-eI5uzW0=",
                "data": "4SxW91vqVg6pz4CXMH2Ouw=="
            }
            return response
        else:
            assert request.encode().decode() == TOKEN_REQUEST_1300
            response = http.HTTPResponse(200)
            response.json = {"device_auth_token": "device token"}
            return response

    async with http.serve(handler, "127.0.0.1", 12345):
        keys = switch.KeySet()
        keys["aes_kek_generation_source"] = bytes.fromhex(
            "cae2728f56af642d5d59dfc23bd314a2")
        keys["master_key_0c"] = bytes.fromhex(
            "f1642c98bddb5850eb23d0cebab7dc05")
        client = dauth.DAuthClient(keys)
        client.set_url("127.0.0.1:12345")
        client.set_system_version(1300)
        client.set_context(None)
        response = await client.device_token(client.BAAS)
        token = response["device_auth_token"]
        assert token == "device token"
Exemplo n.º 4
0
    async def test_exception(self):
        async def handler(client, request):
            raise ValueError("Oops")

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345")
            assert response.status_code == 500
            assert response.status_name == "Internal Server Error"
Exemplo n.º 5
0
    async def test_json(self):
        async def handler(client, request):
            response = http.HTTPResponse(200)
            response.json = {"result": request.json["value"]}
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345", json={"value": True})
            assert response.json["result"] is True
Exemplo n.º 6
0
    async def test_ok(self):
        async def handler(client, request):
            assert request.method == "GET"
            assert request.path == "/test/ok"
            response = http.HTTPResponse(200)
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345/test/ok")
            assert response.success()
Exemplo n.º 7
0
    async def test_text(self):
        async def handler(client, request):
            assert request.body == b"Hello"
            response = http.HTTPResponse(200)
            response.text = request.text.upper()
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345", text="Hello")
            assert response.text == "HELLO"
Exemplo n.º 8
0
    async def test_rawform(self):
        async def handler(client, request):
            response = http.HTTPResponse(200)
            response.rawform = {"$<result>": request.rawform["value+!"]}
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345",
                                      rawform={"value+!": "???"})
            assert response.rawform["$<result>"] == "???"
Exemplo n.º 9
0
    async def test_files(self):
        async def handler(client, request):
            response = http.HTTPResponse(200)
            response.files = {"response": request.files["filename"]}
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345",
                                      boundary="TEST",
                                      files={"filename": b"content"})
            assert response.files == {"response": b"content"}
Exemplo n.º 10
0
    async def test_body(self):
        async def handler(client, request):
            assert request.body == b"abcdef"
            response = http.HTTPResponse(200)
            response.body = request.body[::-1]
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345", body=b"abcdef")
            assert response.success()
            assert response.body == b"fedcba"
Exemplo n.º 11
0
    async def test_headers(self):
        async def handler(client, request):
            assert request.headers["X-Header-1"] == "test1"
            assert request.headers["X-Header-2"] == "test2"
            return http.HTTPResponse(200)

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345",
                                      headers={
                                          "X-Header-1": "test1",
                                          "X-Header-2": "test2"
                                      })
            assert response.success()
Exemplo n.º 12
0
    async def test_continue(self):
        async def handler(client, request):
            response = http.HTTPResponse(200)
            response.body = request.body
            return response

        async with http.serve(handler, "localhost", 12345):
            request = http.HTTPRequest()
            request.continue_threshold = 64
            request.body = b"a" * 80

            response = await http.request("localhost:12345", request)
            assert response.body == b"a" * 80
Exemplo n.º 13
0
    async def test_request(self):
        async def handler(client, request):
            assert request.path == "/path"
            response = http.HTTPResponse(200)
            response.body = request.body
            return response

        async with http.serve(handler, "localhost", 12345):
            request = http.HTTPRequest()
            request.body = b"test"
            request.path = "/path"

            response = await http.request("localhost:12345", request)
            assert response.body == b"test"
Exemplo n.º 14
0
    async def test_xml(self):
        async def handler(client, request):
            assert request.xml.name == "value"
            tree = xml.XMLTree("result")
            tree.text = request.xml.text
            response = http.HTTPResponse(200)
            response.xml = tree
            return response

        async with http.serve(handler, "localhost", 12345):
            tree = xml.XMLTree("value")
            tree.text = "12345"
            response = await http.get("localhost:12345", xml=tree)
            assert response.xml.name == "result"
            assert response.xml.text == "12345"
Exemplo n.º 15
0
async def test_aauth():
    async def handler(client, request):
        text = request.encode().decode()
        assert text.startswith(EXPECTED_REQUEST)
        assert text[1378:1388] == "&cert_key="
        response = http.HTTPResponse(200)
        response.json = {"application_auth_token": "application token"}
        return response

    async with http.serve(handler, "127.0.0.1", 12345):
        client = aauth.AAuthClient()
        client.set_url("127.0.0.1:12345")
        client.set_context(None)
        response = await client.auth_digital(0x0100123001234000, 0x70000,
                                             "device.token", CERT)
        assert response["application_auth_token"] == "application token"
Exemplo n.º 16
0
    async def test_status(self):
        async def handler(client, request):
            status = int(request.headers["X-Status-Code"])
            return http.HTTPResponse(status)

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345",
                                      headers={"X-Status-Code": 404})
            assert response.status_code == 404
            assert response.status_name == "Not Found"
            assert response.error()

            response = await http.get("localhost:12345",
                                      headers={"X-Status-Code": 678})
            assert response.status_code == 678
            assert response.status_name == "Unknown"
            assert response.error()
Exemplo n.º 17
0
    async def test_methods(self):
        async def handler(client, request):
            response = http.HTTPResponse(200)
            response.text = request.method
            return response

        async with http.serve(handler, "localhost", 12345):
            response = await http.get("localhost:12345")
            assert response.text == "GET"

            response = await http.post("localhost:12345")
            assert response.text == "POST"

            response = await http.put("localhost:12345")
            assert response.text == "PUT"

            response = await http.patch("localhost:12345")
            assert response.text == "PATCH"

            response = await http.delete("localhost:12345")
            assert response.text == "DELETE"
Exemplo n.º 18
0
    async def test_certificate(self):
        # Create a self signed server certificate
        serverkey = tls.TLSPrivateKey.generate()
        servercert = tls.TLSCertificate.generate(serverkey)
        servercert.subject["CN"] = "localhost"
        servercert.issuer["CN"] = "localhost"
        servercert.sign(serverkey)

        # Create a certificate authority for the client certificate
        authoritykey = tls.TLSPrivateKey.generate()
        authoritycert = tls.TLSCertificate.generate(authoritykey)
        authoritycert.subject["CN"] = "authority"
        authoritycert.issuer["CN"] = "authority"
        authoritycert.sign(authoritykey)

        # Create a client certificate and sign it
        clientkey = tls.TLSPrivateKey.generate()
        clientcert = tls.TLSCertificate.generate(clientkey)
        clientcert.subject["CN"] = "testclient"
        clientcert.issuer["CN"] = "authority"
        clientcert.sign(authoritykey)

        # Create TLS context for the server
        servercontext = tls.TLSContext()
        servercontext.set_certificate(servercert, serverkey)
        servercontext.set_authority(authoritycert)

        clientcontext = tls.TLSContext()
        clientcontext.set_certificate(clientcert, clientkey)
        clientcontext.set_authority(servercert)

        async def handler(client, request):
            cert = client.remote_certificate()
            assert cert.subject["CN"] == "testclient"
            return http.HTTPResponse(200)

        async with http.serve(handler, "localhost", 12345, servercontext):
            response = await http.get("localhost:12345", context=clientcontext)
            assert response.success()
Exemplo n.º 19
0
async def serve(host, port, context, clients, matchmaker):
    dashboard = Dashboard(clients, matchmaker)
    async with http.serve(dashboard.handle, host, port, context):
        yield