private void CheckCryptoKey() { var appListUri = new Uri(_config.SyncServer, new Uri("app-list", UriKind.Relative)); using var webClient = new WebClientTimeout { Credentials = new NetworkCredential(_config.SyncServerUsername, _config.SyncServerPassword), CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore) }; try { AppList.LoadXmlZip(new MemoryStream(webClient.DownloadData(appListUri)), _config.SyncCryptoKey); } #region Error handling catch (WebException ex) when(ex.Status == WebExceptionStatus.ProtocolError && (ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized) { // Wrap exception to add context information throw new WebException(Resources.SyncCredentialsInvalid, ex, ex.Status, ex.Response); } catch (ZipException ex) { // Wrap exception to add context information if (ex.Message == "Invalid password for AES") { throw new InvalidDataException(Resources.SyncCryptoKeyInvalid); } else { throw new InvalidDataException(Resources.SyncServerDataDamaged, ex); } } #endregion }
/// <summary> /// Tests the sync logic with custom <see cref="AppList"/>s. /// </summary> /// <param name="resetMode">The <see cref="SyncResetMode"/> to pass to <see cref="SyncIntegrationManager.Sync"/>.</param> /// <param name="appListLocal">The current local <see cref="AppList"/>.</param> /// <param name="appListLast">The state of the <see cref="AppList"/> after the last successful sync.</param> /// <param name="appListServer">The current server-side <see cref="AppList"/>.</param> private static void TestSync(SyncResetMode resetMode, AppList appListLocal, AppList?appListLast, AppList appListServer) { string appListLocalPath = AppList.GetDefaultPath(); appListLocal.SaveXml(appListLocalPath); appListLast?.SaveXml(appListLocalPath + SyncIntegrationManager.AppListLastSyncSuffix); using var appListServerPath = new TemporaryFile("0install-test-applist"); { using (var stream = File.Create(appListServerPath)) appListServer.SaveXmlZip(stream, CryptoKey); using (var appListServerFile = File.OpenRead(appListServerPath)) { using var syncServer = new MicroServer("app-list", appListServerFile); var config = new Config { SyncServer = new(syncServer.ServerUri), SyncServerUsername = "******", SyncServerPassword = "******", SyncCryptoKey = CryptoKey }; using (var integrationManager = new SyncIntegrationManager(config, _ => new Feed(), new SilentTaskHandler())) integrationManager.Sync(resetMode); appListServer = AppList.LoadXmlZip(syncServer.FileContent, CryptoKey); } } appListLocal = XmlStorage.LoadXml <AppList>(appListLocalPath); appListLast = XmlStorage.LoadXml <AppList>(appListLocalPath + SyncIntegrationManager.AppListLastSyncSuffix); appListServer.Should().Be(appListLocal, because: "Server and local data should be equal after sync"); appListLast.Should().Be(appListLocal, because: "Last sync snapshot and local data should be equal after sync"); }
private static void CheckCryptoKey(SyncServer server, string key) { var appListUri = new Uri(server.Uri, new Uri("app-list", UriKind.Relative)); using (var webClient = new WebClientTimeout { Credentials = server.Credentials, CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore) }) { try { AppList.LoadXmlZip(new MemoryStream(webClient.DownloadData(appListUri)), key); } #region Error handling catch (WebException ex) { // Wrap exception to add context information if (ex.Status == WebExceptionStatus.ProtocolError) { var response = ex.Response as HttpWebResponse; if (response != null && response.StatusCode == HttpStatusCode.Unauthorized) { throw new WebException(Resources.SyncCredentialsInvalid, ex); } } throw; } catch (ZipException ex) { // Wrap exception to add context information if (ex.Message == "Invalid password") { throw new InvalidDataException(Resources.SyncCryptoKeyInvalid); } throw new InvalidDataException(Resources.SyncServerDataDamaged, ex); } #endregion } }