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.
 
 
 
 
 
 

224 lines
6.1 KiB

  1. #include <config.h>
  2. #include <apt-pkg/fileutl.h>
  3. #include <apt-pkg/netrc.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <string>
  6. #include <gtest/gtest.h>
  7. #include "file-helpers.h"
  8. TEST(NetRCTest, Parsing)
  9. {
  10. FileFd fd;
  11. URI U("http://file.not/open");
  12. EXPECT_FALSE(MaybeAddAuth(fd, U));
  13. EXPECT_TRUE(U.User.empty());
  14. EXPECT_TRUE(U.Password.empty());
  15. EXPECT_EQ("file.not", U.Host);
  16. EXPECT_EQ("/open", U.Path);
  17. createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
  18. machine example.netter login bar password foo
  19. machine example.net login foo password bar
  20. machine example.org:90 login apt password apt
  21. machine example.org:8080
  22. login
  23. example password foobar
  24. machine example.org
  25. login anonymous
  26. password pass
  27. machine example.com/foo login user1 unknown token password pass1
  28. machine example.com/bar password pass2 login user2
  29. unknown token
  30. machine example.com/user login user
  31. machine example.netter login unused password firstentry
  32. machine example.last/debian login debian password rules)apt");
  33. U = URI("http://example.net/foo");
  34. EXPECT_TRUE(MaybeAddAuth(fd, U));
  35. EXPECT_EQ("foo", U.User);
  36. EXPECT_EQ("bar", U.Password);
  37. EXPECT_EQ("example.net", U.Host);
  38. EXPECT_EQ("/foo", U.Path);
  39. EXPECT_TRUE(fd.Seek(0));
  40. U = URI("http://user:pass@example.net/foo");
  41. EXPECT_TRUE(MaybeAddAuth(fd, U));
  42. EXPECT_EQ("user", U.User);
  43. EXPECT_EQ("pass", U.Password);
  44. EXPECT_EQ("example.net", U.Host);
  45. EXPECT_EQ("/foo", U.Path);
  46. EXPECT_TRUE(fd.Seek(0));
  47. U = URI("http://example.org:90/foo");
  48. EXPECT_TRUE(MaybeAddAuth(fd, U));
  49. EXPECT_EQ("apt", U.User);
  50. EXPECT_EQ("apt", U.Password);
  51. EXPECT_EQ("example.org", U.Host);
  52. EXPECT_EQ(90, U.Port);
  53. EXPECT_EQ("/foo", U.Path);
  54. EXPECT_TRUE(fd.Seek(0));
  55. U = URI("http://example.org:8080/foo");
  56. EXPECT_TRUE(MaybeAddAuth(fd, U));
  57. EXPECT_EQ("example", U.User);
  58. EXPECT_EQ("foobar", U.Password);
  59. EXPECT_TRUE(fd.Seek(0));
  60. U = URI("http://example.net:42/foo");
  61. EXPECT_TRUE(MaybeAddAuth(fd, U));
  62. EXPECT_EQ("foo", U.User);
  63. EXPECT_EQ("bar", U.Password);
  64. EXPECT_TRUE(fd.Seek(0));
  65. U = URI("http://example.org/foo");
  66. EXPECT_TRUE(MaybeAddAuth(fd, U));
  67. EXPECT_EQ("anonymous", U.User);
  68. EXPECT_EQ("pass", U.Password);
  69. EXPECT_TRUE(fd.Seek(0));
  70. U = URI("http://example.com/apt");
  71. EXPECT_TRUE(MaybeAddAuth(fd, U));
  72. EXPECT_TRUE(U.User.empty());
  73. EXPECT_TRUE(U.Password.empty());
  74. EXPECT_TRUE(fd.Seek(0));
  75. U = URI("http://example.com/foo");
  76. EXPECT_TRUE(MaybeAddAuth(fd, U));
  77. EXPECT_EQ("user1", U.User);
  78. EXPECT_EQ("pass1", U.Password);
  79. EXPECT_TRUE(fd.Seek(0));
  80. U = URI("http://example.com/fooo");
  81. EXPECT_TRUE(MaybeAddAuth(fd, U));
  82. EXPECT_EQ("user1", U.User);
  83. EXPECT_EQ("pass1", U.Password);
  84. EXPECT_TRUE(fd.Seek(0));
  85. U = URI("http://example.com/fo");
  86. EXPECT_TRUE(MaybeAddAuth(fd, U));
  87. EXPECT_TRUE(U.User.empty());
  88. EXPECT_TRUE(U.Password.empty());
  89. EXPECT_TRUE(fd.Seek(0));
  90. U = URI("http://example.com/bar");
  91. EXPECT_TRUE(MaybeAddAuth(fd, U));
  92. EXPECT_EQ("user2", U.User);
  93. EXPECT_EQ("pass2", U.Password);
  94. EXPECT_TRUE(fd.Seek(0));
  95. U = URI("http://example.com/user");
  96. EXPECT_TRUE(MaybeAddAuth(fd, U));
  97. EXPECT_EQ("user", U.User);
  98. EXPECT_TRUE(U.Password.empty());
  99. EXPECT_TRUE(fd.Seek(0));
  100. U = URI("socks5h://example.last/debian");
  101. EXPECT_TRUE(MaybeAddAuth(fd, U));
  102. EXPECT_EQ("debian", U.User);
  103. EXPECT_EQ("rules", U.Password);
  104. EXPECT_TRUE(fd.Seek(0));
  105. U = URI("socks5h://example.debian/");
  106. EXPECT_TRUE(MaybeAddAuth(fd, U));
  107. EXPECT_TRUE(U.User.empty());
  108. EXPECT_TRUE(U.Password.empty());
  109. EXPECT_TRUE(fd.Seek(0));
  110. U = URI("socks5h://user:pass@example.debian/");
  111. EXPECT_TRUE(MaybeAddAuth(fd, U));
  112. EXPECT_EQ("user", U.User);
  113. EXPECT_EQ("pass", U.Password);
  114. }
  115. TEST(NetRCTest, BadFileNoMachine)
  116. {
  117. FileFd fd;
  118. createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
  119. foo example.org login foo1 password bar
  120. machin example.org login foo2 password bar
  121. machine2 example.org login foo3 password bar
  122. )apt");
  123. URI U("http://example.org/foo");
  124. EXPECT_TRUE(MaybeAddAuth(fd, U));
  125. EXPECT_TRUE(U.User.empty());
  126. EXPECT_TRUE(U.Password.empty());
  127. }
  128. TEST(NetRCTest, BadFileEndsMachine)
  129. {
  130. FileFd fd;
  131. createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
  132. machine example.org login foo1 password bar
  133. machine)apt");
  134. URI U("http://example.org/foo");
  135. EXPECT_TRUE(MaybeAddAuth(fd, U));
  136. EXPECT_EQ("foo1", U.User);
  137. EXPECT_EQ("bar", U.Password);
  138. EXPECT_TRUE(fd.Seek(0));
  139. U = URI("http://example.net/foo");
  140. EXPECT_FALSE(MaybeAddAuth(fd, U));
  141. EXPECT_TRUE(U.User.empty());
  142. EXPECT_TRUE(U.Password.empty());
  143. EXPECT_TRUE(fd.Seek(0));
  144. U = URI("http://foo:bar@example.net/foo");
  145. EXPECT_TRUE(MaybeAddAuth(fd, U));
  146. EXPECT_EQ("foo", U.User);
  147. EXPECT_EQ("bar", U.Password);
  148. }
  149. TEST(NetRCTest, BadFileEndsLogin)
  150. {
  151. FileFd fd;
  152. createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
  153. machine example.org login foo1 password bar
  154. machine example.net login)apt");
  155. URI U("http://example.org/foo");
  156. EXPECT_TRUE(MaybeAddAuth(fd, U));
  157. EXPECT_EQ("foo1", U.User);
  158. EXPECT_EQ("bar", U.Password);
  159. EXPECT_TRUE(fd.Seek(0));
  160. U = URI("http://example.net/foo");
  161. EXPECT_FALSE(MaybeAddAuth(fd, U));
  162. EXPECT_TRUE(U.User.empty());
  163. EXPECT_TRUE(U.Password.empty());
  164. EXPECT_TRUE(fd.Seek(0));
  165. U = URI("http://foo:bar@example.net/foo");
  166. EXPECT_TRUE(MaybeAddAuth(fd, U));
  167. EXPECT_EQ("foo", U.User);
  168. EXPECT_EQ("bar", U.Password);
  169. }
  170. TEST(NetRCTest, BadFileEndsPassword)
  171. {
  172. FileFd fd;
  173. createTemporaryFile("doublesignedfile", fd, nullptr, R"apt(
  174. machine example.org login foo1 password bar
  175. machine example.net password)apt");
  176. URI U("http://example.org/foo");
  177. EXPECT_TRUE(MaybeAddAuth(fd, U));
  178. EXPECT_EQ("foo1", U.User);
  179. EXPECT_EQ("bar", U.Password);
  180. EXPECT_TRUE(fd.Seek(0));
  181. U = URI("http://example.net/foo");
  182. EXPECT_FALSE(MaybeAddAuth(fd, U));
  183. EXPECT_TRUE(U.User.empty());
  184. EXPECT_TRUE(U.Password.empty());
  185. EXPECT_TRUE(fd.Seek(0));
  186. U = URI("http://foo:bar@example.net/foo");
  187. EXPECT_TRUE(MaybeAddAuth(fd, U));
  188. EXPECT_EQ("foo", U.User);
  189. EXPECT_EQ("bar", U.Password);
  190. }