Compare commits

...

1 Commits

Author SHA1 Message Date
Thomas Klaehn
8f5d0dd7f8 Add unit tests for gpio ftdi driver. 2019-07-16 16:49:12 +02:00
11 changed files with 394 additions and 50 deletions

17
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/libftdi1"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

View File

@ -9,7 +9,7 @@ static int ftdi_open(const struct gpio *gpio)
{
int res;
if((gpio->ftdi_dev->ftdi = ftdi_new()) == 0) {
if((gpio->ftdi_dev->ftdi = ftdi_new()) == NULL) {
syslog(LOG_ERR, "ftdi_new failed\n");
return EXIT_FAILURE;
}
@ -35,7 +35,7 @@ static int ftdi_open(const struct gpio *gpio)
int gpio_open(const struct gpio *gpio)
{
int res;
unsigned int init_value = 0;
unsigned char init_value = 0;
if(NULL == gpio) {
syslog(LOG_ERR, "No valid gpio object given.\n");
@ -50,7 +50,6 @@ int gpio_open(const struct gpio *gpio)
}
gpio->ftdi_dev->bit_mask |= (unsigned char)gpio->pin;
syslog(LOG_DEBUG, "bitmask: 0x%02x\n", gpio->ftdi_dev->bit_mask);
res = ftdi_set_bitmode(gpio->ftdi_dev->ftdi, gpio->ftdi_dev->bit_mask, BITMODE_BITBANG);
if(res < 0) {
syslog(LOG_ERR, "unable to set bit bang mode: %d (%s)\n", res,
@ -60,7 +59,7 @@ int gpio_open(const struct gpio *gpio)
return EXIT_FAILURE;
}
res = gpio_write(gpio, init_value);
res = ftdi_write_data(gpio->ftdi_dev->ftdi, &init_value, 1);
if(res < 0) {
syslog(LOG_ERR, "unable to write to gpio %u (%s)\n", gpio->pin,
ftdi_get_error_string(gpio->ftdi_dev->ftdi));
@ -85,6 +84,7 @@ int gpio_close(const struct gpio *gpio)
if(gpio->ftdi_dev->bit_mask == 0) {
ftdi_usb_close(gpio->ftdi_dev->ftdi);
ftdi_free(gpio->ftdi_dev->ftdi);
gpio->ftdi_dev->is_open = false;
}
return EXIT_SUCCESS;

18
test/inc/mock_ftdi.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __MOCK_FTDI_H__
#define __MOCK_FTDI_H__
#include <fff.h>
DEFINE_FFF_GLOBALS;
DECLARE_FAKE_VOID_FUNC(ftdi_free, struct ftdi_context *);
DECLARE_FAKE_VALUE_FUNC(int, ftdi_usb_open, struct ftdi_context *, int, int);
DECLARE_FAKE_VALUE_FUNC(int, ftdi_usb_close, struct ftdi_context *);
DECLARE_FAKE_VALUE_FUNC(struct ftdi_context *, ftdi_new);
DECLARE_FAKE_VALUE_FUNC(const char *, ftdi_get_error_string, struct ftdi_context *);
DECLARE_FAKE_VALUE_FUNC(int, ftdi_set_bitmode, struct ftdi_context *, unsigned char, unsigned char);
DECLARE_FAKE_VALUE_FUNC(int, ftdi_read_data, struct ftdi_context *, unsigned char *, int);
DECLARE_FAKE_VALUE_FUNC(int, ftdi_write_data, struct ftdi_context *, const unsigned char *, int);
#endif

27
test/unit/gpio_close.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/syscall.h>
#include <utest.h>
#include <mock_ftdi.h>
#include <ftdi_dev.h>
#include <gpio.h>
UTEST(gpio_close, invalid_gpio_obj) {
ASSERT_EQ(gpio_close(NULL), EXIT_FAILURE);
}
UTEST(gpio_close, empty_bitmask) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.is_open = true;
ftdi_obj.bit_mask = 0x08;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ASSERT_EQ(gpio_close(&gpio_1), EXIT_SUCCESS);
}

159
test/unit/gpio_open.c Normal file
View File

@ -0,0 +1,159 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/syscall.h>
#include <utest.h>
#include <mock_ftdi.h>
#include <ftdi_dev.h>
#include <gpio.h>
UTEST(gpio_open, invalid_ftdi_new_result) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = NULL;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_FAILURE);
}
UTEST(gpio_open, ftdi_usb_open_failed) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = &ftdi_ctx;
ftdi_usb_open_fake.return_val = -1;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_FAILURE);
}
UTEST(gpio_open, ftdi_read_data_failed) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = &ftdi_ctx;
ftdi_usb_open_fake.return_val = 0;
ftdi_read_data_fake.return_val = -1;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_FAILURE);
}
UTEST(gpio_open, ftdi_open_success) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = &ftdi_ctx;
ftdi_usb_open_fake.return_val = 0;
ftdi_read_data_fake.return_val = 0;
ftdi_set_bitmode_fake.return_val = 0;
ftdi_write_data_fake.return_val = 0;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_SUCCESS);
}
UTEST(gpio_open, invalid_gpio_obj) {
ASSERT_EQ(gpio_open(NULL), EXIT_FAILURE);
}
UTEST(gpio_open, ftdi_set_bitmode_failed) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = &ftdi_ctx;
ftdi_usb_open_fake.return_val = 0;
ftdi_read_data_fake.return_val = 0;
ftdi_set_bitmode_fake.return_val = -1;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_FAILURE);
}
UTEST(gpio_open, ftdi_write_data_failed) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = &ftdi_ctx;
ftdi_usb_open_fake.return_val = 0;
ftdi_read_data_fake.return_val = 0;
ftdi_set_bitmode_fake.return_val = 0;
ftdi_write_data_fake.return_val = -1;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_FAILURE);
}
UTEST(gpio_open, success) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_new_fake.return_val = &ftdi_ctx;
ftdi_usb_open_fake.return_val = 0;
ftdi_read_data_fake.return_val = 0;
ftdi_set_bitmode_fake.return_val = 0;
ftdi_write_data_fake.return_val = 0;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_SUCCESS);
}

