Update166bTests.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Round 166b (update166b): in-app update check (core/UpdateCheck.h, docs/analise/REA_rodada166b.md)
  2. #include "core/UpdateCheck.h"
  3. #include "core/Sha256.h"
  4. namespace
  5. {
  6. // a trimmed copy of GitHub's /releases answer: a draft, two prereleases, a tag that is no version, escapes
  7. const char* updateFixture = R"json([
  8. {"url":"x","html_url":"https://github.com/Ka1-S/LYNX-SHARE/releases/tag/v0.1.23-alpha","tag_name":"v0.1.23-alpha",
  9. "name":"LYNX SHARE 0.1.23-alpha","draft":true,"prerelease":true,"body":"draft","assets":[]},
  10. {"html_url":"https://github.com/Ka1-S/LYNX-SHARE/releases/tag/v0.1.22-alpha","tag_name":"v0.1.22-alpha",
  11. "name":"LYNX SHARE 0.1.22-alpha","draft":false,"prerelease":true,"id":12345678,"author":{"login":"Ka1-S","id":1},
  12. "body":"## New\r\n- **Update** button — see [the docs](https://x/y)\r\n- Café 🌶\r\n\r\n## Fixes\r\n* `crash` fixed\r\n",
  13. "assets":[
  14. {"name":"LYNX-0.1.22-alpha-linux-x86_64.tar.gz","size":40000000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-linux-x86_64.tar.gz"},
  15. {"name":"LYNX-0.1.22-alpha-macos-universal.dmg","size":50000000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-macos-universal.dmg"},
  16. {"name":"LYNX-0.1.22-alpha-setup-x64.exe","size":30000000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-setup-x64.exe"},
  17. {"name":"LYNX-0.1.22-alpha-store.msix","size":30000000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-store.msix"},
  18. {"name":"LYNX-0.1.22-alpha-windows-x64.zip","size":30000000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-windows-x64.zip"},
  19. {"name":"LYNX-0.1.22-alpha-x86_64.AppImage","size":60000000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-x86_64.AppImage"},
  20. {"name":"LYNX-0.1.22-alpha-x86_64.AppImage.zsync","size":200000,"browser_download_url":"https://github.com/d/LYNX-0.1.22-alpha-x86_64.AppImage.zsync"},
  21. {"name":"LYNX.exe","size":90000000,"browser_download_url":"https://github.com/d/LYNX.exe"},
  22. {"name":"SHA256SUMS.txt","size":900,"browser_download_url":"https://github.com/d/SHA256SUMS.txt"}]},
  23. {"html_url":"h","tag_name":"nightly","name":"not a version","draft":false,"prerelease":true,"body":"","assets":[]},
  24. {"html_url":"https://github.com/Ka1-S/LYNX-SHARE/releases/tag/v0.1.21-alpha","tag_name":"v0.1.21-alpha","name":"LYNX SHARE 0.1.21-alpha",
  25. "draft":false,"prerelease":true,"body":null,"assets":[]},
  26. {"html_url":"h","tag_name":"v0.1.20-alpha","draft":false,"prerelease":true,"body":"old","assets":[], "reactions":{"+1":3}, "x":[1,2.5e3,-4,true,false,null]}
  27. ])json";
  28. } // namespace
  29. TEST (update166b_versions_compare_like_semver)
  30. {
  31. using namespace lynx::update;
  32. const auto v = parseVersion ("v0.1.21-alpha");
  33. CHECK (v.valid && v.major == 0 && v.minor == 1 && v.patch == 21 && v.pre.size() == 1 && v.pre[0] == "alpha");
  34. CHECK (v.text() == "0.1.21-alpha");
  35. CHECK (parseVersion ("0.1").valid && parseVersion ("0.1").patch == 0);
  36. CHECK (parseVersion (" V1.2.3-rc.1+build.5 ").valid && parseVersion ("V1.2.3-rc.1+build.5").pre.size() == 2);
  37. for (const char* bad : { "", "nightly", "v1", "1.2.3.4.5", "1.x.3", "1.2.3-", "1.2.3-a..b", "1.2.3-ä" }) // (four numbers: round 190hl)
  38. CHECK (! parseVersion (bad).valid);
  39. auto lt = [] (const char* a, const char* b) { return compare (parseVersion (a), parseVersion (b)) < 0 && compare (parseVersion (b), parseVersion (a)) > 0; };
  40. CHECK (lt ("0.1.20", "0.1.21-alpha")); // a newer prerelease is an update for the running 0.1.20
  41. // the release a build's CMake version names IS its tag (round 190hl; strict semver had "0.1.20-alpha" < "0.1.20")
  42. CHECK (compare (parseVersion ("0.1.20-alpha"), parseVersion ("0.1.20")) == 0);
  43. CHECK (lt ("0.1.9", "0.1.10")); // numbers, not text
  44. CHECK (lt ("0.9.99", "1.0.0"));
  45. CHECK (lt ("1.0.0-alpha", "1.0.0-alpha.1"));
  46. CHECK (lt ("1.0.0-alpha.1", "1.0.0-alpha.beta"));
  47. CHECK (lt ("1.0.0-alpha.beta", "1.0.0-beta"));
  48. CHECK (lt ("1.0.0-beta.2", "1.0.0-beta.11"));
  49. CHECK (compare (parseVersion ("1.0.0-rc.1"), parseVersion ("1.0.0")) == 0); // (round 190hl: as above)
  50. CHECK (compare (parseVersion ("v0.1.20"), parseVersion ("0.1.20+build7")) == 0);
  51. }
  52. TEST (update166b_parses_the_release_list_and_picks_the_newest)
  53. {
  54. using namespace lynx::update;
  55. std::string error;
  56. const auto releases = parseReleases (updateFixture, &error);
  57. CHECK (error.empty());
  58. CHECK (releases.size() == 4); // "nightly" dropped
  59. if (releases.size() != 4)
  60. return;
  61. CHECK (releases[0].draft && releases[0].version.text() == "0.1.23-alpha");
  62. const auto& r = releases[1];
  63. CHECK (r.tag == "v0.1.22-alpha" && r.name == "LYNX SHARE 0.1.22-alpha" && r.prerelease && ! r.draft);
  64. CHECK (r.page == "https://github.com/Ka1-S/LYNX-SHARE/releases/tag/v0.1.22-alpha");
  65. CHECK (r.assets.size() == 9 && r.assets[2].size == 30000000 && r.assets[2].url == "https://github.com/d/LYNX-0.1.22-alpha-setup-x64.exe");
  66. CHECK (r.body.find ("\xE2\x80\x94") != std::string::npos); // —
  67. CHECK (r.body.find ("Caf\xC3\xA9 \xF0\x9F\x8C\xB6") != std::string::npos); // é and a surrogate pair
  68. CHECK (releases[2].body.empty()); // "body": null
  69. // the draft (0.1.23) is skipped: 0.1.22-alpha is the update for 0.1.20 and 0.1.21
  70. CHECK (newestIndex (releases, parseVersion ("0.1.20")) == 1);
  71. CHECK (newestIndex (releases, parseVersion ("0.1.21")) == 1);
  72. CHECK (newestIndex (releases, parseVersion ("0.1.22")) == -1); // up to date (the release build of 0.1.22-alpha)
  73. CHECK (newestIndex (releases, parseVersion ("0.2.0")) == -1);
  74. // one release object, an error answer, garbage
  75. const auto one = parseReleases (R"({"tag_name":"v1.0.0","draft":false,"assets":[{"name":"a","browser_download_url":"u"}]})");
  76. CHECK (one.size() == 1 && one[0].assets.size() == 1);
  77. error.clear();
  78. CHECK (parseReleases (R"({"message":"API rate limit exceeded for 1.2.3.4."})", &error).empty() && error.find ("rate limit") != std::string::npos);
  79. for (const char* bad : { "", "<html>", "[{\"tag_name\":\"v1.0.0\"", "[1,]", "{\"a\" 1}", "\"unterminated" })
  80. {
  81. error.clear();
  82. CHECK (parseReleases (bad, &error).empty() && ! error.empty());
  83. }
  84. std::string deep (200, '[');
  85. CHECK (parseReleases (deep, &error).empty()); // nesting limit, no stack overflow
  86. }
  87. TEST (update166b_flavour_detection_and_method)
  88. {
  89. using namespace lynx::update;
  90. Facts f;
  91. f.os = "windows";
  92. CHECK (detectFlavour (f) == Flavour::windowsPortable);
  93. f.uninstallerBesideExe = true;
  94. CHECK (detectFlavour (f) == Flavour::windowsInstaller);
  95. f.packaged = true; // the Store package wins (it has no Uninstall.exe anyway)
  96. CHECK (detectFlavour (f) == Flavour::store);
  97. CHECK (! checksForUpdates (Flavour::store) && methodFor (Flavour::store) == Method::none);
  98. f = {};
  99. f.os = "macos";
  100. CHECK (detectFlavour (f) == Flavour::macos);
  101. f.os = "linux";
  102. CHECK (detectFlavour (f) == Flavour::linuxTarball);
  103. f.appImagePath = "/home/u/LYNX-0.1.20-alpha-x86_64.AppImage";
  104. CHECK (detectFlavour (f) == Flavour::linuxAppImage);
  105. f.forced = "Portable";
  106. CHECK (detectFlavour (f) == Flavour::windowsPortable);
  107. f.forced = "nonsense";
  108. CHECK (detectFlavour (f) == Flavour::linuxAppImage);
  109. f = {};
  110. CHECK (detectFlavour (f) == Flavour::unknown && ! checksForUpdates (Flavour::unknown));
  111. CHECK (methodFor (Flavour::windowsInstaller) == Method::runInstaller);
  112. CHECK (methodFor (Flavour::windowsPortable) == Method::downloadAndReveal);
  113. CHECK (methodFor (Flavour::macos) == Method::downloadAndReveal);
  114. CHECK (methodFor (Flavour::linuxAppImage) == Method::appImageUpdate);
  115. CHECK (methodFor (Flavour::linuxTarball) == Method::openPage);
  116. for (auto fl : { Flavour::windowsInstaller, Flavour::windowsPortable, Flavour::store, Flavour::macos, Flavour::linuxAppImage, Flavour::linuxTarball })
  117. CHECK (flavourFromId (flavourId (fl)) == fl);
  118. }
  119. TEST (update166b_asset_choice_per_flavour)
  120. {
  121. using namespace lynx::update;
  122. const auto releases = parseReleases (updateFixture);
  123. CHECK (releases.size() == 4);
  124. if (releases.size() != 4)
  125. return;
  126. const auto& assets = releases[1].assets;
  127. // (the x86_64 Linux packages, whatever this build's architecture: round 190oy added the arm64 ones)
  128. auto name = [&] (Flavour f) { const auto i = assetFor (f, assets, "x86_64"); return i < 0 ? std::string ("-") : assets[(std::size_t) i].name; };
  129. CHECK (name (Flavour::windowsInstaller) == "LYNX-0.1.22-alpha-setup-x64.exe");
  130. CHECK (name (Flavour::windowsPortable) == "LYNX-0.1.22-alpha-windows-x64.zip");
  131. CHECK (name (Flavour::macos) == "LYNX-0.1.22-alpha-macos-universal.dmg");
  132. CHECK (name (Flavour::linuxAppImage) == "LYNX-0.1.22-alpha-x86_64.AppImage"); // not the .zsync
  133. CHECK (name (Flavour::linuxTarball) == "LYNX-0.1.22-alpha-linux-x86_64.tar.gz");
  134. CHECK (name (Flavour::store) == "-");
  135. CHECK (checksumsAsset (assets) == 8);
  136. CHECK (assetFor (Flavour::windowsInstaller, releases[2].assets) == -1 && checksumsAsset (releases[2].assets) == -1);
  137. }
  138. TEST (arm64_190oy_linux_packages_per_architecture)
  139. {
  140. // Linux arm64 (round 190oy): an arm64 build updates from LYNX-<v>-aarch64.AppImage / -linux-aarch64.tar.gz and never
  141. // takes the x86_64 packages (nor an x86_64 build the arm64 ones); a release without arm64 packages offers it nothing
  142. using namespace lynx::update;
  143. std::vector<Asset> assets;
  144. for (const char* n : { "LYNX-0.1.31-alpha-x86_64.AppImage", "LYNX-0.1.31-alpha-x86_64.AppImage.zsync",
  145. "LYNX-0.1.31-alpha-linux-x86_64.tar.gz", "LYNX-0.1.31-alpha-aarch64.AppImage",
  146. "LYNX-0.1.31-alpha-aarch64.AppImage.zsync", "LYNX-0.1.31-alpha-linux-aarch64.tar.gz",
  147. "LYNX-Plugins-0.1.31-alpha-linux-aarch64.tar.xz", "SHA256SUMS.txt" })
  148. assets.push_back ({ n, std::string ("https://example.invalid/") + n, 1 });
  149. auto name = [&] (Flavour f, const char* arch) { const auto i = assetFor (f, assets, arch); return i < 0 ? std::string ("-") : assets[(std::size_t) i].name; };
  150. CHECK (name (Flavour::linuxAppImage, "aarch64") == "LYNX-0.1.31-alpha-aarch64.AppImage");
  151. CHECK (name (Flavour::linuxTarball, "aarch64") == "LYNX-0.1.31-alpha-linux-aarch64.tar.gz");
  152. CHECK (name (Flavour::linuxAppImage, "x86_64") == "LYNX-0.1.31-alpha-x86_64.AppImage");
  153. CHECK (name (Flavour::linuxTarball, "x86_64") == "LYNX-0.1.31-alpha-linux-x86_64.tar.gz");
  154. const std::vector<Asset> x86Only (assets.begin(), assets.begin() + 3);
  155. CHECK (assetFor (Flavour::linuxAppImage, x86Only, "aarch64") == -1 && assetFor (Flavour::linuxTarball, x86Only, "aarch64") == -1);
  156. #if defined(__aarch64__)
  157. CHECK (std::string (buildLinuxArch()) == "aarch64");
  158. #else
  159. CHECK (std::string (buildLinuxArch()) == "x86_64");
  160. #endif
  161. CHECK (assetFor (Flavour::linuxAppImage, assets) == assetFor (Flavour::linuxAppImage, assets, buildLinuxArch()));
  162. }
  163. TEST (update166b_checksum_verification)
  164. {
  165. using namespace lynx::update;
  166. const std::string data = "LYNX SHARE setup";
  167. const auto actual = lynx::sha256Hex (reinterpret_cast<const std::uint8_t*> (data.data()), data.size());
  168. std::string upper = actual;
  169. for (auto& c : upper)
  170. c = (char) std::toupper ((unsigned char) c);
  171. const auto sums = std::string (64, 'a') + " LYNX-0.1.22-alpha-windows-x64.zip\n" + upper + " LYNX-0.1.22-alpha-setup-x64.exe\r\n"
  172. + std::string (64, 'b') + " *LYNX.exe\nnot a line\n" + std::string (63, 'c') + " short.txt\n";
  173. const auto expected = checksumFor (sums, "LYNX-0.1.22-alpha-setup-x64.exe");
  174. CHECK (expected == actual); // (lower case)
  175. CHECK (checksumMatches (expected, actual) && checksumMatches (expected, upper));
  176. CHECK (checksumFor (sums, "LYNX.exe") == std::string (64, 'b')); // binary-mode '*'
  177. CHECK (checksumFor (sums, "short.txt").empty() && checksumFor (sums, "missing.exe").empty() && checksumFor (sums, "LYNX").empty());
  178. const std::string tampered = "LYNX SHARE setuP";
  179. CHECK (! checksumMatches (expected, lynx::sha256Hex (reinterpret_cast<const std::uint8_t*> (tampered.data()), tampered.size())));
  180. // the streaming hash (files) equals the one-shot one for every length around the block and padding edges
  181. std::string bytes;
  182. for (int i = 0; i < 300; ++i)
  183. bytes += (char) (i * 37 + 11);
  184. bool same = true;
  185. for (std::size_t n : { 0, 1, 55, 56, 63, 64, 65, 119, 120, 128, 300 })
  186. {
  187. Sha256Stream s1;
  188. for (std::size_t at = 0; at < n; at += 7) // in odd-sized pieces
  189. s1.add (bytes.data() + at, std::min<std::size_t> (7, n - at));
  190. same = same && s1.finish() == lynx::sha256Hex (reinterpret_cast<const std::uint8_t*> (bytes.data()), n);
  191. }
  192. CHECK (same);
  193. Sha256Stream abc;
  194. abc.add ("abc", 3);
  195. CHECK (abc.finish() == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); // FIPS 180-2 example
  196. CHECK (! checksumMatches ("", "") && ! checksumMatches (std::string (64, 'g'), std::string (64, 'g')));
  197. }
  198. TEST (update166b_skip_version_and_settings)
  199. {
  200. using namespace lynx::update;
  201. const auto s = parseSettings ("automatic=0\nskipped=0.1.22-alpha\nlastCheck=1761900000000\njunk\n");
  202. CHECK (! s.automatic && s.skipped == "0.1.22-alpha" && s.lastCheckMs == 1761900000000LL);
  203. const auto again = parseSettings (formatSettings (s));
  204. CHECK (again.automatic == s.automatic && again.skipped == s.skipped && again.lastCheckMs == s.lastCheckMs);
  205. const auto d = parseSettings ("");
  206. CHECK (d.automatic && d.skipped.empty() && d.lastCheckMs == 0); // on by default
  207. CHECK (parseSettings ("skipped=nonsense\nlastCheck=-5").skipped.empty() && parseSettings ("lastCheck=x").lastCheckMs == 0);
  208. Release r;
  209. r.version = parseVersion ("v0.1.22-alpha");
  210. Settings skip;
  211. skip.skipped = "0.1.22-alpha";
  212. CHECK (! shouldShow (r, skip, false)); // ✕ skipped this one
  213. CHECK (shouldShow (r, skip, true)); // Help › Check for updates still shows it
  214. r.version = parseVersion ("v0.1.23-alpha");
  215. CHECK (shouldShow (r, skip, false)); // a newer one shows again
  216. skip.skipped = "0.1.24";
  217. CHECK (! shouldShow (r, skip, false));
  218. CHECK (shouldShow (r, Settings {}, false));
  219. CHECK (cacheFresh (1000, 1000 + cacheLifetimeMs - 1) && ! cacheFresh (1000, 1000 + cacheLifetimeMs) && ! cacheFresh (0, 5) && ! cacheFresh (5000, 10));
  220. }
  221. TEST (update166b_release_notes_as_text)
  222. {
  223. using namespace lynx::update;
  224. const auto lines = releaseNotes ("# LYNX SHARE 0.1.22\r\n\r\nThe **update**\nbutton: see [the docs](https://x).\n\n\n"
  225. "## New\n- A `bar` ![shot](a.png)\n continues here\n - nested <b>bold</b>\n1. first\n\n"
  226. "| Item | Where |\n|---|:---:|\n| Bar | Main window |\n\n---\n```\ncode **stays**\n```\n> quote <https://lynx-share.com>\n\\*not bold\\*\n");
  227. using K = NoteLine::Kind;
  228. std::vector<std::pair<K, std::string>> want {
  229. { K::heading, "LYNX SHARE 0.1.22" }, { K::blank, "" }, { K::text, "The update button: see the docs." }, { K::blank, "" },
  230. { K::heading, "New" }, { K::bullet, "A bar continues here" }, { K::bullet, "nested bold" }, { K::bullet, "1. first" }, { K::blank, "" },
  231. { K::text, "Item \xC2\xB7 Where" }, { K::blank, "" }, { K::text, "Bar \xC2\xB7 Main window" }, { K::blank, "" },
  232. { K::text, "code **stays**" }, { K::text, "quote https://lynx-share.com *not bold*" } };
  233. CHECK (lines.size() == want.size());
  234. for (std::size_t i = 0; i < lines.size() && i < want.size(); ++i)
  235. {
  236. CHECK (lines[i].kind == want[i].first);
  237. CHECK (lines[i].text == want[i].second);
  238. if (lines[i].text != want[i].second)
  239. std::printf (" line %d: [%s] want [%s]\n", (int) i, lines[i].text.c_str(), want[i].second.c_str());
  240. }
  241. CHECK (lines.size() > 6 && lines[4].level == 2 && lines[6].level == 1);
  242. const auto text = releaseNotesText (lines);
  243. CHECK (text.find ("\xE2\x80\xA2 A bar") != std::string::npos && text.find (" \xE2\x80\xA2 nested") != std::string::npos);
  244. // single-mark emphasis goes, snake_case and a lone star stay
  245. const auto em = releaseNotes ("*Update* installs it, _really_; snake_case_name, 2 * 3 = 6, **bold**");
  246. CHECK (em.size() == 1 && em[0].text == "Update installs it, really; snake_case_name, 2 * 3 = 6, bold");
  247. if (em.size() == 1)
  248. std::printf (" em: [%s]\n", em[0].text.c_str());
  249. CHECK (releaseNotes ("").empty() && releaseNotes ("\n\n\n").empty());
  250. // a GitHub alert marker is dropped; the quoted text stays; a bare ">" is safe
  251. const auto alert = releaseNotes ("Intro\n> [!IMPORTANT]\n> Still an alpha.\n>\n> More.");
  252. CHECK (alert.size() == 3 && alert[0].text == "Intro" && alert[1].text == "Still an alpha." && alert[2].text == "More.");
  253. CHECK (releaseNotes (">").empty() && releaseNotes ("> [!NOTE]").empty());
  254. // a line of HTML only (the logo at the top of the release notes) leaves no empty line
  255. const auto logo = releaseNotes ("<p align=\"center\"><img src=\"x.png\" width=\"96\"></p>\n\n**LYNX SHARE 0.1.20 alpha**: text");
  256. CHECK (logo.size() == 1 && logo[0].text == "LYNX SHARE 0.1.20 alpha: text");
  257. }