gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/polkit/spec_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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 polkit_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"gitee.com/mysnapcore/mysnapd/interfaces"
    26  	"gitee.com/mysnapcore/mysnapd/interfaces/ifacetest"
    27  	"gitee.com/mysnapcore/mysnapd/interfaces/polkit"
    28  	"gitee.com/mysnapcore/mysnapd/snap"
    29  )
    30  
    31  type specSuite struct {
    32  	iface    *ifacetest.TestInterface
    33  	spec     *polkit.Specification
    34  	plugInfo *snap.PlugInfo
    35  	plug     *interfaces.ConnectedPlug
    36  	slotInfo *snap.SlotInfo
    37  	slot     *interfaces.ConnectedSlot
    38  }
    39  
    40  var _ = Suite(&specSuite{
    41  	iface: &ifacetest.TestInterface{
    42  		InterfaceName: "test",
    43  		PolkitConnectedPlugCallback: func(spec *polkit.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    44  			return spec.AddPolicy("connected-plug", polkit.Policy("policy-connected-plug"))
    45  		},
    46  		PolkitConnectedSlotCallback: func(spec *polkit.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    47  			return spec.AddPolicy("connected-slot", polkit.Policy("policy-connected-slot"))
    48  		},
    49  		PolkitPermanentPlugCallback: func(spec *polkit.Specification, plug *snap.PlugInfo) error {
    50  			return spec.AddPolicy("permanent-plug", polkit.Policy("policy-permanent-plug"))
    51  		},
    52  		PolkitPermanentSlotCallback: func(spec *polkit.Specification, slot *snap.SlotInfo) error {
    53  			return spec.AddPolicy("permanent-slot", polkit.Policy("policy-permanent-slot"))
    54  		},
    55  	},
    56  	plugInfo: &snap.PlugInfo{
    57  		Snap:      &snap.Info{SuggestedName: "snap1"},
    58  		Name:      "name",
    59  		Interface: "test",
    60  		Apps: map[string]*snap.AppInfo{
    61  			"app1": {
    62  				Snap: &snap.Info{
    63  					SuggestedName: "snap1",
    64  				},
    65  				Name: "app1"}},
    66  	},
    67  	slotInfo: &snap.SlotInfo{
    68  		Snap:      &snap.Info{SuggestedName: "snap2"},
    69  		Name:      "name",
    70  		Interface: "test",
    71  		Apps: map[string]*snap.AppInfo{
    72  			"app2": {
    73  				Snap: &snap.Info{
    74  					SuggestedName: "snap2",
    75  				},
    76  				Name: "app2"}},
    77  	},
    78  })
    79  
    80  func (s *specSuite) SetUpTest(c *C) {
    81  	s.spec = &polkit.Specification{}
    82  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    83  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    84  }
    85  
    86  // The spec.Specification can be used through the interfaces.Specification interface
    87  func (s *specSuite) TestSpecificationIface(c *C) {
    88  	var r interfaces.Specification = s.spec
    89  	c.Assert(r.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    90  	c.Assert(r.AddConnectedSlot(s.iface, s.plug, s.slot), IsNil)
    91  	c.Assert(r.AddPermanentPlug(s.iface, s.plugInfo), IsNil)
    92  	c.Assert(r.AddPermanentSlot(s.iface, s.slotInfo), IsNil)
    93  	c.Assert(s.spec.Policies(), DeepEquals, map[string]polkit.Policy{
    94  		"connected-plug": polkit.Policy("policy-connected-plug"),
    95  		"connected-slot": polkit.Policy("policy-connected-slot"),
    96  		"permanent-plug": polkit.Policy("policy-permanent-plug"),
    97  		"permanent-slot": polkit.Policy("policy-permanent-slot"),
    98  	})
    99  }