build+ci: Synchronize build and CI scripts with ndn-cxx/NFD

This commit also fixes a minor problem causing compilation warnings.

Change-Id: I0c04d58fe9df24f181947b9fde1346283402fc7b
diff --git a/.jenkins.d/00-deps.sh b/.jenkins.d/00-deps.sh
index 7f312a3..4a4db28 100755
--- a/.jenkins.d/00-deps.sh
+++ b/.jenkins.d/00-deps.sh
@@ -1,27 +1,21 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
 JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
 source "$JDIR"/util.sh
 
-[[ -n $NODE_LABELS ]] || exit 0
+set -x
 
 if has OSX $NODE_LABELS; then
     brew update
     brew upgrade
-    brew install boost pkg-config cryptopp
+    brew install boost pkg-config cryptopp openssl
     brew cleanup
 fi
 
 if has Ubuntu $NODE_LABELS; then
-    sudo apt-get update -qq -y
-    sudo apt-get -qq -y install build-essential pkg-config
-    sudo apt-get -qq -y install libcrypto++-dev
-
-    if has Ubuntu-12.04 $NODE_LABELS; then
-        sudo apt-get install -qq -y libboost1.48-all-dev
-    else
-        sudo apt-get install -qq -y libboost-all-dev
-    fi
+    sudo apt-get -qq update
+    sudo apt-get -qq install build-essential pkg-config libboost-all-dev \
+                             libcrypto++-dev libsqlite3-dev libssl-dev \
+                             libpcap-dev
 fi
diff --git a/.jenkins.d/10-ndn-cxx.sh b/.jenkins.d/10-ndn-cxx.sh
index d22bfb6..e5f7585 100755
--- a/.jenkins.d/10-ndn-cxx.sh
+++ b/.jenkins.d/10-ndn-cxx.sh
@@ -1,11 +1,12 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
 JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
 source "$JDIR"/util.sh
 
-pushd /tmp >/dev/null
+set -x
+
+pushd "${CACHE_DIR:-/tmp}" >/dev/null
 
 INSTALLED_VERSION=$((cd ndn-cxx && git rev-parse HEAD) 2>/dev/null || echo NONE)
 
@@ -28,9 +29,9 @@
 
 pushd ndn-cxx >/dev/null
 
-./waf configure -j1 --color=yes --enable-shared --disable-static --without-osx-keychain
-./waf -j1 --color=yes
-sudo ./waf install -j1 --color=yes
+./waf -j1 --color=yes configure --enable-shared --disable-static --without-osx-keychain
+./waf -j1 --color=yes build
+sudo ./waf -j1 --color=yes install
 
 popd >/dev/null
 popd >/dev/null
diff --git a/.jenkins.d/20-build.sh b/.jenkins.d/20-build.sh
index d25d947..de9d590 100755
--- a/.jenkins.d/20-build.sh
+++ b/.jenkins.d/20-build.sh
@@ -1,37 +1,47 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
 JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
 source "$JDIR"/util.sh
 
-sudo rm -Rf /usr/local/include/ndn-group-encrypt
-sudo rm -f /usr/local/lib/libndn-group-encrypt*
-sudo rm -f /usr/local/lib/pkgconfig/ndn-group-encrypt*
+set -x
+
+git submodule init
+git submodule sync
+git submodule update
 
 # Cleanup
 sudo ./waf -j1 --color=yes distclean
 
-# Configure/build in release mode
-./waf -j1 --color=yes configure
+if [[ "$JOB_NAME" != *"limited-build" ]]; then
+  # Configure/build in optimized mode with tests
+  ./waf -j1 --color=yes configure --with-tests
+  ./waf -j1 --color=yes build
+
+  # Cleanup
+  sudo ./waf -j1 --color=yes distclean
+
+  # Configure/build in optimized mode without tests
+  ./waf -j1 --color=yes configure
+  ./waf -j1 --color=yes build
+
+  # Cleanup
+  sudo ./waf -j1 --color=yes distclean
+fi
+
+# Configure/build in debug mode with tests and without precompiled headers
+if [[ "$JOB_NAME" == *"code-coverage" ]]; then
+    COVERAGE="--with-coverage"
+elif ! has OSX-10.9 $NODE_LABELS && ! has OSX-10.11 $NODE_LABELS; then
+    ASAN="--with-sanitizer=address"
+fi
+./waf -j1 --color=yes configure --debug --with-tests $COVERAGE $ASAN
 ./waf -j1 --color=yes build
 
