Skip to content

Commit

Permalink
[gsupplicant] Added tests
Browse files Browse the repository at this point in the history
Not much can be unit-tested though
  • Loading branch information
monich committed Mar 13, 2017
1 parent 12ef8ca commit e06425e
Show file tree
Hide file tree
Showing 11 changed files with 876 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,6 @@ documentation.list
installroot
build
RPMS
test/coverage/full.gcov
test/coverage/lib.gcov
test/coverage/results
26 changes: 25 additions & 1 deletion Makefile
@@ -1,8 +1,9 @@
# -*- Mode: makefile-gmake -*-

.PHONY: clean all debug release pkgconfig
.PHONY: clean all debug release pkgconfig test
.PHONY: print_debug_lib print_release_lib
.PHONY: print_debug_link print_release_link
.PHONY: print_debug_path print_release_path

#
# Required packages
Expand Down Expand Up @@ -69,6 +70,19 @@ SPEC_DIR = spec
DEBUG_BUILD_DIR = $(BUILD_DIR)/debug
RELEASE_BUILD_DIR = $(BUILD_DIR)/release

#
# Code coverage
#

ifndef GCOV
GCOV = 0
endif

ifneq ($(GCOV),0)
CFLAGS += --coverage
LDFLAGS += --coverage
endif

#
# Tools and flags
#
Expand Down Expand Up @@ -155,14 +169,24 @@ print_debug_link:
print_release_link:
@echo $(RELEASE_LINK)

print_debug_path:
@echo $(DEBUG_BUILD_DIR)

print_release_path:
@echo $(RELEASE_BUILD_DIR)

