2012-06-16 11:02:48 +02:00
|
|
|
/* file minunit_example.c */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <limits.h>
|
2012-11-18 22:16:49 +01:00
|
|
|
#include <signal.h>
|
2014-06-23 10:24:03 +02:00
|
|
|
#include <sys/utsname.h>
|
2012-08-23 22:32:49 +02:00
|
|
|
#include "global.h"
|
2012-06-16 11:02:48 +02:00
|
|
|
#include "mbpfan.h"
|
2012-08-22 15:05:52 +02:00
|
|
|
#include "settings.h"
|
2012-06-16 11:02:48 +02:00
|
|
|
#include "minunit.h"
|
|
|
|
|
|
|
|
int tests_run = 0;
|
|
|
|
|
2014-06-23 10:24:03 +02:00
|
|
|
static const char *test_is_legacy_kernel()
|
|
|
|
{
|
|
|
|
|
|
|
|
struct utsname kernel;
|
|
|
|
uname(&kernel);
|
|
|
|
|
|
|
|
mu_assert("Kernel major version is not 3.", kernel.release[0] == '3');
|
|
|
|
|
|
|
|
char *pch;
|
|
|
|
pch = strtok(kernel.release, ".");
|
|
|
|
pch = strtok(NULL, ".");
|
|
|
|
|
|
|
|
int minor = atoi(pch);
|
|
|
|
|
|
|
|
if (minor < 15)
|
|
|
|
mu_assert("Legacy kernel not detected by mbpfan.", is_legacy_kernel() == 1);
|
|
|
|
else
|
|
|
|
mu_assert("Non-legacy kernel not detected by mbpfan", is_legacy_kernel() == 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-16 11:02:48 +02:00
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
static const char *test_sensor_paths()
|
2012-06-16 11:02:48 +02:00
|
|
|
{
|
2012-08-22 15:05:52 +02:00
|
|
|
t_sensors* sensors = retrieve_sensors();
|
|
|
|
mu_assert("No sensors found", sensors != NULL);
|
|
|
|
t_sensors* tmp = sensors;
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
while(tmp != NULL) {
|
|
|
|
mu_assert("Sensor does not have a valid path", tmp->path != NULL);
|
2012-08-23 22:32:49 +02:00
|
|
|
|
|
|
|
if(tmp->path != NULL) {
|
2012-08-22 15:05:52 +02:00
|
|
|
mu_assert("Sensor does not have valid temperature", tmp->temperature > 0);
|
2012-08-23 22:32:49 +02:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
tmp = tmp->next;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
return 0;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
static const char *test_fan_paths()
|
2012-06-16 11:02:48 +02:00
|
|
|
{
|
2012-10-24 11:13:25 +02:00
|
|
|
t_fans* fans = retrieve_fans();
|
|
|
|
mu_assert("No fans found", fans != NULL);
|
|
|
|
t_fans* tmp = fans;
|
2012-08-22 15:05:52 +02:00
|
|
|
int found_fan_path = 0;
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
while(tmp != NULL) {
|
2012-08-23 22:32:49 +02:00
|
|
|
if(tmp->fan_output_path != NULL) {
|
2012-08-22 15:05:52 +02:00
|
|
|
found_fan_path++;
|
2012-08-23 22:32:49 +02:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
tmp = tmp->next;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
mu_assert("No fans found", found_fan_path != 0);
|
|
|
|
return 0;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned time_seed()
|
|
|
|
{
|
2012-08-22 15:05:52 +02:00
|
|
|
time_t now = time ( 0 );
|
|
|
|
unsigned char *p = (unsigned char *)&now;
|
|
|
|
unsigned seed = 0;
|
|
|
|
size_t i;
|
2012-08-23 22:32:49 +02:00
|
|
|
|
|
|
|
for ( i = 0; i < sizeof now; i++ ) {
|
2012-08-22 15:05:52 +02:00
|
|
|
seed = seed * ( UCHAR_MAX + 2U ) + p[i];
|
2012-08-23 22:32:49 +02:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
return seed;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// nothing better than a horrible piece of code to
|
|
|
|
// stress a little bit the CPU
|
|
|
|
int stress(int n)
|
|
|
|
{
|
2012-08-22 15:05:52 +02:00
|
|
|
int f = n;
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
while (f > 0) {
|
|
|
|
while(n > 0) {
|
|
|
|
srand ( time_seed() );
|
|
|
|
n--;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
f--;
|
|
|
|
n = f;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
static const char *test_get_temp()
|
2012-06-16 11:02:48 +02:00
|
|
|
{
|
2012-08-22 15:05:52 +02:00
|
|
|
t_sensors* sensors = retrieve_sensors();
|
|
|
|
mu_assert("No sensors found", sensors != NULL);
|
|
|
|
unsigned short temp_1 = get_temp(sensors);
|
|
|
|
mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150);
|
|
|
|
stress(2000);
|
|
|
|
unsigned short temp_2 = get_temp(sensors);
|
|
|
|
mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
static const char *test_config_file()
|
2012-08-22 15:05:52 +02:00
|
|
|
{
|
|
|
|
FILE *f = NULL;
|
|
|
|
Settings *settings = NULL;
|
|
|
|
f = fopen("/etc/mbpfan.conf", "r");
|
|
|
|
mu_assert("No config file found", f != NULL);
|
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
if (f == NULL) {
|
2012-08-22 15:05:52 +02:00
|
|
|
return 0;
|
2012-08-23 22:32:49 +02:00
|
|
|
}
|
2012-08-22 15:05:52 +02:00
|
|
|
|
|
|
|
settings = settings_open(f);
|
|
|
|
fclose(f);
|
|
|
|
mu_assert("Could not read settings from config file", settings != NULL);
|
2012-08-23 22:32:49 +02:00
|
|
|
|
|
|
|
if (settings == NULL) {
|
2012-08-22 15:05:52 +02:00
|
|
|
return 0;
|
2012-08-23 22:32:49 +02:00
|
|
|
}
|
2012-08-22 15:05:52 +02:00
|
|
|
|
|
|
|
mu_assert("Could not read min_fan_speed from config file",settings_get_int(settings, "general", "min_fan_speed") != 0);
|
|
|
|
mu_assert("Could not read max_fan_speed from config file",settings_get_int(settings, "general", "max_fan_speed") != 0);
|
|
|
|
mu_assert("Could not read low_temp from config file",settings_get_int(settings, "general", "low_temp") != 0);
|
|
|
|
mu_assert("Could not read high_temp from config file",settings_get_int(settings, "general", "high_temp") != 0);
|
|
|
|
mu_assert("Could not read max_temp from config file",settings_get_int(settings, "general", "max_temp") != 0);
|
|
|
|
mu_assert("Could not read polling_interval from config file",settings_get_int(settings, "general", "polling_interval") != 0);
|
|
|
|
/* Destroy the settings object */
|
|
|
|
settings_delete(settings);
|
|
|
|
|
|
|
|
return 0;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
2012-11-18 22:16:49 +01:00
|
|
|
static const char *test_settings()
|
|
|
|
{
|
|
|
|
retrieve_settings("./mbpfan.conf.test1");
|
|
|
|
mu_assert("min_fan_speed value is not 6200", min_fan_speed == 6200);
|
|
|
|
mu_assert("polling_interval is not 1", polling_interval == 1);
|
|
|
|
retrieve_settings("./mbpfan.conf");
|
|
|
|
mu_assert("min_fan_speed value is not 2000", min_fan_speed == 2000);
|
|
|
|
mu_assert("polling_interval is not 7", polling_interval == 7);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int received = 0;
|
|
|
|
|
|
|
|
static void handler(int signal)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch(signal) {
|
|
|
|
case SIGHUP:
|
|
|
|
received = 1;
|
|
|
|
retrieve_settings("./mbpfan.conf.test1");
|
|
|
|
break;
|
2014-06-22 16:48:44 +02:00
|
|
|
|
2012-11-18 22:16:49 +01:00
|
|
|
default:
|
|
|
|
received = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *test_sighup_receive()
|
|
|
|
{
|
|
|
|
signal(SIGHUP, handler);
|
|
|
|
raise(SIGHUP);
|
|
|
|
mu_assert("did not receive SIGHUP signal", received == 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *test_settings_reload()
|
|
|
|
{
|
|
|
|
signal(SIGHUP, handler);
|
|
|
|
retrieve_settings("./mbpfan.conf");
|
|
|
|
mu_assert("min_fan_speed value is not 2000 before SIGHUP", min_fan_speed == 2000);
|
|
|
|
mu_assert("polling_interval is not 7 before SIHUP", polling_interval == 7);
|
|
|
|
raise(SIGHUP);
|
|
|
|
mu_assert("min_fan_speed value is not 6200 after SIGHUP", min_fan_speed == 6200);
|
|
|
|
mu_assert("polling_interval is not 1 after SIHUP", polling_interval == 1);
|
|
|
|
retrieve_settings("./mbpfan.conf");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
static const char *all_tests()
|
2012-06-16 11:02:48 +02:00
|
|
|
{
|
2014-06-23 10:24:03 +02:00
|
|
|
mu_run_test(test_is_legacy_kernel);
|
2012-08-22 15:05:52 +02:00
|
|
|
mu_run_test(test_sensor_paths);
|
|
|
|
mu_run_test(test_fan_paths);
|
|
|
|
mu_run_test(test_get_temp);
|
|
|
|
mu_run_test(test_config_file);
|
2012-11-18 22:16:49 +01:00
|
|
|
mu_run_test(test_settings);
|
|
|
|
mu_run_test(test_sighup_receive);
|
|
|
|
mu_run_test(test_settings_reload);
|
2012-08-22 15:05:52 +02:00
|
|
|
return 0;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int tests()
|
|
|
|
{
|
2012-08-22 15:05:52 +02:00
|
|
|
printf("Starting the tests..\n");
|
|
|
|
printf("It is normal for them to take a bit to finish.\n");
|
2012-08-23 22:32:49 +02:00
|
|
|
const char *result = all_tests();
|
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
if (result != 0) {
|
|
|
|
printf("%s \n", result);
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
} else {
|
|
|
|
printf("ALL TESTS PASSED\n");
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
printf("Tests run: %d\n", tests_run);
|
2012-06-16 11:02:48 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
return result != 0;
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|