From 7e0cd0b7f1286a39ddb86c7a78609ebb55680126 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Wed, 19 Jul 2017 20:51:07 +0530
Subject: [PATCH] magneturi: two passing tests

---
 functorrent.cabal            | 21 ++++++---------------
 src/FuncTorrent/MagnetURI.hs |  7 ++++---
 test/MagneturiTests.hs       |  2 +-
 3 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/functorrent.cabal b/functorrent.cabal
index cec4735..b85e836 100644
--- a/functorrent.cabal
+++ b/functorrent.cabal
@@ -53,7 +53,7 @@ library
                        safe,
                        transformers
 
-executable functorrent
+executable functorrent-exe
   main-is:             Main.hs
   other-extensions:    OverloadedStrings
   hs-source-dirs:      src/main
@@ -79,17 +79,8 @@ test-suite functorrent-test
   type:              exitcode-stdio-1.0
   default-language:  Haskell2010
   hs-source-dirs:    test
-  main-is:           Test.hs
-  other-modules:     BencodeTests
-  build-depends:     base,
-                     functorrent,
-                     bytestring,
-                     containers,
-                     directory,
-                     doctest,
-                     QuickCheck,
-                     tasty,
-                     tasty-hunit,
-                     QuickCheck,
-                     tasty-quickcheck,
-                     test-framework-quickcheck2
+  main-is:           Main.hs
+  other-modules:     MagneturiTests
+  build-depends:     base
+                   , functorrent
+                   , hspec
diff --git a/src/FuncTorrent/MagnetURI.hs b/src/FuncTorrent/MagnetURI.hs
index e0fd545..7a9b20e 100644
--- a/src/FuncTorrent/MagnetURI.hs
+++ b/src/FuncTorrent/MagnetURI.hs
@@ -45,6 +45,7 @@ module FuncTorrent.MagnetURI where
 import Text.ParserCombinators.Parsec
 import qualified Text.Parsec.ByteString as ParsecBS
 import Data.ByteString.Char8 (ByteString, pack)
+import Network.HTTP.Base (urlDecode)
 
 data Magnetinfo = Magnetinfo { infoHash :: ByteString
                              , trackerlist :: [String]
@@ -60,7 +61,7 @@ kvpair :: ParsecBS.Parser (String, String)
 kvpair = do
   k <- many1 letter
   _ <- char '='
-  v <- many1 (letter <|> digit <|> (char ':'))
+  v <- many1 (noneOf "&")
   return (k, v)
 
 magnetBody :: ParsecBS.Parser Magnetinfo
@@ -72,8 +73,8 @@ magnetBody = do
             case k of
               "xt" -> magnetRecord { infoHash = pack v }
               "tr" -> let trVal = trackerlist magnetRecord in
-                        magnetRecord { trackerlist = trVal ++ [v] }
-              "dn" -> magnetRecord { name = v }
+                        magnetRecord { trackerlist = trVal ++ [urlDecode v] }
+              "dn" -> magnetRecord { name = urlDecode v }
               "xl" -> magnetRecord { xlen = Just (read v :: Integer) }
           magnetInfo = Magnetinfo { infoHash = mempty
                                   , trackerlist = mempty
diff --git a/test/MagneturiTests.hs b/test/MagneturiTests.hs
index 3689ea4..7dd83c4 100644
--- a/test/MagneturiTests.hs
+++ b/test/MagneturiTests.hs
@@ -33,5 +33,5 @@ tests = hspec $ do
       `shouldBe` (Right (Magnetinfo {infoHash = "urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", trackerlist = [], name = "", xlen = Nothing}))
     it "Valid MagnetURI with tracker list" $ do
       parseMagneturi "magnet:?xt=urn:btih:1f8a4ee3c3f57e81f8f0b4e658177201fc2a3118&dn=Honey+Bee+2+%5B2017%5D+Malayalam+DVDRiP+x264+AAC+700MB+ZippyMovieZ+E&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"
-      `shouldBe` (Right (Magnetinfo {infoHash = "urn:btih:1f8a4ee3c3f57e81f8f0b4e658177201fc2a3118", trackerlist = [ "udp://tracker.leechers-paradise.org", "udp://zer0day.ch/", "udp://open.demonii.com/", "udp://tracker.coppersurfer.tk/", "udp://exodus.desync.com/"], name = "Honey+Bee+2+%5B2017%5D+Malayalam+DVDRiP+x264+AAC+700MB+ZippyMovieZ+E", xlen = Nothing}))
+      `shouldBe` (Right (Magnetinfo {infoHash = "urn:btih:1f8a4ee3c3f57e81f8f0b4e658177201fc2a3118", trackerlist = [ "udp://tracker.leechers-paradise.org:6969", "udp://zer0day.ch:1337", "udp://open.demonii.com:1337", "udp://tracker.coppersurfer.tk:6969", "udp://exodus.desync.com:6969"], name = "Honey+Bee+2+[2017]+Malayalam+DVDRiP+x264+AAC+700MB+ZippyMovieZ+E", xlen = Nothing}))
 
-- 
2.45.2