Skip to content

Commit aa8e95d

Browse files
authored
Merge pull request #146 from hoefling/default-base64-line-size
new function to set default max columns size for base64 encoding
2 parents 8cc5fa0 + 096dd0c commit aa8e95d

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

src/main.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <xmlsec/xmlsec.h>
1515
#include <xmlsec/crypto.h>
1616
#include <xmlsec/errors.h>
17+
#include <xmlsec/base64.h>
1718

1819
#define _PYXMLSEC_FREE_NONE 0
1920
#define _PYXMLSEC_FREE_XMLSEC 1
@@ -127,6 +128,34 @@ static PyObject* PyXmlSec_PyEnableDebugOutput(PyObject *self, PyObject* args, Py
127128
Py_RETURN_NONE;
128129
}
129130

131+
static char PyXmlSec_PyBase64DefaultLineSize__doc__[] = \
132+
"base64_default_line_size(size = None)\n"
133+
"Configures the default maximum columns size for base64 encoding.\n\n"
134+
"If ``size`` is not given, this function returns the current default size, acting as a getter. "
135+
"If ``size`` is given, a new value is applied and this function returns nothing, acting as a setter.\n"
136+
":param size: new default size value (optional)\n"
137+
":type size: :class:`int` or :data:`None`";
138+
static PyObject* PyXmlSec_PyBase64DefaultLineSize(PyObject *self, PyObject *args, PyObject *kwargs) {
139+
static char *kwlist[] = { "size", NULL };
140+
PyObject *pySize = NULL;
141+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:base64_default_line_size", kwlist, &pySize)) {
142+
return NULL;
143+
}
144+
if (pySize == NULL) {
145+
return PyLong_FromLong(xmlSecBase64GetDefaultLineSize());
146+
}
147+
int size = (int)PyLong_AsLong(pySize);
148+
if (PyErr_Occurred()) {
149+
return NULL;
150+
}
151+
if (size < 0) {
152+
PyErr_SetString(PyExc_ValueError, "size must be positive");
153+
return NULL;
154+
}
155+
xmlSecBase64SetDefaultLineSize(size);
156+
Py_RETURN_NONE;
157+
}
158+
130159
static PyMethodDef PyXmlSec_MainMethods[] = {
131160
{
132161
"init",
@@ -146,6 +175,12 @@ static PyMethodDef PyXmlSec_MainMethods[] = {
146175
METH_VARARGS|METH_KEYWORDS,
147176
PyXmlSec_PyEnableDebugOutput__doc__
148177
},
178+
{
179+
"base64_default_line_size",
180+
(PyCFunction)PyXmlSec_PyBase64DefaultLineSize,
181+
METH_VARARGS|METH_KEYWORDS,
182+
PyXmlSec_PyBase64DefaultLineSize__doc__
183+
},
149184
{NULL, NULL} /* sentinel */
150185
};
151186

src/xmlsec/__init__.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from typing import AnyStr, IO, Iterable, Optional, Type, TypeVar, Union
2+
from typing import AnyStr, IO, Iterable, Optional, Type, TypeVar, Union, overload
33

44
from lxml.etree import _Element
55

@@ -24,6 +24,10 @@ _K = TypeVar('_K', bound=Key)
2424
def enable_debug_trace(enabled: bool = ...) -> None: ...
2525
def init() -> None: ...
2626
def shutdown() -> None: ...
27+
@overload
28+
def base64_default_line_size() -> int: ...
29+
@overload
30+
def base64_default_line_size(size: int) -> None: ...
2731

2832
class EncryptionContext:
2933
key: Optional[Key]

tests/test_main.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import xmlsec
2+
from tests import base
3+
4+
5+
class TestBase64LineSize(base.TestMemoryLeaks):
6+
def tearDown(self):
7+
xmlsec.base64_default_line_size(64)
8+
super(TestBase64LineSize, self).tearDown()
9+
10+
def test_get_base64_default_line_size(self):
11+
self.assertEqual(xmlsec.base64_default_line_size(), 64)
12+
13+
def test_set_base64_default_line_size_positional_arg(self):
14+
xmlsec.base64_default_line_size(0)
15+
self.assertEqual(xmlsec.base64_default_line_size(), 0)
16+
17+
def test_set_base64_default_line_size_keyword_arg(self):
18+
xmlsec.base64_default_line_size(size=0)
19+
self.assertEqual(xmlsec.base64_default_line_size(), 0)
20+
21+
def test_set_base64_default_line_size_with_bad_args(self):
22+
size = xmlsec.base64_default_line_size()
23+
for bad_size in (None, '', object()):
24+
with self.assertRaises(TypeError):
25+
xmlsec.base64_default_line_size(bad_size)
26+
self.assertEqual(xmlsec.base64_default_line_size(), size)
27+
28+
def test_set_base64_default_line_size_rejects_negative_values(self):
29+
size = xmlsec.base64_default_line_size()
30+
with self.assertRaises(ValueError):
31+
xmlsec.base64_default_line_size(-1)
32+
self.assertEqual(xmlsec.base64_default_line_size(), size)

0 commit comments

Comments
 (0)