/// <summary> /// Generates a random IPv6 address. /// </summary> public string Ipv6() { var bytes = Random.Bytes(16); return ($"{bytes[0]:x}{bytes[1]:x}:{bytes[2]:x}{bytes[3]:x}:{bytes[4]:x}{bytes[5]:x}:{bytes[6]:x}{bytes[7]:x}:{bytes[8]:x}{bytes[9]:x}:{bytes[10]:x}{bytes[11]:x}:{bytes[12]:x}{bytes[13]:x}:{bytes[14]:x}{bytes[15]:x}"); }
public Transaction(Output[] Outputs, string Id = null) { this.Id = Id ?? Hash.Sha1(Random.Bytes()); this.Outputs = Outputs; Type = TransactionType.REWARD; InputsConcatenated = Inputs.Stringified(); OutputsConcatenated = Outputs.Stringified(); }
public Transaction(Input[] Inputs, Output[] Outputs, string Id = null) { this.Id = Id ?? Hash.Sha1(Random.Bytes()); this.Inputs = Inputs; this.Outputs = Outputs; InputsConcatenated = Inputs.Stringified(); OutputsConcatenated = Outputs.Stringified(); }
/// <summary> /// Thread-safe wrapper for Bytes(). /// </summary> /// <param name="length">Number of random bytes to return.</param> /// <returns>Array of random bytes.</returns> public sealed override byte[] Bytes(int length) { byte[] arr; lock (this) { arr = _rand.Bytes(length); } return(arr); }
public void DeserializationFailsWithInvalidFrameTerminator() { var buffer = new ArrayBufferWriter <Byte>(8); var value = new RawFrame(Random.Enum <FrameType>(), Random.UShort(), Random.Bytes(Random.UShort())); value.Serialize(buffer); var modifiedBuffer = buffer.WrittenMemory.ToArray(); modifiedBuffer[modifiedBuffer.Length - 1] = 0x00; Assert.Throws <FramingErrorException>(() => RawFrame.Deserialize(modifiedBuffer.AsSpan(), out var _, out var _)); }
public void SerializationIsSymmetric() { var buffer = new ArrayBufferWriter <Byte>(); var value = new RawFrame(Random.Enum <FrameType>(), Random.UShort(), Random.Bytes(Random.UShort())); value.Serialize(buffer); RawFrame.Deserialize(buffer.WrittenMemory.Span, out var deserialized, out var _); Assert.Equal(expected: value.Channel, actual: deserialized.Channel); Assert.Equal(expected: value.Payload.ToArray(), actual: deserialized.Payload.ToArray()); Assert.Equal(expected: value.Size, actual: deserialized.Size); Assert.Equal(expected: value.Type, actual: deserialized.Type); }
public void DeserializationReturnsSurplusData() { var value = new RawFrame(Random.Enum <FrameType>(), Random.UShort(), Random.Bytes(Random.UShort())); var extra = Random.UInt(); var buffer = new ArrayBufferWriter <Byte>(12); buffer.WriteSerializable(value) .WriteUInt32LE(extra); RawFrame.Deserialize(buffer.WrittenMemory.Span, out var _, out var surplus); Assert.Equal(expected: sizeof(UInt32), actual: surplus.Length); Assert.Equal(expected: extra, actual: BitConverter.ToUInt32(surplus)); }
[Test] public void Robustness() { // Blast the buffer with noise to make sure it can handle any input var settings = new VT100.Settings(); var buf = new VT100.Buffer(settings); buf.ReportUnsupportedEscapeSequences = false; var rnd = new Random(0); for (int i = 0; i != 10000; ++i) { var noise = rnd.Bytes().Take(rnd.Next(1000)).Select(x => (char)x).ToArray(); buf.Output(new string(noise)); } }
public void GetForwardedIpAddress_When_Headers_Contains_XForwardedFor_Key_With_Value_Returns_Value() { // Arrange var mockIpAddress = new IPAddress(Random.Bytes(4)); var headerDictionary = new HeaderDictionary(); headerDictionary.Add(HttpRequestExtensions.X_FORWARDED_FOR, mockIpAddress.ToString()); var mockSut = new Mock <HttpRequest>(); mockSut.Setup((e) => e.Headers).Returns(value: headerDictionary); var sut = mockSut.Object; // Act var result = sut.GetForwardedIpAddress(); // Assert result.ShouldBe(mockIpAddress.ToString()); }
/// <summary> /// Draw an array of random and uniform bytes. /// </summary> /// <param name="length">The array length requested.</param> public sealed override byte[] Bytes(int length) { Switch(); return(Rand.Bytes(length)); }