Rev 1280: Add more auth tests. in http://people.samba.org/bzr/jelmer/bzr-svn/0.4

Jelmer Vernooij jelmer at samba.org
Sun Jun 22 21:35:35 BST 2008


At http://people.samba.org/bzr/jelmer/bzr-svn/0.4

------------------------------------------------------------
revno: 1280
revision-id: jelmer at samba.org-20080622203533-v5py10nydd42g0cj
parent: jelmer at samba.org-20080622201700-lbta0usx527q32w0
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: 0.4
timestamp: Sun 2008-06-22 22:35:33 +0200
message:
  Add more auth tests.
modified:
  ra.c                           ra.pyx-20080313140933-qybkqaxe3m4mcll7-1
  tests/test_ra.py               test_ra.py-20080313141743-uzsm7ejitrlqone5-1
=== modified file 'ra.c'
--- a/ra.c	2008-06-22 20:17:00 +0000
+++ b/ra.c	2008-06-22 20:35:33 +0000
@@ -1388,23 +1388,42 @@
 static PyObject *auth_set_parameter(PyObject *self, PyObject *args)
 {
 	AuthObject *auth = (AuthObject *)self;
-	char *name, *value;
-	if (!PyArg_ParseTuple(args, "ss", &name, &value)) {
-        svn_auth_set_parameter(auth->auth_baton, name, (char *)value);
+	char *name;
+	PyObject *value;
+	void *vvalue;
+	if (!PyArg_ParseTuple(args, "sO", &name, &value))
+		return NULL;
+
+	if (!strcmp(name, SVN_AUTH_PARAM_SSL_SERVER_FAILURES)) {
+		vvalue = apr_pcalloc(auth->pool, sizeof(apr_uint32_t));
+		*((apr_uint32_t *)vvalue) = PyInt_AsLong(value);
+	} else {
+		PyErr_Format(PyExc_TypeError, "Unsupported auth parameter %s", name);
+		return NULL;
 	}
 
+    svn_auth_set_parameter(auth->auth_baton, name, (char *)value);
+
 	Py_RETURN_NONE;
 }
 
 static PyObject *auth_get_parameter(PyObject *self, PyObject *args)
 {
 	char *name;
+	const void *value;
 	AuthObject *auth = (AuthObject *)self;
 
 	if (!PyArg_ParseTuple(args, "s", &name))
 		return NULL;
 
-	return PyString_FromString(svn_auth_get_parameter(auth->auth_baton, name));
+	value = svn_auth_get_parameter(auth->auth_baton, name);
+
+	if (!strcmp(name, SVN_AUTH_PARAM_SSL_SERVER_FAILURES)) {
+		return PyInt_FromLong(*((apr_uint32_t *)value));
+	} else {
+		PyErr_Format(PyExc_TypeError, "Unsupported auth parameter %s", name);
+		return NULL;
+	}
 }
 
 typedef struct { 
@@ -1472,6 +1491,12 @@
 	} else if (!strcmp(iterator->cred_kind, SVN_AUTH_CRED_SSL_CLIENT_CERT)) {
 		svn_auth_cred_ssl_client_cert_t *ccert = iterator->credentials;
 		ret = Py_BuildValue("(zb)", ccert->cert_file, ccert->may_save);
+	} else if (!strcmp(iterator->cred_kind, SVN_AUTH_CRED_SSL_CLIENT_CERT_PW)) {
+		svn_auth_cred_ssl_client_cert_pw_t *ccert = iterator->credentials;
+		ret = Py_BuildValue("(zb)", ccert->password, ccert->may_save);
+	} else if (!strcmp(iterator->cred_kind, SVN_AUTH_CRED_SSL_SERVER_TRUST)) {
+		svn_auth_cred_ssl_server_trust_t *ccert = iterator->credentials;
+		ret = Py_BuildValue("(ib)", ccert->accepted_failures, ccert->may_save);
 	} else {
 		PyErr_Format(PyExc_RuntimeError, "Unknown cred kind %s", iterator->cred_kind);
 		return NULL;
@@ -1521,6 +1546,12 @@
 	ret = PyObject_CallFunction(fn, "sb", realm, may_save);
 	if (ret == NULL)
 		return py_svn_error();
+
+	if (!PyTuple_Check(ret)) {
+		PyErr_SetString(PyExc_TypeError, "expected tuple with username credentials");
+		return py_svn_error();
+	}
+
 	*cred = apr_pcalloc(pool, sizeof(**cred));
 	(*cred)->username = apr_pstrdup(pool, PyString_AsString(PyTuple_GetItem(ret, 0)));
 	(*cred)->may_save = (PyTuple_GetItem(ret, 1) == Py_True);
@@ -1583,20 +1614,29 @@
 {
     PyObject *fn = (PyObject *)baton;
 	PyObject *ret;
+	PyObject *py_cert;
 
-	ret = PyObject_CallFunction(fn, "sl(ssssss)b", realm, failures, 
-						  cert_info->hostname, cert_info->fingerprint, 
+	if (cert_info == NULL) {
+		py_cert = Py_None;
+	} else {
+		py_cert = Py_BuildValue("(sssss)", cert_info->hostname, cert_info->fingerprint, 
 						  cert_info->valid_from, cert_info->valid_until, 
-						  cert_info->issuer_dname, cert_info->ascii_cert, 
-						  may_save);
+						  cert_info->issuer_dname, cert_info->ascii_cert);
+	}
+
+	ret = PyObject_CallFunction(fn, "slOb", realm, failures, py_cert, may_save);
 	if (ret == NULL)
 		return py_svn_error();
 
 	/* FIXME: Check that ret is a tuple of size 2 */
+	if (!PyTuple_Check(ret)) {
+		PyErr_SetString(PyExc_TypeError, "expected tuple with server trust credentials");
+		return py_svn_error();
+	}
 	
 	*cred = apr_pcalloc(pool, sizeof(**cred));
-	(*cred)->may_save = (PyTuple_GetItem(ret, 0) == Py_True);
-	(*cred)->accepted_failures = PyLong_AsLong(PyTuple_GetItem(ret, 1));
+	(*cred)->accepted_failures = PyInt_AsLong(PyTuple_GetItem(ret, 0));
+	(*cred)->may_save = (PyTuple_GetItem(ret, 1) == Py_True);
 
     return NULL;
 }

=== modified file 'tests/test_ra.py'
--- a/tests/test_ra.py	2008-06-22 20:17:00 +0000
+++ b/tests/test_ra.py	2008-06-22 20:35:33 +0000
@@ -137,6 +137,19 @@
         self.assertEquals(("filename", 0), creds.next())
         self.assertRaises(StopIteration, creds.next)
 
+    def test_client_cert_pw(self):
+        auth = ra.Auth([ra.get_ssl_client_cert_pw_prompt_provider(lambda realm, may_save: ("supergeheim", 0), 0)])
+        creds = auth.credentials("svn.ssl.client-passphrase", "MyRealm")
+        self.assertEquals(("supergeheim", 0), creds.next())
+        self.assertRaises(StopIteration, creds.next)
+
+    def test_server_trust(self):
+        auth = ra.Auth([ra.get_ssl_server_trust_prompt_provider(lambda realm, failures, certinfo, may_save: (42, 0))])
+        auth.set_parameter("svn:auth:ssl:failures", 23)
+        creds = auth.credentials("svn.ssl.server", "MyRealm")
+        self.assertEquals((42, 0), creds.next())
+        self.assertRaises(StopIteration, creds.next)
+
     def test_retry(self):
         self.i = 0
         def inc_foo(realm, may_save):




More information about the bazaar-commits mailing list