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.

178 lines
7.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008, 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 "talk/base/gunit.h"
  28. #include "talk/base/optionsfile.h"
  29. namespace talk_base {
  30. #ifdef ANDROID
  31. static const char *kTestFile = "/sdcard/.testfile";
  32. #elif CHROMEOS
  33. static const char *kTestFile = "/tmp/.testfile";
  34. #else
  35. static const char *kTestFile = ".testfile";
  36. #endif
  37. static const std::string kTestOptionA = "test-option-a";
  38. static const std::string kTestOptionB = "test-option-b";
  39. static const std::string kTestString1 = "a string";
  40. static const std::string kTestString2 = "different string";
  41. static const std::string kOptionWithEquals = "foo=bar";
  42. static const std::string kOptionWithNewline = "foo\nbar";
  43. static const std::string kValueWithEquals = "baz=quux";
  44. static const std::string kValueWithNewline = "baz\nquux";
  45. static const std::string kEmptyString = "";
  46. static const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', 'i', 'o',
  47. 'n', '\342', '\204', '\242', '\0'}; // Opt(R)io(TM).
  48. static const char kValueWithUtf8[] = {'V', 'a', 'l', '\302', '\256', 'v', 'e',
  49. '\342', '\204', '\242', '\0'}; // Val(R)ue(TM).
  50. static int kTestInt1 = 12345;
  51. static int kTestInt2 = 67890;
  52. static int kNegInt = -634;
  53. static int kZero = 0;
  54. TEST(OptionsFile, GetSetString) {
  55. OptionsFile store(kTestFile);
  56. // Clear contents of the file on disk.
  57. EXPECT_TRUE(store.Save());
  58. std::string out1, out2;
  59. EXPECT_FALSE(store.GetStringValue(kTestOptionA, &out1));
  60. EXPECT_FALSE(store.GetStringValue(kTestOptionB, &out2));
  61. EXPECT_TRUE(store.SetStringValue(kTestOptionA, kTestString1));
  62. EXPECT_TRUE(store.Save());
  63. EXPECT_TRUE(store.Load());
  64. EXPECT_TRUE(store.SetStringValue(kTestOptionB, kTestString2));
  65. EXPECT_TRUE(store.Save());
  66. EXPECT_TRUE(store.Load());
  67. EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out1));
  68. EXPECT_TRUE(store.GetStringValue(kTestOptionB, &out2));
  69. EXPECT_EQ(kTestString1, out1);
  70. EXPECT_EQ(kTestString2, out2);
  71. EXPECT_TRUE(store.RemoveValue(kTestOptionA));
  72. EXPECT_TRUE(store.Save());
  73. EXPECT_TRUE(store.Load());
  74. EXPECT_TRUE(store.RemoveValue(kTestOptionB));
  75. EXPECT_TRUE(store.Save());
  76. EXPECT_TRUE(store.Load());
  77. EXPECT_FALSE(store.GetStringValue(kTestOptionA, &out1));
  78. EXPECT_FALSE(store.GetStringValue(kTestOptionB, &out2));
  79. }
  80. TEST(OptionsFile, GetSetInt) {
  81. OptionsFile store(kTestFile);
  82. // Clear contents of the file on disk.
  83. EXPECT_TRUE(store.Save());
  84. int out1, out2;
  85. EXPECT_FALSE(store.GetIntValue(kTestOptionA, &out1));
  86. EXPECT_FALSE(store.GetIntValue(kTestOptionB, &out2));
  87. EXPECT_TRUE(store.SetIntValue(kTestOptionA, kTestInt1));
  88. EXPECT_TRUE(store.Save());
  89. EXPECT_TRUE(store.Load());
  90. EXPECT_TRUE(store.SetIntValue(kTestOptionB, kTestInt2));
  91. EXPECT_TRUE(store.Save());
  92. EXPECT_TRUE(store.Load());
  93. EXPECT_TRUE(store.GetIntValue(kTestOptionA, &out1));
  94. EXPECT_TRUE(store.GetIntValue(kTestOptionB, &out2));
  95. EXPECT_EQ(kTestInt1, out1);
  96. EXPECT_EQ(kTestInt2, out2);
  97. EXPECT_TRUE(store.RemoveValue(kTestOptionA));
  98. EXPECT_TRUE(store.Save());
  99. EXPECT_TRUE(store.Load());
  100. EXPECT_TRUE(store.RemoveValue(kTestOptionB));
  101. EXPECT_TRUE(store.Save());
  102. EXPECT_TRUE(store.Load());
  103. EXPECT_FALSE(store.GetIntValue(kTestOptionA, &out1));
  104. EXPECT_FALSE(store.GetIntValue(kTestOptionB, &out2));
  105. EXPECT_TRUE(store.SetIntValue(kTestOptionA, kNegInt));
  106. EXPECT_TRUE(store.GetIntValue(kTestOptionA, &out1));
  107. EXPECT_EQ(kNegInt, out1);
  108. EXPECT_TRUE(store.SetIntValue(kTestOptionA, kZero));
  109. EXPECT_TRUE(store.GetIntValue(kTestOptionA, &out1));
  110. EXPECT_EQ(kZero, out1);
  111. }
  112. TEST(OptionsFile, Persist) {
  113. {
  114. OptionsFile store(kTestFile);
  115. // Clear contents of the file on disk.
  116. EXPECT_TRUE(store.Save());
  117. EXPECT_TRUE(store.SetStringValue(kTestOptionA, kTestString1));
  118. EXPECT_TRUE(store.SetIntValue(kTestOptionB, kNegInt));
  119. EXPECT_TRUE(store.Save());
  120. }
  121. {
  122. OptionsFile store(kTestFile);
  123. // Load the saved contents from above.
  124. EXPECT_TRUE(store.Load());
  125. std::string out1;
  126. int out2;
  127. EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out1));
  128. EXPECT_TRUE(store.GetIntValue(kTestOptionB, &out2));
  129. EXPECT_EQ(kTestString1, out1);
  130. EXPECT_EQ(kNegInt, out2);
  131. }
  132. }
  133. TEST(OptionsFile, SpecialCharacters) {
  134. OptionsFile store(kTestFile);
  135. // Clear contents of the file on disk.
  136. EXPECT_TRUE(store.Save());
  137. std::string out;
  138. EXPECT_FALSE(store.SetStringValue(kOptionWithEquals, kTestString1));
  139. EXPECT_FALSE(store.GetStringValue(kOptionWithEquals, &out));
  140. EXPECT_FALSE(store.SetStringValue(kOptionWithNewline, kTestString1));
  141. EXPECT_FALSE(store.GetStringValue(kOptionWithNewline, &out));
  142. EXPECT_TRUE(store.SetStringValue(kOptionWithUtf8, kValueWithUtf8));
  143. EXPECT_TRUE(store.SetStringValue(kTestOptionA, kTestString1));
  144. EXPECT_TRUE(store.Save());
  145. EXPECT_TRUE(store.Load());
  146. EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out));
  147. EXPECT_EQ(kTestString1, out);
  148. EXPECT_TRUE(store.GetStringValue(kOptionWithUtf8, &out));
  149. EXPECT_EQ(kValueWithUtf8, out);
  150. EXPECT_FALSE(store.SetStringValue(kTestOptionA, kValueWithNewline));
  151. EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out));
  152. EXPECT_EQ(kTestString1, out);
  153. EXPECT_TRUE(store.SetStringValue(kTestOptionA, kValueWithEquals));
  154. EXPECT_TRUE(store.Save());
  155. EXPECT_TRUE(store.Load());
  156. EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out));
  157. EXPECT_EQ(kValueWithEquals, out);
  158. EXPECT_TRUE(store.SetStringValue(kEmptyString, kTestString2));
  159. EXPECT_TRUE(store.Save());
  160. EXPECT_TRUE(store.Load());
  161. EXPECT_TRUE(store.GetStringValue(kEmptyString, &out));
  162. EXPECT_EQ(kTestString2, out);
  163. EXPECT_TRUE(store.SetStringValue(kTestOptionB, kEmptyString));
  164. EXPECT_TRUE(store.Save());
  165. EXPECT_TRUE(store.Load());
  166. EXPECT_TRUE(store.GetStringValue(kTestOptionB, &out));
  167. EXPECT_EQ(kEmptyString, out);
  168. }
  169. } // namespace talk_base