| 
									
										
										
										
											2019-02-26 19:19:34 -08:00
										 |  |  | #[macro_use]
 | 
					
						
							|  |  |  | extern crate solana;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  | use assert_cmd::prelude::*;
 | 
					
						
							| 
									
										
										
										
											2019-02-26 16:35:00 -08:00
										 |  |  | use solana::blocktree::create_new_tmp_ledger;
 | 
					
						
							| 
									
										
										
										
											2019-02-22 22:55:46 -07:00
										 |  |  | use solana_sdk::genesis_block::GenesisBlock;
 | 
					
						
							| 
									
										
										
										
											2019-02-21 19:46:04 -07:00
										 |  |  | use solana_sdk::signature::{Keypair, KeypairUtil};
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  | use std::process::Command;
 | 
					
						
							|  |  |  | use std::process::Output;
 | 
					
						
							|  |  |  | use std::sync::Arc;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fn run_ledger_tool(args: &[&str]) -> Output {
 | 
					
						
							| 
									
										
										
										
											2019-01-29 13:10:48 -06:00
										 |  |  |     Command::cargo_bin(env!("CARGO_PKG_NAME"))
 | 
					
						
							|  |  |  |         .unwrap()
 | 
					
						
							|  |  |  |         .args(args)
 | 
					
						
							|  |  |  |         .output()
 | 
					
						
							|  |  |  |         .unwrap()
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fn count_newlines(chars: &[u8]) -> usize {
 | 
					
						
							|  |  |  |     chars.iter().filter(|&c| *c == '\n' as u8).count()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							|  |  |  | fn bad_arguments() {
 | 
					
						
							|  |  |  |     // At least a ledger path is required
 | 
					
						
							|  |  |  |     assert!(!run_ledger_tool(&[]).status.success());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Invalid ledger path should fail
 | 
					
						
							|  |  |  |     assert!(!run_ledger_tool(&["-l", "invalid_ledger", "verify"])
 | 
					
						
							|  |  |  |         .status
 | 
					
						
							|  |  |  |         .success());
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							|  |  |  | fn nominal() {
 | 
					
						
							|  |  |  |     let keypair = Arc::new(Keypair::new());
 | 
					
						
							| 
									
										
										
										
											2019-03-09 19:28:43 -08:00
										 |  |  |     let (genesis_block, _mint_keypair) = GenesisBlock::new_with_leader(100, &keypair.pubkey(), 50);
 | 
					
						
							| 
									
										
										
										
											2019-02-22 22:55:46 -07:00
										 |  |  |     let ticks_per_slot = genesis_block.ticks_per_slot;
 | 
					
						
							| 
									
										
										
										
											2019-02-26 16:35:00 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-02 10:25:16 -08:00
										 |  |  |     let (ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_block);
 | 
					
						
							| 
									
										
										
										
											2019-02-26 16:35:00 -08:00
										 |  |  |     let ticks = ticks_per_slot as usize;
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Basic validation
 | 
					
						
							|  |  |  |     let output = run_ledger_tool(&["-l", &ledger_path, "verify"]);
 | 
					
						
							|  |  |  |     assert!(output.status.success());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Print everything
 | 
					
						
							|  |  |  |     let output = run_ledger_tool(&["-l", &ledger_path, "print"]);
 | 
					
						
							|  |  |  |     assert!(output.status.success());
 | 
					
						
							| 
									
										
										
										
											2019-02-26 16:35:00 -08:00
										 |  |  |     assert_eq!(count_newlines(&output.stdout), ticks);
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Only print the first 5 items
 | 
					
						
							|  |  |  |     let output = run_ledger_tool(&["-l", &ledger_path, "-n", "5", "print"]);
 | 
					
						
							|  |  |  |     assert!(output.status.success());
 | 
					
						
							|  |  |  |     assert_eq!(count_newlines(&output.stdout), 5);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 15:19:10 -08:00
										 |  |  |     // Skip entries with no hashes
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  |     let output = run_ledger_tool(&["-l", &ledger_path, "-h", "1", "print"]);
 | 
					
						
							|  |  |  |     assert!(output.status.success());
 | 
					
						
							| 
									
										
										
										
											2019-02-26 16:35:00 -08:00
										 |  |  |     assert_eq!(count_newlines(&output.stdout), ticks);
 | 
					
						
							| 
									
										
										
										
											2018-12-07 20:44:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Skip entries with fewer than 2 hashes (skip everything)
 | 
					
						
							|  |  |  |     let output = run_ledger_tool(&["-l", &ledger_path, "-h", "2", "print"]);
 | 
					
						
							|  |  |  |     assert!(output.status.success());
 | 
					
						
							|  |  |  |     assert_eq!(count_newlines(&output.stdout), 0);
 | 
					
						
							|  |  |  | }
 |