Skip to content

Commit

Permalink
Issue #120: Byteorder mark removal in UncSource removes first left an…
Browse files Browse the repository at this point in the history
…gle bracket '<' of xml document (#123)

* Fix removing bytorder mark not working
see https://stackoverflow.com/a/3282915/542269

* Added test cases for differently encoded XML files
Todo: Check XmlDeclaration for encoding and read file accordingly
  • Loading branch information
sonntam authored and synhershko committed Mar 24, 2018
1 parent 8f53d90 commit e00b096
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 4 deletions.
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>

0 comments on commit e00b096

Please sign in to comment.