github.com/teepark/go-sysvipc@v0.0.0-20200817232735-d7ca6053ea29/msg_test.go (about)

     1  package sysvipc
     2  
     3  import (
     4  	"os"
     5  	"syscall"
     6  	"testing"
     7  )
     8  
     9  func TestMSGBadGet(t *testing.T) {
    10  	if _, err := GetMsgQueue(0xDA7ABA5E, nil); err != syscall.ENOENT {
    11  		t.Error("GetMsgQueue on a non-existent queue without CREAT should fail")
    12  	}
    13  }
    14  
    15  func TestSendRcv(t *testing.T) {
    16  	msgSetup(t)
    17  	defer msgTeardown(t)
    18  
    19  	if err := q.Send(-1, nil, nil); err != syscall.EINVAL {
    20  		t.Error("msgsnd with negative mtyp should fail", err)
    21  	}
    22  
    23  	if _, _, err := MessageQueue(5).Receive(64, -1, nil); err != syscall.EINVAL {
    24  		t.Error("msgrcv with bad msqid should fail", err)
    25  	}
    26  
    27  	q.Send(6, []byte("test message body"), nil)
    28  	msg, mtyp, err := q.Receive(64, -100, nil)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  
    33  	if string(msg) != "test message body" || mtyp != 6 {
    34  		t.Errorf("%q %v", string(msg), mtyp)
    35  	}
    36  }
    37  
    38  func TestNonBlockingSend(t *testing.T) {
    39  	msgSetup(t)
    40  	defer msgTeardown(t)
    41  
    42  	info, err := q.Stat()
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	err = q.Set(&MQInfo{
    48  		Perms: IpcPerms{
    49  			OwnerUID: info.Perms.OwnerUID,
    50  			OwnerGID: info.Perms.OwnerGID,
    51  			Mode:     info.Perms.Mode,
    52  		},
    53  		MaxBytes: 8,
    54  	})
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	if err := q.Send(3, []byte("more than 8"), &MQSendFlags{DontWait: true}); err != syscall.EAGAIN {
    60  		t.Error("too-long write should have failed", err)
    61  	}
    62  }
    63  
    64  func TestNonBlockingReceive(t *testing.T) {
    65  	msgSetup(t)
    66  	defer msgTeardown(t)
    67  
    68  	_, _, err := q.Receive(64, -99, &MQRecvFlags{DontWait: true})
    69  	if err != syscall.EAGAIN && err != syscall.ENOMSG {
    70  		t.Error("non-blocking read against empty queue should fail", err)
    71  	}
    72  }
    73  
    74  func TestMSGNOERR(t *testing.T) {
    75  	msgSetup(t)
    76  	defer msgTeardown(t)
    77  
    78  	if err := q.Send(3, []byte("this message is pretty long"), nil); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	msg, mtyp, err := q.Receive(22, -99, &MQRecvFlags{Truncate: true})
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	if mtyp != 3 {
    87  		t.Error("wrong type?")
    88  	}
    89  	if string(msg) != "this message is pretty" {
    90  		t.Errorf("not properly truncated. '%s'", string(msg))
    91  	}
    92  }
    93  
    94  func TestMSGStats(t *testing.T) {
    95  	msgSetup(t)
    96  	defer msgTeardown(t)
    97  
    98  	msg := "this is a message in a test"
    99  
   100  	info, err := q.Stat()
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if info.Perms.Mode != 0600 {
   105  		t.Error("wrong permissions?")
   106  	}
   107  	if info.Perms.OwnerUID != os.Getuid() {
   108  		t.Error("wrong owner?")
   109  	}
   110  	if info.Perms.CreatorUID != os.Getuid() {
   111  		t.Error("wrong creator?")
   112  	}
   113  	if info.MsgCount != 0 {
   114  		t.Error("phantom messages?")
   115  	}
   116  
   117  	q.Send(4, []byte(msg), nil)
   118  
   119  	info, err = q.Stat()
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	if info.MsgCount != 1 {
   124  		t.Error("missing message?")
   125  	}
   126  	if info.LastSender != os.Getpid() {
   127  		t.Error("wrong last sender?")
   128  	}
   129  }
   130  
   131  func TestMSGSet(t *testing.T) {
   132  	msgSetup(t)
   133  	defer msgTeardown(t)
   134  
   135  	info, err := q.Stat()
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	mqi := &MQInfo{
   141  		Perms: IpcPerms{
   142  			OwnerUID: info.Perms.OwnerUID,
   143  			OwnerGID: info.Perms.OwnerGID,
   144  			Mode:     0644,
   145  		},
   146  	}
   147  
   148  	err = q.Set(mqi)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	info, err = q.Stat()
   154  	if err != nil {
   155  		t.Fatal(err)
   156  	}
   157  
   158  	if info.Perms.Mode != 0644 {
   159  		t.Error("perms change didn't take")
   160  	}
   161  }
   162  
   163  func TestRemove(t *testing.T) {
   164  	msgSetup(t)
   165  	defer msgTeardown(t)
   166  
   167  	if err := MessageQueue(5).Remove(); err != syscall.EINVAL {
   168  		t.Error("remove on a bad mqid should fail", err)
   169  	}
   170  
   171  	if err := q.Remove(); err != nil {
   172  		t.Fatal(err)
   173  	}
   174  
   175  	if _, err := q.Stat(); err != syscall.EINVAL {
   176  		t.Fatal("stat on a removed queue should fail with EINVAL")
   177  	}
   178  
   179  	// so the msgTeardown doesn't fail
   180  	msgSetup(t)
   181  }
   182  
   183  var q MessageQueue
   184  
   185  func msgSetup(t *testing.T) {
   186  	mq, err := GetMsgQueue(0xDA7ABA5E, &MQFlags{
   187  		Create:    true,
   188  		Exclusive: true,
   189  		Perms:     0600,
   190  	})
   191  	if err != nil {
   192  		t.Fatal(err)
   193  	}
   194  	q = mq
   195  }
   196  
   197  func msgTeardown(t *testing.T) {
   198  	if err := q.Remove(); err != nil {
   199  		t.Fatal(err)
   200  	}
   201  }