1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "URL.h"
10 #include "filesystem/NFSFile.h"
11 #include "test/TestUtils.h"
12 
13 #include <errno.h>
14 #include <string>
15 
16 #include <gtest/gtest.h>
17 
18 using ::testing::Test;
19 using ::testing::WithParamInterface;
20 using ::testing::ValuesIn;
21 
22 struct SplitPath
23 {
24   std::string url;
25   std::string exportPath;
26   std::string relativePath;
27   bool expectedResultExport;
28   bool expectedResultPath;
29 } g_TestData[] = {
30                    {"nfs://192.168.0.1:2049/srv/test/tvmedia/foo.txt", "/srv/test", "//tvmedia/foo.txt", true, true},
31                    {"nfs://192.168.0.1/srv/test/tv/media/foo.txt", "/srv/test/tv", "//media/foo.txt", true, true},
32                    {"nfs://192.168.0.1:2049/srv/test/tvmedia", "/srv/test", "//tvmedia", true, true},
33                    {"nfs://192.168.0.1:2049/srv/test/tvmedia/", "/srv/test", "//tvmedia/", true, true},
34                    {"nfs://192.168.0.1:2049/srv/test/tv/media", "/srv/test/tv", "//media", true, true},
35                    {"nfs://192.168.0.1:2049/srv/test/tv/media/", "/srv/test/tv", "//media/", true, true},
36                    {"nfs://192.168.0.1:2049/srv/test/tv", "/srv/test/tv", "//", true, true},
37                    {"nfs://192.168.0.1:2049/srv/test/", "/srv/test", "//", true, true},
38                    {"nfs://192.168.0.1:2049/", "/", "//", true, true},
39                    {"nfs://192.168.0.1:2049/notexported/foo.txt", "/", "//notexported/foo.txt", true, true},
40 
41                    {"nfs://192.168.0.1:2049/notexported/foo.txt", "/notexported", "//foo.txt", false, false},
42                  };
43 
44 class TestNfs : public Test,
45                 public WithParamInterface<SplitPath>
46 {
47 };
48 
49 class ExportList
50 {
51   public:
52     std::list<std::string> data;
53 
ExportList()54     ExportList()
55     {
56       data.emplace_back("/srv/test");
57       data.emplace_back("/srv/test/tv");
58       data.emplace_back("/");
59       data.sort();
60       data.reverse();
61     }
62 };
63 
64 static ExportList exportList;
65 
TEST_P(TestNfs,splitUrlIntoExportAndPath)66 TEST_P(TestNfs, splitUrlIntoExportAndPath)
67 {
68   CURL url(GetParam().url);
69   std::string exportPath;
70   std::string relativePath;
71   gNfsConnection.splitUrlIntoExportAndPath(url, exportPath, relativePath, exportList.data);
72 
73   if (GetParam().expectedResultExport)
74     EXPECT_STREQ(GetParam().exportPath.c_str(), exportPath.c_str());
75   else
76     EXPECT_STRNE(GetParam().exportPath.c_str(), exportPath.c_str());
77 
78   if (GetParam().expectedResultPath)
79     EXPECT_STREQ(GetParam().relativePath.c_str(), relativePath.c_str());
80   else
81     EXPECT_STRNE(GetParam().relativePath.c_str(), relativePath.c_str());
82 }
83 
84 INSTANTIATE_TEST_SUITE_P(NfsFile, TestNfs, ValuesIn(g_TestData));
85