-# Cleanup
-sudo ./waf -j1 --color=yes distclean
-
-# Configure/build in debug mode
-./waf -j1 --color=yes configure --debug
-./waf -j1 --color=yes build
-
-# Cleanup
-sudo ./waf -j1 --color=yes distclean
-
-# Configure/build in optimized mode with tests
-./waf -j1 --color=yes configure --with-tests
-./waf -j1 --color=yes build
+# (tests will be run against debug version)
 
 # Install
-sudo ./waf install -j1 --color=yes
+sudo ./waf -j1 --color=yes install
 
 if has Linux $NODE_LABELS; then
     sudo ldconfig
diff --git a/.jenkins.d/30-tests.sh b/.jenkins.d/30-tests.sh
index c04b121..01bf070 100755
--- a/.jenkins.d/30-tests.sh
+++ b/.jenkins.d/30-tests.sh
@@ -1,13 +1,45 @@
 #!/usr/bin/env bash
-set -x
 set -e
 
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+set -x
+
 # Prepare environment
-sudo rm -Rf ~/.ndn
+rm -Rf ~/.ndn
+
+if has OSX $NODE_LABELS; then
+    security unlock-keychain -p named-data
+fi
+
+ndnsec-keygen "/tmp/jenkins/$NODE_NAME" | ndnsec-install-cert -
+
+BOOST_VERSION=$(python -c "import sys; sys.path.append('build/c4che'); import _cache; print(_cache.BOOST_VERSION_NUMBER);")
+
+ut_log_args() {
+    if (( BOOST_VERSION >= 106200 )); then
+        echo --logger=HRF,test_suite,stdout:XML,all,build/xunit-${1:-report}.xml
+    else
+        if [[ -n $XUNIT ]]; then
+            echo --log_level=all $( (( BOOST_VERSION >= 106000 )) && echo -- ) \
+                 --log_format2=XML --log_sink2=build/xunit-${1:-report}.xml
+        else
+            echo --log_level=test_suite
+        fi
+    fi
+}
+
+ASAN_OPTIONS="color=always"
+ASAN_OPTIONS+=":detect_leaks=false"
+ASAN_OPTIONS+=":detect_stack_use_after_return=true"
+ASAN_OPTIONS+=":check_initialization_order=true"
+ASAN_OPTIONS+=":strict_init_order=true"
+ASAN_OPTIONS+=":detect_invalid_pointer_pairs=1"
+ASAN_OPTIONS+=":detect_container_overflow=false"
+ASAN_OPTIONS+=":strict_string_checks=true"
+ASAN_OPTIONS+=":strip_path_prefix=${PWD}/"
+export ASAN_OPTIONS
 
 # Run unit tests