45
test/unit/gpio_read.c Normal file
View File

@ -0,0 +1,45 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/syscall.h>
#include <utest.h>
#include <mock_ftdi.h>
#include <ftdi_dev.h>
#include <gpio.h>
#include <stdio.h>
UTEST(gpio_read, invalid_params) {
ASSERT_EQ(gpio_read(NULL, NULL), EXIT_FAILURE);
}
UTEST(gpio_read, value_0) {
unsigned int result;
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = ~0x08;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ASSERT_EQ(gpio_read(&gpio_1, &result), EXIT_SUCCESS);
result = ((ftdi_obj.status_mask) & (gpio_1.pin)) >> 3;
ASSERT_EQ(result, 0)
}
UTEST(gpio_read, value_1) {
unsigned int result;
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = 0x08;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ASSERT_EQ(gpio_read(&gpio_1, &result), EXIT_SUCCESS);
result = ((ftdi_obj.status_mask) & (gpio_1.pin)) >> 3;
ASSERT_EQ(result, 1)
}

43
test/unit/gpio_toggle.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/syscall.h>
#include <utest.h>
#include <mock_ftdi.h>
#include <ftdi_dev.h>
#include <gpio.h>
#include <stdio.h>
UTEST(gpio_toggle, invalid_params) {
ASSERT_EQ(gpio_toggle(NULL), EXIT_FAILURE);
}
UTEST(gpio_toggle, ftdi_write_data_fails) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = 0;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_write_data_fake.return_val = -1;
ASSERT_EQ(gpio_toggle(&gpio_1), EXIT_FAILURE);
}
UTEST(gpio_toggle, success) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = 0;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_write_data_fake.return_val = 0;
ASSERT_EQ(gpio_toggle(&gpio_1), EXIT_SUCCESS);
}

60
test/unit/gpio_write.c Normal file
View File

