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