diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-04-05 14:47:12 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-04-05 15:37:52 +0200 |
commit | 5200d7022b34c0be646ae92de8c638c9437c7072 (patch) | |
tree | df5231709c2be361f07a9332c53beadedb86d3fe /src/tests | |
parent | 7effe02be236921437fb77d8fbeb21eb62e35939 (diff) |
Add permission subsystem
Change-Id: I0bbbf55832faf6413dd6147174934d0d4dc5277d
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/CMakeLists.txt | 20 | ||||
-rw-r--r-- | src/tests/test-perm/CMakeLists.txt | 24 | ||||
-rw-r--r-- | src/tests/test-perm/test-perm.c | 166 |
3 files changed, 210 insertions, 0 deletions
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt new file mode 100644 index 00000000..4a1345ee --- /dev/null +++ b/src/tests/CMakeLists.txt @@ -0,0 +1,20 @@ +########################################################################### +# Copyright 2017 IoT.bzh +# +# author: José Bollo <jose.bollo@iot.bzh> +# +# 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. +########################################################################### + +add_subdirectory(test-perm) + diff --git a/src/tests/test-perm/CMakeLists.txt b/src/tests/test-perm/CMakeLists.txt new file mode 100644 index 00000000..4747d182 --- /dev/null +++ b/src/tests/test-perm/CMakeLists.txt @@ -0,0 +1,24 @@ +########################################################################### +# Copyright 2017 IoT.bzh +# +# author: José Bollo <jose.bollo@iot.bzh> +# +# 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. +########################################################################### + +include_directories(../..) +add_executable(test-perm test-perm.c) +target_link_libraries(test-perm afb-lib) +add_test(NAME test-perm COMMAND test-perm) + + diff --git a/src/tests/test-perm/test-perm.c b/src/tests/test-perm/test-perm.c new file mode 100644 index 00000000..8d63e3c7 --- /dev/null +++ b/src/tests/test-perm/test-perm.c @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2017 "IoT.bzh" + * Author José Bollo <jose.bollo@iot.bzh> + * + * 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. + */ + + +#include <stdio.h> +#include <stdarg.h> +#include <string.h> + +#include "afb-perm.h" + +char *exprs[] = { + "a", + "not a", + "a or b", + "a or b or c", + "a or b or c or d", + "a and b", + "a and b and c", + "a and b and c and d", + "a and b or c and d", + "a or b and c or d", + "(a or b) and (c or d)", + "not (a or b or c or d)", + "a and not (b or c or d)", + "b and not (a or c or d)", + "c and not (a or b or d)", + "d and not (a or b or c)", + NULL +}; + +int check(void *closure, const char *name) +{ + int x; + + x = *(int*)closure; + if (name[0] < 'a' || name[0] > 'd' || name[1]) + return 0; + return 1 & (x >> (name[0] - 'a')); +} + +int test(const char *expr) +{ + int x, r, m, c; + struct afb_perm *perm; + + r = 0; + m = 1; + perm = afb_perm_parse(expr); + if (!perm) + printf("error for %s\n", expr); + else { + printf("\nabcd %s\n", expr); + for (x = 0; x < 16 ; x++) { + c = afb_perm_check(perm, check, &x); + printf("%c%c%c%c %d\n", + '0'+!!(x&1), '0'+!!(x&2), '0'+!!(x&4), '0'+!!(x&8), + c); + if (c) + r |= m; + m <<= 1; + } + } + afb_perm_unref(perm); + return r; +} + +void add(char *buffer, const char *fmt, ...) +{ + char b[60]; + va_list vl; + + va_start(vl, fmt); + vsprintf(b, fmt, vl); + va_end(vl); + strcat(buffer, b); +} + +void mke(int value, int bits, char *buffer) +{ + int nval = 1 << bits; + int sval = 1 << (bits - 1); + int mask = (1 << nval) - 1; + int smask = (1 << sval) - 1; + int val = value & mask; + int val0 = val & smask; + int val1 = (val >> sval) & smask; + char c = (char)('a' + bits - 1); + + if (bits == 1) { + switch(val) { + case 0: add(buffer, "x"); break; + case 1: add(buffer, "not %c", c); break; + case 2: add(buffer, "%c", c); break; + case 3: add(buffer, "(%c or not %c) ", c, c); break; + } + } else if (val0 != val1) { + if (val0) { + add(buffer, "not %c", c); + if (val0 != smask) { + add(buffer, " and ("); + mke(val0, bits - 1, buffer); + add(buffer, ")"); + } + } + if (val0 && val1) + add(buffer, " or "); + if (val1) { + add(buffer, "%c", c); + if (val1 != smask) { + add(buffer, " and ("); + mke(val1, bits - 1, buffer); + add(buffer, ")"); + } + } + } else { + mke(val0, bits - 1, buffer); + } +} + +void makeexpr(int value, char *buffer) +{ + if (!value) + strcpy(buffer, "x"); + else { + buffer[0] = 0; + mke(value, 4, buffer); + } +} + +int fulltest() +{ + char buffer[4096]; + int i, j, r; + + r = 0; + for (i = 0 ; i < 65536 ; i++) { + makeexpr(i, buffer); + j = test(buffer); + printf("[[[ %d %s %d ]]]\n", i, i==j?"==":"!=", j); + if (i != j) + r = 1; + } +} + +int main() +{ + int i = 0; + while(exprs[i]) + test(exprs[i++]); + return fulltest(); +} + |