|
| 1 | +use cryptonote::crypto::{CipherType, encrypt_symmetric}; |
| 2 | +use cryptonote::encoding::{ |
| 3 | + build_url, decode_note, encode_note, generate_qr_code, |
| 4 | + parse_url, NoteData, |
| 5 | +}; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn test_encode_decode_plaintext() { |
| 9 | + let note = |
| 10 | + NoteData::PlainText("Hello, World!".to_string()); |
| 11 | + let encoded = encode_note(¬e).expect("Encoding failed"); |
| 12 | + let decoded = |
| 13 | + decode_note(&encoded).expect("Decoding failed"); |
| 14 | + match decoded { |
| 15 | + NoteData::PlainText(text) => { |
| 16 | + assert_eq!(text, "Hello, World!") |
| 17 | + } |
| 18 | + _ => panic!("Expected PlainText"), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_encode_decode_encrypted() { |
| 24 | + let plaintext = b"Secret message"; |
| 25 | + let encrypted = encrypt_symmetric( |
| 26 | + plaintext, |
| 27 | + "password", |
| 28 | + CipherType::ChaCha20Poly1305, |
| 29 | + ) |
| 30 | + .expect("Encryption failed"); |
| 31 | + let note = NoteData::CipherText(encrypted); |
| 32 | + let encoded = encode_note(¬e).expect("Encoding failed"); |
| 33 | + let decoded = |
| 34 | + decode_note(&encoded).expect("Decoding failed"); |
| 35 | + |
| 36 | + match decoded { |
| 37 | + NoteData::CipherText(enc_data) => { |
| 38 | + assert_eq!( |
| 39 | + note.as_ciphertext().unwrap().ciphertext, |
| 40 | + enc_data.ciphertext |
| 41 | + ); |
| 42 | + } |
| 43 | + _ => panic!("Expected CipherText"), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn test_build_parse_url() { |
| 49 | + let note = NoteData::PlainText("Test note".to_string()); |
| 50 | + let url = build_url("https://example.com/view", ¬e) |
| 51 | + .expect("URL build failed"); |
| 52 | + assert!(url.starts_with("https://example.com/view#note=")); |
| 53 | + let parsed = parse_url(&url).expect("URL parse failed"); |
| 54 | + match parsed { |
| 55 | + NoteData::PlainText(text) => { |
| 56 | + assert_eq!(text, "Test note") |
| 57 | + } |
| 58 | + _ => panic!("Expected PlainText"), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn test_qr_code_generation() { |
| 64 | + let url = "https://example.com/test"; |
| 65 | + let svg = generate_qr_code(url) |
| 66 | + .expect("QR generation failed"); |
| 67 | + assert!(svg.contains("<svg")); |
| 68 | + assert!(svg.contains("</svg>")); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +fn test_invalid_base64() { |
| 73 | + let result = decode_note("not-valid-base64!!!"); |
| 74 | + assert!(result.is_err()); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn test_invalid_url_format() { |
| 79 | + let result = |
| 80 | + parse_url("https://example.com/no-note-param"); |
| 81 | + assert!(result.is_err()); |
| 82 | +} |
0 commit comments