Exemple #1
0
func TestServeZero(t *testing.T) {
	setupServerOnce.Do(setupServer)
	ast := assert.NewAssert(t)
	i, o := bytes.NewBuffer([]byte{0}), new(bytes.Buffer)
	err := serve(i, o)
	ast.Equal(0, o.Len())
	ast.Equal(ErrInvalidRequest, err)
}
Exemple #2
0
func TestUniqueness(t *testing.T) {
	ast := assert.NewAssert(t)
	sf, _ := New(0)
	w0Id, err := sf.Next()
	ast.Nil(err)
	sf2, _ := New(1)
	w1Id, err := sf2.Next()
	ast.Nil(err)
	ast.NotEqual(w0Id, w1Id)
}
Exemple #3
0
func TestSequence(t *testing.T) {
	ast := assert.NewAssert(t)
	sf, _ := New(0)
	prevId, err := sf.Next()
	ast.Nil(err)

	for i := 1; ((int64(i) + 1) & SequenceMax) != 0; i++ {
		curId, err := sf.Next()
		ast.Nil(err)
		ast.NotEqual(prevId, curId)
		prevId = curId
	}
}
func TestBackend(t *testing.T) {
	ast := assert.NewAssert(t)

	DefaultLifetime = 1
	redisStorage, err := NewRedisBackend("redis://:@localhost:6379/15", "dev", false)
	ast.Nil(err)

	backends := []Storage{
		redisStorage,
	}

	backendTests := []backendTest{
		{
			got:   &Session{Id: "1"},
			exp:   &Session{Id: "1"},
			err:   nil,
			sleep: 0,
		},
		{
			got:   &Session{Id: "1", Mask: AdminMask | FullMask},
			exp:   &Session{Id: "1", Mask: AdminMask | FullMask},
			err:   nil,
			sleep: 0,
		},
		{
			got:   &Session{Id: "1"},
			exp:   nil,
			err:   nil,
			sleep: 2,
		},
	}

	for _, backend := range backends {
		for _, test := range backendTests {
			err := backend.Write(test.got)
			ast.Nil(err)

			if test.sleep > 0 {
				time.Sleep(time.Duration(int(time.Second) * test.sleep))
			}

			ses, err := backend.Read(test.got.Id)

			if test.err != nil {
				ast.Equal(test.err, err)
			} else {
				ast.Equal(test.exp, ses)
			}
		}
	}
}
Exemple #5
0
func TestDatabase(t *testing.T) {
	db := dbFromConf(t)
	setUp(t, db)
	defer tearDown(t, db)
	ast := assert.NewAssert(t)

	cnt, err := friendCount(db)
	ast.Nil(err)
	ast.Equal(0, cnt)

	f := &Friend{Name: "Foo"}
	err = friendPut(db, f)
	ast.Nil(err)
	ast.Equal(1, f.FriendID)
}
Exemple #6
0
func TestPolicy(t *testing.T) {
	ast := assert.NewAssert(t)

	var wg sync.WaitGroup
	l, err := net.Listen("tcp", ":9001")
	defer l.Close()

	ast.Nil(err)
	log.Printf("Listen on %v", l.Addr())

	wg.Add(1)
	go func() {
		serve(l)
		wg.Done()
	}()

	for _, p := range policyTests {
		conn, err := net.Dial("tcp", ":9001")
		ast.Nil(err)

		n, err := conn.Write(p.send)

		if !p.failWrite {
			ast.Nil(err)
			ast.Equal(len(p.send), n)
		} else {
			ast.NotNil(err)
			continue
		}

		buf := make([]byte, testBufSize)
		n, err = conn.Read(buf)

		if !p.failRead {
			ast.Nil(err)
			ast.Equal(len(p.exp), n)
			ast.True(bytes.Equal(p.exp, buf[:n]))
		} else {
			ast.NotNil(err)
			continue
		}
	}
	l.Close()
	wg.Wait()
}
Exemple #7
0
func TestServeMoreThanZero(t *testing.T) {
	setupServerOnce.Do(setupServer)
	ast := assert.NewAssert(t)
	i, o := bytes.NewBuffer([]byte{1}), new(bytes.Buffer)
	err := serve(i, o)
	ast.Equal(io.EOF, err)
	ast.Equal(8, o.Len())

	i, o = bytes.NewBuffer([]byte{2}), new(bytes.Buffer)
	err = serve(i, o)
	ast.Equal(io.EOF, err)
	ast.Equal(16, o.Len())

	i, o = bytes.NewBuffer([]byte{255}), new(bytes.Buffer)
	err = serve(i, o)
	ast.Equal(io.EOF, err)
	ast.Equal(255*8, o.Len())
}
func TestPersistance(t *testing.T) {
	ast := assert.NewAssert(t)

	p1 := &Session{}
	p1.Set(AdminMask)
	p1.ProfileID = 1

	buf, err := json.Marshal(p1)
	ast.Nil(err)

	p2 := &Session{}
	err = json.Unmarshal(buf, p2)

	//fmt.Printf("%s\n", buf)
	ast.Nil(err)
	ast.True(p2.HasAdmin())
	ast.True(!p2.HasFull())

	buf, err = json.Marshal(&Session{})

	ast.Nil(err)
	ast.Equal("{}", string(buf))
}
func TestSession(t *testing.T) {
	ast := assert.NewAssert(t)

	p := &Session{}

	ast.True(!p.HasAdmin())
	ast.True(!p.HasFull())

	p.Set(AdminMask | FullMask)

	ast.True(!p.HasAdmin())
	ast.True(p.HasFull())

	p.ProfileID = 1

	ast.True(p.HasAdmin())
	ast.True(p.HasFull())

	p.Unset(AdminMask)

	ast.True(!p.HasAdmin())
	ast.True(p.HasFull())
}