File: TestURILoader.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (190 lines) | stat: -rw-r--r-- 7,554 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkNew.h"
#include "vtkTestUtilities.h"
#include "vtkURILoader.h"

#include <iostream>

#define Check(expr, message)                                                                       \
  do                                                                                               \
  {                                                                                                \
    if (!(expr))                                                                                   \
    {                                                                                              \
      vtkErrorWithObjectMacro(nullptr, "Test failed: \n" << message);                              \
      return false;                                                                                \
    }                                                                                              \
  } while (false)

namespace
{

bool Resolve(vtkURILoader* loader, const std::string& input, const std::string& expected)
{
  auto parsed = vtkURI::Parse(input);
  Check(parsed, "Failed to parse uri \"" + input + "\"");
  auto resolved = loader->Resolve(parsed);
  Check(resolved, "Failed to resolve \"" + input + "\"");

  const auto resolvedStr = resolved->ToString();
  Check(resolvedStr == expected,
    "Resolved URI (" + resolvedStr + ", resolved from \"" + input +
      "\") does not match expected URI(" + expected + ")");

  return true;
}

bool TestResolution()
{
  vtkNew<vtkURILoader> loader;

  // Basic tests: https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.1
  loader->SetBaseURI("http://a/b/c/d;p?q");
  Check(Resolve(loader, "g:h", "g:h"), "Resolution failed");
  Check(Resolve(loader, "g", "http://a/b/c/g"), "Resolution failed");
  Check(Resolve(loader, "./g", "http://a/b/c/g"), "Resolution failed");
  Check(Resolve(loader, "g/", "http://a/b/c/g/"), "Resolution failed");
  Check(Resolve(loader, "/g", "http://a/g"), "Resolution failed");
  Check(Resolve(loader, "//g", "http://g"), "Resolution failed");
  Check(Resolve(loader, "?y", "http://a/b/c/d;p?y"), "Resolution failed");
  Check(Resolve(loader, "g?y", "http://a/b/c/g?y"), "Resolution failed");
  Check(Resolve(loader, "#s", "http://a/b/c/d;p?q#s"), "Resolution failed");
  Check(Resolve(loader, "g#s", "http://a/b/c/g#s"), "Resolution failed");
  Check(Resolve(loader, "g?y#s", "http://a/b/c/g?y#s"), "Resolution failed");
  Check(Resolve(loader, ";x", "http://a/b/c/;x"), "Resolution failed");
  Check(Resolve(loader, "g;x", "http://a/b/c/g;x"), "Resolution failed");
  Check(Resolve(loader, "g;x?y#s", "http://a/b/c/g;x?y#s"), "Resolution failed");
  Check(Resolve(loader, "", "http://a/b/c/d;p?q"), "Resolution failed");
  Check(Resolve(loader, ".", "http://a/b/c/"), "Resolution failed");
  Check(Resolve(loader, "./", "http://a/b/c/"), "Resolution failed");
  Check(Resolve(loader, "..", "http://a/b/"), "Resolution failed");
  Check(Resolve(loader, "../", "http://a/b/"), "Resolution failed");
  Check(Resolve(loader, "../g", "http://a/b/g"), "Resolution failed");
  Check(Resolve(loader, "../..", "http://a/"), "Resolution failed");
  Check(Resolve(loader, "../../", "http://a/"), "Resolution failed");
  Check(Resolve(loader, "../../g", "http://a/g"), "Resolution failed");

  // "Abnormal" tests: https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.2
  Check(Resolve(loader, "/./g", "http://a/g"), "Resolution failed");
  Check(Resolve(loader, "/../g", "http://a/g"), "Resolution failed");
  Check(Resolve(loader, "g.", "http://a/b/c/g."), "Resolution failed");
  Check(Resolve(loader, ".g", "http://a/b/c/.g"), "Resolution failed");
  Check(Resolve(loader, "g..", "http://a/b/c/g.."), "Resolution failed");
  Check(Resolve(loader, "..g", "http://a/b/c/..g"), "Resolution failed");
  Check(Resolve(loader, "./../g", "http://a/b/g"), "Resolution failed");
  Check(Resolve(loader, "./g/.", "http://a/b/c/g/"), "Resolution failed");
  Check(Resolve(loader, "g/./h", "http://a/b/c/g/h"), "Resolution failed");
  Check(Resolve(loader, "g/../h", "http://a/b/c/h"), "Resolution failed");
  Check(Resolve(loader, "g;x=1/./y", "http://a/b/c/g;x=1/y"), "Resolution failed");
  Check(Resolve(loader, "g;x=1/../y", "http://a/b/c/y"), "Resolution failed");
  Check(Resolve(loader, "g?y/./x", "http://a/b/c/g?y/./x"), "Resolution failed");
  Check(Resolve(loader, "g?y/../x", "http://a/b/c/g?y/../x"), "Resolution failed");
  Check(Resolve(loader, "g#s/./x", "http://a/b/c/g#s/./x"), "Resolution failed");
  Check(Resolve(loader, "g#s/../x", "http://a/b/c/g#s/../x"), "Resolution failed");
  // "http:g" can be resolved to "http:g" or "http://a/b/c/g"
  // "http:g" is what a strict parser should output.
  Check(Resolve(loader, "http:g", "http:g"), "Resolution failed");

  return true;
}

bool TestFileLoading(const std::string& tmpDir)
{
  std::ofstream{ tmpDir + "/URI Loader Tmp File.txt" } << "Hello world!";

  {
    vtkNew<vtkURILoader> loader;
    Check(loader->SetBaseDirectory(tmpDir), "Can not set base directory");

    // Percent-encoded to have a valid URI
    auto stream = loader->Load("URI%20Loader%20Tmp%20File.txt");
    Check(stream, "Could not load file URI");

    std::string text;
    text.resize(12);
    // NOLINTNEXTLINE(readability-container-data-pointer)
    Check(stream->Read(&text[0], text.size()) == text.size(), "Truncated stream");
    Check(text == "Hello world!", "Wrong data");
  }

  // same as previous but with SetBaseFileName
  {
    // NOLINTNEXTLINE(bugprone-unused-raii)
    std::ofstream{ tmpDir + "/URI Loader Tmp Ref File.txt" }; // create file

    vtkNew<vtkURILoader> loader;
    Check(loader->SetBaseFileName(tmpDir + "/URI Loader Tmp Ref File.txt"),
      "Can not set base directory");

    // Percent-encoded to have a valid URI
    auto stream = loader->Load("URI%20Loader%20Tmp%20File.txt");
    Check(stream, "Could not load file URI");

    std::string text;
    text.resize(12);
    // NOLINTNEXTLINE(readability-container-data-pointer)
    Check(stream->Read(&text[0], text.size()) == text.size(), "Truncated stream");
    Check(text == "Hello world!", "Wrong data");
  }

  return true;
}

bool TestBase64DataLoading()
{
  vtkNew<vtkURILoader> loader;
  auto stream = loader->Load("data:;base64,SGVsbG8gd29ybGQh");
  Check(stream, "Could not load data URI");

  std::string text;
  text.resize(12);
  // NOLINTNEXTLINE(readability-container-data-pointer)
  Check(stream->Read(&text[0], text.size()) == text.size(), "Truncated stream");
  Check(text == "Hello world!", "Wrong data");

  return true;
}

bool TestRawDataLoading()
{
  vtkNew<vtkURILoader> loader;
  auto stream = loader->Load("data:,%00%40%12hello");
  Check(stream, "Could not load data URI");

  std::vector<char> data;
  data.resize(8);
  Check(stream->Read(data.data(), data.size()) == data.size(), "Truncated stream");
  Check((data == std::vector<char>{ 0x00, 0x40, 0x12, 'h', 'e', 'l', 'l', 'o' }), "Wrong data");

  return true;
}

}

int TestURILoader(int argc, char* argv[])
{
  if (!TestResolution())
  {
    return EXIT_FAILURE;
  }

  char* tempDir =
    vtkTestUtilities::GetArgOrEnvOrDefault("-T", argc, argv, "VTK_TEMP_DIR", "Testing/Temporary");
  if (!TestFileLoading(tempDir))
  {
    return EXIT_FAILURE;
  }
  delete[] tempDir;

  if (!TestBase64DataLoading())
  {
    return EXIT_FAILURE;
  }

  if (!TestRawDataLoading())
  {
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}