コード例 #1
0
ファイル: PhysicalBlock.cs プロジェクト: tatmanblue/ironbar
        public static PhysicalBlock FromString(string data)
        {
            string[] elements = data.Split(":");
            DateTime timeStamp;

            if (false == DateTime.TryParseExact(elements[5], Constants.DateTimeFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out timeStamp))
            {
                throw new LedgerNotValidException($"{elements[5]}");
            }

            PhysicalBlock block = new PhysicalBlock()
            {
                ParentId        = Convert.ToInt32(elements[0]),
                Id              = Convert.ToInt32(elements[1]),
                ParentHash      = elements[2],
                ReferenceId     = Convert.ToInt32(elements[3]),
                LedgerId        = Convert.ToInt32(elements[4]),
                TimeStamp       = timeStamp,
                Nonce           = Nonce.FromString(elements[6]),
                TransactionData = Encoding.UTF8.GetBytes(elements[7])
            };

            // compare hash from string with computedHash. they should match
            if (elements[8] != block.ComputeHash())
            {
                throw new LedgerNotValidException($"block {block.Id}");
            }

            return(block);
        }
コード例 #2
0
        public void TestToString()
        {
            // Arrange
            var expected = "123:text";

            // Act
            var actual = Nonce.FromString(expected).ToString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void EqualsCreateFromStringAndDate()
        {
            // Arrange
            var expected = Nonce.FromDate(DateTime.Now);
            var actual   = Nonce.FromString(expected.ToString());

            // Act
            var result = actual.Equals(expected);

            // Assert
            Assert.IsTrue(result);
        }
コード例 #4
0
        public void Parse()
        {
            // Arrange
            var date     = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var expected = $"{date}:text";

            // Act
            var actual = Nonce.FromString(expected);

            // Assert
            Assert.AreEqual(expected, actual.ToString());
        }
コード例 #5
0
        public void ParseInvalidStringThrows()
        {
            // Arrange
            var    noDelimiter   = "1234";
            var    noAppender    = "1234:";
            var    noDate        = ":1234";
            var    delimiterOnly = ":";
            var    empty         = string.Empty;
            string nullString    = null;

            // Act && Assert
            Assert.Throws <AuthException>(() => { Nonce.FromString(noDelimiter); });
            Assert.Throws <AuthException>(() => { Nonce.FromString(noAppender); });
            Assert.Throws <AuthException>(() => { Nonce.FromString(noDate); });
            Assert.Throws <AuthException>(() => { Nonce.FromString(delimiterOnly); });
            Assert.Throws <AuthException>(() => { Nonce.FromString(empty); });
            Assert.Throws <AuthException>(() => { Nonce.FromString(nullString); });
        }