public void PeekArrayTest() { // arrange ByteCircularBuffer target; byte[] expected; byte[] actual; target = new ByteCircularBuffer(10); target.Put(1); target.Put(2); target.Put(3); expected = new byte[] { 1, 2 }; // act actual = target.Peek(2); // assert actual.Should(). Equal(expected); }
public void PeekEmptyExceptionTest() { // arrange ByteCircularBuffer target; target = new ByteCircularBuffer(10); // act & assert Assert.That(() => target.Peek(), Throws.TypeOf <InvalidOperationException>()); }
public void PeekTest() { // arrange ByteCircularBuffer target; byte expected; byte actual; target = new ByteCircularBuffer(10); target.Put(1); target.Put(2); target.Put(3); expected = 1; // act actual = target.Peek(); // assert actual.Should(). Be(expected); }
public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { Debug.Assert(_decCircularBuffer != null, "_decCircularBuffer != null"); int bufSize; outlength = 0; // drop all into buffer _decCircularBuffer.Put(buf, 0, length); Logging.Debug("---Start Decryption"); if (!_decryptSaltReceived) { bufSize = _decCircularBuffer.Size; // check if we get the leading salt if (bufSize <= saltLen) { // need more return; } _decryptSaltReceived = true; byte[] salt = _decCircularBuffer.Get(saltLen); InitCipher(salt, false, false); Logging.Debug("get salt len " + saltLen); } // handle chunks while (true) { bufSize = _decCircularBuffer.Size; // check if we have any data if (bufSize <= 0) { Logging.Debug("No data in _decCircularBuffer"); return; } // first get chunk length if (bufSize <= CHUNK_LEN_BYTES + tagLen) { // so we only have chunk length and its tag? return; } #region Chunk Decryption byte[] encLenBytes = _decCircularBuffer.Peek(CHUNK_LEN_BYTES + tagLen); uint decChunkLenLength = 0; byte[] decChunkLenBytes = new byte[CHUNK_LEN_BYTES]; // try to dec chunk len cipherDecrypt(encLenBytes, CHUNK_LEN_BYTES + (uint)tagLen, decChunkLenBytes, ref decChunkLenLength); Debug.Assert(decChunkLenLength == CHUNK_LEN_BYTES); // finally we get the real chunk len ushort chunkLen = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(decChunkLenBytes, 0)); if (chunkLen > CHUNK_LEN_MASK) { // we get invalid chunk Logging.Error($"Invalid chunk length: {chunkLen}"); throw new CryptoErrorException(); } Logging.Debug("Get the real chunk len:" + chunkLen); bufSize = _decCircularBuffer.Size; if (bufSize < CHUNK_LEN_BYTES + tagLen /* we haven't remove them */ + chunkLen + tagLen) { Logging.Debug("No more data to decrypt one chunk"); return; } IncrementNonce(false); // we have enough data to decrypt one chunk // drop chunk len and its tag from buffer _decCircularBuffer.Skip(CHUNK_LEN_BYTES + tagLen); byte[] encChunkBytes = _decCircularBuffer.Get(chunkLen + tagLen); byte[] decChunkBytes = new byte[chunkLen]; uint decChunkLen = 0; cipherDecrypt(encChunkBytes, chunkLen + (uint)tagLen, decChunkBytes, ref decChunkLen); Debug.Assert(decChunkLen == chunkLen); IncrementNonce(false); #endregion // output to outbuf Buffer.BlockCopy(decChunkBytes, 0, outbuf, outlength, (int)decChunkLen); outlength += (int)decChunkLen; Logging.Debug("aead dec outlength " + outlength); if (outlength + 100 > TCPHandler.BufferSize) { Logging.Debug("dec outbuf almost full, giving up"); return; } bufSize = _decCircularBuffer.Size; // check if we already done all of them if (bufSize <= 0) { Logging.Debug("No data in _decCircularBuffer, already all done"); return; } } }