-if [[ -n "$XUNIT" ]]; then
-    ./build/unit-tests --log_format=XML --log_sink=build/xunit-report.xml --log_level=all --report_level=no
-else
-    ./build/unit-tests -l test_suite
-fi
+./build/unit-tests $(ut_log_args)
diff --git a/.jenkins.d/util.sh b/.jenkins.d/util.sh
index 81c8931..a89bc27 100644
--- a/.jenkins.d/util.sh
+++ b/.jenkins.d/util.sh
@@ -1,9 +1,18 @@
 has() {
+    local saved_xtrace
+    [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x
+    set +x
+
     local p=$1
     shift
-    local x
-    for x in "$@"; do
-        [[ "${x}" == "${p}" ]] && return 0
+    local i ret=1
+    for i in "$@"; do
+        if [[ "${i}" == "${p}" ]]; then
+            ret=0
+            break
+        fi
     done
-    return 1
+
+    set ${saved_xtrace}
+    return ${ret}
 }
diff --git a/.travis.yml b/.travis.yml
index 4614546..db62c24 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,16 +1,27 @@
-# For Ubuntu platform only
-
-sudo: true
-language: cpp
-os:
-  - linux
-compiler:
-  - gcc
+sudo: required
+language: generic
+env:
+  global:
+    - JOB_NAME=limited-build
+matrix:
+  include:
+    - os: linux
+      dist: trusty
+      env:
+        - CXX=g++
+        - NODE_LABELS="Linux Ubuntu Ubuntu-14.04"
+    - os: linux
+      dist: trusty
+      env:
+        - CXX=clang++
+        - NODE_LABELS="Linux Ubuntu Ubuntu-14.04"
+    - os: osx
+      env:
+        - CXX=clang++
+        - NODE_LABELS="OSX OSX-10.9"
 notifications:
   email:
     on_success: always
     on_failure: always
-env:
-  - NODE_LABELS="Linux Ubuntu Ubuntu-12.04"
 script:
   - ./.jenkins
diff --git a/.waf-tools/default-compiler-flags.py b/.waf-tools/default-compiler-flags.py
index 4f53823..2999e8f 100644
--- a/.waf-tools/default-compiler-flags.py
+++ b/.waf-tools/default-compiler-flags.py
@@ -14,22 +14,21 @@
         flags = ClangFlags()
     else:
         flags = CompilerFlags()
-        Logs.warn('The code has not been yet tested with %s compiler' % cxx)
+        Logs.warn('The code has not yet been tested with %s compiler' % cxx)
 
     areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
 
-    # General flags will alway be applied (e.g., selecting C++11 mode)
+    # General flags are always applied (e.g., selecting C++11 mode)
     generalFlags = flags.getGeneralFlags(conf)
     conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
     conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
     conf.env.DEFINES += generalFlags['DEFINES']
 
-    # Debug or optimization CXXFLAGS and LINKFLAGS  will be applied only if the
+    # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
     # corresponding environment variables are not set.
-    # DEFINES will be always applied
+    # DEFINES are always applied.
     if conf.options.debug:
         extraFlags = flags.getDebugFlags(conf)
-
         if areCustomCxxflagsPresent:
             missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
             if len(missingFlags) > 0:
@@ -41,7 +40,7 @@
 
     if not areCustomCxxflagsPresent:
         conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
-        conf.add_supported_cxxflags(extraFlags['LINKFLAGS'])
+        conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
 
     conf.env.DEFINES += extraFlags['DEFINES']
 
@@ -61,7 +60,7 @@
             supportedFlags += [flag]
 
     self.end_msg(' '.join(supportedFlags))
-    self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS
+    self.env.prepend_value('CXXFLAGS', supportedFlags)
 
 @Configure.conf
 def add_supported_linkflags(self, linkflags):
@@ -79,77 +78,104 @@
             supportedFlags += [flag]
 
     self.end_msg(' '.join(supportedFlags))
-    self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS
+    self.env.prepend_value('LINKFLAGS', supportedFlags)
 
 
 class CompilerFlags(object):
     def getGeneralFlags(self, conf):
-        """Get dict {'CXXFLAGS':[...], LINKFLAGS:[...], DEFINES:[...]} that are always needed"""
+        """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
         return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
 
     def getDebugFlags(self, conf):
-        """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in debug mode"""
+        """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
         return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
 
     def getOptimizedFlags(self, conf):
-        """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in optimized mode"""
+        """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
         return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
 
 class GccBasicFlags(CompilerFlags):
-    """This class defines base flags that work for gcc and clang compiler"""
+    """
+    This class defines basic flags that work for both gcc and clang compilers
+    """
     def getDebugFlags(self, conf):
         flags = super(GccBasicFlags, self).getDebugFlags(conf)
-        flags['CXXFLAGS'] += ['-pedantic', '-Wall',
-                              '-O0',
+        flags['CXXFLAGS'] += ['-O0',
                               '-g3',
+                              '-pedantic',
+                              '-Wall',
+                              '-Wextra',
                               '-Werror',
+                              '-Wno-unused-parameter',
                               '-Wno-error=maybe-uninitialized', # Bug #1615
-                             ]
+                              '-Wno-error=deprecated-declarations', # Bug #3795
+                              ]
         return flags
 
     def getOptimizedFlags(self, conf):
         flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
-        flags['CXXFLAGS'] += ['-pedantic', '-Wall', '-O2', '-g']
+        flags['CXXFLAGS'] += ['-O2',
+                              '-g',
+                              '-pedantic',
+                              '-Wall',
+                              '-Wextra',
+                              '-Wno-unused-parameter',
+                              ]
         return flags
 
 class GccFlags(GccBasicFlags):
     def getGeneralFlags(self, conf):
         flags = super(GccFlags, self).getGeneralFlags(conf)
         version = tuple(int(i) for i in conf.env['CC_VERSION'])
-        if version < (4, 6, 0):
+        if version < (4, 8, 2):
             conf.fatal('The version of gcc you are using (%s) is too old.\n' %
                        '.'.join(conf.env['CC_VERSION']) +
-                       'The minimum supported gcc version is 4.6.0.')
-        elif version < (4, 7, 0):
-            flags['CXXFLAGS'] += ['-std=c++0x']
+                       'The minimum supported gcc version is 4.8.2.')
         else:
             flags['CXXFLAGS'] += ['-std=c++11']
-        if version < (4, 8, 0):
-            flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499
         return flags
 
     def getDebugFlags(self, conf):
         flags = super(GccFlags, self).getDebugFlags(conf)
+        version = tuple(int(i) for i in conf.env['CC_VERSION'])
+        if version < (5, 1, 0):
+            flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
         flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
                               '-fdiagnostics-color', # gcc >= 4.9
-                             ]
+                              ]
+        return flags
+
+    def getOptimizedFlags(self, conf):
+        flags = super(GccFlags, self).getOptimizedFlags(conf)
+        version = tuple(int(i) for i in conf.env['CC_VERSION'])
+        if version < (5, 1, 0):
+            flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
+        flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
         return flags
 
 class ClangFlags(GccBasicFlags):
     def getGeneralFlags(self, conf):
         flags = super(ClangFlags, self).getGeneralFlags(conf)
-        flags['CXXFLAGS'] += ['-std=c++11',
-                              '-Wno-error=unneeded-internal-declaration', # Bug #1588
-                              '-Wno-error=deprecated-register',
-                              '-Wno-error=unused-local-typedef', # Bug #2657
-                              '-Wno-error=keyword-macro',
-                              ]
-        if Utils.unversioned_sys_platform() == "darwin":
+        flags['CXXFLAGS'] += ['-std=c++11']
+        if Utils.unversioned_sys_platform() == 'darwin':
             flags['CXXFLAGS'] += ['-stdlib=libc++']
             flags['LINKFLAGS'] += ['-stdlib=libc++']
         return flags
 
     def getDebugFlags(self, conf):
         flags = super(ClangFlags, self).getDebugFlags(conf)
-        flags['CXXFLAGS'] += ['-fcolor-diagnostics']
+        flags['CXXFLAGS'] += ['-fcolor-diagnostics',
+                              '-Wno-unused-local-typedef', # Bugs #2657 and #3209
+                              '-Wno-error=unneeded-internal-declaration', # Bug #1588
+                              '-Wno-error=deprecated-register',
+                              '-Wno-error=keyword-macro', # Bug #3235
+                              '-Wno-error=infinite-recursion', # Bug #3358
+                              ]
+        return flags
+
+    def getOptimizedFlags(self, conf):
+        flags = super(ClangFlags, self).getOptimizedFlags(conf)
+        flags['CXXFLAGS'] += ['-fcolor-diagnostics',
+                              '-Wno-unused-local-typedef', # Bugs #2657 and #3209
+                              ]
         return flags
diff --git a/.waf-tools/sanitizers.py b/.waf-tools/sanitizers.py
new file mode 100644
index 0000000..a8fe55d
--- /dev/null
+++ b/.waf-tools/sanitizers.py
@@ -0,0 +1,22 @@
+# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+def options(opt):
+    opt.add_option('--with-sanitizer', action='store', default='', dest='sanitizers',
+                   help='Comma-separated list of compiler sanitizers to enable [default=none]')
+
+def configure(conf):
+    for san in conf.options.sanitizers.split(','):
+        if not san:
+            continue
+
+        sanflag = '-fsanitize=%s' % san
+        conf.start_msg('Checking if compiler supports %s' % sanflag)
+
+        if conf.check_cxx(cxxflags=['-Werror', sanflag, '-fno-omit-frame-pointer'],
+                          linkflags=[sanflag], mandatory=False):
+            conf.end_msg('yes')
+            conf.env.append_unique('CXXFLAGS', [sanflag, '-fno-omit-frame-pointer'])
+            conf.env.append_unique('LINKFLAGS', [sanflag])
+        else:
+            conf.end_msg('no', color='RED')
+            conf.fatal('%s sanitizer is not supported by the current compiler' % san)
diff --git a/src/algo/aes.cpp b/src/algo/aes.cpp
index 1bcb6b6..6a7d2e0 100644
--- a/src/algo/aes.cpp
+++ b/src/algo/aes.cpp
@@ -42,7 +42,7 @@
   SecByteBlock key(0x00, params.getKeySize() >> 3);  // Converting key bit-size to byte-size.
   rng.GenerateBlock(key.data(), key.size());
 
-  DecryptKey<Aes> decryptKey(std::move(Buffer(key.data(), key.size())));
+  DecryptKey<Aes> decryptKey(Buffer(key.data(), key.size()));
   return decryptKey;
 }
 
diff --git a/tests/unit-tests/algo/aes.t.cpp b/tests/unit-tests/algo/aes.t.cpp
index 9d15726..740855e 100644
--- a/tests/unit-tests/algo/aes.t.cpp
+++ b/tests/unit-tests/algo/aes.t.cpp
@@ -65,7 +65,7 @@
 
   EncryptParams eparams(tlv::AlgorithmAesEcb, 16);
 
-  DecryptKey<Aes> decryptKey(std::move(Buffer(key, sizeof(key))));
+  DecryptKey<Aes> decryptKey(Buffer(key, sizeof(key)));
   EncryptKey<Aes> encryptKey = Aes::deriveEncryptKey(decryptKey.getKeyBits());
 
   // check if loading key and key derivation
diff --git a/wscript b/wscript
index 6ae908b..b11e7bd 100644
--- a/wscript
+++ b/wscript
@@ -29,7 +29,7 @@
 
 def options(opt):
     opt.load(['compiler_c', 'compiler_cxx', 'gnu_dirs'])
-    opt.load(['boost', 'default-compiler-flags', 'doxygen'],
+    opt.load(['boost', 'default-compiler-flags', 'sanitizers', 'doxygen'],
              tooldir=['.waf-tools'])
 
     syncopt = opt.add_option_group ("NDN-GROUP-ENCRYPT Options")
@@ -40,7 +40,7 @@
                        help='''build unit tests''')
 
 def configure(conf):
-    conf.load(['compiler_c', 'compiler_cxx', 'gnu_dirs', 'boost', 'default-compiler-flags', 'doxygen'])
+    conf.load(['compiler_c', 'compiler_cxx', 'gnu_dirs', 'boost', 'default-compiler-flags', 'sanitizers', 'doxygen'])
 
     if 'PKG_CONFIG_PATH' not in os.environ:
         os.environ['PKG_CONFIG_PATH'] = Utils.subst_vars('${LIBDIR}/pkgconfig', conf.env)