github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/common/gtest_util.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #include "common/address.h"
    22  #include "common/byte.h"
    23  #include <gtest/gtest.h>
    24  
    25  TEST(test_common_util, simple) {
    26    int32_t v = 123;
    27    neb::byte_t buf[4];
    28    neb::number_to_byte(v, buf, 4);
    29  
    30    int32_t ret = neb::byte_to_number<int32_t>(buf, 4);
    31  
    32    EXPECT_EQ(v, ret);
    33  }
    34  
    35  template <typename NT>
    36  void test_number_bytes(NT v, const std::initializer_list<neb::byte_t> &l) {
    37    neb::bytes b = neb::number_to_byte<neb::bytes>(v);
    38  
    39    neb::bytes wb(l);
    40    EXPECT_EQ(b, wb);
    41    EXPECT_EQ(v, neb::byte_to_number<NT>(wb));
    42  }
    43  TEST(test_common_util_byte, from_uint64) {
    44    test_number_bytes<uint64_t>(0, {0, 0, 0, 0, 0, 0, 0, 0});
    45    test_number_bytes<uint64_t>(1024, {0, 0, 0, 0, 0, 0, 4, 0});
    46    test_number_bytes<uint64_t>(18446744073709551615u,
    47                                {255, 255, 255, 255, 255, 255, 255, 255});
    48  }
    49  
    50  TEST(test_common_util_byte, from_uint32) {
    51    test_number_bytes<uint32_t>(0, {0, 0, 0, 0});
    52    test_number_bytes<uint32_t>(1024, {0, 0, 4, 0});
    53    test_number_bytes<uint32_t>(4294967295u, {255, 255, 255, 255});
    54  }
    55  
    56  TEST(test_common_util_byte, from_uint16) {
    57    test_number_bytes<uint16_t>(0, {0, 0});
    58    test_number_bytes<uint16_t>(1024, {4, 0});
    59    test_number_bytes<uint16_t>(65535u, {255, 255});
    60  }
    61  
    62  TEST(test_common_util, from_int64) {
    63    test_number_bytes<int64_t>(0, {0, 0, 0, 0, 0, 0, 0, 0});
    64    test_number_bytes<int64_t>(1024, {0, 0, 0, 0, 0, 0, 4, 0});
    65    test_number_bytes<int64_t>(-1024, {255, 255, 255, 255, 255, 255, 252, 0});
    66    test_number_bytes<int64_t>(9223372036854775807,
    67                               {127, 255, 255, 255, 255, 255, 255, 255});
    68    test_number_bytes<int64_t>(-9223372036854775808ull,
    69                               {128, 0, 0, 0, 0, 0, 0, 0});
    70  }
    71  
    72  TEST(test_common_util, from_int32) {
    73    test_number_bytes<int32_t>(0, {0, 0, 0, 0});
    74    test_number_bytes<int32_t>(1024, {0, 0, 4, 0});
    75    test_number_bytes<int32_t>(-1024, {255, 255, 252, 0});
    76    test_number_bytes<int32_t>(2147483647, {127, 255, 255, 255});
    77    test_number_bytes<int32_t>(-2147483648, {128, 0, 0, 0});
    78  }
    79  TEST(test_common_util, from_int16) {
    80    test_number_bytes<int16_t>(0, {0, 0});
    81    test_number_bytes<int16_t>(1024, {4, 0});
    82    test_number_bytes<int16_t>(-1024, {252, 0});
    83    test_number_bytes<int16_t>(32767, {127, 255});
    84    test_number_bytes<int16_t>(-32768, {128, 0});
    85  }
    86  
    87  TEST(test_common_util_byte, to_string) {
    88    neb::bytes bytes({72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100});
    89    std::string result = neb::byte_to_string(bytes);
    90    std::string want("Hello, world");
    91    EXPECT_EQ(result, want);
    92  
    93    neb::bytes bs = neb::string_to_byte(result);
    94    EXPECT_EQ(bs, bytes);
    95  }
    96  
    97  TEST(test_common_util_byte, test_default) {
    98    neb::fix_bytes<> fb;
    99  
   100    std::string base58 = fb.to_base58();
   101  
   102    EXPECT_EQ(base58, "11111111111111111111111111111111");
   103  }
   104  
   105  TEST(test_common_util_byte, fix_bytes_to_base58) {
   106    neb::fix_bytes<6> fb({32, 119, 111, 114, 108, 100});
   107  
   108    std::string result = fb.to_base58();
   109    neb::fix_bytes<6> tb = neb::fix_bytes<6>::from_base58(result);
   110  
   111    EXPECT_EQ(fb, tb);
   112  }
   113  
   114  TEST(test_common_util_byte, fix_bytes_to_hex) {
   115    neb::fix_bytes<6> fb({132, 11, 111, 104, 18, 100});
   116  
   117    std::string result = fb.to_hex();
   118    neb::fix_bytes<6> tb = neb::fix_bytes<6>::from_hex(result);
   119    EXPECT_EQ(fb, tb);
   120  
   121    auto tf = [](const std::string &hexstring,
   122                 const std::initializer_list<neb::byte_t> &hexbytes) {
   123      neb::bytes bytes(hexbytes);
   124      EXPECT_EQ(bytes.to_hex(), hexstring);
   125      EXPECT_EQ(bytes, neb::bytes::from_hex(hexstring));
   126    };
   127    tf("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
   128       {167, 255, 198, 248, 191, 30,  215, 102, 81, 193, 71,
   129        86,  160, 97,  214, 98,  245, 128, 255, 77, 228, 59,
   130        73,  250, 130, 216, 10,  75,  128, 248, 67, 74});
   131    tf("3550aba97492de38af3066f0157fc532db6791b37d53262ce7688dcc5d461856",
   132       {53,  80, 171, 169, 116, 146, 222, 56,  175, 48,  102,
   133        240, 21, 127, 197, 50,  219, 103, 145, 179, 125, 83,
   134        38,  44, 231, 104, 141, 204, 93,  70,  24,  86});
   135  
   136    tf("", {});
   137  }
   138  
   139  template <typename T>
   140  void test_constructor() {
   141    T bytes_default;
   142  
   143    int result = 0;
   144    neb::byte_t *value = bytes_default.value();
   145  
   146    if (nullptr == value) {
   147      result = 0;
   148    }else{
   149      result = (int)(*value);
   150    }
   151  
   152    int want = 0;
   153    EXPECT_EQ(result, want);
   154  
   155    T bytes_array({132, 11, 111, 104});
   156    SUCCEED();
   157  
   158    neb::byte_t buf[32] = {53,  80, 171, 169, 116, 146, 222, 56,  175, 48,  102,
   159                           240, 21, 127, 197, 50,  219, 103, 145, 179, 125, 83,
   160                           38,  44, 231, 104, 141, 204, 93,  70,  24,  86};
   161    T bytes_set_length(buf, 32);
   162    result = bytes_set_length.size();
   163    want = 32;
   164    EXPECT_EQ(result, want);
   165  
   166    T bytes_copy(bytes_array);
   167    result = bytes_copy.size();
   168    want = bytes_array.size();
   169    EXPECT_EQ(result, want);
   170  
   171    T bytes_right_value(std::move(bytes_set_length));
   172    result = bytes_right_value.size();
   173    want = bytes_set_length.size();
   174    EXPECT_EQ(result, want);
   175  
   176    T bytes_assignment = bytes_default;
   177    result = bytes_assignment.size();
   178    want = bytes_default.size();
   179    EXPECT_EQ(result, want);
   180  
   181    T bytes_assignment_right_value = std::move(bytes_default);
   182    result = bytes_assignment_right_value.size();
   183    want = bytes_default.size();
   184    EXPECT_EQ(result, want);
   185  
   186    EXPECT_TRUE(bytes_assignment == bytes_default);
   187    EXPECT_TRUE(bytes_assignment_right_value != bytes_set_length);
   188  }
   189  
   190  TEST(test_common_util_byte, fix_byte_constructor) {
   191    test_constructor<neb::fix_bytes<>>();
   192    test_constructor<neb::bytes>();
   193  }
   194  
   195  TEST(test_common_util_byte, throw_make_array) {
   196    EXPECT_THROW(neb::fix_bytes<> bytes(
   197                     {53,  80, 171, 169, 116, 146, 222, 56,  175, 48,  102,
   198                      240, 21, 127, 197, 50,  219, 103, 145, 179, 125, 83,
   199                      38,  44, 231, 104, 141, 204, 93,  70,  24,  86,  100}),
   200                 std::out_of_range);
   201  }
   202  
   203  template<typename T>
   204  void test_throw_invalid_input() {
   205    EXPECT_THROW(T::from_hex("102AfbGG"), std::invalid_argument);
   206    EXPECT_THROW(T::from_base58("wOrld"), std::invalid_argument);
   207  }
   208  
   209  TEST(test_common_util_byte, throw_invalid_input) {
   210    test_throw_invalid_input<neb::fix_bytes<>>();
   211    test_throw_invalid_input<neb::bytes>();
   212  }
   213  
   214  template <size_t N>
   215  void test_fixed_bytes(const std::string &hexstring,
   216                        const std::string &base58_string) {
   217    neb::fix_bytes<N> fb = neb::fix_bytes<N>::from_hex(hexstring);
   218    std::string result = fb.to_base58();
   219  
   220    EXPECT_EQ(result, base58_string);
   221  
   222    neb::bytes bytes = neb::bytes::from_hex(hexstring);
   223    result = bytes.to_base58();
   224  
   225    EXPECT_EQ(result, base58_string);
   226  }
   227  
   228  TEST(test_common_util_byte, base58_encoding_decoding) {
   229    test_fixed_bytes<1>("61", "2g");
   230    test_fixed_bytes<3>("626262", "a3gV");
   231    test_fixed_bytes<3>("636363", "aPEr");
   232    test_fixed_bytes<20>("73696d706c792061206c6f6e6720737472696e67",
   233                         "2cFupjhnEsSn59qHXstmK2ffpLv2");
   234    test_fixed_bytes<25>("00eb15231dfceb60925886b67d065299925915aeb172c06647",
   235                         "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L");
   236    test_fixed_bytes<5>("516b6fcd0f", "ABnLTmg");
   237    test_fixed_bytes<9>("bf4f89001e670274dd", "3SEo3LWLoPntC");
   238    test_fixed_bytes<4>("572e4794", "3EFU7m");
   239    test_fixed_bytes<10>("ecac89cad93923c02321", "EJDM8drfXA6uyA");
   240    test_fixed_bytes<4>("10c8511e", "Rt5zm");
   241    test_fixed_bytes<10>("00000000000000000000", "1111111111");
   242    test_fixed_bytes<43>(
   243        "000111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd4"
   244        "3dc62a641155a5",
   245        "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
   246  }
   247  
   248  template<typename T>
   249  void test_base64_encoding_decoding(const std::string& input) {
   250    T b = T::from_base64(input);
   251    std::string result = b.to_base64();
   252  
   253    EXPECT_EQ(result, input);
   254  }
   255  
   256  TEST(test_common_util_byte, base64_encoding_decoding) {
   257    std::string input(
   258        "TmVidWxhcyBpcyBhIG5leHQgZ2VuZXJhdGlvbiBwdWJsaWMgYmxvY2tjaGFpbiwgYWltaW5n"
   259        "IGZvciBhIGNvbnRpbnVvdXNseSBpbXByb3ZpbmcgZWNvc3lzdGVtLg==");
   260    test_base64_encoding_decoding<neb::fix_bytes<94>>(input);
   261    test_base64_encoding_decoding<neb::bytes>(input);
   262  }
   263  
   264  TEST(test_common, address) {
   265    std::string base58_str("n1EjkdHBDpdjFfVaeJqMQW11RhYqrrjCZR7");
   266    auto ret_bytes = neb::bytes::from_base58(base58_str);
   267    std::string s = neb::address_to_string(ret_bytes);
   268    std::string t = std::to_string(ret_bytes);
   269    EXPECT_EQ(s, t);
   270  }