Home | History | Annotate | Download | only in proptools
      1 // Copyright 2015 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package proptools
     16 
     17 import (
     18 	"os/exec"
     19 	"testing"
     20 )
     21 
     22 type escapeTestCase struct {
     23 	name string
     24 	in   string
     25 	out  string
     26 }
     27 
     28 var ninjaEscapeTestCase = []escapeTestCase{
     29 	{
     30 		name: "no escaping",
     31 		in:   `test`,
     32 		out:  `test`,
     33 	},
     34 	{
     35 		name: "leading $",
     36 		in:   `$test`,
     37 		out:  `$$test`,
     38 	},
     39 	{
     40 		name: "trailing $",
     41 		in:   `test$`,
     42 		out:  `test$$`,
     43 	},
     44 	{
     45 		name: "leading and trailing $",
     46 		in:   `$test$`,
     47 		out:  `$$test$$`,
     48 	},
     49 }
     50 
     51 var shellEscapeTestCase = []escapeTestCase{
     52 	{
     53 		name: "no escaping",
     54 		in:   `test`,
     55 		out:  `test`,
     56 	},
     57 	{
     58 		name: "leading $",
     59 		in:   `$test`,
     60 		out:  `'$test'`,
     61 	},
     62 	{
     63 		name: "trailing $",
     64 		in:   `test$`,
     65 		out:  `'test$'`,
     66 	},
     67 	{
     68 		name: "leading and trailing $",
     69 		in:   `$test$`,
     70 		out:  `'$test$'`,
     71 	},
     72 	{
     73 		name: "single quote",
     74 		in:   `'`,
     75 		out:  `''\'''`,
     76 	},
     77 	{
     78 		name: "multiple single quote",
     79 		in:   `''`,
     80 		out:  `''\'''\'''`,
     81 	},
     82 	{
     83 		name: "double quote",
     84 		in:   `""`,
     85 		out:  `'""'`,
     86 	},
     87 	{
     88 		name: "ORIGIN",
     89 		in:   `-Wl,--rpath,${ORIGIN}/../bionic-loader-test-libs`,
     90 		out:  `'-Wl,--rpath,${ORIGIN}/../bionic-loader-test-libs'`,
     91 	},
     92 }
     93 
     94 func TestNinjaEscaping(t *testing.T) {
     95 	for _, testCase := range ninjaEscapeTestCase {
     96 		got := NinjaEscape([]string{testCase.in})[0]
     97 		if got != testCase.out {
     98 			t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.out, got)
     99 		}
    100 	}
    101 }
    102 
    103 func TestShellEscaping(t *testing.T) {
    104 	for _, testCase := range shellEscapeTestCase {
    105 		got := ShellEscape([]string{testCase.in})[0]
    106 		if got != testCase.out {
    107 			t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.out, got)
    108 		}
    109 	}
    110 }
    111 
    112 func TestExternalShellEscaping(t *testing.T) {
    113 	if testing.Short() {
    114 		return
    115 	}
    116 	for _, testCase := range shellEscapeTestCase {
    117 		cmd := "echo -n " + ShellEscape([]string{testCase.in})[0]
    118 		got, err := exec.Command("/bin/sh", "-c", cmd).Output()
    119 		if err != nil {
    120 			t.Error(err)
    121 		}
    122 		if string(got) != testCase.in {
    123 			t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.in, got)
    124 		}
    125 	}
    126 }
    127