From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- comm/third_party/libotr/tests/unit/Makefile.am | 50 ++ comm/third_party/libotr/tests/unit/Makefile.in | 697 +++++++++++++++++++++ comm/third_party/libotr/tests/unit/instag.txt | 4 + comm/third_party/libotr/tests/unit/test_auth.c | 181 ++++++ comm/third_party/libotr/tests/unit/test_b64.c | 114 ++++ comm/third_party/libotr/tests/unit/test_context.c | 214 +++++++ comm/third_party/libotr/tests/unit/test_dh.c | 515 +++++++++++++++ comm/third_party/libotr/tests/unit/test_instag.c | 222 +++++++ comm/third_party/libotr/tests/unit/test_mem.c | 58 ++ comm/third_party/libotr/tests/unit/test_privkey.c | 264 ++++++++ comm/third_party/libotr/tests/unit/test_proto.c | 371 +++++++++++ comm/third_party/libotr/tests/unit/test_sm.c | 343 ++++++++++ comm/third_party/libotr/tests/unit/test_tlv.c | 163 +++++ .../third_party/libotr/tests/unit/test_userstate.c | 52 ++ 14 files changed, 3248 insertions(+) create mode 100644 comm/third_party/libotr/tests/unit/Makefile.am create mode 100644 comm/third_party/libotr/tests/unit/Makefile.in create mode 100644 comm/third_party/libotr/tests/unit/instag.txt create mode 100644 comm/third_party/libotr/tests/unit/test_auth.c create mode 100644 comm/third_party/libotr/tests/unit/test_b64.c create mode 100644 comm/third_party/libotr/tests/unit/test_context.c create mode 100644 comm/third_party/libotr/tests/unit/test_dh.c create mode 100644 comm/third_party/libotr/tests/unit/test_instag.c create mode 100644 comm/third_party/libotr/tests/unit/test_mem.c create mode 100644 comm/third_party/libotr/tests/unit/test_privkey.c create mode 100644 comm/third_party/libotr/tests/unit/test_proto.c create mode 100644 comm/third_party/libotr/tests/unit/test_sm.c create mode 100644 comm/third_party/libotr/tests/unit/test_tlv.c create mode 100644 comm/third_party/libotr/tests/unit/test_userstate.c (limited to 'comm/third_party/libotr/tests/unit') diff --git a/comm/third_party/libotr/tests/unit/Makefile.am b/comm/third_party/libotr/tests/unit/Makefile.am new file mode 100644 index 0000000000..d48a6c7e4f --- /dev/null +++ b/comm/third_party/libotr/tests/unit/Makefile.am @@ -0,0 +1,50 @@ +AM_CFLAGS = -I$(top_srcdir)/include \ + -I$(top_srcdir)/src \ + -I$(top_srcdir)/tests/utils/ \ + -I$(srcdir) \ + @LIBGCRYPT_CFLAGS@ + +LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la + +LIBOTR=$(top_builddir)/src/libotr.la + +noinst_PROGRAMS = test_auth test_proto test_dh \ + test_b64 test_context \ + test_userstate test_tlv \ + test_mem test_sm test_instag \ + test_privkey + +test_auth_SOURCES = test_auth.c +test_auth_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_proto_SOURCES = test_proto.c +test_proto_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_dh_SOURCES = test_dh.c +test_dh_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_b64_SOURCES = test_b64.c +test_b64_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_context_SOURCES = test_context.c +test_context_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_userstate_SOURCES = test_userstate.c +test_userstate_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_tlv_SOURCES = test_tlv.c +test_tlv_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_mem_SOURCES = test_mem.c +test_mem_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_sm_SOURCES = test_sm.c +test_sm_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_instag_SOURCES = test_instag.c +test_instag_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +test_privkey_SOURCES = test_privkey.c +test_privkey_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ + +EXTRA_DIST = instag.txt diff --git a/comm/third_party/libotr/tests/unit/Makefile.in b/comm/third_party/libotr/tests/unit/Makefile.in new file mode 100644 index 0000000000..33f11b540e --- /dev/null +++ b/comm/third_party/libotr/tests/unit/Makefile.in @@ -0,0 +1,697 @@ +# Makefile.in generated by automake 1.14.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2013 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +VPATH = @srcdir@ +am__is_gnu_make = test -n '$(MAKEFILE_LIST)' && test -n '$(MAKELEVEL)' +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +noinst_PROGRAMS = test_auth$(EXEEXT) test_proto$(EXEEXT) \ + test_dh$(EXEEXT) test_b64$(EXEEXT) test_context$(EXEEXT) \ + test_userstate$(EXEEXT) test_tlv$(EXEEXT) test_mem$(EXEEXT) \ + test_sm$(EXEEXT) test_instag$(EXEEXT) test_privkey$(EXEEXT) +subdir = tests/unit +DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \ + $(top_srcdir)/config/depcomp +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/config/libtool.m4 \ + $(top_srcdir)/config/ltoptions.m4 \ + $(top_srcdir)/config/ltsugar.m4 \ + $(top_srcdir)/config/ltversion.m4 \ + $(top_srcdir)/config/lt~obsolete.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +PROGRAMS = $(noinst_PROGRAMS) +am_test_auth_OBJECTS = test_auth.$(OBJEXT) +test_auth_OBJECTS = $(am_test_auth_OBJECTS) +test_auth_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +am_test_b64_OBJECTS = test_b64.$(OBJEXT) +test_b64_OBJECTS = $(am_test_b64_OBJECTS) +test_b64_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_context_OBJECTS = test_context.$(OBJEXT) +test_context_OBJECTS = $(am_test_context_OBJECTS) +test_context_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_dh_OBJECTS = test_dh.$(OBJEXT) +test_dh_OBJECTS = $(am_test_dh_OBJECTS) +test_dh_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_instag_OBJECTS = test_instag.$(OBJEXT) +test_instag_OBJECTS = $(am_test_instag_OBJECTS) +test_instag_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_mem_OBJECTS = test_mem.$(OBJEXT) +test_mem_OBJECTS = $(am_test_mem_OBJECTS) +test_mem_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_privkey_OBJECTS = test_privkey.$(OBJEXT) +test_privkey_OBJECTS = $(am_test_privkey_OBJECTS) +test_privkey_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_proto_OBJECTS = test_proto.$(OBJEXT) +test_proto_OBJECTS = $(am_test_proto_OBJECTS) +test_proto_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_sm_OBJECTS = test_sm.$(OBJEXT) +test_sm_OBJECTS = $(am_test_sm_OBJECTS) +test_sm_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_tlv_OBJECTS = test_tlv.$(OBJEXT) +test_tlv_OBJECTS = $(am_test_tlv_OBJECTS) +test_tlv_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +am_test_userstate_OBJECTS = test_userstate.$(OBJEXT) +test_userstate_OBJECTS = $(am_test_userstate_OBJECTS) +test_userstate_DEPENDENCIES = $(LIBTAP) $(LIBOTR) +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/config/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(test_auth_SOURCES) $(test_b64_SOURCES) \ + $(test_context_SOURCES) $(test_dh_SOURCES) \ + $(test_instag_SOURCES) $(test_mem_SOURCES) \ + $(test_privkey_SOURCES) $(test_proto_SOURCES) \ + $(test_sm_SOURCES) $(test_tlv_SOURCES) \ + $(test_userstate_SOURCES) +DIST_SOURCES = $(test_auth_SOURCES) $(test_b64_SOURCES) \ + $(test_context_SOURCES) $(test_dh_SOURCES) \ + $(test_instag_SOURCES) $(test_mem_SOURCES) \ + $(test_privkey_SOURCES) $(test_proto_SOURCES) \ + $(test_sm_SOURCES) $(test_tlv_SOURCES) \ + $(test_userstate_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBGCRYPT_CFLAGS = @LIBGCRYPT_CFLAGS@ +LIBGCRYPT_CONFIG = @LIBGCRYPT_CONFIG@ +LIBGCRYPT_LIBS = @LIBGCRYPT_LIBS@ +LIBOBJS = @LIBOBJS@ +LIBOTR_LIBTOOL_VERSION = @LIBOTR_LIBTOOL_VERSION@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +AM_CFLAGS = -I$(top_srcdir)/include \ + -I$(top_srcdir)/src \ + -I$(top_srcdir)/tests/utils/ \ + -I$(srcdir) \ + @LIBGCRYPT_CFLAGS@ + +LIBTAP = $(top_builddir)/tests/utils/tap/libtap.la +LIBOTR = $(top_builddir)/src/libotr.la +test_auth_SOURCES = test_auth.c +test_auth_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_proto_SOURCES = test_proto.c +test_proto_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_dh_SOURCES = test_dh.c +test_dh_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_b64_SOURCES = test_b64.c +test_b64_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_context_SOURCES = test_context.c +test_context_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_userstate_SOURCES = test_userstate.c +test_userstate_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_tlv_SOURCES = test_tlv.c +test_tlv_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_mem_SOURCES = test_mem.c +test_mem_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_sm_SOURCES = test_sm.c +test_sm_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_instag_SOURCES = test_instag.c +test_instag_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +test_privkey_SOURCES = test_privkey.c +test_privkey_LDADD = $(LIBTAP) $(LIBOTR) @LIBGCRYPT_LIBS@ +EXTRA_DIST = instag.txt +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu tests/unit/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --gnu tests/unit/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-noinstPROGRAMS: + @list='$(noinst_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +test_auth$(EXEEXT): $(test_auth_OBJECTS) $(test_auth_DEPENDENCIES) $(EXTRA_test_auth_DEPENDENCIES) + @rm -f test_auth$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_auth_OBJECTS) $(test_auth_LDADD) $(LIBS) + +test_b64$(EXEEXT): $(test_b64_OBJECTS) $(test_b64_DEPENDENCIES) $(EXTRA_test_b64_DEPENDENCIES) + @rm -f test_b64$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_b64_OBJECTS) $(test_b64_LDADD) $(LIBS) + +test_context$(EXEEXT): $(test_context_OBJECTS) $(test_context_DEPENDENCIES) $(EXTRA_test_context_DEPENDENCIES) + @rm -f test_context$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_context_OBJECTS) $(test_context_LDADD) $(LIBS) + +test_dh$(EXEEXT): $(test_dh_OBJECTS) $(test_dh_DEPENDENCIES) $(EXTRA_test_dh_DEPENDENCIES) + @rm -f test_dh$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_dh_OBJECTS) $(test_dh_LDADD) $(LIBS) + +test_instag$(EXEEXT): $(test_instag_OBJECTS) $(test_instag_DEPENDENCIES) $(EXTRA_test_instag_DEPENDENCIES) + @rm -f test_instag$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_instag_OBJECTS) $(test_instag_LDADD) $(LIBS) + +test_mem$(EXEEXT): $(test_mem_OBJECTS) $(test_mem_DEPENDENCIES) $(EXTRA_test_mem_DEPENDENCIES) + @rm -f test_mem$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_mem_OBJECTS) $(test_mem_LDADD) $(LIBS) + +test_privkey$(EXEEXT): $(test_privkey_OBJECTS) $(test_privkey_DEPENDENCIES) $(EXTRA_test_privkey_DEPENDENCIES) + @rm -f test_privkey$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_privkey_OBJECTS) $(test_privkey_LDADD) $(LIBS) + +test_proto$(EXEEXT): $(test_proto_OBJECTS) $(test_proto_DEPENDENCIES) $(EXTRA_test_proto_DEPENDENCIES) + @rm -f test_proto$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_proto_OBJECTS) $(test_proto_LDADD) $(LIBS) + +test_sm$(EXEEXT): $(test_sm_OBJECTS) $(test_sm_DEPENDENCIES) $(EXTRA_test_sm_DEPENDENCIES) + @rm -f test_sm$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_sm_OBJECTS) $(test_sm_LDADD) $(LIBS) + +test_tlv$(EXEEXT): $(test_tlv_OBJECTS) $(test_tlv_DEPENDENCIES) $(EXTRA_test_tlv_DEPENDENCIES) + @rm -f test_tlv$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_tlv_OBJECTS) $(test_tlv_LDADD) $(LIBS) + +test_userstate$(EXEEXT): $(test_userstate_OBJECTS) $(test_userstate_DEPENDENCIES) $(EXTRA_test_userstate_DEPENDENCIES) + @rm -f test_userstate$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_userstate_OBJECTS) $(test_userstate_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_auth.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_b64.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_context.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_dh.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_instag.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mem.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_privkey.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_proto.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_sm.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_tlv.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_userstate.Po@am__quote@ + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(PROGRAMS) +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool clean-noinstPROGRAMS \ + mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am check check-am clean clean-generic \ + clean-libtool clean-noinstPROGRAMS cscopelist-am ctags \ + ctags-am distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/comm/third_party/libotr/tests/unit/instag.txt b/comm/third_party/libotr/tests/unit/instag.txt new file mode 100644 index 0000000000..3cf4c31164 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/instag.txt @@ -0,0 +1,4 @@ +alice_xmpp XMPP 01234567 +alice_irc IRC 9abcdef0 +alice_inv IRC WRONG +alice_icq ICQ 98765432 diff --git a/comm/third_party/libotr/tests/unit/test_auth.c b/comm/third_party/libotr/tests/unit/test_auth.c new file mode 100644 index 0000000000..06b5908246 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_auth.c @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2014 - David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +#include +#include +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 5 + +static void test_auth_new(void) +{ + struct context ctx; + OtrlAuthInfo *auth = &ctx.auth; + + /* API call. */ + otrl_auth_new(&ctx); + + ok(auth->authstate == OTRL_AUTHSTATE_NONE && + auth->our_keyid == 0 && + auth->encgx == NULL && + auth->encgx_len == 0 && + utils_is_zeroed(auth->r, 16) && + utils_is_zeroed(auth->hashgx, 32) && + auth->their_pub == NULL && + auth->their_keyid == 0 && + auth->enc_c == NULL && + auth->enc_cp == NULL && + auth->mac_m1 == NULL && + auth->mac_m1p == NULL && + auth->mac_m2 == NULL && + auth->mac_m2p == NULL && + utils_is_zeroed(auth->their_fingerprint, 20) && + auth->initiated == 0 && + auth->protocol_version == 0 && + utils_is_zeroed(auth->secure_session_id, 20) && + auth->secure_session_id_len == 0 && + auth->lastauthmsg == NULL && + auth->commit_sent_time == 0 && + auth->context == &ctx, + "OTR auth info init is valid"); +} + +static void test_auth_clear(void) +{ + struct context ctx; + OtrlAuthInfo *auth = &ctx.auth; + + /* API call. */ + otrl_auth_clear(auth); + + ok(auth->authstate == OTRL_AUTHSTATE_NONE && + auth->our_keyid == 0 && + auth->encgx == NULL && + auth->encgx_len == 0 && + utils_is_zeroed(auth->r, 16) && + utils_is_zeroed(auth->hashgx, 32) && + auth->their_pub == NULL && + auth->their_keyid == 0 && + auth->enc_c == NULL && + auth->enc_cp == NULL && + auth->mac_m1 == NULL && + auth->mac_m1p == NULL && + auth->mac_m2 == NULL && + auth->mac_m2p == NULL && + utils_is_zeroed(auth->their_fingerprint, 20) && + auth->initiated == 0 && + auth->protocol_version == 0 && + utils_is_zeroed(auth->secure_session_id, 20) && + auth->secure_session_id_len == 0 && + auth->lastauthmsg == NULL && + auth->commit_sent_time == 0 && + auth->context == &ctx, + "OTR auth info clear is valid"); +} + +static void test_auth_start_v23(void) +{ + unsigned int version = 3; + gcry_error_t err; + struct context ctx; + OtrlAuthInfo *auth = &ctx.auth; + + /* API call. */ + otrl_auth_new(&ctx); + err = otrl_auth_start_v23(auth, version); + + ok(err == gcry_error(GPG_ERR_NO_ERROR) && + auth->initiated == 1 && + auth->protocol_version == version && + auth->context->protocol_version == version && + auth->our_keyid == 1 && + !utils_is_zeroed(auth->r, sizeof(auth->r)) && + auth->encgx != NULL && + auth->encgx_len > 0 && + !utils_is_zeroed(auth->hashgx, sizeof(auth->hashgx)) && + auth->lastauthmsg != NULL && + auth->authstate == OTRL_AUTHSTATE_AWAITING_DHKEY, + "OTR auth start v23 is valid"); +} + +static void test_otrl_auth_copy_on_key() +{ + struct context m_ctx, ctx; + OtrlAuthInfo *auth = &ctx.auth; + OtrlAuthInfo *m_auth = &m_ctx.auth; + + otrl_auth_new(&ctx); + otrl_auth_new(&m_ctx); + + otrl_auth_start_v23(auth, 3); + otrl_auth_start_v23(m_auth, 3); + + m_auth->authstate = OTRL_AUTHSTATE_NONE; + auth->authstate = OTRL_AUTHSTATE_AWAITING_REVEALSIG, + otrl_auth_copy_on_key(m_auth, auth); + + ok(gcry_mpi_cmp((m_auth->our_dh.priv), (auth->our_dh.priv)) != 0 && + gcry_mpi_cmp((m_auth->our_dh.pub), (auth->our_dh.pub)) != 0 && + m_auth->our_keyid == auth->our_keyid && + memcmp(m_auth->r, auth->r, 16) != 0 && + memcmp(m_auth->encgx, auth->encgx, 16) != 0 && + memcmp(m_auth->hashgx, auth->hashgx, 16) != 0 && + auth->authstate == OTRL_AUTHSTATE_AWAITING_REVEALSIG, + "Copy not done"); + + auth->authstate = OTRL_AUTHSTATE_AWAITING_DHKEY; + m_auth->authstate = OTRL_AUTHSTATE_AWAITING_DHKEY; + otrl_auth_copy_on_key(m_auth, auth); + + ok(m_auth->initiated == auth->initiated && + m_auth->our_keyid == auth->our_keyid && + m_auth->our_dh.groupid == auth->our_dh.groupid && + gcry_mpi_cmp((m_auth->our_dh.priv), (auth->our_dh.priv)) == 0 && + gcry_mpi_cmp((m_auth->our_dh.pub), (auth->our_dh.pub)) == 0 && + m_auth->our_keyid == auth->our_keyid && + memcmp(m_auth->r, auth->r, 16) == 0 && + memcmp(m_auth->encgx, auth->encgx, 16) == 0 && + memcmp(m_auth->hashgx, auth->hashgx, 16) == 0 && + auth->authstate == OTRL_AUTHSTATE_AWAITING_DHKEY, + "Copy OK"); +} + +int main(int argc, char **argv) +{ + /* Libtap call for the number of tests planned. */ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + /* Initialize libotr. */ + otrl_dh_init(); + + test_auth_new(); + test_auth_clear(); + test_auth_start_v23(); + test_otrl_auth_copy_on_key(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_b64.c b/comm/third_party/libotr/tests/unit/test_b64.c new file mode 100644 index 0000000000..6d5599288e --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_b64.c @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +#include +#include +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 10 + +const char *alphanum_encoded = + "?OTR:" "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwCg==" "."; +const char *alphanum_decoded = + "abcdefghijklmnopqrstuvwxyz1234567890\n"; + +static void test_otrl_base64_otr_decode(void) +{ + int ret; + unsigned char *bufp = NULL; + size_t len = 0; + + /* + * Invalid decoding. + */ + + ok(otrl_base64_otr_decode("hello", NULL, NULL) == -2, + "Call with no prefix returned an error"); + ok(otrl_base64_otr_decode("?OTR:" "MTIzNAo=", NULL, NULL) == -2, + "Call with no suffix returned an error"); + /* Message of size 0. */ + ret = otrl_base64_otr_decode("", &bufp, &len); + ok(ret == -2 && bufp == NULL && len == 0, + "Decode b64 with message of len 0"); + /* + * Valid decoding. + */ + + /* Invalid chars are ignored */ + ok(otrl_base64_otr_decode("?OTR:invalid_base64_thing.", &bufp, &len) == 0 + && len == 12, "Invalid b64 data"); + free(bufp); + bufp = NULL; + len = 0; + + ok(otrl_base64_otr_decode(alphanum_encoded, &bufp, &len) == 0, + "Call with valid data successfull"); + ok(strcmp((const char*)bufp, alphanum_decoded) == 0 + && len == 37, "Decoded valid b64 test vector with success"); + free(bufp); + bufp = NULL; + len = 0; + + /* Invalid base64 char. */ + ret = otrl_base64_otr_decode("?OTR:_*&?!!*\"().", &bufp, &len); + ok(ret == 0 && bufp != NULL && len == 0, + "Decode b64 with invalid b64 characters"); + free(bufp); + bufp = NULL; + len = 0; + +} + +static void test_otrl_base64_otr_encode(void) +{ + unsigned char *bufp = NULL; + size_t len = 0; + char *encoded; + + encoded = otrl_base64_otr_encode((const unsigned char *) alphanum_decoded, + strlen(alphanum_decoded)); + + ok(strcmp(encoded, alphanum_encoded) == 0, + "Encoded b64 test vector with success"); + ok(otrl_base64_otr_decode(encoded, &bufp, &len) == 0, + "Decoded previously encoded test vector"); + ok(memcmp(bufp, alphanum_decoded, len) == 0 + && len == strlen(alphanum_decoded), + "Decoded value is exact"); + free(bufp); + free(encoded); +} + +int main(int argc, char** argv) +{ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + test_otrl_base64_otr_decode(); + test_otrl_base64_otr_encode(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_context.c b/comm/third_party/libotr/tests/unit/test_context.c new file mode 100644 index 0000000000..6e173cd252 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_context.c @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include + +#include + +#define NUM_TESTS 22 + +static void test_otrl_context_find_fingerprint(void) +{ + unsigned char fingerprint[20] = {0}; + int add_if_missing = 0, addedp = 0; + + ok(otrl_context_find_fingerprint(NULL, fingerprint, + add_if_missing, &addedp) == NULL, "NULL context detected"); +} + +static ConnContext *new_context(const char *user, const char *account, + const char *protocol) +{ + ConnContext *context; + context = calloc(1, sizeof(ConnContext)); + context->username = strdup(user); + context->accountname = strdup(account); + context->protocol = strdup(protocol); + context->m_context = context; + context->active_fingerprint = calloc(1, sizeof(Fingerprint)); + context->context_priv = calloc(1, sizeof(ConnContextPriv)); + + return context; +} + +static void free_context(ConnContext *context) +{ + free(context->username); + free(context->accountname); + free(context->protocol); + free(context); +} + +static void test_otrl_context_find_recent_instance() +{ + ConnContext *context = new_context("main", "main", "main"); + ConnContext *context_child = new_context("child", "child", "child"); + ConnContext *context_rcvd = new_context("rcvd", "rcvd", "rcvd"); + ConnContext *context_sent = new_context("sent", "sent", "sent"); + ConnContext *tmp; + + context->recent_child = context_child; + context->recent_rcvd_child = context_rcvd; + context->recent_sent_child = context_sent; + + ok(otrl_context_find_recent_instance(NULL, OTRL_INSTAG_RECENT) == NULL, + "NULL context detected"); + + tmp = otrl_context_find_recent_instance(context, OTRL_INSTAG_RECENT); + ok(strcmp(tmp->username, "child") == 0, "OTRL_INSTAG_RECENT ok"); + + tmp = otrl_context_find_recent_instance(context, + OTRL_INSTAG_RECENT_RECEIVED); + ok(strcmp(tmp->username, "rcvd") == 0, "OTRL_INSTAG_RECENT_RECEIVED ok"); + + tmp = otrl_context_find_recent_instance(context, OTRL_INSTAG_RECENT_SENT); + ok(strcmp(tmp->username, "sent") == 0, "OTRL_INSTAG_RECENT_SENT ok"); + + tmp = otrl_context_find_recent_instance(context, INT_MAX); + ok(!tmp, "Invalid instag detected"); + + free_context(context); + free_context(context_child); + free_context(context_rcvd); + free_context(context_sent); +} + +static void test_otrl_context_find_recent_secure_instance(void) +{ + ConnContext *context1 = new_context("1", "1", "1"); + ConnContext *context2 = new_context("2", "2", "2"); + ConnContext *tmp; + + ok(otrl_context_find_recent_secure_instance(NULL) == NULL, + "NULL detected"); + + context1->next = context2; + context2->next = NULL; + context2->m_context = context1; + + context1->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->msgstate = OTRL_MSGSTATE_PLAINTEXT; + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context2, "Same msgstate"); + + context1->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->msgstate = OTRL_MSGSTATE_FINISHED; + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context2, "plaintext then finished"); + + context1->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->msgstate = OTRL_MSGSTATE_ENCRYPTED; + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context2, "Most secure context found"); + + context1->msgstate = OTRL_MSGSTATE_ENCRYPTED; + context2->msgstate = OTRL_MSGSTATE_ENCRYPTED; + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context2, "Most secure context found"); + + context1->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->active_fingerprint->trust = strdup("hello"); + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context2, "Most secure context found"); + free(context2->active_fingerprint); + context2->active_fingerprint = NULL; + + context1->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context2->context_priv->lastrecv = 1; + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context2, "Most secure context found"); + + context1->msgstate = OTRL_MSGSTATE_PLAINTEXT; + context1->context_priv->lastrecv = 2; + context2->msgstate = OTRL_MSGSTATE_PLAINTEXT; + tmp = otrl_context_find_recent_secure_instance(context1); + ok(tmp == context1, "Most secure context found"); + + free_context(context1); + free_context(context2); +} + +static void test_otrl_context_is_fingerprint_trusted() +{ + Fingerprint fprint; + fprint.trust = NULL; + + ok(otrl_context_is_fingerprint_trusted(NULL) == 0, + "NULL fingerprint detected"); + ok(otrl_context_is_fingerprint_trusted(&fprint) == 0, + "NULL trust detected"); + fprint.trust = "1234"; + ok(otrl_context_is_fingerprint_trusted(&fprint) != 0, + "Trusted fingerprint detected"); +} + +static void test_otrl_context_update_recent_child() +{ + ConnContext context1, context2; + context1.m_context = &context1; + context2.m_context = &context1; + + otrl_context_update_recent_child(&context1, 0); + ok(context1.recent_rcvd_child == &context1 && + context1.recent_child == &context1, + "Recent self rcvd set"); + + otrl_context_update_recent_child(&context1, 1); + ok(context1.recent_sent_child == &context1 && + context1.recent_child == &context1, + "Recent self sent set"); + + otrl_context_update_recent_child(&context2, 0); + ok(context1.recent_rcvd_child == &context2 && + context1.recent_child == &context2, + "Recent rcvd set"); + + otrl_context_update_recent_child(&context2, 1); + ok(context1.recent_sent_child == &context2 && + context1.recent_child == &context2, + "Recent sent set"); +} + +static void test_otrl_context_set_trust(void) +{ + Fingerprint fprint; + const char *trust = "I don't trust anyone."; + + fprint.trust = NULL; + + otrl_context_set_trust(&fprint, trust); + ok(strcmp(fprint.trust, trust) == 0, "Fingerprint set with success"); +} + +int main(int argc, char **argv) +{ + plan_tests(NUM_TESTS); + + test_otrl_context_set_trust(); + test_otrl_context_find_recent_instance(); + test_otrl_context_find_fingerprint(); + test_otrl_context_find_recent_secure_instance(); + test_otrl_context_is_fingerprint_trusted(); + test_otrl_context_update_recent_child(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_dh.c b/comm/third_party/libotr/tests/unit/test_dh.c new file mode 100644 index 0000000000..c27c09b886 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_dh.c @@ -0,0 +1,515 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include +#include + +#include +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 38 + +/* + * The re-implementation/inclusion of crypto stuff is necessary because libotr + * doesn't expose them. + */ + +static const char* DH1536_MODULUS_S = "0x" + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"; +static const char *DH1536_GENERATOR_S = "0x02"; +static const int DH1536_MOD_LEN_BITS = 1536; + +static gcry_mpi_t DH1536_MODULUS = NULL; +static gcry_mpi_t DH1536_MODULUS_MINUS_2 = NULL; +static gcry_mpi_t DH1536_GENERATOR = NULL; + +static void test_otrl_dh_keypair_init(void) +{ + DH_keypair kp; + + otrl_dh_keypair_init(&kp); + + ok(kp.groupid == 0 && + kp.priv == NULL && + kp.pub == NULL, + "Keypair initialized"); +} + +static void test_otrl_dh_keypair_copy(void) +{ + DH_keypair k1, k2; + unsigned char *buf; + + k1.groupid = rand(); + + buf = gcry_random_bytes(32, GCRY_WEAK_RANDOM); + gcry_mpi_scan(&(k1.priv), GCRYMPI_FMT_USG, buf, 32, NULL); + gcry_free(buf); + + buf = gcry_random_bytes(32, GCRY_WEAK_RANDOM); + gcry_mpi_scan(&(k1.pub), GCRYMPI_FMT_USG, buf, 32, NULL); + gcry_free(buf); + + otrl_dh_keypair_copy(&k2, &k1); + + ok(k1.groupid == k2.groupid && + gcry_mpi_cmp(k1.priv, k2.priv) == 0 && + gcry_mpi_cmp(k1.pub, k2.pub) == 0, + "Keypair copied"); + + gcry_mpi_release(k1.priv); + gcry_mpi_release(k1.pub); + gcry_mpi_release(k2.priv); + gcry_mpi_release(k2.pub); +} + +static void test_otrl_dh_session_free() +{ + DH_sesskeys sess; + DH_keypair kp1, kp2; + otrl_dh_gen_keypair(DH1536_GROUP_ID, &(kp1)); + otrl_dh_gen_keypair(DH1536_GROUP_ID, &(kp2)); + otrl_dh_session(&sess, &kp1, kp2.pub); + + otrl_dh_session_free(&sess); + + ok(sess.sendenc == NULL && + sess.sendmac == NULL && + sess.rcvenc == NULL && + sess.rcvmac == NULL && + utils_is_zeroed(sess.sendctr, 16) && + utils_is_zeroed(sess.rcvctr, 16) && + utils_is_zeroed(sess.sendmackey, 16) && + utils_is_zeroed(sess.rcvmackey, 16) && + sess.sendmacused == 0 && + sess.rcvmacused == 0 && + utils_is_zeroed(sess.extrakey, OTRL_EXTRAKEY_BYTES), + "Session freed"); +} + + +static void test_otrl_dh_session_blank() +{ + DH_sesskeys sess; + DH_keypair kp1, kp2; + otrl_dh_gen_keypair(DH1536_GROUP_ID, &(kp1)); + otrl_dh_gen_keypair(DH1536_GROUP_ID, &(kp2)); + otrl_dh_session(&sess, &kp1, kp2.pub); + + otrl_dh_session_blank(&sess); + + ok(sess.sendenc == NULL && + sess.sendmac == NULL && + sess.rcvenc == NULL && + sess.rcvmac == NULL && + utils_is_zeroed(sess.sendctr, 16) && + utils_is_zeroed(sess.rcvctr, 16) && + utils_is_zeroed(sess.sendmackey, 16) && + utils_is_zeroed(sess.rcvmackey, 16) && + sess.sendmacused == 0 && + sess.rcvmacused == 0 && + utils_is_zeroed(sess.extrakey, OTRL_EXTRAKEY_BYTES), + "Session blanked"); +} + +static void test_otrl_dh_gen_keypair(void) +{ + DH_keypair kp; + gcry_mpi_t pubkey = NULL; + + otrl_dh_keypair_init(&kp); + + ok(otrl_dh_gen_keypair(DH1536_GROUP_ID+1, &kp) == + gcry_error(GPG_ERR_INV_VALUE), + "Invalid group detected"); + + ok(otrl_dh_gen_keypair(DH1536_GROUP_ID, &kp) == + gcry_error(GPG_ERR_NO_ERROR), + "Valid group set"); + ok(kp.groupid == DH1536_GROUP_ID, "Group set"); + + pubkey = gcry_mpi_new(DH1536_MOD_LEN_BITS); + gcry_mpi_powm(pubkey, DH1536_GENERATOR, kp.priv, DH1536_MODULUS); + ok(gcry_mpi_cmp(pubkey, kp.pub) == 0, "Matching pubkey"); + otrl_dh_keypair_free(&kp); +} + +static void test_otrl_dh_keypair_free(void) +{ + DH_keypair kp; + otrl_dh_gen_keypair(DH1536_GROUP_ID, &kp); + otrl_dh_keypair_free(&kp); + ok(kp.pub == NULL && kp.priv == NULL && kp.groupid == DH1536_GROUP_ID, + "DH_keypair free'd with success"); + +} + +static void invert_DH_keypair(DH_keypair* kp1, DH_keypair* kp2) +{ + DH_keypair tmp; + otrl_dh_keypair_copy(&tmp, kp1); + otrl_dh_keypair_copy(kp1, kp2); + otrl_dh_keypair_copy(kp2, &tmp); + otrl_dh_keypair_free(&tmp); +} + +/* + * This is an helper function. See the next one. + */ +static void _test_ortl_dh_session(const DH_keypair *kp, gcry_mpi_t y) +{ + unsigned char *gabdata; + unsigned char *hashdata; + unsigned char encrypt[32] = {0}; + unsigned char expected_encrypt[32] = {0}; + unsigned char sendbyte, rcvbyte; + const char test_vector[] = "This is a test vector"; + DH_sesskeys sess; + DH_sesskeys sess_expected; + gcry_mpi_t gab; + size_t gablen; + otrl_dh_session_blank(&sess); + otrl_dh_session(&sess, kp, y); + + gab = gcry_mpi_snew(DH1536_MOD_LEN_BITS); + gcry_mpi_powm(gab, y, kp->priv, DH1536_MODULUS); + + gcry_mpi_print(GCRYMPI_FMT_USG, NULL, 0, &gablen, gab); + gabdata = gcry_malloc_secure(gablen + 5); + gabdata[1] = (gablen >> 24) & 0xff; + gabdata[2] = (gablen >> 16) & 0xff; + gabdata[3] = (gablen >> 8) & 0xff; + gabdata[4] = gablen & 0xff; + gcry_mpi_print(GCRYMPI_FMT_USG, gabdata + 5, gablen, NULL, gab); + gcry_mpi_release(gab); + + hashdata = gcry_malloc_secure(20); + + if (gcry_mpi_cmp(kp->pub, y) > 0 ) { + sendbyte = 0x01; + rcvbyte = 0x02; + } else { + sendbyte = 0x02; + rcvbyte = 0x01; + } + + gabdata[0] = sendbyte; + gcry_md_hash_buffer(GCRY_MD_SHA1, hashdata, gabdata, gablen + 5); + + gcry_cipher_open(&(sess_expected.sendenc), GCRY_CIPHER_AES, + GCRY_CIPHER_MODE_CTR, GCRY_CIPHER_SECURE); + gcry_cipher_setkey(sess_expected.sendenc, hashdata, 16); + gcry_cipher_encrypt(sess_expected.sendenc, expected_encrypt, + sizeof(expected_encrypt), test_vector, strlen(test_vector)); + gcry_cipher_encrypt(sess.sendenc, encrypt, sizeof(encrypt), test_vector, + strlen(test_vector)); + ok(memcmp(encrypt, expected_encrypt, sizeof(encrypt)) == 0, "sendenc ok"); + + gcry_md_hash_buffer(GCRY_MD_SHA1, sess_expected.sendmackey, hashdata, 16); + gcry_md_open(&(sess_expected.sendmac), GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC); + gcry_md_setkey(sess_expected.sendmac, sess_expected.sendmackey, 20); + gcry_md_write(sess_expected.sendmac, test_vector, sizeof(test_vector)); + gcry_md_write(sess.sendmac, test_vector, sizeof(test_vector)); + + ok(memcmp(gcry_md_read(sess_expected.sendmac, 0), + gcry_md_read(sess.sendmac, 0), 32) == 0, + "Sendmac ok"); + + gabdata[0] = rcvbyte; + gcry_md_hash_buffer(GCRY_MD_SHA1, hashdata, gabdata, gablen + 5); + gcry_cipher_open(&(sess_expected.rcvenc), GCRY_CIPHER_AES, + GCRY_CIPHER_MODE_CTR, GCRY_CIPHER_SECURE); + gcry_cipher_setkey(sess_expected.rcvenc, hashdata, 16); + gcry_cipher_encrypt(sess_expected.rcvenc, expected_encrypt, + sizeof(expected_encrypt), test_vector, strlen(test_vector)); + gcry_cipher_encrypt(sess.rcvenc, encrypt, sizeof(encrypt), test_vector, + strlen(test_vector)); + ok(memcmp(encrypt, expected_encrypt, sizeof(encrypt)) == 0, "Sendenc ok"); + + gcry_md_hash_buffer(GCRY_MD_SHA1, sess_expected.rcvmackey, hashdata, 16); + gcry_md_open(&(sess_expected.rcvmac), GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC); + gcry_md_setkey(sess_expected.rcvmac, sess_expected.rcvmackey, 20); + gcry_md_write(sess_expected.rcvmac, test_vector, sizeof(test_vector)); + gcry_md_write(sess.rcvmac, test_vector, sizeof(test_vector)); + ok(memcmp(gcry_md_read(sess_expected.sendmac, 0), + gcry_md_read(sess.sendmac, 0), 32) == 0, + "rcvmac ok"); + + gabdata[0] = 0xff; + + gcry_md_hash_buffer(GCRY_MD_SHA256, sess_expected.extrakey, gabdata, + gablen + 5); + ok(memcmp(sess_expected.extrakey, sess.extrakey, 32) == 0, "extrakey set"); + + gcry_free(gabdata); + gcry_free(hashdata); +} + +/* + * This function is a little bit tricky, since it uses an array of 3 + * DH_keypair. The first one has a smaller pubkey than the second, which has a + * smaller pubkey than the third one. + * + * The second key is used as "main" key. The two other ones are used to test + * the otrl_dh_session, with a biggest and a smallest key than the "main" one. + */ +static void test_otrl_dh_session(void) +{ + int i; + DH_keypair kp[3]; /* kp[0] < kp[1] < kp[2] */ + DH_sesskeys sess; + + for (i = 0; i < 3; i++) { + otrl_dh_gen_keypair(DH1536_GROUP_ID, &(kp[i])); + } + + /* Sort the array. */ + for (i = 0; i < 2; i++) { + if (gcry_mpi_cmp(kp[i].pub, kp[i + 1].pub) > 0) { + invert_DH_keypair(kp + i, kp + i + 1); + } + } + + if (gcry_mpi_cmp(kp[0].pub, kp[1].pub) > 0) { + invert_DH_keypair(kp, kp + 1); + } + + kp[1].groupid++; + + ok(otrl_dh_session(&sess, &(kp[1]), kp[0].pub) == + gcry_error(GPG_ERR_INV_VALUE), + "Invalid group detected"); + kp[1].groupid--; + + _test_ortl_dh_session(&(kp[1]), kp[0].pub); + _test_ortl_dh_session(&(kp[1]), kp[2].pub); +} + +static void test_otrl_dh_compute_v2_auth_keys(void) +{ + const char test_vector[] = "This is a test vector"; + size_t slen = 0; + size_t sessionidlenp = 0; + unsigned char *sdata = NULL; + unsigned char *hashdata = NULL; + gcry_mpi_t s = NULL; + unsigned char ctr[16] = {0}; + + DH_keypair our_dh, their_dh; + gcry_mpi_t public_key = NULL; + + unsigned char sessionid[8]; + gcry_md_hd_t mac_m1 = NULL, mac_m1p = NULL, mac_m2 = NULL, mac_m2p = NULL; + gcry_cipher_hd_t enc_c = NULL, enc_cp = NULL; + unsigned char encrypt[32] = {0}; + + unsigned char sessionid_expected[8]; + + gcry_md_hd_t mac_m1_expected = NULL, mac_m1p_expected = NULL; + gcry_md_hd_t mac_m2_expected = NULL, mac_m2p_expected = NULL; + gcry_cipher_hd_t enc_c_expected = NULL, enc_cp_expected = NULL; + unsigned char expected_encrypt[32] = {0}; + + otrl_dh_gen_keypair(DH1536_GROUP_ID, &our_dh); + otrl_dh_gen_keypair(DH1536_GROUP_ID, &their_dh); + + our_dh.groupid++; + ok(otrl_dh_compute_v2_auth_keys(&our_dh, their_dh.pub, + sessionid, &sessionidlenp, &enc_c, &enc_cp, + &mac_m1, &mac_m1p, &mac_m2, &mac_m2p) + == gcry_error(GPG_ERR_INV_VALUE), + "Invalid group detected"); + our_dh.groupid--; + + gcry_mpi_scan(&public_key, GCRYMPI_FMT_USG, "1", 0, NULL); + + ok(otrl_dh_compute_v2_auth_keys(&our_dh, public_key, + sessionid, &sessionidlenp, &enc_c, &enc_cp, + &mac_m1, &mac_m1p, &mac_m2, &mac_m2p) + == gcry_error(GPG_ERR_INV_VALUE), + "Public key too small"); + + gcry_mpi_scan(&public_key, GCRYMPI_FMT_HEX, + (const unsigned char *) DH1536_MODULUS_S, 0, NULL); + gcry_mpi_add_ui(public_key, DH1536_MODULUS, 1); + + ok(otrl_dh_compute_v2_auth_keys(&our_dh, DH1536_MODULUS, + sessionid, &sessionidlenp, &enc_c, &enc_cp, + &mac_m1, &mac_m1p, &mac_m2, &mac_m2p) + == gcry_error(GPG_ERR_INV_VALUE), + "Public key too big"); + + ok(otrl_dh_compute_v2_auth_keys(&our_dh, their_dh.pub, sessionid, + &sessionidlenp, &enc_c, &enc_cp, &mac_m1, &mac_m1p, &mac_m2, + &mac_m2p) == gcry_error(GPG_ERR_NO_ERROR), + "Auth keys generated"); + + ok(sessionidlenp == 8, "Session id len p set to correct value"); + + s = gcry_mpi_snew(DH1536_MOD_LEN_BITS); + gcry_mpi_powm(s, their_dh.pub, our_dh.priv, DH1536_MODULUS); + + gcry_mpi_print(GCRYMPI_FMT_USG, NULL, 0, &slen, s); + sdata = gcry_malloc_secure(slen + 5); + sdata[1] = (slen >> 24) & 0xff; + sdata[2] = (slen >> 16) & 0xff; + sdata[3] = (slen >> 8) & 0xff; + sdata[4] = slen & 0xff; + gcry_mpi_print(GCRYMPI_FMT_USG, sdata+5, slen, NULL, s); + gcry_mpi_release(s); + + hashdata = gcry_malloc_secure(32); + sdata[0] = 0x00; + gcry_md_hash_buffer(GCRY_MD_SHA256, hashdata, sdata, slen+5); + memmove(sessionid_expected, hashdata, 8); + ok(memcmp(sessionid_expected, sessionid, 8) == 0, "Session id is correct"); + + sdata[0] = 0x01; + gcry_md_hash_buffer(GCRY_MD_SHA256, hashdata, sdata, slen+5); + + gcry_cipher_open(&enc_c_expected, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR, + GCRY_CIPHER_SECURE); + gcry_cipher_setkey(enc_c_expected, hashdata, 16); + gcry_cipher_setctr(enc_c_expected, ctr, 16); + + gcry_cipher_encrypt(enc_c_expected, expected_encrypt, + sizeof(expected_encrypt), test_vector, strlen(test_vector)); + gcry_cipher_encrypt(enc_c, encrypt, sizeof(encrypt), test_vector, + strlen(test_vector)); + ok(memcmp(encrypt, expected_encrypt, sizeof(encrypt)) == 0, "Enc ok"); + + gcry_cipher_open(&(enc_cp_expected), GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR, + GCRY_CIPHER_SECURE); + gcry_cipher_setkey(enc_cp_expected, hashdata+16, 16); + gcry_cipher_setctr(enc_cp_expected, ctr, 16); + gcry_cipher_encrypt(enc_cp_expected, expected_encrypt, + sizeof(expected_encrypt), test_vector, strlen(test_vector)); + gcry_cipher_encrypt(enc_cp, encrypt, sizeof(encrypt), test_vector, + strlen(test_vector)); + ok(memcmp(encrypt, expected_encrypt, sizeof(encrypt)) == 0, "Encp ok"); + + sdata[0] = 0x02; + gcry_md_hash_buffer(GCRY_MD_SHA256, hashdata, sdata, slen+5); + gcry_md_open(&mac_m1_expected, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); + gcry_md_setkey(mac_m1_expected, hashdata, 32); + gcry_md_write(mac_m1_expected, test_vector, sizeof(test_vector)); + gcry_md_write(mac_m1, test_vector, sizeof(test_vector)); + ok(memcmp(gcry_md_read(mac_m1_expected, 0), + gcry_md_read(mac_m1, 0), 32) == 0, + "mac_m1 set"); + + sdata[0] = 0x03; + gcry_md_hash_buffer(GCRY_MD_SHA256, hashdata, sdata, slen+5); + gcry_md_open(&mac_m2_expected, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); + gcry_md_setkey(mac_m2_expected, hashdata, 32); + gcry_md_write(mac_m2_expected, test_vector, sizeof(test_vector)); + gcry_md_write(mac_m2, test_vector, sizeof(test_vector)); + ok(memcmp(gcry_md_read(mac_m2_expected, 0), + gcry_md_read(mac_m2, 0), 32) == 0, + "mac_m2 set"); + + sdata[0] = 0x04; + gcry_md_hash_buffer(GCRY_MD_SHA256, hashdata, sdata, slen+5); + gcry_md_open(&mac_m1p_expected, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); + gcry_md_setkey(mac_m1p_expected, hashdata, 32); + gcry_md_write(mac_m1p_expected, test_vector, sizeof(test_vector)); + gcry_md_write(mac_m1p, test_vector, sizeof(test_vector)); + + ok(memcmp(gcry_md_read(mac_m1p_expected, 0), + gcry_md_read(mac_m1p, 0), 32) == 0, + "mac_m1p set"); + + sdata[0] = 0x05; + gcry_md_hash_buffer(GCRY_MD_SHA256, hashdata, sdata, slen+5); + gcry_md_open(&mac_m2p_expected, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); + gcry_md_setkey(mac_m2p_expected, hashdata, 32); + gcry_md_write(mac_m2p_expected, test_vector, sizeof(test_vector)); + gcry_md_write(mac_m2p, test_vector, sizeof(test_vector)); + + ok(memcmp(gcry_md_read(mac_m2p_expected, 0), + gcry_md_read(mac_m2p, 0), 32) == 0, + "mac_m2p set"); + + gcry_free(sdata); + gcry_free(hashdata); +} + +static void test_otrl_dh_incctr() +{ + unsigned char ctr[8] = {0}; + otrl_dh_incctr(ctr); + ok(ctr[7] == 1 && utils_is_zeroed(ctr, 7), "Counter set"); + ctr[7] = 255; + otrl_dh_incctr(ctr); + ok(ctr[7] == 0 && ctr[6] == 1 && utils_is_zeroed(ctr, 5), + "Counter set"); + memset(ctr, 255, sizeof(ctr)); + otrl_dh_incctr(ctr); + ok(utils_is_zeroed(ctr, sizeof(ctr)), "Counter set"); +} + +static void test_otrl_dh_cmpctr() +{ + unsigned char ctr1[8] = {0}, ctr2[8] = {0}; + ok(otrl_dh_cmpctr(ctr1, ctr2) == 0, "Null counters are equals"); + ctr1[1]++; + ok(otrl_dh_cmpctr(ctr1, ctr2) > 0, "Ctr1 is bigger than ctr2"); + ctr2[0]++; + ok(otrl_dh_cmpctr(ctr1, ctr2) < 0, "Ctr2 is bigger than ctr1"); +} + +int main(int argc, char **argv) +{ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + otrl_dh_init(); + + gcry_mpi_scan(&DH1536_MODULUS, GCRYMPI_FMT_HEX, + (const unsigned char *)DH1536_MODULUS_S, 0, NULL); + gcry_mpi_scan(&DH1536_GENERATOR, GCRYMPI_FMT_HEX, + (const unsigned char *)DH1536_GENERATOR_S, 0, NULL); + DH1536_MODULUS_MINUS_2 = gcry_mpi_new(DH1536_MOD_LEN_BITS); + gcry_mpi_sub_ui(DH1536_MODULUS_MINUS_2, DH1536_MODULUS, 2); + + test_otrl_dh_gen_keypair(); + test_otrl_dh_keypair_free(); + test_otrl_dh_keypair_init(); + test_otrl_dh_compute_v2_auth_keys(); + test_otrl_dh_session(); + test_otrl_dh_keypair_copy(); + test_otrl_dh_session_blank(); + test_otrl_dh_session_free(); + test_otrl_dh_incctr(); + test_otrl_dh_cmpctr(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_instag.c b/comm/third_party/libotr/tests/unit/test_instag.c new file mode 100644 index 0000000000..3758ae2f2b --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_instag.c @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 13 + +/* Current directory of this executable. */ +static char curdir[PATH_MAX]; +static char instag_filepath[PATH_MAX]; + +static void test_otrl_instag_forget(void) +{ + OtrlInsTag *instag1 = calloc(1, sizeof(OtrlInsTag)); + OtrlInsTag *instag2 = calloc(1, sizeof(OtrlInsTag)); + + instag1->tous = &instag1; + instag1->accountname = strdup("name one"); + instag1->protocol = strdup("protocol one"); + instag1->next = instag2; + instag1->next->tous = &(instag1->next); + instag2->accountname = strdup("name two"); + instag2->protocol = strdup("protocol two"); + + otrl_instag_forget(NULL); + ok(1, "Forget on NULL didn't segfault"); + + otrl_instag_forget(instag2); + ok(instag1->next == NULL, "Instag forgotten without segfault"); +} + +static void test_otrl_instag_forget_all(void) +{ + OtrlUserState us = otrl_userstate_create(); + OtrlInsTag *p = malloc(sizeof(OtrlInsTag)); + p->accountname = strdup("account name"); + p->protocol = strdup("protocol name"); + p->instag = otrl_instag_get_new(); + + otrl_instag_forget_all(us); + ok(1, "Forget all on empty user state"); + + p->next = us->instag_root; + p->tous = &(us->instag_root); + us->instag_root = p; + + otrl_instag_forget_all(us); + ok(1, "Forget all on a non-empty user state"); +} + +static void test_otrl_instag_find(void) +{ + OtrlUserState us = otrl_userstate_create(); + OtrlInsTag *p1 = malloc(sizeof(OtrlInsTag)); + OtrlInsTag *p2 = malloc(sizeof(OtrlInsTag)); + + p1->accountname = strdup("account one"); + p1->protocol = strdup("protocol one"); + p1->instag = otrl_instag_get_new(); + p1->next = us->instag_root; + p1->tous = &(us->instag_root); + us->instag_root = p1; + + p2->accountname = strdup("account two"); + p2->protocol = strdup("protocol two"); + p2->instag = otrl_instag_get_new(); + p2->next = us->instag_root; + p2->next->tous = &(p2->next); + p2->tous = &(us->instag_root); + us->instag_root = p2; + + ok(otrl_instag_find(us, "account two", "protocol two") == p2, + "Found instag"); + ok(otrl_instag_find(us, "account one", "protocol two") == NULL, + "Instag not found"); + ok(otrl_instag_find(us, "account three", "protocol three") == NULL, + "Instag not found"); +} + +static void test_otrl_instag_read(void) +{ + OtrlUserState us = otrl_userstate_create(); + OtrlInsTag *one, *two, *three, *four; + char sone[9] = {0}, stwo[9] = {0}, sfour[9] = {0}; + one = two = three = four = NULL; + ok(otrl_instag_read(us, "/non_existent_file") == + gcry_error_from_errno(ENOENT), + "Non-existent file detected"); + + ok(otrl_instag_read(us, instag_filepath) == GPG_ERR_NO_ERROR, + "Instag called with success"); + + one = otrl_instag_find(us, "alice_xmpp", "XMPP"); + snprintf(sone, sizeof(sone), "%08x", one->instag); + + two = otrl_instag_find(us, "alice_irc", "IRC"); + snprintf(stwo, sizeof(stwo), "%08x", two->instag); + + three = otrl_instag_find(us, "alice_inv", "IRC"); + + four = otrl_instag_find(us, "alice_icq", "ICQ"); + snprintf(sfour, sizeof(sfour), "%08x", four->instag); + + ok(one && two && !three && four && + strcmp(sone, "01234567") == 0 && + strcmp(stwo, "9abcdef0") == 0 && + strcmp(sfour, "98765432") == 0, + "Instag succesfully read"); +} + +static void test_otrl_instag_read_FILEp(void) +{ + FILE* instf = fopen(instag_filepath, "rb"); + OtrlUserState us = otrl_userstate_create(); + OtrlInsTag* one, *two, *three, *four; + char sone[9] = {0}, stwo[9] = {0}, sfour[9] = {0}; + one = two = three = four = NULL; + + ok(otrl_instag_read_FILEp(us, instf) == gcry_error(GPG_ERR_NO_ERROR), + "Instead read from FILEp"); + fclose(instf); + + one = otrl_instag_find(us, "alice_xmpp", "XMPP"); + snprintf(sone, sizeof(sone), "%08x", one->instag); + + two = otrl_instag_find(us, "alice_irc", "IRC"); + snprintf(stwo, sizeof(stwo), "%08x", two->instag); + + three = otrl_instag_find(us, "alice_inv", "IRC"); + + four = otrl_instag_find(us, "alice_icq", "ICQ"); + snprintf(sfour, sizeof(sfour), "%08x", four->instag); + + ok(one && two && !three && four && + strcmp(sone, "01234567") == 0 && + strcmp(stwo, "9abcdef0") == 0 && + strcmp(sfour, "98765432") == 0, + "Instag succesfully read"); +} + +static void test_otrl_instag_get_new(void) +{ + ok(otrl_instag_get_new() != 0, "New instag generated"); +} + +static ssize_t get_exe_path(char *buf, size_t len) +{ + char *path_end; + + if (readlink("/proc/self/exe", buf, len) < 0) { + return -ENOMEM; + } + + /* + * Workaround to handle libtool path of the binary that is actually in the + * $(buildir)/.libs. This is to make sure unit test works outside of tree. + */ + path_end = strstr(buf, ".libs/"); + if (!path_end) { + path_end = strrchr(buf, '/'); + if (!path_end) { + return -errno; + } + *(++path_end) = '\0'; + } else { + *path_end = '\0'; + } + + return path_end - buf; +} + +int main(int argc, char **argv) +{ + /* Libtap call for the number of tests planned. */ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + if (get_exe_path(curdir, sizeof(curdir)) < 0) { + return -ENOMEM; + } + + /* Build the full path of the instag.txt file. */ + (void) snprintf(instag_filepath, sizeof(instag_filepath), "%s%s", curdir, + "instag.txt"); + + test_otrl_instag_forget(); + test_otrl_instag_forget_all(); + test_otrl_instag_find(); + test_otrl_instag_read(); + test_otrl_instag_read_FILEp(); + test_otrl_instag_get_new(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_mem.c b/comm/third_party/libotr/tests/unit/test_mem.c new file mode 100644 index 0000000000..24eae20151 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_mem.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include +#include + +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 5 + +static void test_otrl_mem_differ(void) +{ + const unsigned char buf1[] = "\x00" "12" "\x00" "34"; + const unsigned char buf2[] = "\x00" "13" "\x00" "34"; + const unsigned char buf3[] = "\x00" "13" "\x00" "345"; + + ok(otrl_mem_differ(buf1, buf1, sizeof(buf1)) == 0, + "Identical buf are identical"); + ok(otrl_mem_differ(buf1, buf2, sizeof(buf1)) == 1, + "buf1 and buf2 are not identical"); + ok(otrl_mem_differ(buf2, buf3, sizeof(buf1)) == 1, + "buf1 and buf2 are not identical"); + ok(otrl_mem_differ(buf1, NULL, 0) == 0, + "buf1 and NULL are identical"); + ok(otrl_mem_differ(NULL, NULL, 0) == 0, + "NULL and NULL are identical"); +} + +int main(int argc, char **argv) +{ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + test_otrl_mem_differ(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_privkey.c b/comm/third_party/libotr/tests/unit/test_privkey.c new file mode 100644 index 0000000000..9e2db46189 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_privkey.c @@ -0,0 +1,264 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +#include +#include + +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 13 + +static OtrlUserState us = NULL; +static char filename[] = "/tmp/libotr-testing-XXXXXX"; +static FILE* f = NULL; + +/* + * Create a public key block from a private key + */ +static void make_pubkey(unsigned char **pubbufp, size_t *publenp, + gcry_sexp_t privkey) +{ + gcry_mpi_t p,q,g,y; + gcry_sexp_t dsas,ps,qs,gs,ys; + size_t np,nq,ng,ny; + enum gcry_mpi_format format = GCRYMPI_FMT_USG; + + *pubbufp = NULL; + *publenp = 0; + + /* Extract the public parameters */ + dsas = gcry_sexp_find_token(privkey, "dsa", 0); + ps = gcry_sexp_find_token(dsas, "p", 0); + qs = gcry_sexp_find_token(dsas, "q", 0); + gs = gcry_sexp_find_token(dsas, "g", 0); + ys = gcry_sexp_find_token(dsas, "y", 0); + gcry_sexp_release(dsas); + + p = gcry_sexp_nth_mpi(ps, 1, GCRYMPI_FMT_USG); + gcry_sexp_release(ps); + q = gcry_sexp_nth_mpi(qs, 1, GCRYMPI_FMT_USG); + gcry_sexp_release(qs); + g = gcry_sexp_nth_mpi(gs, 1, GCRYMPI_FMT_USG); + gcry_sexp_release(gs); + y = gcry_sexp_nth_mpi(ys, 1, GCRYMPI_FMT_USG); + gcry_sexp_release(ys); + + *publenp = 0; + gcry_mpi_print(format, NULL, 0, &np, p); + *publenp += np + 4; + gcry_mpi_print(format, NULL, 0, &nq, q); + *publenp += nq + 4; + gcry_mpi_print(format, NULL, 0, &ng, g); + *publenp += ng + 4; + gcry_mpi_print(format, NULL, 0, &ny, y); + *publenp += ny + 4; + + *pubbufp = malloc(*publenp); + + gcry_mpi_release(p); + gcry_mpi_release(q); + gcry_mpi_release(g); + gcry_mpi_release(y); +} + +static void test_otrl_privkey_generate_FILEp(void) +{ + int fd = mkstemp(filename); + f = fdopen(fd, "w+b"); + + unlink(filename); // The file will be removed on close + us = otrl_userstate_create(); + ok(otrl_privkey_generate_FILEp(us, f, "alice", "irc") + == gcry_error(GPG_ERR_NO_ERROR) && + us->privkey_root != NULL, + "key generated"); +} + +static void test_otrl_privkey_hash_to_human(void) +{ + int i; + char human[OTRL_PRIVKEY_FPRINT_HUMAN_LEN]; + unsigned char hash[20]; + + for(i = 0; i < 20; i++) { + hash[i] = 'A' + i; + } + + otrl_privkey_hash_to_human(human, hash); + ok(strcmp("41424344 45464748 494A4B4C 4D4E4F50 51525354", human) == 0, + "Hash to human ok"); +} + +static void test_otrl_privkey_fingerprint(void) +{ + char fingerprint[OTRL_PRIVKEY_FPRINT_HUMAN_LEN] = {0}; + char expected_fingerprint[OTRL_PRIVKEY_FPRINT_HUMAN_LEN] = {0}; + unsigned char hash[20] = {0}; + char *fp = otrl_privkey_fingerprint(us, fingerprint, "alice", "irc"); + const OtrlPrivKey *p = otrl_privkey_find(us, "alice", "irc"); + + gcry_md_hash_buffer(GCRY_MD_SHA1, hash, p->pubkey_data, p->pubkey_datalen); + otrl_privkey_hash_to_human(expected_fingerprint, hash); + + ok(fp == fingerprint && + memcmp(fingerprint, expected_fingerprint, + OTRL_PRIVKEY_FPRINT_HUMAN_LEN) == 0, + "Privkey fingerprint ok"); +} + +static void test_otrl_privkey_fingerprint_raw(void) +{ + unsigned char hash[20] = {0}; + unsigned char expected_hash[20] = {0}; + unsigned char *h = otrl_privkey_fingerprint_raw(us, hash, "alice", "irc"); + + const OtrlPrivKey *p = otrl_privkey_find(us, "alice", "irc"); + gcry_md_hash_buffer(GCRY_MD_SHA1, expected_hash, p->pubkey_data, + p->pubkey_datalen); + + ok(h == hash && memcmp(hash, expected_hash, 20) == 0, + "Raw privkey fingerprint ok"); +} + +static void test_otrl_privkey_find(void) +{ + OtrlPrivKey *p = NULL; + + ok(otrl_privkey_find(us, "bob", "xmpp") == NULL, + "Privkey not found"); + + ok(otrl_privkey_find(us, "alice", "xmpp") == NULL, + "Privkey not found because of wrong protocol"); + + ok(otrl_privkey_find(us, "bob", "irc") == NULL, + "Privkey not found because of wrong name"); + + p = otrl_privkey_find(us, "alice", "irc"); + ok(p != NULL && strcmp(p->accountname, "alice") == 0 && + strcmp(p->protocol, "irc") == 0, + "Privkey found"); +} + +static void test_otrl_privkey_sign(void) +{ + unsigned char *sig = NULL; + size_t siglen; + const char *data = "Some data to sign."; + size_t len = strlen(data); + OtrlPrivKey *p = otrl_privkey_find(us, "alice", "irc"); + + p->pubkey_type = OTRL_PUBKEY_TYPE_DSA + 1; + + ok(otrl_privkey_sign(&sig, &siglen, p, + (unsigned char *) data, len) == gcry_error(GPG_ERR_INV_VALUE), + "Wrong pubkey type detected"); + free(sig); + + p->pubkey_type = OTRL_PUBKEY_TYPE_DSA; + + ok(otrl_privkey_sign(&sig, &siglen, p, + (unsigned char *) data, len) == gcry_error(GPG_ERR_NO_ERROR), + "data signed"); + free(sig); + + ok(otrl_privkey_sign(&sig, &siglen, p, (unsigned char*)data, 0) == + gcry_error(GPG_ERR_NO_ERROR), "data with len 0 signed"); + free(sig); +} + +static void test_otrl_privkey_verify(void) +{ + unsigned char *sigbuf = NULL; + size_t siglen; + const char *data = "Some data to sign."; + OtrlPrivKey *privkey = otrl_privkey_find(us, "alice", "irc"); + gcry_mpi_t p,q,g,y; + gcry_sexp_t dsas, ps, qs, gs, ys; + gcry_sexp_t pubs = NULL; + gcry_error_t ret; + + /* Extract pubkey */ + dsas = gcry_sexp_find_token(privkey->privkey, "dsa", 0); + ps = gcry_sexp_find_token(dsas, "p", 0); + qs = gcry_sexp_find_token(dsas, "q", 0); + gs = gcry_sexp_find_token(dsas, "g", 0); + ys = gcry_sexp_find_token(dsas, "y", 0); + gcry_sexp_release(dsas); + p = gcry_sexp_nth_mpi(ps, 1, GCRYMPI_FMT_USG); + q = gcry_sexp_nth_mpi(qs, 1, GCRYMPI_FMT_USG); + g = gcry_sexp_nth_mpi(gs, 1, GCRYMPI_FMT_USG); + y = gcry_sexp_nth_mpi(ys, 1, GCRYMPI_FMT_USG); + gcry_sexp_release(ps); + gcry_sexp_release(qs); + gcry_sexp_release(gs); + gcry_sexp_release(ys); + + gcry_sexp_build(&pubs, NULL, "(public-key (dsa (p %m)(q %m)(g %m)(y %m)))", + p, q, g, y); + + gcry_mpi_release(p); + gcry_mpi_release(q); + gcry_mpi_release(g); + gcry_mpi_release(y); + + otrl_privkey_sign(&sigbuf, &siglen, privkey, (unsigned char*)data, + strlen(data)); + + ok(otrl_privkey_verify(sigbuf, siglen, OTRL_PUBKEY_TYPE_DSA, pubs, + (unsigned char *) data, strlen(data)) == 0, "Signature ok"); + + ret = otrl_privkey_verify(sigbuf, siglen, OTRL_PUBKEY_TYPE_DSA, pubs, + (unsigned char *) data + 1, strlen(data) - 1); + ok(gcry_error(ret) == gcry_error(GPG_ERR_BAD_SIGNATURE), + "Wrong signature"); + + free(sigbuf); +} + +int main(int argc, char **argv) +{ + OtrlPrivKey *p; + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + /* Set to quick random so we don't wait on /dev/random. */ + gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); + + test_otrl_privkey_generate_FILEp(); //This must be the first one + p = otrl_privkey_find(us, "alice", "irc"); + make_pubkey(&(p->pubkey_data), &(p->pubkey_datalen), p->privkey); + + test_otrl_privkey_hash_to_human(); + test_otrl_privkey_fingerprint(); + test_otrl_privkey_fingerprint_raw(); + test_otrl_privkey_sign(); + test_otrl_privkey_verify(); + test_otrl_privkey_find(); + + fclose(f); + otrl_userstate_free(us); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_proto.c b/comm/third_party/libotr/tests/unit/test_proto.c new file mode 100644 index 0000000000..5b522efc0b --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_proto.c @@ -0,0 +1,371 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include + +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 48 + +static ConnContext *new_context(const char *user, const char *accountname, + const char *protocol) +{ + ConnContext *context; + OtrlSMState *smstate; + + context = malloc(sizeof(ConnContext)); + + context->username = strdup(user); + context->accountname = strdup(accountname); + context->protocol = strdup(protocol); + + context->msgstate = OTRL_MSGSTATE_PLAINTEXT; + otrl_auth_new(context); + + smstate = malloc(sizeof(OtrlSMState)); + otrl_sm_state_new(smstate); + context->smstate = smstate; + + context->our_instance = 0; + context->their_instance = OTRL_INSTAG_MASTER; + context->fingerprint_root.fingerprint = NULL; + context->fingerprint_root.context = context; + context->fingerprint_root.next = NULL; + context->fingerprint_root.tous = NULL; + context->active_fingerprint = NULL; + memset(context->sessionid, 0, 20); + context->sessionid_len = 0; + context->protocol_version = 0; + context->otr_offer = OFFER_NOT; + context->app_data = NULL; + context->app_data_free = NULL; + context->context_priv = otrl_context_priv_new(); + context->next = NULL; + context->m_context = context; + context->recent_rcvd_child = NULL; + context->recent_sent_child = NULL; + context->recent_child = NULL; + + return context; +} + +static void test_otrl_proto_whitespace_bestversion(void) +{ + unsigned int ret; + const char *start, *end; + const char *test1 = OTRL_MESSAGE_TAG_BASE OTRL_MESSAGE_TAG_V2; + const char *test2 = OTRL_MESSAGE_TAG_BASE OTRL_MESSAGE_TAG_V3; + const char *test3 = OTRL_MESSAGE_TAG_BASE "foobar"; + + ret = otrl_proto_whitespace_bestversion(test1, &start, &end, + OTRL_POLICY_ALLOW_V2); + ok(ret == 2, "Best version whitespace v2"); + + ret = otrl_proto_whitespace_bestversion(test1, &start, &end, + OTRL_POLICY_ALLOW_V2 | OTRL_POLICY_ALLOW_V3); + ok(ret == 2, "Best version whitespace v2 dual policy"); + + ret = otrl_proto_whitespace_bestversion(test2, &start, &end, + OTRL_POLICY_ALLOW_V3); + ok(ret == 3, "Best version whitespace v3"); + + ret = otrl_proto_whitespace_bestversion(test2, &start, &end, + OTRL_POLICY_ALLOW_V2 | OTRL_POLICY_ALLOW_V3); + ok(ret == 3, "Best version whitespace v3 dual policy"); + + ret = otrl_proto_whitespace_bestversion(test3, &start, &end, + OTRL_POLICY_ALLOW_V2 | OTRL_POLICY_ALLOW_V3); + ok(ret == 0, "Best version whitespace invalid"); +} + +static void test_otrl_proto_query_bestversion(void) +{ + const char *query2 = "?OTRv2?\nalice has requested an " + "Off-the-Record " + "private conversation. However, you do not have a plugin " + "to support that.\nSee " + "https://otr.cypherpunks.ca/ for more information."; + + const char *query23 = "?OTRv23?\nalice has requested an " + "Off-the-Record " + "private conversation. However, you do not have a plugin " + "to support that.\nSee " + "https://otr.cypherpunks.ca/ for more information."; + + const char *query3 = "?OTRv3?\nalice has requested an " + "Off-the-Record " + "private conversation. However, you do not have a plugin " + "to support that.\nSee " + "https://otr.cypherpunks.ca/ for more information."; + + ok(otrl_proto_query_bestversion(query2, OTRL_POLICY_ALLOW_V2) == 2, + "The best from query2 is 2"); + ok(otrl_proto_query_bestversion(query3, OTRL_POLICY_ALLOW_V3) == 3, + "The best from query3 is 3"); + ok(otrl_proto_query_bestversion(query23, OTRL_POLICY_ALLOW_V2) == 2, + "The best from query23 is 2"); + ok(otrl_proto_query_bestversion(query23, OTRL_POLICY_ALLOW_V3) == 3, + "The best from query23 is 3"); +} + +static void test_otrl_proto_default_query_msg(void) +{ + const char *expected2 = "?OTRv2?\nalice has requested an " + "Off-the-Record " + "private conversation. However, you do not have a plugin " + "to support that.\nSee " + "https://otr.cypherpunks.ca/ for more information."; + + const char *expected23 = "?OTRv23?\nalice has requested an " + "Off-the-Record " + "private conversation. However, you do not have a plugin " + "to support that.\nSee " + "https://otr.cypherpunks.ca/ for more information."; + + const char *expected3 = "?OTRv3?\nalice has requested an " + "Off-the-Record " + "private conversation. However, you do not have a plugin " + "to support that.\nSee " + "https://otr.cypherpunks.ca/ for more information."; + + const char *msg2 = otrl_proto_default_query_msg("alice", + OTRL_POLICY_ALLOW_V2); + const char *msg23 = otrl_proto_default_query_msg("alice", + OTRL_POLICY_ALLOW_V2 | OTRL_POLICY_ALLOW_V3); + const char *msg3 = otrl_proto_default_query_msg("alice", + OTRL_POLICY_ALLOW_V3); + ok(strcmp(expected2, msg2) == 0, "OTRv2 default query message is valid"); + ok(strcmp(expected23, msg23) == 0, + "OTRv23 default query message is valid"); + ok(strcmp(expected3, msg3) == 0, "OTRv3 default query message is valid"); +} + +void test_otrl_init(void) +{ + extern unsigned int otrl_api_version; + + const unsigned int expected = rand(); + otrl_api_version = expected; + ok(otrl_init(OTRL_VERSION_MAJOR+1, 0, 0) == gcry_error(GPG_ERR_INV_VALUE), + "Too recent major version"); + ok(otrl_api_version == expected, "Api number unchanged"); + + ok(otrl_init(OTRL_VERSION_MAJOR-1, 0, 0) == gcry_error(GPG_ERR_INV_VALUE), + "Too old major version"); + ok(otrl_api_version == expected, "Api number unchanged"); + + ok(otrl_init(OTRL_VERSION_MAJOR, OTRL_VERSION_MINOR+1, 0) + == gcry_error(GPG_ERR_INV_VALUE), + "Too recent minor version"); + ok(otrl_api_version = expected, "Api number unchanged"); + + ok(otrl_init(OTRL_VERSION_MAJOR, OTRL_VERSION_MINOR?OTRL_VERSION_MINOR-1:0, + OTRL_VERSION_SUB) == gcry_error(GPG_ERR_NO_ERROR), + "Inferior minor version"); + ok(otrl_api_version = expected, "Api number unchanged"); + + otrl_api_version = 0; + + ok(otrl_init(OTRL_VERSION_MAJOR, OTRL_VERSION_MINOR, OTRL_VERSION_SUB) + == gcry_error(GPG_ERR_NO_ERROR), "Exact version"); + ok(otrl_api_version == ( + (OTRL_VERSION_MAJOR << 16) | + (OTRL_VERSION_MINOR << 8) | + (OTRL_VERSION_SUB) + ), "Api version set for exact version"); +} + +static void test_otrl_proto_message_type(void) +{ + OtrlMessageType ret; + + const char *test1 = "This is plaintext"; + ret = otrl_proto_message_type(test1); + ok(ret == OTRL_MSGTYPE_NOTOTR, "Proto message type is not OTR"); + + const char *test2 = OTRL_MESSAGE_TAG_BASE "This is plaintext"; + ret = otrl_proto_message_type(test2); + ok(ret == OTRL_MSGTYPE_TAGGEDPLAINTEXT, + "Proto message type is tagged plaintext"); + + const char *test3 = "?OTR:AAIC"; + ret = otrl_proto_message_type(test3); + ok(ret == OTRL_MSGTYPE_DH_COMMIT, "Proto message type v2 is dh commit"); + + const char *test4 = "?OTR:AAMC"; + ret = otrl_proto_message_type(test4); + ok(ret == OTRL_MSGTYPE_DH_COMMIT, "Proto message type v3 is dh commit"); + + const char *test5 = "?OTR:AAIK"; + ret = otrl_proto_message_type(test5); + ok(ret == OTRL_MSGTYPE_DH_KEY, "Proto message type v2 is DH key"); + + const char *test6 = "?OTR:AAMK"; + ret = otrl_proto_message_type(test6); + ok(ret == OTRL_MSGTYPE_DH_KEY, "Proto message type v3 is DH key"); + + const char *test7 = "?OTR:AAIR"; + ret = otrl_proto_message_type(test7); + ok(ret == OTRL_MSGTYPE_REVEALSIG, "Proto message type v2 is revealsig"); + + const char *test8 = "?OTR:AAMR"; + ret = otrl_proto_message_type(test8); + ok(ret == OTRL_MSGTYPE_REVEALSIG, "Proto message type v3 is revealsig"); + + const char *test9 = "?OTR:AAIS"; + ret = otrl_proto_message_type(test9); + ok(ret == OTRL_MSGTYPE_SIGNATURE, "Proto message type v2 is a signature"); + + const char *test10 = "?OTR:AAMS"; + ret = otrl_proto_message_type(test10); + ok(ret == OTRL_MSGTYPE_SIGNATURE, "Proto message type v3 is a signature"); + + const char *test11 = "?OTR:AAID"; + ret = otrl_proto_message_type(test11); + ok(ret == OTRL_MSGTYPE_DATA, "Proto message type v2 is a data msg"); + + const char *test12 = "?OTR:AAMD"; + ret = otrl_proto_message_type(test12); + ok(ret == OTRL_MSGTYPE_DATA, "Proto message type v3 is a data msg"); + + const char *test13 = "?OTR?"; + ret = otrl_proto_message_type(test13); + ok(ret == OTRL_MSGTYPE_QUERY, "Proto message type is a query"); + + const char *test14 = "?OTR?v"; + ret = otrl_proto_message_type(test14); + ok(ret == OTRL_MSGTYPE_QUERY, "Proto message type is a query"); + + const char *test15 = "?OTR Error:"; + ret = otrl_proto_message_type(test15); + ok(ret == OTRL_MSGTYPE_ERROR, "Proto message type is an error"); + + const char *test16 = "?OTR: Please verify me"; + ret = otrl_proto_message_type(test16); + ok(ret == OTRL_MSGTYPE_UNKNOWN, "Proto message type is unknown"); + + const char *test17 = "?OTR:AAMA"; + ret = otrl_proto_message_type(test17); + ok(ret == OTRL_MSGTYPE_UNKNOWN, "Proto message type is unknown"); +} + +static void test_otrl_proto_message_version(void) +{ + int ret; + + const char *test1 = "?OTR:AAI"; + ret = otrl_proto_message_version(test1); + ok(ret == 2, "Protocol message version is 2"); + + const char *test2 = "?OTR:AAM"; + ret = otrl_proto_message_version(test2); + ok(ret == 3, "Protocol message version is 3"); + + const char *test3 = "?OTR:BLAH"; + ret = otrl_proto_message_version(test3); + ok(ret == 0, "Protocol message version is unknown"); +} + +static void test_otrl_proto_instance(void) +{ + /* Canary that shouldn't get modified on error. */ + unsigned int inst_from = 42, inst_to = 42; + gcry_error_t ret; + + /* + * Instance tags only supported in protocol v3 (AAM in b64). The msg type + * here is "A" which does not represent a valid one but we don't care + * followed by the Sender Instance set to 1 and Receiver Instance set to 2. + */ + const char *test1 = "?OTR:AAMAAAAAAQAAAAI=="; + ret = otrl_proto_instance(test1, &inst_from, &inst_to); + ok(ret == gcry_error(GPG_ERR_NO_ERROR) + && inst_from == 1 + && inst_to == 2, + "Proto instance find for v3"); + + /* Reset canary. */ + inst_from = inst_to = 42; + + /* Len is not enough here. */ + const char *test2 = "?OTR:AAMAAA="; + ret = otrl_proto_instance(test2, &inst_from, &inst_to); + ok(ret == gcry_error(GPG_ERR_INV_VALUE) + && inst_from == 42 + && inst_to == 42, "Proto instance failed for v3"); + + /* Reset canary. */ + inst_from = inst_to = 42; + + /* Message from protocol v2. */ + const char *test3 = "?OTR:AAIAAAAAAQAAAAI=="; + ret = otrl_proto_instance(test3, &inst_from, &inst_to); + ok(ret == gcry_error(GPG_ERR_INV_VALUE) + && inst_from == 42 + && inst_to == 42, "Proto instance failed for v2"); +} + +static void test_otrl_version(void) +{ + ok(strcmp(otrl_version(), OTRL_VERSION) == 0, "Otrl version OK"); +} + +static void test_otrl_proto_create_data(void) +{ + char *encmessagep = NULL, *msg = "HELO"; + unsigned char flags = 12; + unsigned char *extrakey = NULL; + OtrlTLV *tlvs = NULL; + ConnContext *context = + new_context("Alice", "Alice's account", "Secret protocol"); + + context->msgstate = OTRL_MSGSTATE_PLAINTEXT; + ok(otrl_proto_create_data(&encmessagep, context, msg, tlvs, flags, + extrakey) == gcry_error(GPG_ERR_CONFLICT), + "Conflict detected for msgstate plaintext"); + + context->msgstate = OTRL_MSGSTATE_ENCRYPTED; + context->context_priv->their_keyid = 0; + ok(otrl_proto_create_data(&encmessagep, context, msg, tlvs, flags, + extrakey) == gcry_error(GPG_ERR_CONFLICT), + "Conflict detected for msgstate encrypted"); +} + +int main(int argc, char **argv) +{ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + test_otrl_proto_default_query_msg(); + test_otrl_proto_query_bestversion(); + test_otrl_init(); + test_otrl_proto_whitespace_bestversion(); + test_otrl_proto_message_type(); + test_otrl_proto_message_version(); + test_otrl_proto_instance(); + test_otrl_version(); + test_otrl_proto_create_data(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_sm.c b/comm/third_party/libotr/tests/unit/test_sm.c new file mode 100644 index 0000000000..8f4162ab1a --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_sm.c @@ -0,0 +1,343 @@ +/* + * Copyright (C) 2014 - David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include +#include + +#include +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 23 + +/* Copied from sm.c */ +static const int SM_MOD_LEN_BITS = 1536; +static const char *SM_GENERATOR_S = "0x02"; +static gcry_mpi_t SM_GENERATOR = NULL; + +static const int SM_MSG1_LEN = 6; +static const int SM_MSG2_LEN = 11; +static const int SM_MSG3_LEN = 8; +static const int SM_MSG4_LEN = 3; + +/* Alice and Bob SM state for the SMP tests. */ +static OtrlSMState *astate; +static OtrlSMState *bstate; +static const char *secret = "truie"; +static unsigned char *alice_output; +static int alice_output_len; +static unsigned char *bob_output; +static int bob_output_len; + +/* Stub. */ +void otrl_sm_msg1_init(gcry_mpi_t **msg1); +void otrl_sm_msg2_init(gcry_mpi_t **msg2); +void otrl_sm_msg3_init(gcry_mpi_t **msg3); +void otrl_sm_msg4_init(gcry_mpi_t **msg4); +void otrl_sm_msg_free(gcry_mpi_t **message, int msglen); + +static OtrlSMState *alloc_sm_state(void) +{ + OtrlSMState *smst = malloc(sizeof(*smst)); + ok(smst, "SM State allocated"); + + return smst; +} + +static void test_sm_state_new(void) +{ + OtrlSMState *smst; + + smst = alloc_sm_state(); + + otrl_sm_state_new(smst); + ok(!smst->secret && + !smst->x2 && + !smst->x3 && + !smst->g1 && + !smst->g2 && + !smst->g3 && + !smst->g3o && + !smst->p && + !smst->q && + !smst->pab && + !smst->qab && + smst->nextExpected == OTRL_SMP_EXPECT1 && + smst->received_question == 0 && + smst->sm_prog_state == OTRL_SMP_PROG_OK, + "SM state new"); + + otrl_sm_state_free(smst); + free(smst); +} + +static void test_sm_state_init(void) +{ + OtrlSMState *smst; + + smst = alloc_sm_state(); + + otrl_sm_state_new(smst); + otrl_sm_state_init(smst); + ok(!gcry_mpi_cmp(smst->secret, gcry_mpi_snew(SM_MOD_LEN_BITS)) && + !smst->x2 && + !smst->x3 && + !gcry_mpi_cmp(smst->g1, gcry_mpi_copy(SM_GENERATOR)) && + !gcry_mpi_cmp(smst->g2, gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(smst->g3, gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(smst->g3o, gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(smst->p, gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(smst->q, gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(smst->pab, gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(smst->qab, gcry_mpi_new(SM_MOD_LEN_BITS)) && + smst->nextExpected == OTRL_SMP_EXPECT1 && + smst->received_question == 0 && + smst->sm_prog_state == OTRL_SMP_PROG_OK, + "SM state init"); + + otrl_sm_state_free(smst); + free(smst); +} + +static void test_sm_msg1_init(void) +{ + gcry_mpi_t *msg; + + otrl_sm_msg1_init(&msg); + ok(msg && + !gcry_mpi_cmp(msg[0], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[1] && + !gcry_mpi_cmp(msg[2], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[3], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[4] && + !gcry_mpi_cmp(msg[5], gcry_mpi_new(SM_MOD_LEN_BITS)), + "SM msg1 initialized"); + otrl_sm_msg_free(&msg, SM_MSG1_LEN); + /* Test once here. */ + ok(!msg, "SM msg1 freed"); +} + +static void test_sm_msg2_init(void) +{ + gcry_mpi_t *msg; + + otrl_sm_msg2_init(&msg); + ok(msg && + !gcry_mpi_cmp(msg[0], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[1] && + !gcry_mpi_cmp(msg[2], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[3], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[4] && + !gcry_mpi_cmp(msg[5], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[6], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[7], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[8] && + !gcry_mpi_cmp(msg[9], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[10], gcry_mpi_new(SM_MOD_LEN_BITS)), + "SM msg2 initialized"); + otrl_sm_msg_free(&msg, SM_MSG2_LEN); +} + +static void test_sm_msg3_init(void) +{ + gcry_mpi_t *msg; + + otrl_sm_msg3_init(&msg); + ok(msg && + !gcry_mpi_cmp(msg[0], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[1], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[2] && + !gcry_mpi_cmp(msg[3], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[4], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !gcry_mpi_cmp(msg[5], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[6] && + !gcry_mpi_cmp(msg[7], gcry_mpi_new(SM_MOD_LEN_BITS)), + "SM msg3 initialized"); + otrl_sm_msg_free(&msg, SM_MSG3_LEN); +} + +static void test_sm_msg4_init(void) +{ + gcry_mpi_t *msg; + + otrl_sm_msg4_init(&msg); + ok(msg && + !gcry_mpi_cmp(msg[0], gcry_mpi_new(SM_MOD_LEN_BITS)) && + !msg[1] && + !gcry_mpi_cmp(msg[2], gcry_mpi_new(SM_MOD_LEN_BITS)), + "SM msg4 initialized"); + otrl_sm_msg_free(&msg, SM_MSG4_LEN); +} + +static void test_sm_step1(void) +{ + gcry_error_t err; + unsigned char hash_secret[SM_DIGEST_SIZE]; + + astate = alloc_sm_state(); + otrl_sm_state_new(astate); + otrl_sm_state_init(astate); + + gcry_md_hash_buffer(SM_HASH_ALGORITHM, hash_secret, secret, + strlen(secret)); + + err = otrl_sm_step1(astate, hash_secret, sizeof(hash_secret), + &alice_output, &alice_output_len); + ok(err == GPG_ERR_NO_ERROR, "SMP step1 success"); + + gcry_mpi_t secret_mpi; + gcry_mpi_scan(&secret_mpi, GCRYMPI_FMT_USG, hash_secret, + sizeof(hash_secret), NULL); + ok(!gcry_mpi_cmp(astate->secret, secret_mpi) && + astate->received_question == 0 && + astate->x2 && + astate->x3 && + astate->sm_prog_state == OTRL_SMP_PROG_OK && + alice_output && alice_output_len > 0, + "SMP step 1 validated"); + gcry_mpi_release(secret_mpi); +} + +static void test_sm_step2a(void) +{ + gcry_error_t err; + + bstate = alloc_sm_state(); + otrl_sm_state_new(bstate); + otrl_sm_state_init(bstate); + + err = otrl_sm_step2a(bstate, alice_output, alice_output_len, 1); + ok(err == GPG_ERR_NO_ERROR, "SMP step2a success"); + + ok(bstate->received_question == 1 && + bstate->sm_prog_state == OTRL_SMP_PROG_OK && + bstate->g3o && + bstate->x2 && + bstate->x3, + "SMP step2a validate"); +} + +static void test_sm_step2b(void) +{ + gcry_error_t err; + unsigned char hash_secret[SM_DIGEST_SIZE]; + + gcry_md_hash_buffer(SM_HASH_ALGORITHM, hash_secret, secret, + strlen(secret)); + + err = otrl_sm_step2b(bstate, hash_secret, sizeof(hash_secret), &bob_output, + &bob_output_len); + ok(err == GPG_ERR_NO_ERROR, "SMP step2b success"); + + /* Generate expected data. */ + gcry_mpi_t secret_mpi; + gcry_mpi_scan(&secret_mpi, GCRYMPI_FMT_USG, hash_secret, + sizeof(hash_secret), NULL); + ok(bob_output && bob_output_len > 0 && + !gcry_mpi_cmp(bstate->secret, secret_mpi) && + bstate->p && + bstate->q, + "SMP step2b validate"); + gcry_mpi_release(secret_mpi); +} + +static void test_sm_step3(void) +{ + gcry_error_t err; + + free(alice_output); + + err = otrl_sm_step3(astate, bob_output, bob_output_len, &alice_output, + &alice_output_len); + ok(err == GPG_ERR_NO_ERROR, "SMP step3 success"); + + ok(alice_output && alice_output_len > 0 && + astate->sm_prog_state == OTRL_SMP_PROG_OK && + astate->g3o && + astate->g2 && + astate->g3 && + astate->p && + astate->q && + astate->qab && + astate->pab, + "SMP step3 validate"); +} + +static void test_sm_step4(void) +{ + gcry_error_t err; + + free(bob_output); + + err = otrl_sm_step4(bstate, alice_output, alice_output_len, &bob_output, + &bob_output_len); + ok(err == gcry_error(GPG_ERR_NO_ERROR), "SMP step4 success"); + + ok(bob_output && bob_output_len > 0 && + bstate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED && + bstate->pab && + bstate->qab, + "SMP step4 validate"); +} + +static void test_sm_step5(void) +{ + gcry_error_t err; + + err = otrl_sm_step5(astate, bob_output, bob_output_len); + ok(err == gcry_error(GPG_ERR_NO_ERROR), "SMP step5 success"); + + ok(astate && astate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED, + "SMP step5 validate"); +} + +int main(int argc, char **argv) +{ + /* Libtap call for the number of tests planned. */ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + /* Initialize sm subsystem. We can't really unit test that because every + * value that is being initialized is static to sm.c. */ + otrl_sm_init(); + + /* Init variables we need for testing. */ + gcry_mpi_scan(&SM_GENERATOR, GCRYMPI_FMT_HEX, + (const unsigned char *)SM_GENERATOR_S, 0, NULL); + + test_sm_state_new(); + test_sm_state_init(); + test_sm_msg1_init(); + test_sm_msg2_init(); + test_sm_msg3_init(); + test_sm_msg4_init(); + + test_sm_step1(); + test_sm_step2a(); + test_sm_step2b(); + test_sm_step3(); + test_sm_step4(); + test_sm_step5(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_tlv.c b/comm/third_party/libotr/tests/unit/test_tlv.c new file mode 100644 index 0000000000..d05f9b8323 --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_tlv.c @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include +#include + +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 11 + +static void test_otrl_tlv_new() +{ + const unsigned short type = OTRL_TLV_SMP1Q; + const char *data = "This is some test data"; + const unsigned short len = strlen(data); + + OtrlTLV* tlv = otrl_tlv_new(type, len, (unsigned char*)data); + ok(tlv->type == type && + tlv->len == len && + memcmp(tlv->data, data, len) == 0 && + tlv->data[len] == '\0' && + tlv->next == NULL, + "TLV created with success"); + otrl_tlv_free(tlv); +} + +static void test_otrl_tlv_parse() +{ + const unsigned char serialized1[] = + {'\x01', '\x02', '\x00', '\x03', '1', '2', '3'}; + const unsigned char serialized2[] = {'\x02', '\x04', '\x00', '\x00'}; + const unsigned char serialized3[] = + {'\x04', '\x02', '\x00', '\x03', '1', '2', '3', + '\x02', '\x02', '\xff', '\xff', '1', '3', '3', '7'}; + const unsigned char serialized4[] = + {'\x04', '\x02', '\x00', '\x03', '1', '2', '3', + '\x02', '\x02', '\x00', '\x04', '1', '3', '3', '7'}; + + OtrlTLV *tlv1 = otrl_tlv_parse(serialized1, sizeof(serialized1)); + OtrlTLV *tlv2 = otrl_tlv_parse(serialized2, sizeof(serialized2)); + OtrlTLV *tlv3 = otrl_tlv_parse(serialized3, sizeof(serialized3)); + OtrlTLV *tlv4 = otrl_tlv_parse(serialized4, sizeof(serialized4)); + + ok(tlv1->type == 258 && + tlv1->len == 3 && + tlv1->next == NULL && + memcmp(tlv1->data, "123", tlv1->len) == 0 && + tlv1->data[tlv1->len] == 0, + "Single-TLV chain constructed with success"); + otrl_tlv_free(tlv1); + + ok(tlv2->type == 516 && + tlv2->len == 0 && + tlv2->next == NULL, + "tlv2 chain with no data constructed with success"); + otrl_tlv_free(tlv2); + + ok(tlv3->type == 1026 && + tlv3->len == 3 && + memcmp(tlv3->data, "123", tlv3->len) == 0 && + tlv3->data[tlv3->len] == 0 && + tlv3->next == NULL, + "tlv3 chain with overflow constructed with success"); + otrl_tlv_free(tlv3); + + ok(tlv4->type == 1026 && + tlv4->len == 3 && + memcmp(tlv4->data, "123", tlv4->len) == 0 && + tlv4->data[tlv4->len] == 0 && + tlv4->next != NULL, + "First part of the 2-part tlv chain build"); + + ok(tlv4->next != NULL && + tlv4->next->type == 514 && + tlv4->next->len == 4 && + memcmp(tlv4->next->data, "1337", tlv4->next->len) == 0 && + tlv4->next->data[tlv4->next->len] == 0 && + tlv4->next->next == NULL, + "Second part of the 2-part tlv chain build"); + otrl_tlv_free(tlv4); +} + +static void test_otrl_tlv_seriallen() +{ + const unsigned char serialized[] = + {'\x04', '\x02', '\x00', '\x03', '1', '2', '3', + '\x02', '\x02', '\x00', '\x04', '1', '3', '3', '7'}; + OtrlTLV* tlv = otrl_tlv_parse(serialized, sizeof(serialized)); + ok(otrl_tlv_seriallen(tlv) == 4 + 3 + 4 + 4, + "Size correctly guessed"); + otrl_tlv_free(tlv); +} + +static void test_otrl_tlv_serialize() +{ + const unsigned char serialized[] = + {'\x04', '\x02', '\x00', '\x03', '1', '2', '3', + '\x02', '\x02', '\x00', '\x04', '1', '3', '3', '7'}; + OtrlTLV *tlv = otrl_tlv_parse(serialized, sizeof(serialized)); + unsigned char *buf = malloc(otrl_tlv_seriallen(tlv)); + otrl_tlv_serialize(buf, tlv); + ok(memcmp(serialized, buf, sizeof(serialized)) == 0, + "tlv correctly serialized"); + free(tlv); + + tlv = otrl_tlv_parse(buf, sizeof(serialized)); + otrl_tlv_serialize(buf, tlv); + ok(memcmp(serialized, buf, sizeof(serialized)) == 0, + "tlv correctly unserialized and serialized again"); + otrl_tlv_free(tlv); +} + +static void test_otrl_tlv_find() +{ + const unsigned char serialized[] = + {'\x04', '\x02', '\x00', '\x03', '1', '2', '3', + '\x02', '\x02', '\x00', '\x04', '1', '3', '3', '7', + '\x01', '\x03', '\x0', '\x01', 'A', + '\x01', '\x02', '\x0', '\x01', 'B'}; + OtrlTLV *tlv = otrl_tlv_parse(serialized, sizeof(serialized)); + OtrlTLV *result = otrl_tlv_find(tlv, (1<<8) + 3); + ok(result == tlv->next->next, "TLV found"); + + result = otrl_tlv_find(tlv, 7); + ok(result == NULL, "Unexistent TLV not found"); + + otrl_tlv_free(tlv); +} + +int main(int argc, char **argv) +{ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + test_otrl_tlv_new(); + test_otrl_tlv_parse(); + test_otrl_tlv_seriallen(); + test_otrl_tlv_serialize(); + test_otrl_tlv_find(); + + return 0; +} diff --git a/comm/third_party/libotr/tests/unit/test_userstate.c b/comm/third_party/libotr/tests/unit/test_userstate.c new file mode 100644 index 0000000000..62e996e68c --- /dev/null +++ b/comm/third_party/libotr/tests/unit/test_userstate.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2014 - Julien Voisin + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include +#include + +#include + +GCRY_THREAD_OPTION_PTHREAD_IMPL; + +#define NUM_TESTS 1 + +static void test_otrl_userstate_create() +{ + OtrlUserState us = otrl_userstate_create(); + ok(us->context_root == NULL && + us->privkey_root == NULL && + us->instag_root == NULL && + us->pending_root == NULL && + us->timer_running == 0, + "OtrlUserState ok"); + otrl_userstate_free(us); +} + +int main(int argc, char** argv) +{ + plan_tests(NUM_TESTS); + + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + OTRL_INIT; + + test_otrl_userstate_create(); + + return 0; +} -- cgit v1.2.3