1+ package com .eatthepath .noise ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import javax .crypto .AEADBadTagException ;
7+ import javax .crypto .ShortBufferException ;
8+ import java .nio .ByteBuffer ;
9+ import java .security .NoSuchAlgorithmException ;
10+
11+ import static org .junit .jupiter .api .Assertions .*;
12+
13+ class NoiseTransportImplTest {
14+
15+ private NoiseTransportImpl noiseTransport ;
16+
17+ @ BeforeEach
18+ void setUp () throws NoSuchAlgorithmException , AEADBadTagException {
19+ final NoiseHandshake initiatorHandshake =
20+ NoiseHandshakeBuilder .forNNInitiator ()
21+ .setComponentsFromProtocolName ("Noise_NN_25519_AESGCM_SHA256" )
22+ .build ();
23+
24+ final NoiseHandshake responderHandshake =
25+ NoiseHandshakeBuilder .forNNResponder ()
26+ .setComponentsFromProtocolName ("Noise_NN_25519_AESGCM_SHA256" )
27+ .build ();
28+
29+ responderHandshake .readMessage (initiatorHandshake .writeMessage ((byte []) null ));
30+ initiatorHandshake .readMessage (responderHandshake .writeMessage ((byte []) null ));
31+
32+ noiseTransport = (NoiseTransportImpl ) initiatorHandshake .toTransport ();
33+ }
34+
35+ @ Test
36+ void getPlaintextLength () {
37+ final int ciphertextLength = 77 ;
38+ assertEquals (ciphertextLength - 16 , noiseTransport .getPlaintextLength (ciphertextLength ));
39+ }
40+
41+ @ Test
42+ void getCiphertextLength () {
43+ final int plaintextLength = 83 ;
44+ assertEquals (plaintextLength + 16 , noiseTransport .getCiphertextLength (plaintextLength ));
45+ }
46+
47+ @ Test
48+ void writeMessageOversize () {
49+ // We want to make sure we're testing the size of the resulting message (which may include key material and AEAD
50+ // tags) rather than the length of just the payload
51+ final int plaintextLength = NoiseTransportImpl .MAX_NOISE_MESSAGE_SIZE - 1 ;
52+ final int messageLength = noiseTransport .getCiphertextLength (plaintextLength );
53+
54+ assertTrue (messageLength > NoiseTransportImpl .MAX_NOISE_MESSAGE_SIZE );
55+
56+ assertThrows (IllegalArgumentException .class ,
57+ () -> noiseTransport .writeMessage (new byte [plaintextLength ]));
58+
59+ assertThrows (IllegalArgumentException .class ,
60+ () -> noiseTransport .writeMessage (new byte [plaintextLength ], 0 , plaintextLength , new byte [messageLength ], 0 ));
61+
62+ assertThrows (IllegalArgumentException .class ,
63+ () -> noiseTransport .writeMessage (ByteBuffer .allocate (plaintextLength )));
64+
65+ assertThrows (IllegalArgumentException .class ,
66+ () -> noiseTransport .writeMessage (ByteBuffer .allocate (plaintextLength ), ByteBuffer .allocate (messageLength )));
67+ }
68+
69+ @ Test
70+ void writeMessageShortBuffer () {
71+ final byte [] plaintext = new byte [32 ];
72+ final byte [] message = new byte [noiseTransport .getCiphertextLength (plaintext .length ) - 1 ];
73+
74+ assertThrows (ShortBufferException .class , () ->
75+ noiseTransport .writeMessage (plaintext , 0 , plaintext .length , message , 0 ));
76+
77+ assertThrows (ShortBufferException .class , () ->
78+ noiseTransport .writeMessage (ByteBuffer .wrap (plaintext ), ByteBuffer .wrap (message )));
79+ }
80+
81+ @ Test
82+ void readMessageOversize () throws NoSuchAlgorithmException {
83+ final int messageLength = NoiseTransportImpl .MAX_NOISE_MESSAGE_SIZE + 1 ;
84+
85+ assertThrows (IllegalArgumentException .class , () ->
86+ noiseTransport .readMessage (new byte [messageLength ]));
87+
88+ assertThrows (IllegalArgumentException .class , () ->
89+ noiseTransport .readMessage (new byte [messageLength ], 0 , messageLength , new byte [messageLength ], 0 ));
90+
91+ assertThrows (IllegalArgumentException .class , () ->
92+ noiseTransport .readMessage (ByteBuffer .allocate (messageLength )));
93+
94+ assertThrows (IllegalArgumentException .class , () ->
95+ noiseTransport .readMessage (ByteBuffer .allocate (messageLength ), ByteBuffer .allocate (messageLength )));
96+ }
97+
98+ @ Test
99+ void readMessageShortBuffer () {
100+ final byte [] message = new byte [128 ];
101+ final int plaintextLength = noiseTransport .getPlaintextLength (message .length );
102+
103+ assertThrows (ShortBufferException .class , () ->
104+ noiseTransport .readMessage (message , 0 , message .length , new byte [plaintextLength - 1 ], 0 ));
105+
106+ assertThrows (ShortBufferException .class , () ->
107+ noiseTransport .readMessage (ByteBuffer .wrap (message ), ByteBuffer .allocate (plaintextLength - 1 )));
108+ }
109+ }
0 commit comments