bats tests for the driver + libinfnoise cleanup
This commit is contained in:
		
							
								
								
									
										6
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | [submodule "software/tests/test_helper/bats-support"] | ||||||
|  | 	path = software/tests/test_helper/bats-support | ||||||
|  | 	url = https://github.com/ztombol/bats-support | ||||||
|  | [submodule "software/tests/test_helper/bats-assert"] | ||||||
|  | 	path = software/tests/test_helper/bats-assert | ||||||
|  | 	url = https://github.com/ztombol/bats-assert | ||||||
| @@ -1,30 +1,6 @@ | |||||||
| # Usage examples for Infinite Noise | # Usage examples for Infinite Noise | ||||||
|  |  | ||||||
| ## Driver (binary) | ## Integrate the binary to python | ||||||
|  |  | ||||||
| ### Command-Line |  | ||||||
|  |  | ||||||
| Raw output to file: |  | ||||||
|  |  | ||||||
|     infnoise --raw > output.txt |  | ||||||
|  |  | ||||||
| Whitened output to file: |  | ||||||
|  |  | ||||||
|     infnoise > output.txt |  | ||||||
|  |  | ||||||
| Whietened, mutliplied output to file: |  | ||||||
|  |  | ||||||
|     infnoise --multiplier 10 > output.txt |  | ||||||
|  |  | ||||||
| Debug mode: |  | ||||||
|  |  | ||||||
|     infnoise --debug --no-output |  | ||||||
|  |  | ||||||
| /dev/random mode: |  | ||||||
|  |  | ||||||
|     infnoise --debug --dev-random (--multiplier) |  | ||||||
|  |  | ||||||
| ### Integrate |  | ||||||
|  |  | ||||||
| See the python examples `serial-numbers.py` and `randomserver.py` to see how you could integrate it with python. | See the python examples `serial-numbers.py` and `randomserver.py` to see how you could integrate it with python. | ||||||
|  |  | ||||||
| @@ -44,7 +20,7 @@ A simple webserver based on the web.py framework to serve random data via a REST | |||||||
|  |  | ||||||
| It has only two resources: `/get` and `/status`.  | It has only two resources: `/get` and `/status`.  | ||||||
|  |  | ||||||
| ## Library | ## libinfnoise | ||||||
|  |  | ||||||
| TODO | TODO | ||||||
|  |  | ||||||
|   | |||||||
| @@ -176,12 +176,10 @@ int main(int argc, char **argv) | |||||||
|     uint8_t keccakState[KeccakPermutationSizeInBytes]; |     uint8_t keccakState[KeccakPermutationSizeInBytes]; | ||||||
|     KeccakInitializeState(keccakState); |     KeccakInitializeState(keccakState); | ||||||
|  |  | ||||||
|     uint8_t result[1024]; // only used in noOutput mode (and libinfnoise) |  | ||||||
|  |  | ||||||
|     uint64_t totalBytesWritten = 0u; |     uint64_t totalBytesWritten = 0u; | ||||||
|     while(true) { |     while(true) { | ||||||
|         uint64_t prevTotalBytesWritten = totalBytesWritten; |         uint64_t prevTotalBytesWritten = totalBytesWritten; | ||||||
|         uint64_t bytesWritten = readData_private(&ftdic, keccakState, result, &message, opts.noOutput, opts.raw, opts.outputMultiplier, opts.devRandom); // calling libinfnoise's private readData method |         uint64_t bytesWritten = readData_private(&ftdic, keccakState, NULL, &message, opts.noOutput, opts.raw, opts.outputMultiplier, opts.devRandom); // calling libinfnoise's private readData method | ||||||
|  |  | ||||||
|         if (totalBytesWritten == (unsigned long)-1) { |         if (totalBytesWritten == (unsigned long)-1) { | ||||||
|             fputs(message, stderr); |             fputs(message, stderr); | ||||||
|   | |||||||
| @@ -88,8 +88,9 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin | |||||||
|         if (!noOutput) { |         if (!noOutput) { | ||||||
|             outputBytes(bytes, BUFLEN/8u, entropy, writeDevRandom); |             outputBytes(bytes, BUFLEN/8u, entropy, writeDevRandom); | ||||||
|         } else { |         } else { | ||||||
|  | 	    if (result != NULL) { | ||||||
|                 memcpy(result, bytes, BUFLEN/8u * sizeof(uint8_t)); |                 memcpy(result, bytes, BUFLEN/8u * sizeof(uint8_t)); | ||||||
|             //result=bytes; |             } | ||||||
| 	} | 	} | ||||||
|         return BUFLEN/8u; |         return BUFLEN/8u; | ||||||
|     } |     } | ||||||
| @@ -109,10 +110,12 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin | |||||||
| 	if (!noOutput) { | 	if (!noOutput) { | ||||||
| 	    outputBytes(dataOut, entropy/8u, entropy & 0x7u, writeDevRandom); | 	    outputBytes(dataOut, entropy/8u, entropy & 0x7u, writeDevRandom); | ||||||
| 	} else { | 	} else { | ||||||
|  | 	    if (result != NULL) { | ||||||
|                 memcpy(result, dataOut, entropy/8u * sizeof(uint8_t)); |                 memcpy(result, dataOut, entropy/8u * sizeof(uint8_t)); | ||||||
|             } |             } | ||||||
|  | 	} | ||||||
|         return entropy/8u; |         return entropy/8u; | ||||||
|     } // todo: write to result array |     } | ||||||
|  |  | ||||||
|     // Output 256*outputMultipler bits. |     // Output 256*outputMultipler bits. | ||||||
|     uint32_t numBits = outputMultiplier*256u; |     uint32_t numBits = outputMultiplier*256u; | ||||||
| @@ -132,18 +135,14 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin | |||||||
| 	if (!noOutput) { | 	if (!noOutput) { | ||||||
|             outputBytes(dataOut, bytesToWrite, entropyThisTime, writeDevRandom); |             outputBytes(dataOut, bytesToWrite, entropyThisTime, writeDevRandom); | ||||||
| 	} else { | 	} else { | ||||||
|             // append data in result array until we have finished squeezing the keccak sponge |             //memcpy(result + bytesWritten, dataOut, bytesToWrite * sizeof(uint8_t)); //doesn't work? | ||||||
| 	    // its important to have an result array of the approriate size: outputMultiplier*32 |  | ||||||
|             //fprintf(stderr, "bytes written: %d\n", bytesWritten); |  | ||||||
|             //fprintf(stderr, "bytes to write: %d\n", bytesToWrite); |  | ||||||
|  |  | ||||||
|             //memcpy(result + bytesWritten, dataOut, bytesToWrite * sizeof(uint8_t)); //doesn't work |  | ||||||
|             // alternative: loop through dataOut and append array elements to result.. |             // alternative: loop through dataOut and append array elements to result.. | ||||||
|  | 	    if (result != NULL) { | ||||||
|                 for (uint32_t i =0; i < bytesToWrite; i++ ) { |                 for (uint32_t i =0; i < bytesToWrite; i++ ) { | ||||||
|                  fprintf(stderr, "                 result[%d] = dataOut[%d];\n", bytesWritten + i, i); |  | ||||||
|                     result[bytesWritten + i] = dataOut[i]; |                     result[bytesWritten + i] = dataOut[i]; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  | 	} | ||||||
|         bytesWritten += bytesToWrite; |         bytesWritten += bytesToWrite; | ||||||
|         numBits -= bytesToWrite*8u; |         numBits -= bytesToWrite*8u; | ||||||
|         entropy -= entropyThisTime; |         entropy -= entropyThisTime; | ||||||
| @@ -155,7 +154,6 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin | |||||||
|         fprintf(stderr, "Internal error outputing bytes\n"); |         fprintf(stderr, "Internal error outputing bytes\n"); | ||||||
|         exit(1); |         exit(1); | ||||||
|     } |     } | ||||||
|     fprintf(stderr, "bytes written: %d\n", bytesWritten); |  | ||||||
|     return bytesWritten; |     return bytesWritten; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -364,12 +362,12 @@ int main() { | |||||||
|     bool debug = false; |     bool debug = false; | ||||||
|  |  | ||||||
|     // calculate output size based on the parameters: |     // calculate output size based on the parameters: | ||||||
|     // when using the multiplier, we need a result array of max 1024 bytes - otherwise 64(BUFLEN/8) bytes |     // when using the multiplier, we need a result array of 32*MULTIPLIER - otherwise 64(BUFLEN/8) bytes | ||||||
|     uint32_t resultSize; |     uint32_t resultSize; | ||||||
|     if (multiplier == 0 || rawOutput == true) { |     if (multiplier == 0 || rawOutput == true) { | ||||||
|         resultSize = BUFLEN/8u; |         resultSize = BUFLEN/8u; | ||||||
|     } else { |     } else { | ||||||
|         resultSize = 1024; // optimize? |         resultSize = multiplier*32u; | ||||||
|     } |     } | ||||||
|     fprintf(stderr, "%d\n", resultSize); |     fprintf(stderr, "%d\n", resultSize); | ||||||
|  |  | ||||||
| @@ -381,10 +379,10 @@ int main() { | |||||||
|         uint64_t bytesWritten = 0u; |         uint64_t bytesWritten = 0u; | ||||||
|         bytesWritten = readData(&ftdic, keccakState, result, multiplier); |         bytesWritten = readData(&ftdic, keccakState, result, multiplier); | ||||||
|  |  | ||||||
| 	// check for -1! | 	// check for -1, indicating an error | ||||||
|         totalBytesWritten += bytesWritten; |         totalBytesWritten += bytesWritten; | ||||||
|  |  | ||||||
| 	// make sure to only read as many bytes as readData returned. Only those have passed the health check in this round (usually all but..) | 	// make sure to only read as many bytes as readData returned. Only those have passed the health check in this round (usually all) | ||||||
|         fwrite(result, 1, bytesWritten, stdout); |         fwrite(result, 1, bytesWritten, stdout); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								software/tests/README
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								software/tests/README
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | bats tests for the binary driver.  | ||||||
							
								
								
									
										87
									
								
								software/tests/infnoise.bats
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								software/tests/infnoise.bats
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,87 @@ | |||||||
|  | #!/usr/bin/env bats | ||||||
|  |  | ||||||
|  | load 'test_helper/bats-support/load' | ||||||
|  | load 'test_helper/bats-assert/load' | ||||||
|  |  | ||||||
|  | # Tests for the infnoise binary | ||||||
|  |  | ||||||
|  | @test "test --raw output for expected entropy" { | ||||||
|  |   # capture some data | ||||||
|  |   TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX` | ||||||
|  |   timeout 5s ./infnoise --raw > $TMP_FILE || true | ||||||
|  |  | ||||||
|  |   # run ent | ||||||
|  |   run ent $TMP_FILE | ||||||
|  |  | ||||||
|  |   assert_line --index 0 --regexp '^Entropy = 7.2[5-9][0-9]+ bits per byte.$' | ||||||
|  |  | ||||||
|  |   # cleanup | ||||||
|  |   rm $TMP_FILE | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @test "test whitened output for expected entropy" { | ||||||
|  |   # capture some data | ||||||
|  |   TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX` | ||||||
|  |   timeout 5s ./infnoise > $TMP_FILE || true | ||||||
|  |  | ||||||
|  |   # run ent | ||||||
|  |   run ent $TMP_FILE | ||||||
|  |  | ||||||
|  |   # check ent's result | ||||||
|  |   assert_line --index 0 --regexp '^Entropy = 7.99[0-9]+ bits per byte.$' | ||||||
|  |  | ||||||
|  |   # cleanup | ||||||
|  |   rm $TMP_FILE | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @test "test whitened output (multiplier=10) for expected entropy" { | ||||||
|  |   # capture some data | ||||||
|  |   TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX` | ||||||
|  |   timeout 5s ./infnoise --multiplier 10 > $TMP_FILE || true | ||||||
|  |  | ||||||
|  |   # run ent | ||||||
|  |   run ent $TMP_FILE | ||||||
|  |  | ||||||
|  |   assert_line --index 0 --regexp '^Entropy = 7.99[0-9]+ bits per byte.$' | ||||||
|  |  | ||||||
|  |   # cleanup | ||||||
|  |   rm $TMP_FILE | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @test "test --no-output --debug" { | ||||||
|  |   # capture some data | ||||||
|  |   TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX` | ||||||
|  |   run timeout 5s ./infnoise --no-output --debug | ||||||
|  |  | ||||||
|  |   echo $output | ||||||
|  |   [ "$status" -eq 124 ] | ||||||
|  |  | ||||||
|  |   assert_line --index 0 --regexp '^Generated 1048576 bits.  OK to use data.  Estimated entropy per bit: 0\.[0-8][6-8][0-9]+, estimated K: 1\.8[1-5][0-9]+$' | ||||||
|  |   assert_line --index 1 --regexp '^num1s:50.[0-9]+%, even misfires:0.[0-1][0-9]+%, odd misfires:0.[0-1][0-9]+%$' | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @test "test --list-devices" { | ||||||
|  |   run ./infnoise --list-devices | ||||||
|  |   echo $output | ||||||
|  |   [ "$status" -eq 0 ] | ||||||
|  |  | ||||||
|  |   # FTDI serial: | ||||||
|  |   assert_line --index 1 --regexp '^Manufacturer: FTDI, Description: FT240X USB FIFO, Serial: [0-9A-Z]+$' | ||||||
|  |  | ||||||
|  |   # 13-37.org serial: | ||||||
|  |   assert_line --index 0 --regexp '^Manufacturer: 13-37.org, Description: Infinite Noise TRNG, Serial: [0-9A-F]+$' | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @test "test --serial with not connected serial (results in error)" { | ||||||
|  |   run ./infnoise --serial 4711 | ||||||
|  |   echo $output | ||||||
|  |   [ "$status" -eq 1 ] | ||||||
|  |   [ "${lines[0]}" = "Can't find Infinite Noise Multiplier. Try running as super user?" ] | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @test "test --help" { | ||||||
|  |   run ./infnoise --help | ||||||
|  |   echo $output | ||||||
|  |   [ "$status" -eq 0 ] | ||||||
|  |   [ "${lines[0]}" = "Usage: infnoise [options]" ] | ||||||
|  | } | ||||||
							
								
								
									
										1
									
								
								software/tests/test_helper/bats-assert
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
							
						
						
									
										1
									
								
								software/tests/test_helper/bats-assert
									
									
									
									
									
										Submodule
									
								
							 Submodule software/tests/test_helper/bats-assert added at 9f88b4207d
									
								
							
							
								
								
									
										1
									
								
								software/tests/test_helper/bats-support
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
							
						
						
									
										1
									
								
								software/tests/test_helper/bats-support
									
									
									
									
									
										Submodule
									
								
							 Submodule software/tests/test_helper/bats-support added at 004e707638
									
								
							
		Reference in New Issue
	
	Block a user