+# We will create, and destroy, a new database with a random name
+my $host = "test.indexdata.com:2118";
+my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
+
+# Connect anonymously, and expect this to fail
+my $conn = makeconn($host, undef, undef, 1011);
+
+# Connect as a user, but with incorrect password -- expect failure
+Net::Z3950::ZOOM::connection_destroy($conn);
+$conn = makeconn($host, "user", "badpw", 1011);
+
+# Connect as a non-privileged user with correct password
+Net::Z3950::ZOOM::connection_destroy($conn);
+$conn = makeconn($host, "user", "frog", 0);
+
+# Non-privileged user can't create database
+makedb($conn, $dbname, 223);
+
+# Connect as a privileged user with correct password, check DB is absent
+Net::Z3950::ZOOM::connection_destroy($conn);
+$conn = makeconn($host, "admin", "fish", 0);
+Net::Z3950::ZOOM::connection_option_set($conn, databaseName => $dbname);
+count_hits($conn, "the", 109);
+
+# Now create the database and check that it is present but empty
+makedb($conn, $dbname, 0);
+count_hits($conn, "the", 0, 0);
+
+# Trying to create the same database again will fail EEXIST
+makedb($conn, $dbname, 224);
+
+# Add a single record, and check that it can be found
+updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
+count_hits($conn, "the", 0, 1);
+
+# Add the same record with the same ID: overwrite => no change
+updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
+count_hits($conn, "the", 0, 1);
+
+# Add it again record with different ID => new copy added
+updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
+count_hits($conn, "the", 0, 2);
+
+# Now drop the newly-created database
+dropdb($conn, $dbname, 0);
+
+# A second dropping should fail, as the database is no longer there.
+dropdb($conn, $dbname, 10004);
+
+
+sub makeconn {
+ my($host, $user, $password, $expected_error) = @_;
+
+ my $options = Net::Z3950::ZOOM::options_create();
+ Net::Z3950::ZOOM::options_set($options, user => $user)
+ if defined $user;
+ Net::Z3950::ZOOM::options_set($options, password => $password)
+ if defined $password;
+
+ my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
+ my $conn = Net::Z3950::ZOOM::connection_create($options);
+ $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
+ ok($errcode == 0, "unconnected connection object created");
+
+ Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
+ $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
+ ok($errcode == $expected_error,
+ "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
+
+ return $conn;
+}
+
+
+sub makedb {
+ my($conn, $dbname, $expected_error) = @_;
+
+ my $o = Net::Z3950::ZOOM::options_create();
+ my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
+ # Inspection of the ZOOM-C code shows that this can never fail, in fact.
+ ok(defined $p, "created package");
+
+ Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
+ my $val = Net::Z3950::ZOOM::package_option_get($p, "databaseName");
+ ok($val eq $dbname, "package option retrieved as expected");