tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

randseed.c (2982B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 ** File: rngseed.c
      8 ** Description:
      9 ** Test NSPR's Random Number Seed generator
     10 **
     11 ** Initial test: Just make sure it outputs some data.
     12 **
     13 ** ... more? ... check some iterations to ensure it is random (no dupes)
     14 ** ... more? ... histogram distribution of random numbers
     15 */
     16 
     17 #include "plgetopt.h"
     18 #include "nspr.h"
     19 #include "prrng.h"
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <string.h>
     23 
     24 /*
     25 ** Test harness infrastructure
     26 */
     27 PRLogModuleInfo* lm;
     28 PRLogModuleLevel msgLevel = PR_LOG_NONE;
     29 PRIntn debug = 0;
     30 PRUint32 failed_already = 0;
     31 /* end Test harness infrastructure */
     32 
     33 PRIntn optRandCount = 30;
     34 char buf[40];
     35 PRSize bufSize = sizeof(buf);
     36 PRSize rSize;
     37 PRIntn i;
     38 
     39 /*
     40 ** Emit help text for this test
     41 */
     42 static void Help(void) {
     43  printf("Template: Help(): display your help message(s) here");
     44  exit(1);
     45 } /* end Help() */
     46 
     47 static void PrintRand(void* buf, PRIntn size) {
     48  PRUint32* rp = buf;
     49  PRIntn i;
     50 
     51  printf("%4.4d--\n", size);
     52  while (size > 0) {
     53    switch (size) {
     54      case 1:
     55        printf("%2.2X\n", *(rp++));
     56        size -= 4;
     57        break;
     58      case 2:
     59        printf("%4.4X\n", *(rp++));
     60        size -= 4;
     61        break;
     62      case 3:
     63        printf("%6.6X\n", *(rp++));
     64        size -= 4;
     65        break;
     66      default:
     67        while (size >= 4) {
     68          PRIntn i = 3;
     69          do {
     70            printf("%8.8X ", *(rp++));
     71            size -= 4;
     72          } while (i--);
     73          i = 3;
     74          printf("\n");
     75        }
     76        break;
     77    } /* end switch() */
     78  } /* end while() */
     79 } /* end PrintRand() */
     80 
     81 int main(int argc, char** argv) {
     82  {
     83    /*
     84    ** Get command line options
     85    */
     86    PLOptStatus os;
     87    PLOptState* opt = PL_CreateOptState(argc, argv, "hdv");
     88 
     89    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     90      if (PL_OPT_BAD == os) {
     91        continue;
     92      }
     93      switch (opt->option) {
     94        case 'd': /* debug */
     95          debug = 1;
     96          msgLevel = PR_LOG_ERROR;
     97          break;
     98        case 'v': /* verbose mode */
     99          msgLevel = PR_LOG_DEBUG;
    100          break;
    101        case 'h': /* help message */
    102          Help();
    103          break;
    104        default:
    105          break;
    106      }
    107    }
    108    PL_DestroyOptState(opt);
    109  }
    110 
    111  lm = PR_NewLogModule("Test"); /* Initialize logging */
    112  for (i = 0; i < optRandCount; i++) {
    113    memset(buf, 0, bufSize);
    114    rSize = PR_GetRandomNoise(buf, bufSize);
    115    if (!rSize) {
    116      fprintf(stderr, "Not implemented\n");
    117      failed_already = PR_TRUE;
    118      break;
    119    }
    120    if (debug) {
    121      PrintRand(buf, rSize);
    122    }
    123  }
    124 
    125  if (debug) {
    126    printf("%s\n", (failed_already) ? "FAIL" : "PASS");
    127  }
    128  return ((failed_already == PR_TRUE) ? 1 : 0);
    129 } /* main() */
    130 /* end template.c */