Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #120: Byteorder mark removal in UncSource removes first left angle bracket '<' of xml document #123

Merged
merged 2 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/NAppUpdate.Framework/Sources/UncSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ public UncSource(string feedUncPath, string uncPath)
/// </summary>
public string UncPath { get; set; }

private readonly string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());

public string GetUpdatesFeed()
{
string data = File.ReadAllText(FeedUncPath, Encoding.UTF8);

if (data.StartsWith(_byteOrderMarkUtf8))
data = data.Remove(0, _byteOrderMarkUtf8.Length);
// Remove byteorder mark if necessary
int indexTagOpening = data.IndexOf('<');
if (indexTagOpening > 0)
{
data = data.Substring( indexTagOpening );
}

return data;
}
Expand Down
1 change: 1 addition & 0 deletions src/NAppUpdate.Tests/NAppUpdate.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="Conditions\FileVersionConditionTests.cs" />
<Compile Include="Core\UpdateStarterTests.cs" />
<Compile Include="FeedReaders\NauXmlFeedReaderTests.cs" />
<Compile Include="Sources\SourceEncodingTests.cs" />
<Compile Include="Integration\FileDownloaderTests.cs" />
<Compile Include="Integration\SimpleWebSourceTests.cs" />
<Compile Include="Tasks\BasicTaskTests.cs" />
Expand Down
61 changes: 61 additions & 0 deletions src/NAppUpdate.Tests/Sources/SourceEncodingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NAppUpdate.Framework;
using NAppUpdate.Framework.Sources;
using NAppUpdate.Framework.Conditions;
using NAppUpdate.Framework.Tasks;
using NAppUpdate.Framework.FeedReaders;

namespace NAppUpdate.Tests.Sources
{
/// <summary>
/// Tests for different source file feed encodings: UTF-8 UTF-16LE UTF-16BE ANSI
/// </summary>
[TestClass]
public class SourceEncodingTests
{
private void ReadUpdateFeed(IUpdateSource sc)
{
IUpdateFeedReader fr = new NauXmlFeedReader();

var tasks = fr.Read(sc.GetUpdatesFeed());

Assert.IsNotNull(tasks);
Assert.IsTrue(tasks.Count == 3);
Assert.IsTrue(tasks[0].Description.StartsWith("ÖÜÄ - some non 7-bit chars"));
}

[TestMethod]
public void ReadUTF8File()
{
ReadUpdateFeed(new UncSource(@"..\..\Sources\TestFeedXML.utf-8.xml", null));
}

[TestMethod]
public void ReadUTF8BOMFile()
{
ReadUpdateFeed(new UncSource(@"..\..\Sources\TestFeedXML.utf-8.bom.xml", null));
}

[TestMethod]
public void ReadUTF16BigEndianFile()
{
ReadUpdateFeed(new UncSource(@"..\..\Sources\TestFeedXML.utf-16.be.xml", null));
}

[TestMethod]
public void ReadUTF16LittleEndianFile()
{
ReadUpdateFeed(new UncSource(@"..\..\Sources\TestFeedXML.utf-16.le.xml", null));
}

[TestMethod]
public void ReadANSIFile()
{
ReadUpdateFeed(new UncSource(@"..\..\Sources\TestFeedXML.ansi.xml", null));
}
}
}
24 changes: 24 additions & 0 deletions src/NAppUpdate.Tests/Sources/TestFeedXML.ansi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Feed>
<Tasks>
<FileUpdateTask hotswap="false" updateTo="file:///c:/test/htmlayout.dll" localPath="htmlayout.dll">
<Description>��� - some non 7-bit chars</Description>
<Conditions>
<FileVersionCondition what="below" version="3.3.2.10" />
<OSCondition type="and-not" bit="64" />
<FileSizeCondition type="and" what="above" size="1000" />
<FileDateCondition type="or" what="is" timestamp="20091010T000000" />
</Conditions>
</FileUpdateTask>
<FileUpdateTask updateTo="file:///c:/test/htmlayout.dll" localPath="htmlayout64.dll">
<Description>��� - some non 7-bit chars (for 64 bit platforms)</Description>
<Conditions>
<FileVersionCondition what="below" version="3.3.2.10" />
<OSCondition type="and" bit="64" />
<FileSizeCondition type="and-not" what="below" size="1000" />
<FileExistsCondition type="or" />
</Conditions>
</FileUpdateTask>
<FileUpdateTask hotswap="true" localPath="logo.png" updateTo="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" />
</Tasks>
</Feed>
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions src/NAppUpdate.Tests/Sources/TestFeedXML.utf-8.bom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<Feed>
<Tasks>
<FileUpdateTask hotswap="false" updateTo="file:///c:/test/htmlayout.dll" localPath="htmlayout.dll">
<Description>ÖÜÄ - some non 7-bit chars</Description>
<Conditions>
<FileVersionCondition what="below" version="3.3.2.10" />
<OSCondition type="and-not" bit="64" />
<FileSizeCondition type="and" what="above" size="1000" />
<FileDateCondition type="or" what="is" timestamp="20091010T000000" />
</Conditions>
</FileUpdateTask>
<FileUpdateTask updateTo="file:///c:/test/htmlayout.dll" localPath="htmlayout64.dll">
<Description>ÖÜÄ - some non 7-bit chars (for 64 bit platforms)</Description>
<Conditions>
<FileVersionCondition what="below" version="3.3.2.10" />
<OSCondition type="and" bit="64" />
<FileSizeCondition type="and-not" what="below" size="1000" />
<FileExistsCondition type="or" />
</Conditions>
</FileUpdateTask>
<FileUpdateTask hotswap="true" localPath="logo.png" updateTo="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" />
</Tasks>
</Feed>
24 changes: 24 additions & 0 deletions src/NAppUpdate.Tests/Sources/TestFeedXML.utf-8.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<Feed>
<Tasks>
<FileUpdateTask hotswap="false" updateTo="file:///c:/test/htmlayout.dll" localPath="htmlayout.dll">
<Description>ÖÜÄ - some non 7-bit chars</Description>
<Conditions>
<FileVersionCondition what="below" version="3.3.2.10" />
<OSCondition type="and-not" bit="64" />
<FileSizeCondition type="and" what="above" size="1000" />
<FileDateCondition type="or" what="is" timestamp="20091010T000000" />
</Conditions>
</FileUpdateTask>
<FileUpdateTask updateTo="file:///c:/test/htmlayout.dll" localPath="htmlayout64.dll">
<Description>ÖÜÄ - some non 7-bit chars (for 64 bit platforms)</Description>
<Conditions>
<FileVersionCondition what="below" version="3.3.2.10" />
<OSCondition type="and" bit="64" />
<FileSizeCondition type="and-not" what="below" size="1000" />
<FileExistsCondition type="or" />
</Conditions>
</FileUpdateTask>
<FileUpdateTask hotswap="true" localPath="logo.png" updateTo="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" />
</Tasks>
</Feed>