"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.2/libcanlock/test/canlocktest_legacy.c" (7 May 2017, 4689 Bytes) of package /linux/misc/tin-2.4.2.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * COPYRIGHT AND PERMISSION NOTICE
    3  *
    4  * Copyright (c) 2003 G.J. Andruk
    5  *
    6  * All rights reserved.
    7  *
    8  * Permission is hereby granted, free of charge, to any person obtaining
    9  * a copy of this software and associated documentation files (the
   10  * "Software"), to deal in the Software without restriction, including
   11  * without limitation the rights to use, copy, modify, merge, publish,
   12  * distribute, and/or sell copies of the Software, and to permit persons
   13  * to whom the Software is furnished to do so, provided that the above
   14  * copyright notice(s) and this permission notice appear in all copies of
   15  * the Software and that both the above copyright notice(s) and this
   16  * permission notice appear in supporting documentation.
   17  *
   18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
   21  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
   22  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
   23  * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
   24  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
   25  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   27  *
   28  * Except as contained in this notice, the name of a copyright holder
   29  * shall not be used in advertising or otherwise to promote the sale, use
   30  * or other dealings in this Software without prior written authorization
   31  * of the copyright holder.
   32  */
   33 
   34 /*
   35  * Test program for legacy API from version 2
   36  *
   37  * This program doesn't really do anything but lightly exercise all the
   38  * library functions, so you can make sure it all compiled correctly.
   39  * Everything's kept simple so that you can also see how they would be
   40  * called in a real application.
   41  */
   42 
   43 #include <stdio.h>
   44 #include <stdlib.h>
   45 #include <string.h>
   46 #include "canlock.h"
   47 
   48 
   49 #define BUFFSIZE 512
   50 
   51 
   52 static int checker(char *key, char *lock)
   53 {
   54    int res = -1;
   55    char *rawkey, *rawlock;
   56    char keytype[BUFFSIZE], locktype[BUFFSIZE];
   57 
   58    printf("L %s K %s ", lock, key);
   59 
   60    rawkey = lock_strip_alpha(key, keytype);
   61    rawlock = lock_strip_alpha(lock, locktype);
   62 
   63    if (!strcmp(keytype, locktype))
   64    {
   65       if (!strcmp(keytype, "sha1"))
   66       {
   67          if (!sha_verify(rawkey, rawlock))
   68          {
   69             printf(" GOOD\n");
   70             res = 0;
   71          }
   72          else
   73             printf(" BAD\n");
   74       }
   75       else
   76          printf(" SCHEME NOT SUPPORTED\n");
   77    }
   78    else
   79       printf(" SCHEME MISMATCH %s/%s\n", keytype, locktype);
   80 
   81    return res;
   82 }
   83 
   84 
   85 int main(void)
   86 {
   87    char cankey[256], canlock[256], *lkey, *llock;
   88    unsigned char secret[] = "fluffy",
   89       message[] = "<lkr905851929.22670@meow.invalid>";
   90    int rv, rv2;
   91    int failed = 0;
   92 
   93    printf("Secret %s\n", secret);
   94    printf("Message %s\n", message);
   95 
   96    llock = sha_lock(secret, strlen((char *) secret),
   97                     message, strlen((char *) message));
   98    lkey = sha_key(secret, strlen((char *) secret),
   99                   message, strlen((char *)message));
  100 
  101    printf("%s%s %s\n", "SHA Expect Lock/Key:\n",
  102           "L sha1:ScU1gyAi9bd/aFEOyzg4m99lwXs=",
  103           "K sha1:C1Me/4n0l/V778Ih3J2UnhAoHrA=");
  104 
  105    rv = checker(lkey, llock);
  106    free((void *) llock);
  107    free((void *) lkey);
  108    printf("---\n");
  109    if (rv) failed = 1;
  110 
  111 /*********/
  112 
  113    printf("Testing against usefor cancel lock draft 01 samples...\n");
  114 
  115    sprintf(canlock, "%s", "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=");
  116    sprintf(cankey, "%s", "sha1:aaaBBBcccDDDeeeFFF");
  117    rv = checker(cankey, canlock);
  118    printf("---above should have been GOOD---\n");
  119    if (rv) failed = 1;
  120 
  121 /*********/
  122 
  123    sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
  124    sprintf(cankey, "%s", "sha1:chW8hNeDx3iNUsGBU6/ezDk88P4=");
  125    rv = checker(cankey, canlock);
  126 
  127    sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
  128    sprintf(cankey, "%s", "sha1:4srkWaRIzvK51ArAP:Hc");
  129    rv2 = checker(cankey, canlock);
  130    printf("---above should have been GOOD and BAD---\n");
  131    if (rv || !rv2) failed = 1;
  132 
  133 /*********/
  134 
  135    sprintf(canlock, "%s", "sha1:JyEBL4w9/abCBuzCxMIE/E73GM4=");
  136    sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
  137    rv = checker(cankey, canlock);
  138 
  139    sprintf(canlock, "%s", "sha1:2Bmg+zWaY1noRiCdy8k3IapwSDU=");
  140    sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
  141    rv2 = checker(cankey, canlock);
  142    printf("---above should have been GOOD and BAD---\n");
  143    if (rv || !rv2) failed = 1;
  144    printf("\n");
  145 
  146    /* Check for success */
  147    if (!failed) exit(EXIT_SUCCESS);
  148    exit(EXIT_FAILURE);
  149 }