gitee.com/mysnapcore/mysnapd@v0.1.0/x11/xauth_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package x11_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"gitee.com/mysnapcore/mysnapd/x11"
    30  )
    31  
    32  // Hook up check.v1 into the "go test" runner
    33  func Test(t *testing.T) { TestingT(t) }
    34  
    35  type xauthTestSuite struct{}
    36  
    37  var _ = Suite(&xauthTestSuite{})
    38  
    39  func (s *xauthTestSuite) TestXauthFileNotAvailable(c *C) {
    40  	err := x11.ValidateXauthorityFile("/does/not/exist")
    41  	c.Assert(err, NotNil)
    42  }
    43  
    44  func (s *xauthTestSuite) TestXauthFileExistsButIsEmpty(c *C) {
    45  	xauthPath, err := x11.MockXauthority(0)
    46  	c.Assert(err, IsNil)
    47  	defer os.Remove(xauthPath)
    48  
    49  	err = x11.ValidateXauthorityFile(xauthPath)
    50  	c.Assert(err, ErrorMatches, "Xauthority file is invalid")
    51  }
    52  
    53  func (s *xauthTestSuite) TestXauthFileExistsButHasInvalidContent(c *C) {
    54  	f, err := ioutil.TempFile("", "xauth")
    55  	c.Assert(err, IsNil)
    56  	defer os.Remove(f.Name())
    57  
    58  	data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}
    59  	n, err := f.Write(data)
    60  	c.Assert(err, IsNil)
    61  	c.Assert(n, Equals, len(data))
    62  
    63  	err = x11.ValidateXauthorityFile(f.Name())
    64  	c.Assert(err, ErrorMatches, "unexpected EOF")
    65  }
    66  
    67  func (s *xauthTestSuite) TestValidXauthFile(c *C) {
    68  	for _, n := range []int{1, 2, 4} {
    69  		path, err := x11.MockXauthority(n)
    70  		c.Assert(err, IsNil)
    71  		err = x11.ValidateXauthorityFile(path)
    72  		c.Assert(err, IsNil)
    73  	}
    74  }