# < Summary

Information

Class: | Renci.SshNet.Messages.Message
Assembly: | Renci.SshNet
File(s): | \home\appveyor\projects\ssh-net\src\Renci.SshNet\Messages\Message.cs

Line coverage
85%

Covered lines: | 69
Uncovered lines: | 12
Coverable lines: | 81
Total lines: | 191
Line coverage: | 85.1%

Branch coverage
64%

Covered branches: | 9
Total branches: | 14
Branch coverage: | 64.2%

Method coverage

Feature is only available for sponsors

Upgrade to PRO version

# Metrics

Method | Branch coverage | Cyclomatic complexity | Line coverage
--- | --- | --- | ---
get_BufferCapacity() | 100% | 1 | 100%
WriteBytes(...) | 50% | 2 | 85.71%
GetPacket(...) | 62.5% | 8 | 82.22%
GetPacketDataLength(...) | 100% | 1 | 100%
GetPaddingLength(...) | 100% | 2 | 100%
ToString() | 50% | 2 | 75%

# File(s)

## \home\appveyor\projects\ssh-net\src\Renci.SshNet\Messages\Message.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System.Globalization;`
|  | `2` |  | `using System.IO;`
|  | `3` |  | ``
|  | `4` |  | `using Renci.SshNet.Abstractions;`
|  | `5` |  | `using Renci.SshNet.Common;`
|  | `6` |  | `using Renci.SshNet.Compression;`
|  | `7` |  | ``
|  | `8` |  | `namespace Renci.SshNet.Messages`
|  | `9` |  | `{`
|  | `10` |  | `/// <summary>`
|  | `11` |  | `/// Base class for all SSH protocol messages.`
|  | `12` |  | `/// </summary>`
|  | `13` |  | `public abstract class Message : SshData`
|  | `14` |  | `{`
|  | `15` |  | `/// <summary>`
|  | `16` |  | `/// Gets the size of the message in bytes.`
|  | `17` |  | `/// </summary>`
|  | `18` |  | `/// <value>`
|  | `19` |  | `/// The size of the messages in bytes.`
|  | `20` |  | `/// </value>`
|  | `21` |  | `protected override int BufferCapacity`
|  | `22` |  | `{`
|  | `23` |  | `get`
| 52720 | `24` |  | `{`
| 52720 | `25` |  | `return 1; // Message type`
| 52720 | `26` |  | `}`
|  | `27` |  | `}`
|  | `28` |  | ``
|  | `29` |  | `/// <summary>`
|  | `30` |  | `/// Writes the message to the specified <see cref="SshDataStream"/>.`
|  | `31` |  | `/// </summary>`
|  | `32` |  | `/// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param>`
|  | `33` |  | `protected override void WriteBytes(SshDataStream stream)`
| 56944 | `34` |  | `{`
| 56944 | `35` |  | `var enumerator = GetType().GetCustomAttributes<MessageAttribute>(inherit: true).GetEnumerator();`
|  | `36` |  | `try`
| 56944 | `37` |  | `{`
| 56944 | `38` |  | `if (!enumerator.MoveNext())`
| 0 | `39` |  | `{`
| 0 | `40` |  | `throw new SshException(string.Format(CultureInfo.CurrentCulture, "Type '{0}' is not a valid message`
|  | `41` |  | `}`
|  | `42` |  | ``
| 56944 | `43` |  | `var messageAttribute = enumerator.Current;`
| 56944 | `44` |  | `stream.WriteByte(messageAttribute.Number);`
| 56944 | `45` |  | `base.WriteBytes(stream);`
| 56944 | `46` |  | `}`
|  | `47` |  | `finally`
| 56944 | `48` |  | `{`
| 56944 | `49` |  | `enumerator.Dispose();`
| 56944 | `50` |  | `}`
| 56944 | `51` |  | `}`
|  | `52` |  | ``
|  | `53` |  | `internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor)`
| 54510 | `54` |  | `{`
|  | `55` |  | `const int outboundPacketSequenceSize = 4;`
|  | `56` |  | ``
| 54510 | `57` |  | `var messageLength = BufferCapacity;`
|  | `58` |  | ``
| 54510 | `59` |  | `if (messageLength == -1 || compressor != null)`
| 1823 | `60` |  | `{`
| 1823 | `61` |  | `using (var sshDataStream = new SshDataStream(DefaultCapacity))`
| 1823 | `62` |  | `{`
|  | `63` |  | `// skip:`
|  | `64` |  | `// * 4 bytes for the outbound packet sequence`
|  | `65` |  | `// * 4 bytes for the packet data length`
|  | `66` |  | `// * one byte for the packet padding length`
| 1823 | `67` |  | `_ = sshDataStream.Seek(outboundPacketSequenceSize + 4 + 1, SeekOrigin.Begin);`
|  | `68` |  | ``
| 1823 | `69` |  | `if (compressor != null)`
| 0 | `70` |  | `{`
|  | `71` |  | `// obtain uncompressed message payload`
| 0 | `72` |  | `using (var uncompressedDataStream = new SshDataStream(messageLength != -1 ? messageLength : Defa`
| 0 | `73` |  | `{`
| 0 | `74` |  | `WriteBytes(uncompressedDataStream);`
|  | `75` |  | ``
|  | `76` |  | `// compress message payload`
| 0 | `77` |  | `var compressedMessageData = compressor.Compress(uncompressedDataStream.ToArray());`
|  | `78` |  | ``
|  | `79` |  | `// add compressed message payload`
| 0 | `80` |  | `sshDataStream.Write(compressedMessageData, 0, compressedMessageData.Length);`
| 0 | `81` |  | `}`
| 0 | `82` |  | `}`
|  | `83` |  | `else`
| 1823 | `84` |  | `{`
|  | `85` |  | `// add message payload`
| 1823 | `86` |  | `WriteBytes(sshDataStream);`
| 1823 | `87` |  | `}`
|  | `88` |  | ``
| 1823 | `89` |  | `messageLength = (int) sshDataStream.Length - (outboundPacketSequenceSize + 4 + 1);`
|  | `90` |  | ``
| 1823 | `91` |  | `var packetLength = messageLength + 4 + 1;`
|  | `92` |  | ``
|  | `93` |  | `// determine the padding length`
| 1823 | `94` |  | `var paddingLength = GetPaddingLength(paddingMultiplier, packetLength);`
|  | `95` |  | ``
|  | `96` |  | `// add padding bytes`
| 1823 | `97` |  | `var paddingBytes = new byte[paddingLength];`
| 1823 | `98` |  | `CryptoAbstraction.GenerateRandom(paddingBytes);`
| 1823 | `99` |  | `sshDataStream.Write(paddingBytes, 0, paddingLength);`
|  | `100` |  | ``
| 1823 | `101` |  | `var packetDataLength = GetPacketDataLength(messageLength, paddingLength);`
|  | `102` |  | ``
|  | `103` |  | `// skip bytes for outbound packet sequence`
| 1823 | `104` |  | `_ = sshDataStream.Seek(outboundPacketSequenceSize, SeekOrigin.Begin);`
|  | `105` |  | ``
|  | `106` |  | `// add packet data length`
| 1823 | `107` |  | `sshDataStream.Write(packetDataLength);`
|  | `108` |  | ``
|  | `109` |  | `// add packet padding length`
| 1823 | `110` |  | `sshDataStream.WriteByte(paddingLength);`
|  | `111` |  | ``
| 1823 | `112` |  | `return sshDataStream.ToArray();`
|  | `113` |  | `}`
|  | `114` |  | `}`
|  | `115` |  | `else`
| 52687 | `116` |  | `{`
| 52687 | `117` |  | `var packetLength = messageLength + 4 + 1;`
|  | `118` |  | ``
|  | `119` |  | `// determine the padding length`
| 52687 | `120` |  | `var paddingLength = GetPaddingLength(paddingMultiplier, packetLength);`
|  | `121` |  | ``
| 52687 | `122` |  | `var packetDataLength = GetPacketDataLength(messageLength, paddingLength);`
|  | `123` |  | ``
|  | `124` |  | `// lets construct an SSH data stream of the exact size required`
| 52687 | `125` |  | `using (var sshDataStream = new SshDataStream(packetLength + paddingLength + outboundPacketSequenceSize))`
| 52687 | `126` |  | `{`
|  | `127` |  | `// skip bytes for outbound packet sequenceSize`
| 52687 | `128` |  | `_ = sshDataStream.Seek(outboundPacketSequenceSize, SeekOrigin.Begin);`
|  | `129` |  | ``
|  | `130` |  | `// add packet data length`
| 52687 | `131` |  | `sshDataStream.Write(packetDataLength);`
|  | `132` |  | ``
|  | `133` |  | `// add packet padding length`
| 52687 | `134` |  | `sshDataStream.WriteByte(paddingLength);`
|  | `135` |  | ``
|  | `136` |  | `// add message payload`
| 52687 | `137` |  | `WriteBytes(sshDataStream);`
|  | `138` |  | ``
|  | `139` |  | `// add padding bytes`
| 52687 | `140` |  | `var paddingBytes = new byte[paddingLength];`
| 52687 | `141` |  | `CryptoAbstraction.GenerateRandom(paddingBytes);`
| 52687 | `142` |  | `sshDataStream.Write(paddingBytes, 0, paddingLength);`
|  | `143` |  | ``
| 52687 | `144` |  | `return sshDataStream.ToArray();`
|  | `145` |  | `}`
|  | `146` |  | `}`
| 54510 | `147` |  | `}`
|  | `148` |  | ``
|  | `149` |  | `private static uint GetPacketDataLength(int messageLength, byte paddingLength)`
| 54510 | `150` |  | `{`
| 54510 | `151` |  | `return (uint) (messageLength + paddingLength + 1);`
| 54510 | `152` |  | `}`
|  | `153` |  | ``
|  | `154` |  | `private static byte GetPaddingLength(byte paddingMultiplier, long packetLength)`
| 54510 | `155` |  | `{`
| 54510 | `156` |  | `var paddingLength = (byte)((-packetLength) & (paddingMultiplier - 1));`
|  | `157` |  | ``
| 54510 | `158` |  | `if (paddingLength < paddingMultiplier)`
| 54510 | `159` |  | `{`
| 54510 | `160` |  | `paddingLength += paddingMultiplier;`
| 54510 | `161` |  | `}`
|  | `162` |  | ``
| 54510 | `163` |  | `return paddingLength;`
| 54510 | `164` |  | `}`
|  | `165` |  | ``
|  | `166` |  | `/// <summary>`
|  | `167` |  | `/// Returns a <see cref="string"/> that represents this instance.`
|  | `168` |  | `/// </summary>`
|  | `169` |  | `/// <returns>`
|  | `170` |  | `/// A <see cref="string"/> that represents this instance.`
|  | `171` |  | `/// </returns>`
|  | `172` |  | `public override string ToString()`
| 106374 | `173` |  | `{`
| 106374 | `174` |  | `using (var enumerator = GetType().GetCustomAttributes<MessageAttribute>(inherit: true).GetEnumerator())`
| 106374 | `175` |  | `{`
| 106374 | `176` |  | `if (!enumerator.MoveNext())`
| 0 | `177` |  | `{`
| 0 | `178` |  | `return string.Format(CultureInfo.CurrentCulture, "'{0}' without Message attribute.", GetType().FullN`
|  | `179` |  | `}`
|  | `180` |  | ``
| 106374 | `181` |  | `return enumerator.Current.Name;`
|  | `182` |  | `}`
| 106374 | `183` |  | `}`
|  | `184` |  | ``
|  | `185` |  | `/// <summary>`
|  | `186` |  | `/// Process the current message for the specified <see cref="Session"/>.`
|  | `187` |  | `/// </summary>`
|  | `188` |  | `/// <param name="session">The <see cref="Session"/> for which to process the current message.</param>`
|  | `189` |  | `internal abstract void Process(Session session);`
|  | `190` |  | `}`
|  | `191` |  | `}`

# Methods/Properties

get_BufferCapacity()
WriteBytes(Renci.SshNet.Common.SshDataStream)
GetPacket(System.Byte,Renci.SshNet.Compression.Compressor)
GetPacketDataLength(System.Int32,System.Byte)
GetPaddingLength(System.Byte,System.Int64)
ToString()