You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
9.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2009, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <vector>
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/json.h"
  30. static Json::Value in_s("foo");
  31. static Json::Value in_sn("99");
  32. static Json::Value in_si("-99");
  33. static Json::Value in_sb("true");
  34. static Json::Value in_sd("1.2");
  35. static Json::Value in_n(12);
  36. static Json::Value in_i(-12);
  37. static Json::Value in_u(34U);
  38. static Json::Value in_b(true);
  39. static Json::Value in_d(1.2);
  40. static Json::Value big_sn("12345678901234567890");
  41. static Json::Value big_si("-12345678901234567890");
  42. static Json::Value big_u(0xFFFFFFFF);
  43. static Json::Value bad_a(Json::arrayValue);
  44. static Json::Value bad_o(Json::objectValue);
  45. TEST(JsonTest, GetString) {
  46. std::string out;
  47. EXPECT_TRUE(GetStringFromJson(in_s, &out));
  48. EXPECT_EQ("foo", out);
  49. EXPECT_TRUE(GetStringFromJson(in_sn, &out));
  50. EXPECT_EQ("99", out);
  51. EXPECT_TRUE(GetStringFromJson(in_si, &out));
  52. EXPECT_EQ("-99", out);
  53. EXPECT_TRUE(GetStringFromJson(in_i, &out));
  54. EXPECT_EQ("-12", out);
  55. EXPECT_TRUE(GetStringFromJson(in_n, &out));
  56. EXPECT_EQ("12", out);
  57. EXPECT_TRUE(GetStringFromJson(in_u, &out));
  58. EXPECT_EQ("34", out);
  59. EXPECT_TRUE(GetStringFromJson(in_b, &out));
  60. EXPECT_EQ("true", out);
  61. // Not supported here yet.
  62. EXPECT_FALSE(GetStringFromJson(bad_a, &out));
  63. EXPECT_FALSE(GetStringFromJson(bad_o, &out));
  64. }
  65. TEST(JsonTest, GetInt) {
  66. int out;
  67. EXPECT_TRUE(GetIntFromJson(in_sn, &out));
  68. EXPECT_EQ(99, out);
  69. EXPECT_TRUE(GetIntFromJson(in_si, &out));
  70. EXPECT_EQ(-99, out);
  71. EXPECT_TRUE(GetIntFromJson(in_n, &out));
  72. EXPECT_EQ(12, out);
  73. EXPECT_TRUE(GetIntFromJson(in_i, &out));
  74. EXPECT_EQ(-12, out);
  75. EXPECT_TRUE(GetIntFromJson(in_u, &out));
  76. EXPECT_EQ(34, out);
  77. EXPECT_TRUE(GetIntFromJson(in_b, &out));
  78. EXPECT_EQ(1, out);
  79. EXPECT_FALSE(GetIntFromJson(in_s, &out));
  80. EXPECT_FALSE(GetIntFromJson(big_sn, &out));
  81. EXPECT_FALSE(GetIntFromJson(big_si, &out));
  82. EXPECT_FALSE(GetIntFromJson(big_u, &out));
  83. EXPECT_FALSE(GetIntFromJson(bad_a, &out));
  84. EXPECT_FALSE(GetIntFromJson(bad_o, &out));
  85. }
  86. TEST(JsonTest, GetUInt) {
  87. unsigned int out;
  88. EXPECT_TRUE(GetUIntFromJson(in_sn, &out));
  89. EXPECT_EQ(99U, out);
  90. EXPECT_TRUE(GetUIntFromJson(in_n, &out));
  91. EXPECT_EQ(12U, out);
  92. EXPECT_TRUE(GetUIntFromJson(in_u, &out));
  93. EXPECT_EQ(34U, out);
  94. EXPECT_TRUE(GetUIntFromJson(in_b, &out));
  95. EXPECT_EQ(1U, out);
  96. EXPECT_TRUE(GetUIntFromJson(big_u, &out));
  97. EXPECT_EQ(0xFFFFFFFFU, out);
  98. EXPECT_FALSE(GetUIntFromJson(in_s, &out));
  99. // TODO: Fail reading negative strings.
  100. // EXPECT_FALSE(GetUIntFromJson(in_si, &out));
  101. EXPECT_FALSE(GetUIntFromJson(in_i, &out));
  102. EXPECT_FALSE(GetUIntFromJson(big_sn, &out));
  103. EXPECT_FALSE(GetUIntFromJson(big_si, &out));
  104. EXPECT_FALSE(GetUIntFromJson(bad_a, &out));
  105. EXPECT_FALSE(GetUIntFromJson(bad_o, &out));
  106. }
  107. TEST(JsonTest, GetBool) {
  108. bool out;
  109. EXPECT_TRUE(GetBoolFromJson(in_sb, &out));
  110. EXPECT_EQ(true, out);
  111. EXPECT_TRUE(GetBoolFromJson(in_n, &out));
  112. EXPECT_EQ(true, out);
  113. EXPECT_TRUE(GetBoolFromJson(in_i, &out));
  114. EXPECT_EQ(true, out);
  115. EXPECT_TRUE(GetBoolFromJson(in_u, &out));
  116. EXPECT_EQ(true, out);
  117. EXPECT_TRUE(GetBoolFromJson(in_b, &out));
  118. EXPECT_EQ(true, out);
  119. EXPECT_TRUE(GetBoolFromJson(big_u, &out));
  120. EXPECT_EQ(true, out);
  121. EXPECT_FALSE(GetBoolFromJson(in_s, &out));
  122. EXPECT_FALSE(GetBoolFromJson(in_sn, &out));
  123. EXPECT_FALSE(GetBoolFromJson(in_si, &out));
  124. EXPECT_FALSE(GetBoolFromJson(big_sn, &out));
  125. EXPECT_FALSE(GetBoolFromJson(big_si, &out));
  126. EXPECT_FALSE(GetBoolFromJson(bad_a, &out));
  127. EXPECT_FALSE(GetBoolFromJson(bad_o, &out));
  128. }
  129. TEST(JsonTest, GetDouble) {
  130. double out;
  131. EXPECT_TRUE(GetDoubleFromJson(in_sn, &out));
  132. EXPECT_EQ(99, out);
  133. EXPECT_TRUE(GetDoubleFromJson(in_si, &out));
  134. EXPECT_EQ(-99, out);
  135. EXPECT_TRUE(GetDoubleFromJson(in_sd, &out));
  136. EXPECT_EQ(1.2, out);
  137. EXPECT_TRUE(GetDoubleFromJson(in_n, &out));
  138. EXPECT_EQ(12, out);
  139. EXPECT_TRUE(GetDoubleFromJson(in_i, &out));
  140. EXPECT_EQ(-12, out);
  141. EXPECT_TRUE(GetDoubleFromJson(in_u, &out));
  142. EXPECT_EQ(34, out);
  143. EXPECT_TRUE(GetDoubleFromJson(in_b, &out));
  144. EXPECT_EQ(1, out);
  145. EXPECT_TRUE(GetDoubleFromJson(in_d, &out));
  146. EXPECT_EQ(1.2, out);
  147. EXPECT_FALSE(GetDoubleFromJson(in_s, &out));
  148. }
  149. TEST(JsonTest, GetFromArray) {
  150. Json::Value a, out;
  151. a.append(in_s);
  152. a.append(in_i);
  153. a.append(in_u);
  154. a.append(in_b);
  155. EXPECT_TRUE(GetValueFromJsonArray(a, 0, &out));
  156. EXPECT_TRUE(GetValueFromJsonArray(a, 3, &out));
  157. EXPECT_FALSE(GetValueFromJsonArray(a, 99, &out));
  158. EXPECT_FALSE(GetValueFromJsonArray(a, 0xFFFFFFFF, &out));
  159. }
  160. TEST(JsonTest, GetFromObject) {
  161. Json::Value o, out;
  162. o["string"] = in_s;
  163. o["int"] = in_i;
  164. o["uint"] = in_u;
  165. o["bool"] = in_b;
  166. EXPECT_TRUE(GetValueFromJsonObject(o, "int", &out));
  167. EXPECT_TRUE(GetValueFromJsonObject(o, "bool", &out));
  168. EXPECT_FALSE(GetValueFromJsonObject(o, "foo", &out));
  169. EXPECT_FALSE(GetValueFromJsonObject(o, "", &out));
  170. }
  171. namespace {
  172. template <typename T>
  173. std::vector<T> VecOf3(const T& a, const T& b, const T& c) {
  174. std::vector<T> in;
  175. in.push_back(a);
  176. in.push_back(b);
  177. in.push_back(c);
  178. return in;
  179. }
  180. template <typename T>
  181. Json::Value JsonVecOf3(const T& a, const T& b, const T& c) {
  182. Json::Value in(Json::arrayValue);
  183. in.append(a);
  184. in.append(b);
  185. in.append(c);
  186. return in;
  187. }
  188. } // unnamed namespace
  189. TEST(JsonTest, ValueVectorToFromArray) {
  190. std::vector<Json::Value> in = VecOf3<Json::Value>("a", "b", "c");
  191. Json::Value out = ValueVectorToJsonArray(in);
  192. EXPECT_EQ(in.size(), out.size());
  193. for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
  194. EXPECT_EQ(in[i].asString(), out[i].asString());
  195. }
  196. Json::Value inj = JsonVecOf3<Json::Value>("a", "b", "c");
  197. EXPECT_EQ(inj, out);
  198. std::vector<Json::Value> outj;
  199. EXPECT_TRUE(JsonArrayToValueVector(inj, &outj));
  200. for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
  201. EXPECT_EQ(in[i], outj[i]);
  202. }
  203. }
  204. TEST(JsonTest, IntVectorToFromArray) {
  205. std::vector<int> in = VecOf3<int>(1, 2, 3);
  206. Json::Value out = IntVectorToJsonArray(in);
  207. EXPECT_EQ(in.size(), out.size());
  208. for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
  209. EXPECT_EQ(in[i], out[i].asInt());
  210. }
  211. Json::Value inj = JsonVecOf3<int>(1, 2, 3);
  212. EXPECT_EQ(inj, out);
  213. std::vector<int> outj;
  214. EXPECT_TRUE(JsonArrayToIntVector(inj, &outj));
  215. for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
  216. EXPECT_EQ(in[i], outj[i]);
  217. }
  218. }
  219. TEST(JsonTest, UIntVectorToFromArray) {
  220. std::vector<unsigned int> in = VecOf3<unsigned int>(1, 2, 3);
  221. Json::Value out = UIntVectorToJsonArray(in);
  222. EXPECT_EQ(in.size(), out.size());
  223. for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
  224. EXPECT_EQ(in[i], out[i].asUInt());
  225. }
  226. Json::Value inj = JsonVecOf3<unsigned int>(1, 2, 3);
  227. EXPECT_EQ(inj, out);
  228. std::vector<unsigned int> outj;
  229. EXPECT_TRUE(JsonArrayToUIntVector(inj, &outj));
  230. for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
  231. EXPECT_EQ(in[i], outj[i]);
  232. }
  233. }
  234. TEST(JsonTest, StringVectorToFromArray) {
  235. std::vector<std::string> in = VecOf3<std::string>("a", "b", "c");
  236. Json::Value out = StringVectorToJsonArray(in);
  237. EXPECT_EQ(in.size(), out.size());
  238. for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
  239. EXPECT_EQ(in[i], out[i].asString());
  240. }
  241. Json::Value inj = JsonVecOf3<std::string>("a", "b", "c");
  242. EXPECT_EQ(inj, out);
  243. std::vector<std::string> outj;
  244. EXPECT_TRUE(JsonArrayToStringVector(inj, &outj));
  245. for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
  246. EXPECT_EQ(in[i], outj[i]);
  247. }
  248. }
  249. TEST(JsonTest, BoolVectorToFromArray) {
  250. std::vector<bool> in = VecOf3<bool>(false, true, false);
  251. Json::Value out = BoolVectorToJsonArray(in);
  252. EXPECT_EQ(in.size(), out.size());
  253. for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
  254. EXPECT_EQ(in[i], out[i].asBool());
  255. }
  256. Json::Value inj = JsonVecOf3<bool>(false, true, false);
  257. EXPECT_EQ(inj, out);
  258. std::vector<bool> outj;
  259. EXPECT_TRUE(JsonArrayToBoolVector(inj, &outj));
  260. for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
  261. EXPECT_EQ(in[i], outj[i]);
  262. }
  263. }
  264. TEST(JsonTest, DoubleVectorToFromArray) {
  265. std::vector<double> in = VecOf3<double>(1.0, 2.0, 3.0);
  266. Json::Value out = DoubleVectorToJsonArray(in);
  267. EXPECT_EQ(in.size(), out.size());
  268. for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
  269. EXPECT_EQ(in[i], out[i].asDouble());
  270. }
  271. Json::Value inj = JsonVecOf3<double>(1.0, 2.0, 3.0);
  272. EXPECT_EQ(inj, out);
  273. std::vector<double> outj;
  274. EXPECT_TRUE(JsonArrayToDoubleVector(inj, &outj));
  275. for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
  276. EXPECT_EQ(in[i], outj[i]);
  277. }
  278. }