@ -0,0 +1,60 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/syscall.h>
#include <utest.h>
#include <mock_ftdi.h>
#include <ftdi_dev.h>
#include <gpio.h>
UTEST(gpio_write, invalid_params) {
ASSERT_EQ(gpio_write(NULL, 0), EXIT_FAILURE);
}
UTEST(gpio_write, value_0) {
unsigned int result;
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = 0xff;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_write_data_fake.return_val = 0;
ASSERT_EQ(gpio_write(&gpio_1, 0), EXIT_SUCCESS);
result = ((ftdi_obj.status_mask) & (gpio_1.pin)) >> 3;
ASSERT_EQ(result, 0)
}
UTEST(gpio_write, value_1) {
unsigned int result;
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = 0;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_write_data_fake.return_val = 0;
ASSERT_EQ(gpio_write(&gpio_1, 1), EXIT_SUCCESS);
result = ((ftdi_obj.status_mask) | (gpio_1.pin)) >> 3;
ASSERT_EQ(result, 1)
}
UTEST(gpio_write, ftdi_write_data_failed) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
ftdi_obj.status_mask = 0;
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ftdi_write_data_fake.return_val = -1;
ASSERT_EQ(gpio_write(&gpio_1, 0), EXIT_FAILURE);
}

8
test/unit/main.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
#include <time.h>
#include <sys/syscall.h>
#include <utest.h>
UTEST_MAIN();

13
test/unit/mock_ftdi.c Normal file
View File

@ -0,0 +1,13 @@
#include <mock_ftdi.h>
DEFINE_FFF_GLOBALS;
DEFINE_FAKE_VOID_FUNC(ftdi_free, struct ftdi_context *);
DEFINE_FAKE_VALUE_FUNC(int, ftdi_usb_open, struct ftdi_context *, int, int);
DEFINE_FAKE_VALUE_FUNC(int, ftdi_usb_close, struct ftdi_context *);
DEFINE_FAKE_VALUE_FUNC(struct ftdi_context *, ftdi_new);
DEFINE_FAKE_VALUE_FUNC(const char *, ftdi_get_error_string, struct ftdi_context *);
DEFINE_FAKE_VALUE_FUNC(int, ftdi_set_bitmode, struct ftdi_context *, unsigned char, unsigned char);
DEFINE_FAKE_VALUE_FUNC(int, ftdi_read_data, struct ftdi_context *, unsigned char *, int);
DEFINE_FAKE_VALUE_FUNC(int, ftdi_write_data, struct ftdi_context *, const unsigned char *, int);

View File

@ -1,46 +0,0 @@
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/syscall.h>
#include <fff.h>
#include <utest.h>
#include <ftdi_dev.h>
#include <gpio.h>
DEFINE_FFF_GLOBALS;
FAKE_VOID_FUNC(ftdi_free, struct ftdi_context *);
FAKE_VALUE_FUNC(int, ftdi_usb_open, struct ftdi_context *, int, int);
FAKE_VALUE_FUNC(int, ftdi_usb_close, struct ftdi_context *);
FAKE_VALUE_FUNC(struct ftdi_context *, ftdi_new);
FAKE_VALUE_FUNC(const char *, ftdi_get_error_string, struct ftdi_context *);
FAKE_VALUE_FUNC(int, ftdi_set_bitmode, struct ftdi_context *, unsigned char, unsigned char);
FAKE_VALUE_FUNC(int, ftdi_read_data, struct ftdi_context *, unsigned char *, int);
FAKE_VALUE_FUNC(int, ftdi_write_data, struct ftdi_context *, const unsigned char *, int);
UTEST_MAIN();
UTEST(gpio, gpio_open) {
struct gpio gpio_1;
struct ftdi_dev ftdi_obj;
struct ftdi_context ftdi_ctx;
ftdi_obj.ftdi = NULL,
ftdi_obj.is_open = false,
ftdi_obj.vendor_id = 0x0403,
ftdi_obj.product_id = 0x6001,
ftdi_obj.bit_mask = 0,
ftdi_obj.status_mask = 0,
gpio_1.pin = 0x08;
gpio_1.ftdi_dev = &ftdi_obj;
ASSERT_EQ(gpio_open(NULL), EXIT_FAILURE);
ftdi_new_fake.return_val = &ftdi_ctx;
ASSERT_EQ(gpio_open(&gpio_1), EXIT_SUCCESS);
}