Skip to content

Commit

Permalink
Fixed actualLength and added initial integration_test.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ Bahnken committed Apr 6, 2017
1 parent e18af61 commit 3d12448
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
15 changes: 15 additions & 0 deletions integration_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

echo 'Should be getting: "Auth Failed"'
psql -U postgres -h 127.0.0.1 -p 5432
psql -h 127.0.0.1 -p 5432 -U postgres
psql -h 127.0.0.1 -p 5432 -U postgres 'sslmode=disable'

echo '------------------------------------------'
echo 'Should be getting: "ERROR: No such user:"'
for x in $(seq 1 50); do
sleep .2
a=$(printf "%-${x}s" "a")
user="${a// /a}"
psql -U ${user} -h 127.0.0.1 -p 5432
done
19 changes: 16 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,25 @@ func indexOfLastFilledByte(buf readBuf) int {
for i := 0; i < len(buf); i += 4 {
word := buf[i : i+4]
if isNullWord(word) {
return i - 1
return i - numberOfTrailingNulls(buf[i-4:i])
}
}
return len(buf) - 1
}

// Takes a word like: %v[108, 0, 0, 0] and returns 3, the number of trailing nulls.
func numberOfTrailingNulls(word []byte) int {
counter := 0
for i := len(word) - 1; i >= 0; i-- {
if word[i] == 0 {
counter++
} else {
return counter
}
}
return counter
}

func isNullWord(word []byte) bool {
for _, v := range word {
if v != 0 {
Expand All @@ -189,8 +202,8 @@ func isNullWord(word []byte) bool {

func (p *PostgresServer) handleStartup(buff readBuf, conn net.Conn) bool {
buf := readBuf(buff)
// Read out the initial two numbers so we are just left with the k/v pairs.
actualLength := indexOfLastFilledByte(buf) + 1
// Actual length finds the last byte and then adds two, because there is two null terminators at the end of the packet.
actualLength := indexOfLastFilledByte(buf) + 2
claimedLength := buf.int32()

if (actualLength == 0) || (claimedLength != actualLength) {
Expand Down

0 comments on commit 3d12448

Please sign in to comment.