1// Copyright 2014 The Android Open Source Project
2//
3// This software is licensed under the terms of the GNU General Public
4// License version 2, as published by the Free Software Foundation, and
5// may be copied, distributed, and modified under those terms.
6//
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10// GNU General Public License for more details.
11
12#include "android/utils/win32_cmdline_quote.h"
13
14#include <gtest/gtest.h>
15
16#include "android/utils/system.h"
17
18// Helper class to hold a scoped heap-allocated string pointer that gets
19// deleted on scope exit.
20class ScopedCString {
21public:
22  ScopedCString(const char* str) : str_(str) {}
23  const char* str() const { return str_; }
24  ~ScopedCString() { AFREE((void*)str_); }
25private:
26  const char* str_;
27};
28
29#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
30
31TEST(win32_cmdline_quote,Test) {
32  static const struct {
33      const char* input;
34      const char* expected;
35  } kData[] = {
36      { "foo", "foo" },
37      { "foo bar", "\"foo bar\"" },
38      { "foo\\bar", "foo\\bar" },
39      { "foo\\\\bar", "foo\\\\bar" },
40      { "foo\"bar", "\"foo\\\"bar\"" },
41      { "foo\\\"bar", "\"foo\\\\\\\"bar\"" },
42      { "foo\\bar zoo", "\"foo\\bar zoo\"" },
43  };
44  for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
45      const char* input = kData[n].input;
46      const char* expected = kData[n].expected;
47
48      ScopedCString out(win32_cmdline_quote(input));
49      EXPECT_STREQ(expected, out.str()) << "Quoting '" << input << "'";
50  }
51}
52