Fix build for boost 1.71.0
[platform/core/security/libwebappenc.git] / tests / test-common.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  *
16  *
17  * @file        test-common.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  */
21 #include "test-common.h"
22
23 #include <sstream>
24 #include <iomanip>
25 #include <string>
26
27 namespace Wae {
28 namespace Test {
29
30 std::string bytes_to_hex(const std::vector<unsigned char> &bytes)
31 {
32         std::stringstream ss;
33         ss << std::hex;
34
35         for (auto b : bytes)
36                 ss << std::setw(2) << std::setfill('0') << static_cast<int>(b);
37
38         return ss.str();
39 }
40
41 std::string bytes_to_hex(const raw_buffer_s *rb)
42 {
43         if (!is_buffer_valid(rb))
44                 return std::string();
45
46         return bytes_to_hex(rb->buf, rb->size);
47 }
48
49 std::string bytes_to_hex(const unsigned char *ptr, size_t len)
50 {
51         std::stringstream ss;
52         ss << std::hex;
53
54         for (size_t i = 0; i < len; ++i)
55                 ss << std::setw(2) << std::setfill('0') << static_cast<int>(ptr[i]);
56
57         return ss.str();
58 }
59
60 std::vector<unsigned char> bytearr_to_vec(const raw_buffer_s *rb)
61 {
62         if (!is_buffer_valid(rb))
63                 return std::vector<unsigned char>();
64
65         return bytearr_to_vec(rb->buf, rb->size);
66 }
67
68 std::vector<unsigned char> bytearr_to_vec(const unsigned char *ptr, size_t len)
69 {
70         if (ptr == nullptr || len == 0)
71                 return std::vector<unsigned char>();
72
73         std::vector<unsigned char> vec(len);
74
75         for (size_t i = 0; i < len; ++i)
76                 vec[i] = ptr[i];
77
78         return vec;
79 }
80
81 } // namespace Test
82 } // namespace Wae