clean:
rm -f *~ $(SRC_DIR)/*~ $(INCLUDE_DIR)/*~ rpm/*~
rm -fr $(BUILD_DIR) RPMS installroot
rm -fr debian/tmp debian/lib$(NAME) debian/lib$(NAME)-dev
rm -f documentation.list debian/files debian/*.substvars
rm -f debian/*.debhelper.log debian/*.debhelper debian/*~
make -C test cleaner
make -C tools/wpa-tool clean

test:
make -C test test

$(GEN_DIR):
mkdir -p $@

Expand Down
3 changes: 3 additions & 0 deletions rpm/libgsupplicant.spec
Expand Up @@ -34,6 +34,9 @@ make KEEP_SYMBOLS=1 release pkgconfig
rm -rf %{buildroot}
make install-dev DESTDIR=%{buildroot}

%check
make -C test test

%post -p /sbin/ldconfig

%postun -p /sbin/ldconfig
Expand Down
34 changes: 20 additions & 14 deletions src/gsupplicant_util.c
Expand Up @@ -42,11 +42,13 @@ gsupplicant_name_int_find_bit(
const GSupNameIntPair* list,
gsize count)
{
gsize i;
for (i=0; i<count; i++) {
if (list[i].value & value) {
if (bit) *bit = list[i].value;
return list[i].name;
if (value) {
gsize i;
for (i=0; i<count; i++) {
if (list[i].value & value) {
if (bit) *bit = list[i].value;
return list[i].name;
}
}
}
if (bit) *bit = 0;
Expand Down Expand Up @@ -140,15 +142,18 @@ gsupplicant_name_int_concat(
gsize count)
{
GString* buf = NULL;
gsize i;
for (i=0; i<count && value; i++) {
if (list[i].value & value) {
if (!buf) {
buf = g_string_new(NULL);
} else if (buf->len > 0) {
g_string_append_c(buf, separator);
if (value) {
gsize i;
for (i=0; i<count && value; i++) {
if ((list[i].value & value) && list[i].name[0]) {
if (!buf) {
buf = g_string_new(NULL);
if (!separator) separator = ',';
} else {
g_string_append_c(buf, separator);
}
g_string_append(buf, list[i].name);
}
g_string_append(buf, list[i].name);
}
}
return buf ? g_string_free(buf, FALSE) : NULL;
Expand Down Expand Up @@ -217,7 +222,8 @@ gsupplicant_format_bytes(
g_string_append_printf(buf, "%02x", data[i]);
}
if (append_length) {
g_string_append_printf(buf, " (%u)", (guint)size);
if (size > 0) g_string_append_c(buf, ' ');
g_string_append_printf(buf, "(%u)", (guint)size);
}
str = g_string_free(buf, FALSE);
g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, gsupplicant_idle_cb,
Expand Down
11 changes: 11 additions & 0 deletions test/Makefile
@@ -0,0 +1,11 @@
# -*- Mode: makefile-gmake -*-

cleaner: clean
@rm -f *~
@rm -f common/*~
@rm -f coverage/*.gcov
@rm -fr coverage/results

all:
%:
@$(MAKE) -C test_util $*
175 changes: 175 additions & 0 deletions test/common/Makefile
@@ -0,0 +1,175 @@
# -*- Mode: makefile-gmake -*-

.PHONY: clean all debug release lib-release lib-debug

#
# Real test makefile defines EXE (and possibly SRC) and includes this one.
#

ifndef EXE
${error EXE not defined}
endif

SRC ?= $(EXE).c
COMMON_SRC ?= test_main.c

#
# Required packages
#

PKGS += libglibutil gobject-2.0 glib-2.0 gio-2.0

#
# Default target
#

all: debug release

#
# Directories
#

SRC_DIR = .
LIB_DIR = ../..
COMMON_DIR = ../common
BUILD_DIR = build
DEBUG_BUILD_DIR = $(BUILD_DIR)/debug
RELEASE_BUILD_DIR = $(BUILD_DIR)/release

#
# Code coverage
#

ifndef GCOV
GCOV = 0
endif

ifneq ($(GCOV),0)
CFLAGS += --coverage
LDFLAGS += --coverage
SUBMAKE_OPTS += GCOV=1
endif

#
# Tools and flags
#

CC = $(CROSS_COMPILE)gcc
LD = $(CC)
WARNINGS = -Wall
INCLUDES = -I$(LIB_DIR)/include -I$(LIB_DIR)/src -I$(COMMON_DIR)
BASE_FLAGS = -fPIC
BASE_LDFLAGS = $(BASE_FLAGS) $(LDFLAGS)
BASE_CFLAGS = $(BASE_FLAGS) $(CFLAGS)
FULL_CFLAGS = $(BASE_CFLAGS) $(DEFINES) $(WARNINGS) $(INCLUDES) -MMD -MP \
$(shell pkg-config --cflags $(PKGS))
FULL_LDFLAGS = $(BASE_LDFLAGS)
LIBS = $(shell pkg-config --libs $(PKGS))
QUIET_MAKE = make --no-print-directory
DEBUG_FLAGS = -g
RELEASE_FLAGS =

ifndef KEEP_SYMBOLS
KEEP_SYMBOLS = 0
endif

ifneq ($(KEEP_SYMBOLS),0)
RELEASE_FLAGS += -g
SUBMAKE_OPTS += KEEP_SYMBOLS=1
endif

DEBUG_LDFLAGS = $(FULL_LDFLAGS) $(DEBUG_FLAGS)
RELEASE_LDFLAGS = $(FULL_LDFLAGS) $(RELEASE_FLAGS)
DEBUG_CFLAGS = $(FULL_CFLAGS) $(DEBUG_FLAGS) -DDEBUG
RELEASE_CFLAGS = $(FULL_CFLAGS) $(RELEASE_FLAGS) -O2

#
# Files
#

DEBUG_OBJS = \
$(COMMON_SRC:%.c=$(DEBUG_BUILD_DIR)/common_%.o) \
$(SRC:%.c=$(DEBUG_BUILD_DIR)/%.o)
RELEASE_OBJS = \
$(COMMON_SRC:%.c=$(RELEASE_BUILD_DIR)/common_%.o) \
$(SRC:%.c=$(RELEASE_BUILD_DIR)/%.o)

DEBUG_LIB_FILE := $(shell $(QUIET_MAKE) -C $(LIB_DIR) print_debug_lib)
RELEASE_LIB_FILE := $(shell $(QUIET_MAKE) -C $(LIB_DIR) print_release_lib)
DEBUG_LIB_PATH := $(shell $(QUIET_MAKE) -C $(LIB_DIR) print_debug_path)
DEBUG_LIB := $(LIB_DIR)/$(DEBUG_LIB_FILE)
RELEASE_LIB := $(LIB_DIR)/$(RELEASE_LIB_FILE)

#
# Dependencies
#

DEPS = $(DEBUG_OBJS:%.o=%.d) $(RELEASE_OBJS:%.o=%.d)
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(DEPS)),)
-include $(DEPS)
endif
endif

$(DEBUG_OBJS): | $(DEBUG_BUILD_DIR)
$(RELEASE_OBJS): | $(RELEASE_BUILD_DIR)

#
# Rules
#

DEBUG_EXE = $(DEBUG_BUILD_DIR)/$(EXE)
RELEASE_EXE = $(RELEASE_BUILD_DIR)/$(EXE)

debug: lib-debug $(DEBUG_EXE)

release: lib-release $(RELEASE_EXE)

clean:
rm -f *~
rm -fr $(BUILD_DIR)

cleaner: clean
@make -C $(LIB_DIR) clean

test_banner:
@echo "===========" $(EXE) "=========== "

test: test_banner debug
@LD_LIBRARY_PATH="$(LIB_DIR)/$(DEBUG_LIB_PATH)" $(DEBUG_EXE)

valgrind: test_banner debug
@LD_LIBRARY_PATH="$(LIB_DIR)/$(DEBUG_LIB_PATH)" G_DEBUG=gc-friendly G_SLICE=always-malloc valgrind --tool=memcheck --leak-check=full --show-possibly-lost=no $(DEBUG_EXE)

$(DEBUG_BUILD_DIR):
mkdir -p $@

$(RELEASE_BUILD_DIR):
mkdir -p $@

$(DEBUG_BUILD_DIR)/%.o : $(SRC_DIR)/%.c
$(CC) -c $(DEBUG_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(RELEASE_BUILD_DIR)/%.o : $(SRC_DIR)/%.c
$(CC) -c $(RELEASE_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(DEBUG_BUILD_DIR)/common_%.o : $(COMMON_DIR)/%.c
$(CC) -c $(DEBUG_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(RELEASE_BUILD_DIR)/common_%.o : $(COMMON_DIR)/%.c
$(CC) -c $(RELEASE_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(DEBUG_EXE): $(DEBUG_LIB) $(DEBUG_BUILD_DIR) $(DEBUG_OBJS)
$(LD) $(DEBUG_LDFLAGS) $(DEBUG_OBJS) $< $(LIBS) -o $@

$(RELEASE_EXE): $(RELEASE_LIB) $(RELEASE_BUILD_DIR) $(RELEASE_OBJS)
$(LD) $(RELEASE_LDFLAGS) $(RELEASE_OBJS) $< $(LIBS) -o $@
ifeq ($(KEEP_SYMBOLS),0)
strip $@
endif

lib-debug:
@make $(SUBMAKE_OPTS) -C $(LIB_DIR) debug

lib-release:
@make $(SUBMAKE_OPTS) -C $(LIB_DIR) release
59 changes: 59 additions & 0 deletions test/common/test_common.h
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Jolla Ltd nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TEST_COMMON_H
#define TEST_COMMON_H

#include <gutil_types.h>

#define TEST_FLAG_DEBUG (0x01)

typedef struct test_opt {
int flags;
} TestOpt;

/* Should be invoked after g_test_init */
void
test_init(
TestOpt* opt,
int argc,
char* argv[]);

#endif /* TEST_COMMON_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/

0 comments on commit e06425e

Please sign in to comment.