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.
 
 
 
 
 
 

217 lines
8.8 KiB

  1. #include <config.h>
  2. #include <apt-pkg/configuration.h>
  3. #include <apt-pkg/proxy.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <string>
  6. #include <gtest/gtest.h>
  7. TEST(URITest, BasicHTTP)
  8. {
  9. URI U("http://www.debian.org:90/temp/test");
  10. EXPECT_EQ("http", U.Access);
  11. EXPECT_EQ("", U.User);
  12. EXPECT_EQ("", U.Password);
  13. EXPECT_EQ(90, U.Port);
  14. EXPECT_EQ("www.debian.org", U.Host);
  15. EXPECT_EQ("/temp/test", U.Path);
  16. EXPECT_EQ("http://www.debian.org:90/temp/test", (std::string)U);
  17. EXPECT_EQ("http://www.debian.org:90", URI::SiteOnly(U));
  18. EXPECT_EQ("http://www.debian.org:90/temp/test", URI::ArchiveOnly(U));
  19. EXPECT_EQ("http://www.debian.org:90/temp/test", URI::NoUserPassword(U));
  20. // Login data
  21. U = URI("http://jgg:foo@ualberta.ca/blah");
  22. EXPECT_EQ("http", U.Access);
  23. EXPECT_EQ("jgg", U.User);
  24. EXPECT_EQ("foo", U.Password);
  25. EXPECT_EQ(0, U.Port);
  26. EXPECT_EQ("ualberta.ca", U.Host);
  27. EXPECT_EQ("/blah", U.Path);
  28. EXPECT_EQ("http://jgg:foo@ualberta.ca/blah", (std::string)U);
  29. EXPECT_EQ("http://ualberta.ca", URI::SiteOnly(U));
  30. EXPECT_EQ("http://ualberta.ca/blah", URI::ArchiveOnly(U));
  31. EXPECT_EQ("http://ualberta.ca/blah", URI::NoUserPassword(U));
  32. // just a user
  33. U = URI("https://apt@example.org/blah");
  34. EXPECT_EQ("https", U.Access);
  35. EXPECT_EQ("apt", U.User);
  36. EXPECT_EQ("", U.Password);
  37. EXPECT_EQ(0, U.Port);
  38. EXPECT_EQ("example.org", U.Host);
  39. EXPECT_EQ("/blah", U.Path);
  40. EXPECT_EQ("https://apt@example.org/blah", (std::string)U);
  41. EXPECT_EQ("https://example.org", URI::SiteOnly(U));
  42. EXPECT_EQ("https://example.org/blah", URI::ArchiveOnly(U));
  43. EXPECT_EQ("https://example.org/blah", URI::NoUserPassword(U));
  44. }
  45. TEST(URITest, SingeSlashFile)
  46. {
  47. URI U("file:/usr/bin/foo");
  48. EXPECT_EQ("file", U.Access);
  49. EXPECT_EQ("", U.User);
  50. EXPECT_EQ("", U.Password);
  51. EXPECT_EQ(0, U.Port);
  52. EXPECT_EQ("", U.Host);
  53. EXPECT_EQ("/usr/bin/foo", U.Path);
  54. EXPECT_EQ("file:/usr/bin/foo", (std::string)U);
  55. EXPECT_EQ("file:", URI::SiteOnly(U));
  56. EXPECT_EQ("file:/usr/bin/foo", URI::ArchiveOnly(U));
  57. EXPECT_EQ("file:/usr/bin/foo", URI::NoUserPassword(U));
  58. }
  59. TEST(URITest, BasicCDROM)
  60. {
  61. URI U("cdrom:Moo Cow Rom:/debian");
  62. EXPECT_EQ("cdrom", U.Access);
  63. EXPECT_EQ("", U.User);
  64. EXPECT_EQ("", U.Password);
  65. EXPECT_EQ(0, U.Port);
  66. EXPECT_EQ("Moo Cow Rom", U.Host);
  67. EXPECT_EQ("/debian", U.Path);
  68. EXPECT_EQ("cdrom://Moo Cow Rom/debian", (std::string)U);
  69. EXPECT_EQ("cdrom://Moo Cow Rom", URI::SiteOnly(U));
  70. EXPECT_EQ("cdrom://Moo Cow Rom/debian", URI::ArchiveOnly(U));
  71. EXPECT_EQ("cdrom://Moo Cow Rom/debian", URI::NoUserPassword(U));
  72. }
  73. TEST(URITest, RelativeGzip)
  74. {
  75. URI U("gzip:./bar/cow");
  76. EXPECT_EQ("gzip", U.Access);
  77. EXPECT_EQ("", U.User);
  78. EXPECT_EQ("", U.Password);
  79. EXPECT_EQ(0, U.Port);
  80. EXPECT_EQ(".", U.Host);
  81. EXPECT_EQ("/bar/cow", U.Path);
  82. EXPECT_EQ("gzip://./bar/cow", (std::string)U);
  83. EXPECT_EQ("gzip://.", URI::SiteOnly(U));
  84. EXPECT_EQ("gzip://./bar/cow", URI::ArchiveOnly(U));
  85. EXPECT_EQ("gzip://./bar/cow", URI::NoUserPassword(U));
  86. }
  87. TEST(URITest, NoSlashFTP)
  88. {
  89. URI U("ftp:ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb");
  90. EXPECT_EQ("ftp", U.Access);
  91. EXPECT_EQ("", U.User);
  92. EXPECT_EQ("", U.Password);
  93. EXPECT_EQ(0, U.Port);
  94. EXPECT_EQ("ftp.fr.debian.org", U.Host);
  95. EXPECT_EQ("/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", U.Path);
  96. EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", (std::string)U);
  97. EXPECT_EQ("ftp://ftp.fr.debian.org", URI::SiteOnly(U));
  98. EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", URI::ArchiveOnly(U));
  99. EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", URI::NoUserPassword(U));
  100. }
  101. TEST(URITest, RFC2732)
  102. {
  103. URI U("http://[1080::8:800:200C:417A]/foo");
  104. EXPECT_EQ("http", U.Access);
  105. EXPECT_EQ("", U.User);
  106. EXPECT_EQ("", U.Password);
  107. EXPECT_EQ(0, U.Port);
  108. EXPECT_EQ("1080::8:800:200C:417A", U.Host);
  109. EXPECT_EQ("/foo", U.Path);
  110. EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", (std::string)U);
  111. EXPECT_EQ("http://[1080::8:800:200C:417A]", URI::SiteOnly(U));
  112. EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", URI::ArchiveOnly(U));
  113. EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", URI::NoUserPassword(U));
  114. // with port
  115. U = URI("http://[::FFFF:129.144.52.38]:80/index.html");
  116. EXPECT_EQ("http", U.Access);
  117. EXPECT_EQ("", U.User);
  118. EXPECT_EQ("", U.Password);
  119. EXPECT_EQ(80, U.Port);
  120. EXPECT_EQ("::FFFF:129.144.52.38", U.Host);
  121. EXPECT_EQ("/index.html", U.Path);
  122. EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", (std::string)U);
  123. EXPECT_EQ("http://[::FFFF:129.144.52.38]:80", URI::SiteOnly(U));
  124. EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", URI::ArchiveOnly(U));
  125. EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", URI::NoUserPassword(U));
  126. // extra colon
  127. U = URI("http://[::FFFF:129.144.52.38:]:80/index.html");
  128. EXPECT_EQ("http", U.Access);
  129. EXPECT_EQ("", U.User);
  130. EXPECT_EQ("", U.Password);
  131. EXPECT_EQ(80, U.Port);
  132. EXPECT_EQ("::FFFF:129.144.52.38:", U.Host);
  133. EXPECT_EQ("/index.html", U.Path);
  134. EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", (std::string)U);
  135. EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80", URI::SiteOnly(U));
  136. EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", URI::ArchiveOnly(U));
  137. EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", URI::NoUserPassword(U));
  138. // extra colon port
  139. U = URI("http://[::FFFF:129.144.52.38:]/index.html");
  140. EXPECT_EQ("http", U.Access);
  141. EXPECT_EQ("", U.User);
  142. EXPECT_EQ("", U.Password);
  143. EXPECT_EQ(0, U.Port);
  144. EXPECT_EQ("::FFFF:129.144.52.38:", U.Host);
  145. EXPECT_EQ("/index.html", U.Path);
  146. EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", (std::string)U);
  147. EXPECT_EQ("http://[::FFFF:129.144.52.38:]", URI::SiteOnly(U));
  148. EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", URI::ArchiveOnly(U));
  149. EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", URI::NoUserPassword(U));
  150. // My Evil Corruption of RFC 2732 to handle CDROM names!
  151. // Fun for the whole family! */
  152. U = URI("cdrom:[The Debian 1.2 disk, 1/2 R1:6]/debian/");
  153. EXPECT_EQ("cdrom", U.Access);
  154. EXPECT_EQ("", U.User);
  155. EXPECT_EQ("", U.Password);
  156. EXPECT_EQ(0, U.Port);
  157. EXPECT_EQ("The Debian 1.2 disk, 1/2 R1:6", U.Host);
  158. EXPECT_EQ("/debian/", U.Path);
  159. EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian/", (std::string)U);
  160. EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]", URI::SiteOnly(U));
  161. EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian", URI::ArchiveOnly(U));
  162. EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian/", URI::NoUserPassword(U));
  163. // no brackets
  164. U = URI("cdrom:Foo Bar Cow/debian/");
  165. EXPECT_EQ("cdrom", U.Access);
  166. EXPECT_EQ("", U.User);
  167. EXPECT_EQ("", U.Password);
  168. EXPECT_EQ(0, U.Port);
  169. EXPECT_EQ("Foo Bar Cow", U.Host);
  170. EXPECT_EQ("/debian/", U.Path);
  171. EXPECT_EQ("cdrom://Foo Bar Cow/debian/", (std::string)U);
  172. EXPECT_EQ("cdrom://Foo Bar Cow", URI::SiteOnly(U));
  173. EXPECT_EQ("cdrom://Foo Bar Cow/debian", URI::ArchiveOnly(U));
  174. EXPECT_EQ("cdrom://Foo Bar Cow/debian/", URI::NoUserPassword(U));
  175. // percent encoded password
  176. U = URI("ftp://foo:b%40r@example.org");
  177. EXPECT_EQ("foo", U.User);
  178. EXPECT_EQ("b@r", U.Password);
  179. EXPECT_EQ("ftp://foo:b%40r@example.org/", (std::string) U);
  180. EXPECT_EQ("ftp://example.org", URI::SiteOnly(U));
  181. EXPECT_EQ("ftp://example.org", URI::ArchiveOnly(U));
  182. EXPECT_EQ("ftp://example.org/", URI::NoUserPassword(U));
  183. // percent encoded user
  184. U = URI("ftp://f%40o:bar@example.org");
  185. EXPECT_EQ("f@o", U.User);
  186. EXPECT_EQ("bar", U.Password);
  187. EXPECT_EQ("ftp://f%40o:bar@example.org/", (std::string) U);
  188. EXPECT_EQ("ftp://example.org", URI::SiteOnly(U));
  189. EXPECT_EQ("ftp://example.org", URI::ArchiveOnly(U));
  190. EXPECT_EQ("ftp://example.org/", URI::NoUserPassword(U));
  191. }
  192. TEST(URITest, AutoProxyTest)
  193. {
  194. URI u0("http://www.debian.org:90/temp/test");
  195. URI u1("http://www.debian.org:91/temp/test");
  196. _config->Set("Acquire::http::Proxy-Auto-Detect", "./apt-proxy-script");
  197. // Scenario 0: Autodetecting a simple proxy
  198. AutoDetectProxy(u0);
  199. EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com");
  200. // Scenario 1: Proxy stays the same if it is already set
  201. AutoDetectProxy(u1);
  202. EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com");
  203. // Scenario 2: Reading with stderr output works fine
  204. _config->Clear("Acquire::http::proxy::www.debian.org");
  205. AutoDetectProxy(u1);
  206. EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com/foo");
  207. // Scenario 1 again: Proxy stays the same if it is already set
  208. AutoDetectProxy(u0);
  209. EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com/foo");
  210. }