"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.6.2/libcanlock/test/canlocktest_legacy.c" (23 Aug 2021, 4958 Bytes) of package /linux/misc/tin-2.6.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. See also the last Fossies "Diffs" side-by-side code changes report for "canlocktest_legacy.c": 2.4.5_vs_2.6.0.

    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 #if CL_API_V2
   53 static int checker(char *key, char *lock)
   54 {
   55    int res = -1;
   56    char *rawkey, *rawlock;
   57    char keytype[BUFFSIZE], locktype[BUFFSIZE];
   58 
   59    printf("L %s K %s ", lock, key);
   60 
   61    rawkey = lock_strip_alpha(key, keytype);
   62    rawlock = lock_strip_alpha(lock, locktype);
   63 
   64    if (!strcmp(keytype, locktype))
   65    {
   66       if (!strcmp(keytype, "sha1"))
   67       {
   68          if (!sha_verify(rawkey, rawlock))
   69          {
   70             printf(" GOOD\n");
   71             res = 0;
   72          }
   73          else
   74             printf(" BAD\n");
   75       }
   76       else
   77          printf(" SCHEME NOT SUPPORTED\n");
   78    }
   79    else
   80       printf(" SCHEME MISMATCH %s/%s\n", keytype, locktype);
   81 
   82    return res;
   83 }
   84 #endif  /* CL_API_V2 */
   85 
   86 
   87 int main(void)
   88 {
   89 #if CL_API_V2
   90    char cankey[256], canlock[256], *lkey, *llock;
   91    unsigned char secret[] = "fluffy",
   92       message[] = "<lkr905851929.22670@meow.invalid>";
   93    int rv, rv2;
   94    int failed = 0;
   95 
   96    printf("Secret %s\n", secret);
   97    printf("Message %s\n", message);
   98 
   99    llock = sha_lock(secret, strlen((char *) secret),
  100                     message, strlen((char *) message));
  101    lkey = sha_key(secret, strlen((char *) secret),
  102                   message, strlen((char *)message));
  103 
  104    printf("%s%s %s\n", "SHA Expect Lock/Key:\n",
  105           "L sha1:ScU1gyAi9bd/aFEOyzg4m99lwXs=",
  106           "K sha1:C1Me/4n0l/V778Ih3J2UnhAoHrA=");
  107 
  108    rv = checker(lkey, llock);
  109    free((void *) llock);
  110    free((void *) lkey);
  111    printf("---\n");
  112    if (rv) failed = 1;
  113 
  114 /*********/
  115 
  116    printf("Testing against usefor cancel lock draft 01 samples...\n");
  117 
  118    sprintf(canlock, "%s", "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=");
  119    sprintf(cankey, "%s", "sha1:aaaBBBcccDDDeeeFFF");
  120    rv = checker(cankey, canlock);
  121    printf("---above should have been GOOD---\n");
  122    if (rv) failed = 1;
  123 
  124 /*********/
  125 
  126    sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
  127    sprintf(cankey, "%s", "sha1:chW8hNeDx3iNUsGBU6/ezDk88P4=");
  128    rv = checker(cankey, canlock);
  129 
  130    sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
  131    sprintf(cankey, "%s", "sha1:4srkWaRIzvK51ArAP:Hc");
  132    rv2 = checker(cankey, canlock);
  133    printf("---above should have been GOOD and BAD---\n");
  134    if (rv || !rv2) failed = 1;
  135 
  136 /*********/
  137 
  138    sprintf(canlock, "%s", "sha1:JyEBL4w9/abCBuzCxMIE/E73GM4=");
  139    sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
  140    rv = checker(cankey, canlock);
  141 
  142    sprintf(canlock, "%s", "sha1:2Bmg+zWaY1noRiCdy8k3IapwSDU=");
  143    sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
  144    rv2 = checker(cankey, canlock);
  145    printf("---above should have been GOOD and BAD---\n");
  146    if (rv || !rv2) failed = 1;
  147    printf("\n");
  148 
  149    /* Check for success */
  150    if (!failed) exit(EXIT_SUCCESS);
  151    exit(EXIT_FAILURE);
  152 #else  /* CL_API_V2 */
  153    /* Execute as NOP and return 77 (defined by automake and means "skipped") */
  154    printf("---legacy API tests were skipped (Version 2 API disabled)---\n");
  155    exit(77);
  156 #endif  /* CL_API_V2 */
  157 }