Skip to content

Commit

Permalink
tests: kv: add permission tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Aug 25, 2023
1 parent 0c65e1e commit ca2f59e
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/tests/kv_permissions/kv_no_modify/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

ELF2TAB_ARGS += --write_id 9092 --read_ids 9092

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
20 changes: 20 additions & 0 deletions examples/tests/kv_permissions/kv_no_modify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
KV Permission Unit Test - No Modify Permissions
================

This test app has no modify permissions.

Needs to have the `examples/services/unit_test_supervisor` app installed as
well.

Output
------

Should get expected output like:

```
4.000: exists [✓]
4.001: set_get [✓]
4.002: set_set [✓]
4.003: set_delete [✓]
Summary 4: [4/4] Passed, [0/4] Failed, [0/4] Incomplete
```
89 changes: 89 additions & 0 deletions examples/tests/kv_permissions/kv_no_modify/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <kv_system.h>
#include <unit_test.h>

#define KEY_LEN 200
#define DATA_LEN 3000

uint8_t key_buf[KEY_LEN];
uint8_t data_buf[DATA_LEN];
uint8_t value_buf[DATA_LEN];


static bool test_exists(void) {
int ret = kv_system_check_status();
CHECK(ret == RETURNCODE_SUCCESS);
return true;
}

static bool test_set_get(void) {
int ret;
char key[] = "kvnowritetestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 45;
for (uint32_t i = 0; i < value_len; i++) {
value_buf[i] = (uint8_t) i;
}

// If key exists this should fail.
ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS || ret == RETURNCODE_ENOSUPPORT);

ret = kv_system_get_sync(key_buf, strlen(key), data_buf, DATA_LEN, &value_len);
CHECK(ret == RETURNCODE_SUCCESS);

return true;
}

static bool test_set_set(void) {
int ret;
char key[] = "kvnowritetestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 3;
for (uint32_t i = 0; i < value_len; i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS || ret == RETURNCODE_ENOSUPPORT);

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

static bool test_set_delete(void) {
int ret;
char key[] = "kvnowritetestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 3;
for (uint32_t i = 0; i < value_len; i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS || ret == RETURNCODE_ENOSUPPORT);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

int main(void) {
unit_test_fun tests[] = {
TEST(exists),
TEST(set_get),
TEST(set_set),
TEST(set_delete),
};
unit_test_runner(tests, sizeof(tests) / sizeof(unit_test_fun), 2000, "org.tockos.unit_test");
return 0;
}
13 changes: 13 additions & 0 deletions examples/tests/kv_permissions/kv_no_read/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

ELF2TAB_ARGS += --write_id 9091 --access_ids 9091

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
18 changes: 18 additions & 0 deletions examples/tests/kv_permissions/kv_no_read/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
KV Permission Unit Test - No Read Permissions
================

This test app has no read permissions.

Needs to have the `examples/services/unit_test_supervisor` app installed as
well.

Output
------

Should get expected output like:

```
3.000: exists [✓]
3.001: set_get_no_permissions [✓]
Summary 3: [2/2] Passed, [0/2] Failed, [0/2] Incomplete
```
48 changes: 48 additions & 0 deletions examples/tests/kv_permissions/kv_no_read/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <kv_system.h>
#include <unit_test.h>

#define KEY_LEN 200
#define DATA_LEN 3000

uint8_t key_buf[KEY_LEN];
uint8_t data_buf[DATA_LEN];
uint8_t value_buf[DATA_LEN];


static bool test_exists(void) {
int ret = kv_system_check_status();
CHECK(ret == RETURNCODE_SUCCESS);
return true;
}

static bool test_set_get_no_permissions(void) {
int ret;
char key[] = "kvnoreadtestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 45;
for (uint32_t i = 0; i < value_len; i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_get_sync(key_buf, strlen(key), data_buf, DATA_LEN, &value_len);
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

int main(void) {
unit_test_fun tests[] = {
TEST(exists),
TEST(set_get_no_permissions),
};
unit_test_runner(tests, sizeof(tests) / sizeof(unit_test_fun), 2000, "org.tockos.unit_test");
return 0;
}
13 changes: 13 additions & 0 deletions examples/tests/kv_permissions/kv_no_write/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

ELF2TAB_ARGS += --read_ids 9090 --access_ids 9090

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
18 changes: 18 additions & 0 deletions examples/tests/kv_permissions/kv_no_write/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
KV Permission Unit Test - No Write Permission
================

This app has no write ID and cannot create KV objects.

Needs to have the `examples/services/unit_test_supervisor` app installed as
well.

Output
------

Should get expected output like:

```
2.000: exists [✓]
2.001: set_no_permissions [✓]
Summary 2: [2/2] Passed, [0/2] Failed, [0/2] Incomplete
```
45 changes: 45 additions & 0 deletions examples/tests/kv_permissions/kv_no_write/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <kv_system.h>
#include <unit_test.h>

#define KEY_LEN 200
#define DATA_LEN 3000

uint8_t key_buf[KEY_LEN];
uint8_t data_buf[DATA_LEN];
uint8_t value_buf[DATA_LEN];


static bool test_exists(void) {
int ret = kv_system_check_status();
CHECK(ret == RETURNCODE_SUCCESS);
return true;
}

static bool test_set_no_permissions(void) {
int ret;
char key[] = "kvnowritetestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 45;
for (uint32_t i = 0; i < value_len; i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_EINVAL);

return true;
}

int main(void) {
unit_test_fun tests[] = {
TEST(exists),
TEST(set_no_permissions),
};
unit_test_runner(tests, sizeof(tests) / sizeof(unit_test_fun), 2000, "org.tockos.unit_test");
return 0;
}

0 comments on commit ca2f59e

Please sign in to comment.