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.

345 lines
15 KiB

  1. %% Test suite for the rebar pkg index caching and decompression
  2. %% mechanisms.
  3. -module(rebar_pkg_SUITE).
  4. -compile(export_all).
  5. -include_lib("common_test/include/ct.hrl").
  6. -include_lib("eunit/include/eunit.hrl").
  7. -include("rebar.hrl").
  8. -define(bad_etag, <<"abcdef">>).
  9. -define(good_etag, <<"22e1d7387c9085a462340088a2a8ba67">>).
  10. -define(bad_checksum, <<"D576B442A68C7B92BACDE1EFE9C6E54D8D6C74BDB71D8175B9D3C6EC8C7B62A7">>).
  11. -define(good_checksum, <<"1C6CE379D191FBAB41B7905075E0BF87CBBE23C77CECE775C5A0B786B2244C35">>).
  12. -define(BADPKG_ETAG, <<"BADETAG">>).
  13. all() -> [good_uncached, good_cached, badindexchk, badpkg,
  14. badhash_nocache, badhash_cache,
  15. bad_to_good, good_disconnect, bad_disconnect, pkgs_provider,
  16. find_highest_matching].
  17. init_per_suite(Config) ->
  18. application:start(meck),
  19. Config.
  20. end_per_suite(_Config) ->
  21. application:stop(meck).
  22. init_per_testcase(pkgs_provider=Name, Config) ->
  23. %% Need to mock out a registry for this test now because it will try to update it automatically
  24. Priv = ?config(priv_dir, Config),
  25. Tid = ets:new(registry_table, [public]),
  26. ets:insert_new(Tid, []),
  27. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  28. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  29. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  30. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  31. %% meck:new(rebar_packages, [passthrough]),
  32. %% meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  33. %% meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  34. %% rebar_prv_update:hex_to_index(rebar_state:new()),
  35. Config;
  36. init_per_testcase(good_uncached=Name, Config0) ->
  37. Config = [{good_cache, false},
  38. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  39. | Config0],
  40. mock_config(Name, Config);
  41. init_per_testcase(good_cached=Name, Config0) ->
  42. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  43. Config1 = [{good_cache, true},
  44. {pkg, Pkg}
  45. | Config0],
  46. Config = mock_config(Name, Config1),
  47. copy_to_cache(Pkg, Config),
  48. Config;
  49. init_per_testcase(badindexchk=Name, Config0) ->
  50. Config = [{good_cache, false},
  51. {pkg, {<<"badindexchk">>, <<"1.0.0">>}}
  52. | Config0],
  53. mock_config(Name, Config);
  54. init_per_testcase(badpkg=Name, Config0) ->
  55. Config = [{good_cache, false},
  56. {pkg, {<<"badpkg">>, <<"1.0.0">>}}
  57. | Config0],
  58. mock_config(Name, Config);
  59. init_per_testcase(badhash_nocache=Name, Config0) ->
  60. Config = [{good_cache, false},
  61. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  62. | Config0],
  63. mock_config(Name, Config);
  64. init_per_testcase(badhash_cache=Name, Config0) ->
  65. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  66. Config1 = [{good_cache, true},
  67. {pkg, Pkg}
  68. | Config0],
  69. Config = mock_config(Name, Config1),
  70. copy_to_cache(Pkg, Config),
  71. Config;
  72. init_per_testcase(bad_to_good=Name, Config0) ->
  73. Config1 = [{good_cache, false},
  74. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  75. | Config0],
  76. Config = mock_config(Name, Config1),
  77. Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>),
  78. Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>),
  79. ec_file:copy(Source, Dest),
  80. Config;
  81. init_per_testcase(good_disconnect=Name, Config0) ->
  82. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  83. Config1 = [{good_cache, false},
  84. {pkg, Pkg}
  85. | Config0],
  86. Config = mock_config(Name, Config1),
  87. copy_to_cache(Pkg, Config),
  88. %% meck:unload(httpc),
  89. meck:new(httpc, [passthrough, unsticky]),
  90. meck:expect(httpc, request, fun(_, _, _, _) -> {error, econnrefused} end),
  91. Config;
  92. init_per_testcase(bad_disconnect=Name, Config0) ->
  93. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  94. Config1 = [{good_cache, false},
  95. {pkg, Pkg}
  96. | Config0],
  97. Config = mock_config(Name, Config1),
  98. %% meck:unload(httpc),
  99. %% meck:new(httpc, [passthrough, unsticky]),
  100. %% meck:expect(httpc, request, fun(_, _, _, _) -> {error, econnrefused} end),
  101. meck:expect(hex_repo, get_tarball, fun(_, _, _) ->
  102. {error, econnrefused}
  103. end),
  104. Config;
  105. init_per_testcase(Name, Config0) ->
  106. Config = [{good_cache, false},
  107. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  108. | Config0],
  109. mock_config(Name, Config).
  110. end_per_testcase(_, Config) ->
  111. unmock_config(Config),
  112. Config.
  113. good_uncached(Config) ->
  114. Tmp = ?config(tmp_dir, Config),
  115. {Pkg,Vsn} = ?config(pkg, Config),
  116. State = ?config(state, Config),
  117. ?assertEqual({ok, true},
  118. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  119. Cache = ?config(cache_dir, Config),
  120. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  121. good_cached(Config) ->
  122. Tmp = ?config(tmp_dir, Config),
  123. {Pkg,Vsn} = ?config(pkg, Config),
  124. State = ?config(state, Config),
  125. Cache = ?config(cache_dir, Config),
  126. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  127. ?assert(filelib:is_regular(CachedFile)),
  128. {ok, Content} = file:read_file(CachedFile),
  129. ?assertEqual({ok, true},
  130. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  131. {ok, Content} = file:read_file(CachedFile).
  132. badindexchk(Config) ->
  133. Tmp = ?config(tmp_dir, Config),
  134. {Pkg,Vsn} = ?config(pkg, Config),
  135. State = ?config(state, Config),
  136. ?assertMatch({bad_registry_checksum, _Path},
  137. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  138. %% The cached file is there for forensic purposes
  139. Cache = ?config(cache_dir, Config),
  140. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  141. badpkg(Config) ->
  142. Tmp = ?config(tmp_dir, Config),
  143. {Pkg,Vsn} = ?config(pkg, Config),
  144. State = ?config(state, Config),
  145. Cache = ?config(cache_dir, Config),
  146. CachePath = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  147. ETagPath = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".etag">>),
  148. rebar_pkg_resource:store_etag_in_cache(ETagPath, ?BADPKG_ETAG),
  149. ?assertMatch({bad_download, _Path},
  150. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State, false)),
  151. %% The cached/etag files are there for forensic purposes
  152. ?assert(filelib:is_regular(ETagPath)),
  153. ?assert(filelib:is_regular(CachePath)).
  154. badhash_nocache(Config) ->
  155. Tmp = ?config(tmp_dir, Config),
  156. {Pkg,Vsn} = ?config(pkg, Config),
  157. State = ?config(state, Config),
  158. ?assertMatch({unexpected_hash, _Path, ?bad_checksum, ?good_checksum},
  159. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum}, State)),
  160. %% The cached file is there for forensic purposes
  161. Cache = ?config(cache_dir, Config),
  162. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  163. badhash_cache(Config) ->
  164. Tmp = ?config(tmp_dir, Config),
  165. {Pkg,Vsn} = ?config(pkg, Config),
  166. Cache = ?config(cache_dir, Config),
  167. State = ?config(state, Config),
  168. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  169. ?assert(filelib:is_regular(CachedFile)),
  170. {ok, Content} = file:read_file(CachedFile),
  171. ?assertMatch({unexpected_hash, _Path, ?bad_checksum, ?good_checksum},
  172. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum}, State)),
  173. %% The cached file is there still, unchanged.
  174. ?assert(filelib:is_regular(CachedFile)),
  175. ?assertEqual({ok, Content}, file:read_file(CachedFile)).
  176. bad_to_good(Config) ->
  177. Tmp = ?config(tmp_dir, Config),
  178. {Pkg,Vsn} = ?config(pkg, Config),
  179. State = ?config(state, Config),
  180. Cache = ?config(cache_dir, Config),
  181. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  182. ?assert(filelib:is_regular(CachedFile)),
  183. {ok, Contents} = file:read_file(CachedFile),
  184. ?assertEqual({ok, true},
  185. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  186. %% Cache has refreshed
  187. ?assert({ok, Contents} =/= file:read_file(CachedFile)).
  188. good_disconnect(Config) ->
  189. Tmp = ?config(tmp_dir, Config),
  190. {Pkg,Vsn} = ?config(pkg, Config),
  191. State = ?config(state, Config),
  192. Cache = ?config(cache_dir, Config),
  193. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  194. ETagFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".etag">>),
  195. ?assert(filelib:is_regular(CachedFile)),
  196. {ok, Content} = file:read_file(CachedFile),
  197. rebar_pkg_resource:store_etag_in_cache(ETagFile, ?BADPKG_ETAG),
  198. ?assertEqual({ok, true},
  199. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  200. {ok, Content} = file:read_file(CachedFile).
  201. bad_disconnect(Config) ->
  202. Tmp = ?config(tmp_dir, Config),
  203. {Pkg,Vsn} = ?config(pkg, Config),
  204. State = ?config(state, Config),
  205. ?assertEqual({fetch_fail, Pkg, Vsn},
  206. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)).
  207. pkgs_provider(Config) ->
  208. Config1 = rebar_test_utils:init_rebar_state(Config),
  209. rebar_test_utils:run_and_check(
  210. Config1, [], ["pkgs"],
  211. {ok, []}
  212. ).
  213. find_highest_matching(_Config) ->
  214. State = rebar_state:new(),
  215. {ok, Vsn} = rebar_packages:find_highest_matching(
  216. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"1.0.0">>, ?PACKAGE_TABLE, State),
  217. ?assertEqual(<<"1.0.1">>, Vsn),
  218. {ok, Vsn1} = rebar_packages:find_highest_matching(
  219. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"1.0">>, ?PACKAGE_TABLE, State),
  220. ?assertEqual(<<"1.1.1">>, Vsn1),
  221. {ok, Vsn2} = rebar_packages:find_highest_matching(
  222. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"2.0">>, ?PACKAGE_TABLE, State),
  223. ?assertEqual(<<"2.0.0">>, Vsn2),
  224. %% regression test. ~> constraints higher than the available packages would result
  225. %% in returning the first package version instead of 'none'.
  226. ?assertEqual(none, rebar_packages:find_highest_matching(<<"test">>, <<"1.0.0">>, <<"goodpkg">>,
  227. <<"~> 5.0">>, ?PACKAGE_TABLE, State)).
  228. %%%%%%%%%%%%%%%
  229. %%% Helpers %%%
  230. %%%%%%%%%%%%%%%
  231. mock_config(Name, Config) ->
  232. Priv = ?config(priv_dir, Config),
  233. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  234. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  235. Tid = ets:new(registry_table, [public]),
  236. AllDeps = [
  237. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]},
  238. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  239. {{<<"goodpkg">>,<<"1.0.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  240. {{<<"goodpkg">>,<<"1.1.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  241. {{<<"goodpkg">>,<<"2.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  242. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]}
  243. ],
  244. ets:insert_new(Tid, AllDeps),
  245. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  246. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  247. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  248. catch ets:delete(?PACKAGE_TABLE),
  249. rebar_packages:new_package_table(),
  250. lists:foreach(fun({{N, Vsn}, [Deps, Checksum, _]}) ->
  251. case ets:member(?PACKAGE_TABLE, {ec_cnv:to_binary(N), Vsn}) of
  252. false ->
  253. ets:insert(?PACKAGE_TABLE, #package{key =
  254. {ec_cnv:to_binary(N), Vsn},
  255. dependencies = Deps,
  256. checksum = Checksum});
  257. true ->
  258. ok
  259. end
  260. end, AllDeps),
  261. meck:new(hex_repo, [passthrough]),
  262. meck:expect(hex_repo, get_package,
  263. fun(_Config, PkgName) ->
  264. Matches = ets:match_object(Tid, {{PkgName,'_'}, '_'}),
  265. Releases =
  266. [#{checksum => Checksum,
  267. version => Vsn,
  268. dependencies => Deps} ||
  269. {{_, Vsn}, [Deps, Checksum, _]} <- Matches],
  270. {ok, {200, #{}, #{releases => Releases}}}
  271. end),
  272. %% The state returns us a fake registry
  273. meck:new(rebar_state, [passthrough]),
  274. meck:expect(rebar_state, get,
  275. fun(_State, rebar_packages_cdn, _Default) ->
  276. "http://test.com/";
  277. (_, _, Default) ->
  278. Default
  279. end),
  280. meck:expect(rebar_state, resources,
  281. fun(_State) ->
  282. [rebar_resource:new(pkg, rebar_pkg_resource,
  283. #{hex_config => hex_core:default_config()})]
  284. end),
  285. meck:new(rebar_dir, [passthrough]),
  286. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  287. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  288. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  289. meck:new(rebar_prv_update, [passthrough]),
  290. meck:expect(rebar_prv_update, do, fun(State) -> {ok, State} end),
  291. %% Cache fetches are mocked -- we assume the server and clients are
  292. %% correctly used.
  293. GoodCache = ?config(good_cache, Config),
  294. {Pkg,Vsn} = ?config(pkg, Config),
  295. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  296. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  297. meck:expect(hex_repo, get_tarball, fun(_, _, _) when GoodCache ->
  298. {ok, {304, #{<<"etag">> => ?good_etag}, <<>>}};
  299. (_, _, _) ->
  300. {ok, {200, #{<<"etag">> => ?good_etag}, PkgContents}}
  301. end),
  302. [{cache_root, CacheRoot},
  303. {cache_dir, CacheDir},
  304. {tmp_dir, TmpDir},
  305. {mock_table, Tid} | Config].
  306. unmock_config(Config) ->
  307. meck:unload(),
  308. catch ets:delete(?config(mock_table, Config)).
  309. copy_to_cache({Pkg,Vsn}, Config) ->
  310. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  311. Source = filename:join(?config(data_dir, Config), Name),
  312. Dest = filename:join(?config(cache_dir, Config), Name),
  313. ec_file:copy(Source, Dest).