Exemplo n.º 1
0
async def test_dauth():
    async def handler(request):
        if request.path == "/v6/challenge":
            assert request.encode().decode() == CHALLENGE_REQUEST
            response = http.HTTPResponse(200)
            response.json = {
                "challenge": "vaNgVZZH7gUse0y3t8Cksuln-TAVtvBmcD-ow59qp0E=",
                "data": "dlL7ZBNSLmYo1hUlKYZiUA=="
            }
            return response
        else:
            assert request.encode().decode() == TOKEN_REQUEST
            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_context(None)
        response = await client.device_token(client.BAAS)
        token = response["device_auth_token"]
        assert token == "device token"
Exemplo n.º 2
0
    async def test_get(self):
        async def handler(request):
            assert request.path == "/test/get"
            return http.HTTPResponse(200)

        async with http.serve(handler, HOST, 12345):
            response = await http.get("%s:12345/test/get" % HOST)
            assert response.success()
Exemplo n.º 3
0
    async def test_exception(self):
        async def handler(request):
            raise ValueError("Oops")

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            response = await http.request(request)
            assert response.status_code == 500
            assert response.status_name == "Internal Server Error"
Exemplo n.º 4
0
async def test_error():
    async def handler(req):
        response = http.HTTPResponse(404)
        response.body = b"not found"
        return response

    async with http.serve(handler, HOST, 12345):
        with pytest.raises(websocket.WSError):
            async with websocket.connect("TestProtocol", HOST,
                                         12345) as client:
                pass
Exemplo n.º 5
0
    async def test_json(self):
        async def handler(request):
            response = http.HTTPResponse(200)
            response.json = {"result": request.json["value"]}
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.json = {"value": True}
            response = await http.request(request)
            assert response.json["result"] is True
Exemplo n.º 6
0
    async def test_form(self):
        async def handler(request):
            response = http.HTTPResponse(200)
            response.form = {"$<result>": request.form["&value"]}
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.form = {"&value": "&=?"}
            response = await http.request(request)
            assert response.form["$<result>"] == "&=?"
Exemplo n.º 7
0
    async def test_plainform(self):
        async def handler(request):
            assert request.text == "+value +=!=?"
            response = http.HTTPResponse(200)
            response.plainform = {"$<result>": request.plainform["+value +"]}
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.plainform = {"+value +": "!=?"}
            response = await http.request(request)
            assert response.plainform["$<result>"] == "!=?"
Exemplo n.º 8
0
    async def test_continue(self):
        async def handler(request):
            response = http.HTTPResponse(200)
            response.body = request.body
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.continue_threshold = 64
            request.body = b"a" * 80
            response = await http.request(request)
            assert response.body == b"a" * 80
Exemplo n.º 9
0
    async def test_text(self):
        async def handler(request):
            assert request.body == b"Hello"
            response = http.HTTPResponse(200)
            response.text = request.text.upper()
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.text = "Hello"
            response = await http.request(request)
            assert response.text == "HELLO"
Exemplo n.º 10
0
    async def test_body(self):
        async def handler(request):
            assert request.body == b"abcdef"
            response = http.HTTPResponse(200)
            response.headers["X-Content-Size"] = len(request.body)
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.body = b"abcdef"
            response = await http.request(request)
            assert response.headers["X-Content-Size"] == "6"
Exemplo n.º 11
0
    async def test_ok(self):
        async def handler(request):
            assert request.method == "GET"
            assert request.path == "/test/ok"
            response = http.HTTPResponse(200)
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest.get("/test/ok")
            request.headers["Host"] = "%s:12345" % HOST
            response = await http.request(request)
            assert response.status_code == 200
            assert response.status_name == "OK"
            assert response.success()
Exemplo n.º 12
0
async def test_aauth():
    async def handler(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.º 13
0
    async def test_xml(self):
        async def handler(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, HOST, 12345):
            tree = xml.XMLTree("value")
            tree.text = "12345"
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % HOST
            request.xml = tree
            response = await http.request(request)
            assert response.xml.name == "result"
            assert response.xml.text == "12345"
Exemplo n.º 14
0
    async def test_certificate(self):
        # Create a self signed server certificate
        serverkey = tls.TLSPrivateKey.generate()
        servercert = tls.TLSCertificate.generate(serverkey)
        servercert.subject["CN"] = NAME
        servercert.issuer["CN"] = NAME
        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(request):
            assert request.certificate.subject["CN"] == "testclient"
            return http.HTTPResponse(200)

        async with http.serve(handler, HOST, 12345, servercontext):
            request = http.HTTPRequest()
            request.headers["Host"] = "%s:12345" % NAME
            response = await http.request(request, clientcontext)
            assert response.success()
Exemplo n.º 15
0
async def test_baas():
    async def handler(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_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.º 16
0
    async def test_error(self):
        async def handler(request):
            assert request.method == "POST"
            assert request.path == "/test/error"
            status = int(request.headers["X-Status-Code"])
            response = http.HTTPResponse(status)
            return response

        async with http.serve(handler, HOST, 12345):
            request = http.HTTPRequest.post("/test/error")
            request.headers["Host"] = "%s:12345" % HOST
            request.headers["X-Status-Code"] = 404
            response = await http.request(request)
            assert response.status_code == 404
            assert response.status_name == "Not Found"
            assert response.error()

            request = http.HTTPRequest.post("/test/error")
            request.headers["Host"] = "%s:12345" % HOST
            request.headers["X-Status-Code"] = 678
            response = await http.request(request)
            assert response.status_code == 678
            assert response.status_name == "Unknown"